html - 图像网格的 CSS 掩码

标签 html css mask grid-layout

如何创建一个图像网格,其中的图像在相同高度的行和相同的宽度列中很好地排列,让我们可以在响应式设计中使用 CSS 获得不同纵横比的图片?

让我们以带有网格的示例页面为例,它不言自明: http://destadesign.com/test/capricorn/test.html 第二行的图像突出了网格。

响应式设计要求我们使用百分比(%)值而不是固定的像素值,并且只针对宽度,以便自动计算高度,这在这种情况下使事情变得复杂。

我会想到图像的剪贴蒙版(?) 来实现这一点(相同大小的 div,包含不同大小的图像),但是我缺乏创建如此复杂的 CSS 的技能。非常感谢任何具体的帮助,但一些一般性的想法和指南也会派上用场。

HTML div-imgage 框具有这样的(相当自然?)结构:

<div id="1" class="figure">
  <a href="#" class="link1">        
    <img src="images/pic_mountain.jpg" alt="TARGI W PARYŻU">
    <div class="figcaption">
      <h4>test 1</h4>  
    </div>
  </a>
</div><!-- /end .figure -->

具有非必要悬停和文本居中效果的 CSS:

.figure {
    position: relative;
    float: left;
    width: 10%;
    margin-right: 1%;
    left:20px;
   }
     .figure a{
      display:block;
     width:100%;
     height:100%;
     position:relative;
     z-index:2;
   }
    .figure a img{
    width:100%;
    display:block;
    }
.figcaption {
    font-size: 0.8em;
    letter-spacing: 0.1em;
    text-align: center;
    position: absolute;
    top: 0;
    left:0;
    width:100%;
    z-index: 2;
    height:100%;
    background-color:rgba(0,0,0,.4);
    transition:all 0.4s ease;

}
.figcaption h4{
  position:absolute;
  top:50%;
  left:50%;
  padding:0;
  margin:0;
  -moz-transform:translate(-50%, -50%);
  -webkit-transform:translate(-50%, -50%);
  transform:translate(-50%, -50%);
}
.figure a:hover .figcaption {
  opacity:0;
}

为方便起见,请继续使用此 CodePen: http://codepen.io/anon/pen/GopQPZ

最佳答案

不使用 CSS 掩码,an example :

body {
  background-color: black;
  text-align: center;
}

.figure {
  display: inline-block;
  margin: 2vw;
}

a {
  border: 2px solid grey;
  border-radius: 5px;
  display: inline-block;
  height: 10vw;
  overflow: hidden;
  position: relative;
  width: 10vw;
}
a img {
  height: 150%;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
}
<div id="figure1" class="figure">
  <a href="http://static.comicvine.com/uploads/scale_super/8/82727/1525513-the_moutain____by_vincentfavre.jpg" class="link1" target="_blank">
    <img src="http://static.comicvine.com/uploads/scale_super/8/82727/1525513-the_moutain____by_vincentfavre.jpg" alt="TARGI W PARYŻU">
    <div class="figcaption">
      <h4>test 1</h4>  
    </div>
  </a>
</div>
<div id="figure2" class="figure">
  <a href="http://www.britishairways.com/cms/global/assets/images/content/760x350_beach_Pod-3.jpg" class="link2" target="_blank">
    <img src="http://www.britishairways.com/cms/global/assets/images/content/760x350_beach_Pod-3.jpg" alt="TARGI W PARYŻU">
    <div class="figcaption">
      <h4>test 2</h4>  
    </div>
  </a>
</div>
<div id="figure3" class="figure">
  <a href="http://www.barnesandnoble.com/blog/barnesy/wp-content/uploads/2013/08/country_western.jpg" class="link3" target="_blank">        
    <img src="http://www.barnesandnoble.com/blog/barnesy/wp-content/uploads/2013/08/country_western.jpg">
    <div class="figcaption">
      <h4>test 3</h4>  
    </div>
  </a>
</div>
<div id="figure4" class="figure">
  <a href="http://junebugweddings.com/img/photobug/January2013/beautiful-landscape-wedding-portrait-by-julian-kanz.jpg" class="link4" target="_blank">        
    <img src="http://junebugweddings.com/img/photobug/January2013/beautiful-landscape-wedding-portrait-by-julian-kanz.jpg">
    <div class="figcaption">
      <h4>test 4</h4>  
    </div>
  </a>
</div>

关于html - 图像网格的 CSS 掩码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34254204/

相关文章:

html - 使div(height)占据父级剩余高度

javascript - 如何在JavaScript中搜索没有https协议(protocol)的链接

css - 设置内部div的高度

javascript - 使用 Javascript 渲染元素来加快页面速度?

c++ - 如何在 C/C++ 中对字符串应用掩码?

javascript - SyntaxHighlighter 插入新行和编号错误

html - 如何创建响应式 4 div 菱形?

javascript - Viewport.el.mask根本不出现在javascript中,太快了?

css - Firefox 需要 SVG <mask> 标记,但在 Chrome 中似乎会破坏 CSS 掩码

css div 高度设置为零