html - 具有填充图像问题的响应式网格

标签 html css responsive-design grid

我有以下网格系统:

http://jsfiddle.net/tev60L6z/1

html {
  box-sizing: border-box;
}
*,
*:before,
*:after {
  box-sizing: inherit;
}
img {
  height: auto;
  max-width: 100%;
}
.container {
  width: 100%;
}
.third {
  float: left;
  width: 33.334%;
  padding-right: 24px;
}
.last {
  padding-right: 0;
}
.clearfix:before,
.clearfix:after {
  content: "";
  display: table;
}
.clearfix:after {
  clear: both
}
<div class="container clearfix">
  <article class="third">
    <img src="http://3.bp.blogspot.com/-6Q23mHrTwxc/UxGT360FBMI/AAAAAAAAAJ0/pj86RO2vGyg/s320/google-maps-api.jpg" alt="image" />
    <h1>Title</h1>
    <p>Description</p>
  </article>
  <article class="third">
    <img src="http://3.bp.blogspot.com/-6Q23mHrTwxc/UxGT360FBMI/AAAAAAAAAJ0/pj86RO2vGyg/s320/google-maps-api.jpg" />
    <h1>Title</h1>
    <p>Description</p>
  </article>
  <article class="third last">
    <img src="http://3.bp.blogspot.com/-6Q23mHrTwxc/UxGT360FBMI/AAAAAAAAAJ0/pj86RO2vGyg/s320/google-maps-api.jpg" alt="image" />
    <h1>Title</h1>
    <p>Description</p>
  </article>
</div>

3 列在我的容器中的位置相同,但最后一张图像的高度大于其余图像。我知道这是因为第三列没有填充。

有没有办法在保持 3 个相等宽度的同时均衡图像高度?我已经在图像上尝试了 max-width,但这会在第三列的右侧增加一个 24px 的间隙。

最佳答案

我建议在最后一个元素上保留填充并在容器上使用负右边距:

html {
  box-sizing: border-box;
}
*,
*:before,
*:after {
  box-sizing: inherit;
}
img {
  height: auto;
  max-width: 100%;
}
.container {
  width: 100%;
  overflow: hidden;
}
.wrap {
  margin-right: -24px;
}
.third {
  float: left;
  width: 33.334%;
  padding-right: 24px;
}
.clearfix:before,
.clearfix:after {
  content: "";
  display: table;
}
.clearfix:after {
  clear: both
}
<div class="container clearfix">
  <div class="wrap">
    <article class="third">
      <img src="http://3.bp.blogspot.com/-6Q23mHrTwxc/UxGT360FBMI/AAAAAAAAAJ0/pj86RO2vGyg/s320/google-maps-api.jpg" alt="image" />
      <h1>Title</h1>

      <p>Description</p>
    </article>
    <article class="third">
      <img src="http://3.bp.blogspot.com/-6Q23mHrTwxc/UxGT360FBMI/AAAAAAAAAJ0/pj86RO2vGyg/s320/google-maps-api.jpg" />
      <h1>Title</h1>

      <p>Description</p>
    </article>
    <article class="third last">
      <img src="http://3.bp.blogspot.com/-6Q23mHrTwxc/UxGT360FBMI/AAAAAAAAAJ0/pj86RO2vGyg/s320/google-maps-api.jpg" alt="image" />
      <h1>Title</h1>

      <p>Description</p>
    </article>
  </div>
</div>

引用:grid with inner padding only

关于html - 具有填充图像问题的响应式网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27312870/

相关文章:

javascript - 测试 HTML Web 表单时出现问题

javascript - 仅在第一个 li 内查找 h2

html - 侧边栏高度不随内容高度一起变化

CSS:响应式 CSS 不适用于不同的浏览器

javascript - 如何缩短代码或将其存储在数据文件中

html - Bootstrap : Layout not responsive even while using correct classes

html - CSS HTML 页脚

jquery - 第 n 个 child 高度属性的问题

javascript - 使用 css 或 javascript 进行响应式网页设计?

html - 我的 srcset 定义有问题,还是当前的浏览器支持很弱?