【问题标题】:CSS Grid fixed height and variable widthCSS Grid 固定高度和可变宽度
【发布时间】:2018-10-08 18:42:01
【问题描述】:

我正在尝试使用 CSS Grid 创建一个图片库。我需要这样的东西:

但能做到的只有这个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>

【问题讨论】:

  • 应该将 500px-tallimage 缩小到 150px 高吗?
  • 因为网格是为单元格而不是图像定义的
  • 为什么不用flexbox来代替?
  • 你的网格应该有 5 列。有些单元格跨越两列
  • 好的,所以我放弃了这个想法,因为它需要很多技巧或库来实现。

标签: html css css-grid


【解决方案1】:

纯CSS解决方案可以在jsfiddle找到

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 - 请注意,您必须将砌体应用到父元素,在本例中是容器的 id

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

查看jsFiddle here

【讨论】:

  • 这并没有提供问题的答案。一旦你有足够的reputation,你就可以comment on any post;相反,provide answers that don't require clarification from the asker。 - From Review
  • @SamvelAleqsanyan 在这篇文章中更新了一个工作示例。提供的 IsotopeJS 链接上提供了更多配置选项。
  • @schmitty890 OP 正在寻找 CSS Grid 解决方案,而不是 IsotopJS 解决方案
  • @MikeDiglio 已添加纯 css 解决方案以及 isotopeJS 解决方案。建议使用 IsotopeJS,因为它是一个专门为开发人员处理此功能的库。
  • @SamvelAleqsanyan 我将跳入回答问题而不是提供不需要提问者澄清的答案,因为我在 js/html/css/jquery 领域拥有丰富的经验。随意阅读有关 Isotope.js 库的文档,因为它使用 javascript 库为这个特定问题提供了解决方案。否则,如果该库无法在应用程序中使用,我提供了纯 css 解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-22
  • 1970-01-01
  • 2016-07-10
  • 1970-01-01
  • 2012-03-20
  • 2014-05-02
相关资源
最近更新 更多