css - 带有媒体查询的响应式和流畅的缩略图网格

标签 css grid media-queries

我想出了这个解决方案,用于响应式和流畅的缩略图网格,使用媒体查询:

HTML

<div class="container>
  <div class="thumbnail">
    <img src="https://placekitten.com/g/250/250" alt="..." />
  </div>
  <div class="thumbnail">
    <img src="https://placekitten.com/g/250/250" alt="..." />
  </div>
  <div class="thumbnail">
    <img src="https://placekitten.com/g/250/250" alt="..." />
  </div>
  </div>
  <div class="thumbnail">
    <img src="https://placekitten.com/g/250/250" alt="..." />
  </div>
  <div class="thumbnail">
    <img src="https://placekitten.com/g/250/250" alt="..." />
  </div>
  //...

</div>  

CSS

@media only screen and (min-width: 251px) { .thumbnail img { width: 50%; } }
@media only screen and (min-width: 501px) { .thumbnail img { width: 33.3333333%; } }
@media only screen and (min-width: 751px) { .thumbnail img { width: 25%; } }
@media only screen and (min-width: 1001px) { .thumbnail img { width: 20% ; } }
@media only screen and (min-width: 1251px) { .thumbnail img { width: 16.6666666%; } }
//...

Sample

这种做法有什么问题吗?

我不想为此使用大量媒体查询,但对于没有间距的网格来说,这似乎是一个不错的解决方案,考虑到它基本上只使用媒体查询,而媒体查询得到广泛支持。

最佳答案

尝试在不编写太多媒体查询的情况下使用 flex 布局。 看css-tricks .
你会有这样的东西:

.container {
 display: flex; 
}
.thumbnail {
 flex: 1;
}
.thumbnail > img {
 width: 100%;
 height: auto;
}

关于css - 带有媒体查询的响应式和流畅的缩略图网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27302805/

相关文章:

c# - 在 WPF 中手动添加对 TextBox 的引用

css - bootstrap3 的网格问题...网格之间需要空间

html - 在媒体查询中重置 CSS

html - 制作响应式 HTML 设计 "UNRESPONSIVE"

javascript - 使用秒而不是百分比来表达 CSS3 @keyframes

css - 如何在openlayers中向左和向右调整多边形上的叠加

html - 如何将CSS属性传递给child的child? (div级联)

python - 在二维网格 python 上插值

html - 响应式 html div 元素?

html - 按钮切成两半