将inline-svg设置为div的背景

时间:2019-10-19 01:37:58

标签: html css containers inline-svg

我在CSS网格上放置了一个大约15个字符的段落,我想在其中添加背景元素,并且希望它具有某种个性,因此我使用了嵌入式SVG内容。

我尝试将其与bottom放在position:relative上,但是当您更改视口大小时,缩放比例不好。我将SVG代码包装在一个容器中:

.marquee-container {
    height: 0;
    position: absolute;
    width: 500px;
}

以及SVG的样式:

.svg-marquee {
    fill: teal;
    stroke-width: 4;
    stroke-miterlimit: 10;
    cursor: pointer;
    transition: .5s;
}

这是HTML标记

<div class="home-works">
    <div class="head">
        <h1>Entries</h1>
    </div>
    <img class="thumbnail" src="img/profile-picture.png" width="100%"/>
    <div class="main-content">                
        <div class="marquee-container">
            <svg version="1.1"
                        xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/"
                        x="0px" y="0px" viewBox="0 0 634 175" style="enable-background:new 0 0 634 175;"
                        xml:space="preserve">
                            <path class="svg-marquee" d="M595.9,173C317,173,317,153,38.1,153C27.3,153,2,157.6,2,151C2,87.5,34.8,87.5,34.8,24c0-6.6-7.5-22,3.3-22
                            C317,2,317,22,595.9,22c10.8,0,36.1-4.6,36.1,2c0,63.5-32.8,63.5-32.8,127C599.2,157.6,606.7,173,595.9,173z"/>
            </svg>
        </div>
        <div class="post">
            <h3>title</h3>
            <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore placeat
                                maiores ad ullam illo, blanditiis ipsam libero! Aspernatur, mollitia suscipit?
            </p>
        </div>
    </div>
</div>  

我的项目位于CSS网格布局中,其中post是我用来设置段落内容样式的类。它不在任何grid-template-areas内,而只是已定义区域内的一个类。

.post {
    text-align: left;
    position: relative;
    z-index: 1;
}

因此缩放效果不佳,我想将元素放置在要包含的段落的背景中,最好的方法是什么?

.marquee-container {
  height: 0;
  position: absolute;
  width: 500px;
}

.svg-marquee {
  fill: teal;
  stroke-width: 4;
  stroke-miterlimit: 10;
  cursor: pointer;
  transition: .5s;
}

.post {
  text-align: left;
  position: relative;
  z-index: 1;
}
<div class="home-works">

  <div class="head">
    <h1>Entries</h1>
  </div>
  <img class="thumbnail" src="img/profile-picture.png" width="100%" />
  <div class="main-content">

    <div class="marquee-container">
      <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" x="0px" y="0px" viewBox="0 0 634 175" style="enable-background:new 0 0 634 175;" xml:space="preserve">
                            <path class="svg-marquee" d="M595.9,173C317,173,317,153,38.1,153C27.3,153,2,157.6,2,151C2,87.5,34.8,87.5,34.8,24c0-6.6-7.5-22,3.3-22
                            C317,2,317,22,595.9,22c10.8,0,36.1-4.6,36.1,2c0,63.5-32.8,63.5-32.8,127C599.2,157.6,606.7,173,595.9,173z"/>
                    </svg>
    </div>
    <div class="post">
      <h3>title</h3>
      <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore placeat maiores ad ullam illo, blanditiis ipsam libero! Aspernatur, mollitia suscipit?
      </p>
    </div>
  </div>
</div>

1 个答案:

答案 0 :(得分:1)

由于您要绝对定位.marquee-container。 您需要将父级的位置设置为relative,因此,在父级position: relative上添加.main-content

我不明白您为什么将.marquee-container的高度设置为0。 我建议将其设置为heightauto,并设置一个bottom: 0;(因为您希望它与容​​器的底部对齐)。

width设置为100%,因为您希望它具有可伸缩性。还要添加一个负数z-index,以使元素不会掩盖内容。

检查下面的代码段。

.main-content {
  position: relative;
}
.marquee-container {
  position: absolute;
  bottom: 0;
  height: auto;
  width: 100%;
  z-index: -5;
}

.svg-marquee {
  fill: teal;
  stroke-width: 4;
  stroke-miterlimit: 10;
  cursor: pointer;
  transition: .5s;
}

.post {
  text-align: left;
  position: relative;
  z-index: 1;
}
<div class="home-works">

  <div class="head">
    <h1>Entries</h1>
  </div>
  <img class="thumbnail" src="img/profile-picture.png" width="100%" />
  <div class="main-content">

    <div class="marquee-container">
      <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" x="0px" y="0px" viewBox="0 0 634 175" style="enable-background:new 0 0 634 175;" xml:space="preserve">
                            <path class="svg-marquee" d="M595.9,173C317,173,317,153,38.1,153C27.3,153,2,157.6,2,151C2,87.5,34.8,87.5,34.8,24c0-6.6-7.5-22,3.3-22
                            C317,2,317,22,595.9,22c10.8,0,36.1-4.6,36.1,2c0,63.5-32.8,63.5-32.8,127C599.2,157.6,606.7,173,595.9,173z"/>
                    </svg>
    </div>
    <div class="post">
      <h3>title</h3>
      <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore placeat maiores ad ullam illo, blanditiis ipsam libero! Aspernatur, mollitia suscipit?
      </p>
    </div>
  </div>
</div>