如何使SVG背景图像到达容器底部?

时间:2019-08-08 12:19:03

标签: css svg

我正在尝试将SVG用作伪元素的背景图像。图像不如容器高,所以我想放在容器的底部,让背景颜色占据顶部,使其看起来无缝。如果我使用负边距,则可以将图像放置在容器的底部,但是我试图使其保持响应状态,这是行不通的。
如何使SVG停留在底部?

.container {
  display: grid;
  grid-template-columns: 1fr 1fr;
}

.left-half {
  position: relative;
  background: #ff9e2c;
  grid-column: 1;
}

.right-half {
  background: #b6701e;
  grid-column: 2;
}

.left-half::after {
  content: "";
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: url(../img/svg/SVG/mountMckinley.svg) no-repeat;
  background-size: 100%;
}
<section class="container">
  <div class="left-half">
    <article>
      <h1>Left Half</h1>
    </article>
  </div>
  <div class="right-half">
    <article>
      <h1 class="font">William Cunningham</h1>
      <p>Web Design, Teaching, Photography, & Fly-Fishing</p>
    </article>
  </div>
</section>

html, body, section, div {
  height: 100vh;
}

body {
  color: #fff;
  font-family: sans-serif;
  font-size: 1.25rem;
  line-height: 150%;
  text-shadow: 0 2px 2px #b6701e;
}

article {
  position: relative;
  top: 50%;
  text-align: center;
  transform: translate(0, -50%);
  padding: 1rem;
}
.container {
  display: grid;
  grid-template-columns: 1fr 1fr;
}

.left-half {
  position: relative;
  background: #ff9e2c;
  grid-column: 1;
}

.right-half {
  background: #b6701e;
  grid-column: 2;
}

.left-half::after {
  content: "";
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: url(https://williamcunningham.me/mountMckinley.svg) no-repeat;
  background-size: 100%;
}
<section class="container">
  <div class="left-half">
    <article>
      <h1>Left Half</h1>
    </article>
  </div>
  <div class="right-half">
    <article>
      <h1 class="font">Right Half</h1>
      
    </article>
  </div>
</section>

2 个答案:

答案 0 :(得分:3)

在您的背景元素中添加“底部”应该做到这一点:

background: url(https://williamcunningham.me/mountMckinley.svg) no-repeat bottom;

答案 1 :(得分:2)

position: absolute; bottom:0;不起作用的原因是因为您给它一个height: 100%;,使得带有背景图像的元素占据了整个块的高度。

您只需将background-position: bottom;添加到.left-half::after {},使其始终位于底部。

html, body, section, div {
  height: 100vh;
}

body {
  color: #fff;
  font-family: sans-serif;
  font-size: 1.25rem;
  line-height: 150%;
  text-shadow: 0 2px 2px #b6701e;
}

article {
  position: relative;
  top: 50%;
  text-align: center;
  transform: translate(0, -50%);
  padding: 1rem;
}
.container {
  display: grid;
  grid-template-columns: 1fr 1fr;
}

.left-half {
  position: relative;
  background: #ff9e2c;
  grid-column: 1;
}

.right-half {
  background: #b6701e;
  grid-column: 2;
}

.left-half::after {
  content: "";
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: url(https://williamcunningham.me/mountMckinley.svg) no-repeat;
  background-size: 100%;
  background-position: bottom;
}
<section class="container">
  <div class="left-half">
    <article>
      <h1>Left Half</h1>
    </article>
  </div>
  <div class="right-half">
    <article>
      <h1 class="font">Right Half</h1>
      
    </article>
  </div>
</section>