0

我在我的 CSS Grid 上找到了一个位置,我在其中插入了一个大约 15 个字符长的段落,我想为其添加一个背景元素,并且我希望它具有一些个性,因此我使用了内联 SVG 内容。

我试图bottom用 a将它放置在,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是我用来设置段落内容样式的类。它不在 any 里面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>

4

1 回答 1

1

既然你是.marquee-container绝对定位。您需要一个父级将位置设置为relative,因此,添加position: relative父级.main-content

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

设置为width100%因为您希望它具有可扩展性。还要添加一个负数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>

于 2019-10-19T02:01:24.130 回答