html - 带 mask 的响应式 div

标签 html css

基于 Mask position incorrect when I stop using a background image 上的面具想法我在水平、相同高度的响应式 div 上进行了测试。

结果, mask 无法展开以覆盖 div。

HTML:

<div class="IndexBanners">
<div class="bannerimages effect first">
<iframe class="embed-responsive-item" frameborder="0" src="https://www.youtube.com/embed/GfaiXgY114U" height="100%" width="100%"></iframe>
<div class="black-box">
  <h2>Watch Video</h2>
</div>
</div>
<div class="bannerimages effect">
<a href="https://placehold.it"><img src="http://placehold.it/795x436"></a>
<div class="black-box">
  <h2>View News</h2>
</div>
</div>

CSS:

.IndexBanners {
    display: flex;
    margin-top: 20px;
}

.bannerimages {
    flex: 1 0 0;
}

img {
    max-width: 100%;
    height: auto;
    vertical-align: top;
}

.black-box {
    text-align: center;
    font-size: 16px;
    color: #fff;
    background-color: rgba(00, 00, 00, 0.8);
    width: 49%;
    height: 66%;
    opacity: 0.75;
    transition: all 0.5s ease-in-out;
    position: absolute;
    top: 20px;
}

.black-box:hover {
    opacity: 0.0;
    transition: all 0.5s ease-in-out;
}

h2 {
    padding-top: 23%;
    margin: 0;
}

@media (max-width:600px) {
.IndexBanners {
    display: block;
}
.first {
    position: relative;
    padding-bottom: 56.25%;
    height: 0;
}
.first iframe {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
  }
}

我设置的测试 fiddle 在这里:https://jsfiddle.net/3hekqxf7/ 您将从 fiddle 中看到,实际上只有一种尺寸可以使 mask 正确覆盖 div。鉴于我对 mask 使用了百分比值,我预计它会随着 mask 的 div 一起展开/缩小。

问题是,你究竟应该如何在响应式 div 上获得响应式掩码?!

最佳答案

让叠加层响应视频父级的方法是在父级上使用 position: relative,然后使用 position: absolute;顶部:0;左:0;右:0; bottom: 0; 在叠加层上,它将符合父级的任何形状。

.IndexBanners {
  display: flex;
  margin-top: 20px;
}

.bannerimages {
  flex: 1 0 0;
  position: relative;
}

img {
  max-width: 100%;
  height: auto;
  vertical-align: top;
}

.black-box {
    text-align: center;
    font-size: 16px;
    color: #fff;
    background-color: rgba(00,00,00,0.8);
    
    
    opacity: 0.75;
    transition: all 0.5s ease-in-out;
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
}

.black-box:hover {
    opacity: 0.0;
    transition: all 0.5s ease-in-out;
}

h2 {
    padding-top: 23%;
    margin: 0;
}

@media (max-width:600px) {
  .IndexBanners {
    display: block;
  }
  .first {
    position: relative;
    padding-bottom: 56.25%;
    height: 0;
  }
  .first iframe {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
  }
}
<div class="IndexBanners">
  <div class="bannerimages effect first">
    <iframe class="embed-responsive-item" frameborder="0" src="https://www.youtube.com/embed/GfaiXgY114U" height="100%" width="100%"></iframe>
    <!--a href="http://placehold.it"><img src="http://placehold.it/795x436"></a-->
    <div class="black-box">
        <h2>Watch Video</h2>
    </div>
  </div>
  <div class="bannerimages effect">
    <a href="http://placehold.it"><img src="http://placehold.it/795x436"></a>
    <div class="black-box">
        <h2>View News</h2>
    </div>
  </div>
</div>

关于html - 带 mask 的响应式 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44221756/

相关文章:

jQuery 无法检索占位符属性值?

html - css 仅在移动设备中发出不同的行为

javascript - 添加按钮以充当一项功能

html - 我如何创建这个 "fade in message box"

css - 居中 block 菜单 `<li>` 元素在 `<div>`

html - "non-opacity""opacity"元素的边框

asp.net - 将 ASP 包含添加到 HTML 中

python - 向 Python tkinter 解释如何使用带有 SASS 的类

jquery - Nivoslider 干扰导航列表菜单

html - 为什么 text-align 对一线伪元素不起作用?