css 中的内联 svg 隐藏在 div 后面

Inline svg in css is hidden behind div

我在 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>

您将它包含在 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>

我将十六进制颜色更改为 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>