CSS网格固定高度和可变宽度

时间:2018-04-27 21:28:24

标签: html css css3 css-grid

我正在尝试使用CSS Grid制作图片库。我需要这样的东西:

THIS

但是所有人都可以实现JSFiddle

问题是,DIV占用了剩下的所有空间,我不想要那个。



#gallery {
  background: #cfc;
  padding: 32px;
  display: grid;
  grid-auto-rows: 350px;
  grid-template-columns: repeat(auto-fit, auto);
  grid-gap: 10px;
}

.cell {
  background: #fcc;
}

.cell>img {
  height: 100%;
}

<div id="gallery">
  <div class="cell">
    <img src="http://via.placeholder.com/350x500" />
  </div>
  <div class="cell">
    <img src="http://via.placeholder.com/250x150" />
  </div>
  <div class="cell">
    <img src="http://via.placeholder.com/150x150" />
  </div>
  <div class="cell">
    <img src="http://via.placeholder.com/370x150" />
  </div>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

可以在此jsfiddle

中找到纯CSS解决方案

HTML

<div class="wrap">
    <div class="flex-c">
        <div class="flex-i big"></div>
        <div class="flex-i red"></div>
        <div class="flex-i big green"></div>
        <div class="flex-i green"></div>
        <div class="flex-i red"></div>
        <div class="flex-i blue"></div>
        <div class="flex-i green"></div>
        <div class="flex-i red"></div>
        <div class="flex-i blue"></div>
        <div class="flex-i green"></div>
        <div class="flex-i red"></div>
        <div class="flex-i blue"></div>
        <div class="flex-i green"></div>
        <div class="flex-i red"></div>
        <div class="flex-i blue"></div>
        <div class="flex-i green"></div>
        <div class="flex-i red"></div>
        <div class="flex-i blue"></div>
        <div class="flex-i green"></div>
        <div class="flex-i red"></div>
        <div class="flex-i blue"></div>
    </div>
</div>

CSS

.flex-c:after {
    content: '';
    display: table;
    clear: both;
}
.flex-i.red { background-color: red; }
.flex-i.blue {background-color: blue; }
.flex-i.green {background-color: green;}
.flex-i {
    transform: rotate(90deg) scaleY(-1);
    height: 100px;
    width: 100px;
    background-color: gray;
    margin: 0 10px 10px 0;
    float: left;
}
.flex-i:after,
.flex-i.big:before {
    content: '';
    height: 100px;
    width: 100px;
    left: 110px;
    top: 0;
    position: absolute;
    background-color: pink;
}

.flex-i.big:after {
    left: 220px;
}
.flex-i.big:before {
    left: 220px;
    bottom: 0;
    top: auto;
}

.big {
    width: 210px;
    height: 210px;
}

此外

您可以从使用Isotopes JS masonry layout

等库中受益

它可以帮助您生成您已经显示的布局样式。

这是一个有效的例子。

HTML

<div id="container">
    <div class="item"></div>
    <div class="item w2"></div>
    <div class="item"></div>
    <div class="item w2"></div>
    <div class="item h2"></div>
    <div class="item"></div>
    <div class="item h2"></div>
    <div class="item w2 h2"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item h2"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item w2"></div>
    <div class="item h2"></div>
    <div class="item"></div>
    <div class="item h2"></div>
</div>

CSS

#container {
    border: 1px solid;
    padding: 7px;
}

.item {
    width: 60px;
    height: 60px;
    float: left;
    margin: 3px;
    background: #CCC;
}

.item.w2 {
    width: 130px;
}

.item.h2 {
    height: 130px;
}

JS - 请注意,您必须将砌体应用于父元素,在本例中为父元素

$( function() {
    var $container = $('#container').masonry({
        itemSelector: '.item',
        columnWidth: 70
    });
});

查看jsFiddle here