【问题标题】:Inline svg in css is hidden behind divcss中的内联svg隐藏在div后面
【发布时间】:2019-07-25 16:50:29
【问题描述】:

我在 CSS 中使用了内联 SVG,以便可以看到图像。但是,它不起作用,因为图像隐藏在整个 div 后面,即使我添加了 z-index,也看不到图像。另外,图片放在div的底部,需要放在顶部。

下面我放置了我的 HTML 和 CSS 代码,以便您更容易看到我在做什么。

.curve {
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='-150 0 1000 270' style='transform: translateY(-5px)'><path d='M-314,267 C105,364 400,100 812,279' fill='none' stroke='#53DD6C' stroke-width='120' stroke-linecap='round' /></svg>");
  background-repeat: no-repeat;
}
<div class="curve">
  <div class="beforeFooter">
    <div id="blankGTitle">
      <h2>Impacting lives.</h2>
    </div>

    <div id="blankGText">
      <h3>Create your event and make a difference.</h3>
    </div>

    <div id="buttono4">
      <nuxt-link to="/create-event">
        <buttons-vue text="create my event" type="buttonWhiteBackground buttonForInsetShadow buttonForInsetShadow:hover buttonForInsetShadow:active" />
      </nuxt-link>
    </div>

    <div id="blankGText2">
      <nuxt-link to="/organiser" id="blankGText2" class="wText">
        <p class="underline">Tell me more</p>
      </nuxt-link>
    </div>
  </div>
</div>

【问题讨论】:

  • 您的背景图片 URL 无效,因为它包含未转义的 # 字符。
  • @RobertLongson 我已经更改了stroke,但它仍然是隐藏的。
  • 随时编辑问题以进行更新。

标签: html css svg vue.js background-image


【解决方案1】:

您将它包含在background-image 中是否有原因?您可以尝试将其从背景图像中移出到 HTML 中,然后使用 CSS position: absolute 将其设置为背景。

我稍微更改了您的示例以包含我的想法。如果这不是您想要的,请告诉我,我也许可以进一步帮助您?

.curve {
  position: relative;
}

.curve svg {
  position: absolute;
  z-index: -3;
  bottom: 0;
}

.curve svg path {
  fill: none;
  stroke: #53DD6C;
  stroke-width: 120;
  stroke-linecap: round;
}
<div class="curve">
  <svg xmlns='http://www.w3.org/2000/svg' viewBox='-150 0 1000 270'> <path d='M-314,267 C105,364 400,100 812,279'/> </svg>
  <div class="beforeFooter">
    <div id="blankGTitle">
      <h2>Impacting lives.</h2>
    </div>

    <div id="blankGText">
      <h3>Create your event and make a difference.</h3>
    </div>

    <div id="buttono4">
      <nuxt-link to="/create-event">
        <buttons-vue text="create my event" type="buttonWhiteBackground buttonForInsetShadow buttonForInsetShadow:hover buttonForInsetShadow:active" />
      </nuxt-link>
    </div>

    <div id="blankGText2">
      <nuxt-link to="/organiser" id="blankGText2" class="wText">
        <p class="underline">Tell me more</p>
      </nuxt-link>
    </div>
  </div>
</div>

或者如果你想要它在顶部,你可以像这样旋转它:

.curve {
  position: relative;
}

.curve svg {
  position: absolute;
  z-index: -3;
  transform: rotate(180deg);
  top: 0;
}

.curve svg path {
  fill: none;
  stroke: #53DD6C;
  stroke-width: 120;
  stroke-linecap: round;
}
<div class="curve">
  <svg xmlns='http://www.w3.org/2000/svg' viewBox='-150 0 1000 270'> <path d='M-314,267 C105,364 400,100 812,279'/> </svg>
  <div class="beforeFooter">
    <div id="blankGTitle">
      <h2>Impacting lives.</h2>
    </div>

    <div id="blankGText">
      <h3>Create your event and make a difference.</h3>
    </div>

    <div id="buttono4">
      <nuxt-link to="/create-event">
        <buttons-vue text="create my event" type="buttonWhiteBackground buttonForInsetShadow buttonForInsetShadow:hover buttonForInsetShadow:active" />
      </nuxt-link>
    </div>

    <div id="blankGText2">
      <nuxt-link to="/organiser" id="blankGText2" class="wText">
        <p class="underline">Tell me more</p>
      </nuxt-link>
    </div>
  </div>
</div>

【讨论】:

    【解决方案2】:

    我将十六进制颜色更改为 rgb,firefox has issues with hex colours for inline svg。它以十六进制正确显示在 Chrome 上。

    <!doctype html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>SVG</title>
        <style>
            .curve {
                background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='-150 0 1000 270' style='transform: translateY(-5px)'><path d='M-314,267 C105,364 400,100 812,279' fill='none' stroke='rgb(83,221,108)' stroke-width='120' stroke-linecap='round' /></svg>");
                background-repeat: no-repeat;
                }
        </style>
    </head>
    
    <main>
        <div class="curve">
            <div class="beforeFooter">
                <div id="blankGTitle">
                    <h2>Impacting lives.</h2>
                </div>
    
                <div id="blankGText">
                    <h3>Create your event and make a difference.</h3>
                </div>
    
                <div id="buttono4">
                    <nuxt-link to="/create-event">
                        <buttons-vue text="create my event"
                            type="buttonWhiteBackground buttonForInsetShadow buttonForInsetShadow:hover buttonForInsetShadow:active" ></buttons-vue>
                    </nuxt-link>
                </div>
    
                <div id="blankGText2_">
                    <nuxt-link to="/organiser" id="blankGText2" class="wText">
                        <p class="underline">Tell me more</p>
                    </nuxt-link>
                </div>
            </div>
        </div>
    </main>
    

    【讨论】:

      猜你喜欢
      • 2014-07-03
      • 2021-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多