首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SVG作为背景图像未出现

SVG作为背景图像未出现
EN

Stack Overflow用户
提问于 2017-09-14 05:19:25
回答 1查看 3.2K关注 0票数 0

我已经尝试了我在网上找到的通常的解决方案,但是无法让背景图像出现。

在第一个标题中,SVG映像似乎将其容器推开得比viewBox所需的要远得多。在第二个标题中,背景图像根本没有出现。(SVG代码相同)

HTML:

代码语言:javascript
复制
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;
}
代码语言:javascript
复制
<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/

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-14 05:28:28

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

代码语言:javascript
复制
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;
}
代码语言:javascript
复制
<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>

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46211065

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档