【问题标题】:How to place cover div onto object fit images in responsive flex container?如何将封面 div 放置到响应式 flex 容器中的对象适合图像上?
【发布时间】:2017-11-11 21:06:28
【问题描述】:

这是我的演示,以说明我的意思:

* {
  box-sizing: border-box;
}

body, html {
  height: 100%;
  width: 100%;
}

body {
  background: #333;
  margin: 0;
  padding: 0;
}

.content {
  display: flex;
  height: 100%;
  padding: 20px;
}

.panel {
  flex: 0 0 200px;
  display: flex;
  margin-left: 20px;
}

.widget {
  background: white;
  width: 100%;
  height: 100%;
}

.videoContainer {
  display: flex;
  flex: 1 1 100%;
  flex-wrap: wrap;
 }

.video {
  height: 50%;
  width: 50%;
  padding: 2px;
  position: relative;
}

.videoCover {
  position: absolute;
  background: red;
  opacity: 0.4;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

img {
  width: 100%;
  height: 100%;
  object-fit: contain; /* Default for videos */
}
<div class="content">
  <div class="videoContainer">
    <div class="video">
      <div class="videoCover"></div>
      <img width="600" height="400" src="http://thatgrapejuice.net/wp-content/uploads/2016/03/rihanna-thatgrapejuice-mariah-carey-billboard-600x400.jpg">
    </div>

    <div class="video">
      <img width="600" height="400" src="http://www.radioandmusic.com/sites/www.radioandmusic.com/files/images/entertainment/2015/09/28/rihanna-%2812%29.jpg">
    </div>

    <div class="video">
      <img width="600" height="400" src="http://m.buro247.com.au/thumb/600x960_0/images/640-rihanna-charity1.jpg">
    </div>

     <div class="video">
      <img width="600" height="400" src="http://hjb.hu/wp-content/uploads/2014/02/rihanna2.jpg">
    </div> 
  </div>
  
  <div class="panel">
    <div class="widget"></div>
  </div>
  
</div>  

垂直或水平调整浏览器大小,可以清楚地看到问题。所以基本上我想把红色封面完全放在图像上。所以我假设我需要一个与图像大小完全相同的 div。 似乎对象拟合做得很好,但正因为如此,我不能放置在红色 div 上。

纯css可以做吗?我应该如何修改dom和css? 非常感谢!

澄清我想要达到的目标是:

【问题讨论】:

  • 请遵循以红色弹出的规则并将您的代码发布到问题本身。阅读minimal reproducible example 了解更多信息。
  • 您可以使用 JS sn-ps 在您的帖子中包含 Javascript 代码(它是编辑菜单中的图标之一)
  • edit 你的问题成为主题:包括一个重复问题的minimal reproducible example。寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括:(1) 期望的行为,(2) 特定问题或错误,以及 (3) 在问题本身。另请参阅:What topics can I ask about here?How to Ask。这个问题是关于 JavaScript/HTML/CSS,所以你应该考虑使用snippet
  • ans777,人们要求您在问题中发布代码,而不是添加链接。
  • 我发布了一个答案,尽管仅使用 CSS 远非微不足道,而且如果没有给出纵横比,那么没有脚本的标记实际上是不可能的。所以为了能够改进我的答案,1:是否可以将纵横比添加到标记中? ... 2:你能说出那个封面是干什么用的吗?

标签: html css flexbox responsive object-fit


【解决方案1】:

我能想到的最好的就是这个,我把所有东西都包裹在一个 cover 中,然后用一个伪的 red cover

我还添加了一些媒体查询,因为需要控制视频的宽度,所以它们不会变得比它们的比例高度更宽,如果,可以让它们变窄。

您可能需要详细说明这些设置,可能还需要一两个查询,但我认为可以很好地做到这一点,并且能够避免对脚本的需要。

并且通过删除object-fit,您还可以获得更好的跨浏览器解决方案。

作为旁注,这是我参与的一个答案,它显示了实现你想要的东西需要什么:Scale element proportional to Background Cover/Contain。它有一个脚本和一个 CSS 版本

* {
  box-sizing: border-box;
}

body, html {
  height: 100%;
  width: 100%;
}

body {
  background: #333;
  margin: 0;
  padding: 0;
}

.content {
  display: flex;
  height: 100%;
  padding: 20px;
}

.panel {
  flex: 0 0 200px;
  display: flex;
  margin-left: 20px;
}

.widget {
  background: white;
  width: 100%;
  height: 100%;
}

.videoContainer {
  display: flex;
  flex: 1 1 100%;
  flex-wrap: wrap;
  align-items: center;
 }

.video {
  height: 50%;
  width: 50%;
  padding: 2px;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.videoCover {
  position: relative;
  overflow: hidden;
}
.video:nth-child(1) .videoCover::after {
  content: '';
  position: absolute;
  background: red;
  opacity: 0.4;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

img {
  display: block;
  width: 100%;
  height: auto;
}

@media (min-aspect-ratio: 600/400) {
  .video:nth-child(1) .videoCover::after {
    width: 80%;
  }
  img {
    width: 80%;
  }
}
@media (min-aspect-ratio: 800/400) {
  .video:nth-child(1) .videoCover::after {
    width: 60%;
  }
  img {
    width: 60%;
  }
}
@media (min-aspect-ratio: 1000/400) {
  .video:nth-child(1) .videoCover::after {
    width: 40%;
  }
  img {
    width: 40%;
  }
}
<div class="content">
  <div class="videoContainer">
    <div class="video">
      <div class="videoCover">
        <img width="600" height="400" src="http://thatgrapejuice.net/wp-content/uploads/2016/03/rihanna-thatgrapejuice-mariah-carey-billboard-600x400.jpg">
      </div>
    </div>

    <div class="video">
      <div class="videoCover">
        <img width="600" height="400" src="http://www.radioandmusic.com/sites/www.radioandmusic.com/files/images/entertainment/2015/09/28/rihanna-%2812%29.jpg">
      </div>
    </div>

    <div class="video">
      <div class="videoCover">
        <img width="600" height="400" src="http://m.buro247.com.au/thumb/600x960_0/images/640-rihanna-charity1.jpg">
      </div>
    </div>

    <div class="video">
      <div class="videoCover">
        <img width="600" height="400" src="http://hjb.hu/wp-content/uploads/2014/02/rihanna2.jpg">
      </div>
    </div> 
  </div>
  
  <div class="panel">
    <div class="widget"></div>
  </div>
  
</div>

【讨论】:

  • 抱歉,回答迟了。非常感谢您的努力!是的,我相信这是仅使用 css 的最佳解决方案。正如您所说,我还认为我们应该跳过 object-fit 属性。 :/如果不计算我想象的视频大小,可能无法完成此操作。再次感谢!
  • @ans777 如果您优化媒体查询,您可以仅使用 CSS 非常接近,只是需要一些时间:)
猜你喜欢
  • 1970-01-01
  • 2015-08-22
  • 1970-01-01
  • 1970-01-01
  • 2015-02-19
  • 1970-01-01
  • 1970-01-01
  • 2021-11-19
  • 1970-01-01
相关资源
最近更新 更多