具有不同高度和宽度的响应图像网格

Responsive Image Grid with Different Heights and Widths

本文关键字:响应 图像 网格 高度      更新时间:2023-09-26

我需要能够创建一个具有不同图像宽度的响应图像网格,(链接到下面的图像。)

http://jessewintondesign.com/imagegrid.jpg

这是我现在拥有的。

http://magnumdigitalcinema.com/dev

这个^在我的27英寸iMac上很合适,看起来也很好,但反应不灵敏,由于高度和宽度的不同,我还没能找到一个好的解决方案。Masonry.js不适合我,因为我正在Wordpress中开发这个网站,网格是动态创建的。我需要知道是否还有其他选择。提前感谢大家!

这是一个潜在的有用资源:http://susy.oddbird.net/

此外,使用CSS也可以做到这一点。看看这个过去的问题:具有不相等图像大小的简单CSS网格

以下是查询的解决方案。

按照相同的比例制作所有图像并固定(最大宽度:100%;)若并没有将图像标记为相同的比例,那个么在媒体查询时就必须使用不同的代码(高度/宽度)。

我想明白了。只需使用具有百分比值而不是像素值的padding-bottom,并将图像添加为CSS背景图像。由于它在Wordpress中,背景图像通过特色图像加载在每个帖子上。

以下是基本的HTML&CSS。同样,HTML是动态的,因为它是Wordpress主题。

HTML

<section id="portfolio-items">
      <figure class="portfolio-item" style="background: url('http://www.magnumdigitalcinema.com/wp-content/uploads/2015/06/Tyskie1.png');">
        <a href="#" class="play"></a><!-- / .play -->
        <a href="https://www.youtube.com/watch?v=g2WV-cM9CjA">
          <div class="video-info">
            <hgroup>
                <h2 class="name">The Tyskie</h2><!-- / .name -->
                <h5 class="type">Commercial | <span class="client">Challenge</span></h5><!-- / .type -->
            </hgroup>
          </div><!-- / .video-info -->      
          </a>
      </figure><!-- .portfolio-item -->     
      <figure class="portfolio-item" style="background: url('http://www.magnumdigitalcinema.com/wp-content/uploads/2015/06/Field4.png');">
        <a href="#" class="play"></a><!-- / .play -->
        <a href="https://www.youtube.com/watch?v=wiG8Pf6Cnxo">
          <div class="video-info">
            <hgroup>
                <h2 class="name">The Future</h2><!-- / .name -->
                <h5 class="type">Commercial | <span class="client">KE Precision AG</span></h5><!-- / .type -->
            </hgroup>
          </div><!-- / .video-info -->      
          </a>
      </figure><!-- .portfolio-item -->     
      <figure class="portfolio-item" style="background: url('http://www.magnumdigitalcinema.com/wp-content/uploads/2015/06/Untitled-8.png');">
        <a href="#" class="play"></a><!-- / .play -->
        <a href="https://www.youtube.com/watch?v=3xh5c20o8vM">
          <div class="video-info">
            <hgroup>
                <h2 class="name">Psalm 23</h2><!-- / .name -->
                <h5 class="type">Music Video | <span class="client">Corner Room</span></h5><!-- / .type -->
            </hgroup>
          </div><!-- / .video-info -->      
          </a>
      </figure><!-- .portfolio-item -->     
      <figure class="portfolio-item" style="background: url('http://www.magnumdigitalcinema.com/wp-content/uploads/2015/06/Screen-Shot-2015-07-17-at-12.48.48-PM.png');">
        <a href="#" class="play"></a><!-- / .play -->
        <a href="https://www.youtube.com/watch?v=TKfE1jTBFVk">
          <div class="video-info">
            <hgroup>
                <h2 class="name">Pat’s Story</h2><!-- / .name -->
                <h5 class="type">Testimonial | <span class="client">Climate Corp</span></h5><!-- / .type -->
            </hgroup>
          </div><!-- / .video-info -->      
          </a>
      </figure><!-- .portfolio-item -->     
      <figure class="portfolio-item" style="background: url('http://www.magnumdigitalcinema.com/wp-content/uploads/2015/06/chefhartley.jpg');">
        <a href="#" class="play"></a><!-- / .play -->
        <a href="https://www.youtube.com/watch?v=qSSKGHfpE1s">
          <div class="video-info">
            <hgroup>
                <h2 class="name">Chef Hartley</h2><!-- / .name -->
                <h5 class="type">Promotional | <span class="client">Hartley</span></h5><!-- / .type -->
            </hgroup>
          </div><!-- / .video-info -->      
          </a>
      </figure><!-- .portfolio-item -->     
      <figure class="portfolio-item" style="background: url('http://www.magnumdigitalcinema.com/wp-content/uploads/2015/06/Field1.png');">
        <a href="#" class="play"></a><!-- / .play -->
        <a href="https://www.youtube.com/watch?v=qpNHqjcr_kM">
          <div class="video-info">
            <hgroup>
                <h2 class="name">Intelliplanter</h2><!-- / .name -->
                <h5 class="type">Promotional | <span class="client">KE Precision AG</span></h5><!-- / .type -->
            </hgroup>
          </div><!-- / .video-info -->      
          </a>
      </figure><!-- .portfolio-item -->     
      <figure class="portfolio-item" style="background: url('http://www.magnumdigitalcinema.com/wp-content/uploads/2015/06/NF-TitleScreenShot1.png');">
        <a href="#" class="play"></a><!-- / .play -->
        <a href="https://www.youtube.com/watch?v=l9pyQvF7fNQ">
          <div class="video-info">
            <hgroup>
                <h2 class="name">Not Forgotten</h2><!-- / .name -->
                <h5 class="type">Documentary | <span class="client">Cahaba Park</span></h5><!-- / .type -->
            </hgroup>
          </div><!-- / .video-info -->      
          </a>
      </figure><!-- .portfolio-item -->     
      <figure class="portfolio-item" style="background: url('http://www.magnumdigitalcinema.com/wp-content/uploads/2015/06/MCGeneral-1.jpg');">
        <a href="#" class="play"></a><!-- / .play -->
        <a href="https://www.youtube.com/watch?v=W19cfOlxBKo">
          <div class="video-info">
            <hgroup>
                <h2 class="name">Our Vision</h2><!-- / .name -->
                <h5 class="type">Promotional | <span class="client">Morning Center</span></h5><!-- / .type -->
            </hgroup>
          </div><!-- / .video-info -->      
          </a>
      </figure><!-- .portfolio-item -->     
      <figure class="portfolio-item" style="background: url('http://www.magnumdigitalcinema.com/wp-content/uploads/2015/06/redbull2.jpg');">
        <a href="#" class="play"></a><!-- / .play -->
        <a href="https://www.youtube.com/watch?v=AWK4sfHqVfo">
          <div class="video-info">
            <hgroup>
                <h2 class="name">Energy</h2><!-- / .name -->
                <h5 class="type">Commercial | <span class="client">Challenge</span></h5><!-- / .type -->
            </hgroup>
          </div><!-- / .video-info -->      
          </a>
      </figure><!-- .portfolio-item -->     
    </section>

CSS

.portfolio-item {
    display: inline-block;
    position: relative;
    background-position: center !important; 
    background-size: cover !important;
    -webkit-background-size: cover !important;
    -moz-background-size: cover !important;
    -webkit-background-size: cover !important;
}
/* specific portfolio items */
.portfolio-item:nth-of-type(1) {
    width: 70%;
    padding-bottom: 30%;
    z-index: 0;
}
.portfolio-item:nth-of-type(2),
.portfolio-item:nth-of-type(3) {
    position: absolute;
    right: 0;
    width: 30%;
    top: 0;
    z-index: 0;
    padding-bottom: 15%;
}
.portfolio-item:nth-of-type(3) {
    margin-top: 15%;
}
.portfolio-item:nth-of-type(4),
.portfolio-item:nth-of-type(5),
.portfolio-item:nth-of-type(6),
.portfolio-item:nth-of-type(10),
.portfolio-item:nth-of-type(11),
.portfolio-item:nth-of-type(12) {
    width: 33.37%;
    margin-right: -4px;
    margin-top: -11px;
    padding-bottom: 17%;
    z-index: 0;
    display: inline-block !Important;
}
.portfolio-item:nth-of-type(7),
.portfolio-item:nth-of-type(8) {
    position: absolute;
    left: -2px;
    width: 30%;
    padding-bottom: 16%;
    display: block;
}
.portfolio-item:nth-of-type(7) {
    margin-top: -10px;
    margin-left: 2px;
    z-index: 0;
}
.portfolio-item:nth-of-type(8) {
    margin-top: 15%;
    margin-left: 2px;
    padding-bottom: 16.1% !important;
    z-index: 0;
}
.portfolio-item:nth-of-type(9) {
    width: 70%;
    padding-bottom: 31.5%;
    float: right;
    top: -10px;
    right: 0px;
    z-index: 0;
}

.portfolio-item:nth-of-type(2) .video-info h2,
.portfolio-item:nth-of-type(3) .video-info h2,
.portfolio-item:nth-of-type(7) .video-info h2,
.portfolio-item:nth-of-type(8) .video-info h2 {
    font-size: 40px;
}
.portfolio-item:nth-of-type(2) .video-info h5,
.portfolio-item:nth-of-type(3) .video-info h5,
.portfolio-item:nth-of-type(7) .video-info h5,
.portfolio-item:nth-of-type(8) .video-info h5  {
    font-size: 17px;
}
.portfolio-item:nth-of-type(4) .video-info h2,
.portfolio-item:nth-of-type(5) .video-info h2,
.portfolio-item:nth-of-type(6) .video-info h2,
.portfolio-item:nth-of-type(10) .video-info h2,
.portfolio-item:nth-of-type(11) .video-info h2,
.portfolio-item:nth-of-type(12) .video-info h2 {
    font-size: 40px;
}
.portfolio-item:nth-of-type(4) .video-info h5,
.portfolio-item:nth-of-type(5) .video-info h5,
.portfolio-item:nth-of-type(6) .video-info h5,
.portfolio-item:nth-of-type(10) .video-info h5,
.portfolio-item:nth-of-type(11) .video-info h5,
.portfolio-item:nth-of-type(12) .video-info h5 {
    font-size: 17px;
}

/* play button */
.portfolio-item .play {
    font-family: 'Icons';
    color: #fff;
    font-size: 60px;
    vertical-align: middle;
    text-align: center;
    width: 100%;
    height: 100%;
    position: absolute;
}
.play:before {
    content: '';
    display: inline-block;
    position: relative;
    vertical-align: middle;
    height: 100%;
}
.video-info {
    background: rgba(0,0,0,0.8);
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    position: absolute;
    width: 100%;
    height: 100%;
    opacity: 0;
    z-index: 1000;
    text-align: center;
    transition: opacity .2s linear;
    -moz-transition: opacity .2s linear;
    -o-transition: opacity .2s linear;
    -webkit-transition: opacity .2s linear;
}
.video-info:before {    
    content: ' ';
    vertical-align: middle;
    height: 100%;
    display: inline-block;
}
.video-info:hover {
    opacity: 1;
}
.video-info hgroup {
    position: relative;
    display: inline-block;
    vertical-align: middle;
    margin: 0 auto !important;
}
.video-info h2, .video-info h5 {
    font-family: 'AvenirNext-Regular';
    text-align: center;
    letter-spacing: 2px;
}
.video-info h2 {
    font-size: 50px;
}
.video-info h5 {
    color: #d9544c;
    font-size: 16px;
    margin-top: -25px;
}
.video-info h5 span {
    color: #fff;
}

谢谢大家的帮助!