SVG 作为背景图像未出现

SVG as background image not appearing

我已经尝试了我在网上找到的常用解决方案,但无法获得 要显示的背景图片。

在第一个 header 中,SVG 图像似乎将其容器推出比其 viewBox 所需的更远的地方。在第二个 header 中,背景图像根本没有出现。 (SVG代码相同)

HTML:

header {
  margin: 1rem auto 0 auto;
  border: 1px solid red;
}

.container {
  border: 2px dotted orange;
}

.container svg {
  border: 1px dashed blue;
  width: 100%;
  height: 100%;
}

#inline-svg {
  border: 2px dashed pink;
}

#header1 {}

#header2 {
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' id='background-svg' width='400px' height='200px' viewBox='0 0 400px 200px'>
 <ellipse cx='200' cy='100' rx='200' ry='100' fill='green' /> </svg>");
 background-size: auto auto;
}

#background-svg {
  border: 3px solid black;
}
<header id="header1" class="masthead">
  <div id="1st-container" class="container">
    <svg xmlns='http://www.w3.org/2000/svg' id='inline-svg' width='200px' height='100px' viewBox='0 0 200px 200px'>
      <ellipse cx='200' cy='100' rx='200' ry='100' fill='green' />
    </svg>
  </div>
</header>

<header id="header2" class="masthead">
  <div id="2nd-container" class="container">
    hello
  </div>
</header>

https://jsfiddle.net/abalter/c4fmou2n/

当您在 url 中添加 svg 代码时,请不要破坏它,否则它无法正常工作。

header {
  margin: 1rem auto 0 auto;
  border: 1px solid red;
}
.container {
  border: 2px dotted orange;
}
.container svg {
  border: 1px dashed blue;
  width: 100%;
  height: 100%;
}
#inline-svg {
  border: 2px dashed pink;
}
#header1 {}
#header2 {
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' id='background-svg' width='400px' height='200px' viewBox='0 0 400px 200px'><ellipse cx='200' cy='100' rx='200' ry='100' fill='green' /> </svg>");
  background-size: auto auto;
}
#background-svg {
  border: 3px solid black;
}
<header id="header1" class="masthead">
  <div id="1st-container" class="container">
    <svg xmlns='http://www.w3.org/2000/svg' id='inline-svg' width='200px' height='100px' viewBox='0 0 200px 200px'>
      <ellipse cx='200' cy='100' rx='200' ry='100' fill='green' />
    </svg>
  </div>
</header>

<header id="header2" class="masthead">
  <div id="2nd-container" class="container">
    hello
  </div>
</header>