首页 > 解决方案 > 将图像固定到 div 的底部,剪辑图像的顶部

问题描述

我有一个大图像,我在我的网站顶部用作启动图像。图像宽度是视口的 100%,图像会根据视口的大小垂直裁剪(同时保持纵横比)。

运行下面的代码片段,打开完整页面视图,然后调整浏览器窗口的大小以了解我的意思。或者,这是一个 Codepen

#logo {
  height: 100px;
}

#header {
  width: 100%;
  max-height: calc(100vh - 200px);
  overflow: hidden;
}

#header img {
  width: 100%;
}
<div id="logo">
  I'm a div. I probably contain a logo and I take up 100px at the top.
</div>
<div id="header">
  <img src="https://justus.ws/silence.jpg">
</div>

<div id="content">
  <p>And I'm some content. Weeeee!</p>
</div>

我希望它的行为方式是将图像固定到 div 的底部,并根据需要剪辑图像的顶部。我怎样才能做到这一点?

谢谢!

编辑:我正在尝试完成以下操作:

标签: htmlcss

解决方案


我会将图像设置为绝对位置,底部为零。

这是代码

#logo {
  height: 100px;
}

#header {
  width: 100%;
  height: calc(100vh - 200px);
  overflow: hidden;
  position: relative;
}

#header img {
  width: 100%;
  position: absolute;
  bottom:0;
}
<div id="logo">
  I'm a div. I probably contain a logo and I take up 100px at the top.
</div>
<div id="header">
  <img src="https://justus.ws/silence.jpg">
</div>

<div id="content">
  <p>And I'm some content. Weeeee!</p>
</div>

我希望这回答了你的问题。


推荐阅读