CSS英雄图片高度问题

标签 css

我使用以下代码在我的网站上显示像一个 flex 响应横幅图像,我从另一个网站 ( https://www.svenskakyrkan.se/onsala/adventsmusik ) 获得代码,它按预期工作。

.hero {
  width: 90%;
  max-width: 1350px;
  margin: 0 auto;
  position: relative;
  padding-top: 3rem;
}

.hero .hero-image-container {
  position: relative;
  width: 100%;
}

.hero .hero-image {
  width: 100%;
  height: auto;
  position: relative;
  min-height: 240px;
  max-height: 800px;
  background: center center #fff;
  background-size: cover;
  overflow: hidden;
  margin: 0;
}

.hero .hero-image:after {
  padding-bottom: 56.25%;
}
<div class="hero">
  <div class="hero-image-container">

    <figure class="hero-image" style="background-image: url('/images/banner.jpg'); max-height: 504px;"></figure>
  </div>
</div>

问题是图像只有 240 像素高,因此高度自动不起作用。

最佳答案

需要给伪元素内容,让它display:block:

.hero {
  width: 90%;
  max-width: 1350px;
  margin: 0 auto;
  position: relative;
  padding-top: 3rem;
}

.hero .hero-image-container {
  position: relative;
  width: 100%;
}

.hero .hero-image {
  width: 100%;
  height: auto;
  position: relative;
  min-height: 240px;
  max-height: 800px;
  background: center center #fff;
  background-size: cover;
  overflow: hidden;
  margin: 0;
}

.hero .hero-image:after {
  content:'';
  display:block;
  padding-bottom: 56.25%;
}
<div class="hero">
  <div class="hero-image-container">

    <figure class="hero-image" style="background-image: url('https://www.fillmurray.com/1000/500'); max-height: 504px;"></figure>
  </div>
</div>

关于CSS英雄图片高度问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53500221/

相关文章:

javascript - 在它们之间切换时,jQuery 文本显示在事件文本下方

html - 像 Div 一样的 Google 聊天

html - 按左-右-左顺序对齐 Bootstrap 列

javascript - 图像映射替代方案(也许在 javascript 中?)

asp.net - 如何让 Google Prettify 渲染得更像 Visual Studio

jQuery - 悬停时将样式应用于表中的所有垂直 TD

javascript - 如何使灯箱滚动灯箱而不是页面

html - Google map 和 Contact Form 7 对齐

css - React js css内联样式边距右侧不起作用

php - 如何将图像并排放置而不破坏它们