html - 在 CSS 中使用 Flexbox/Grid,使图像在移动设备上堆叠

标签 html css responsive-design

我正在尝试掌握 CSS3 中的网格布局。我想编写一些 HTML/CSS,使我的 3x1 网格在移动设备上查看时能够堆叠。现在,方 block 只会不断缩小直到变小。我之前已经能够使用移动 CSS,但使用网格对我来说是新的。任何人都可以提供一些指示或建议吗?

这是我的代码:

HTML:

<div class="container"> 

<div class="grid">
  <div class="cell">
    <img src="https://www.easycalculation.com/area/images/big-square.gif"> 

  </div>
  <div class="cell">
    <img src="https://www.easycalculation.com/area/images/big-square.gif">
  </div>
  <div class="cell">
    <img src="https://www.easycalculation.com/area/images/big-square.gif">
  </div>
</div>

CSS:

.container {
  width: 100%;
  margin: 0 auto;
}

.grid {
  display: flex;         
  flex-wrap: wrap;      
  justify-content: space-around;

}

.cell {
  flex: 0 0 32%;         
  margin-bottom: 5px;
}

img {
    max-width: 100%;
    height: auto;
}

body {
    background-color: #dedede;
}

这里还有一个 CodePen 链接:https://codepen.io/anfperez/pen/QrEBLZ

最佳答案

试试这个 CSS: (代码笔 https://codepen.io/bonniemellott/pen/ELypzm )

.container {
  width: 100%;
  margin: 0 auto;
}

.grid {
  display: flex;         
  justify-content: space-between;
  flex-wrap: wrap;
}

.cell {
     //removed flex attribute
  margin-bottom: 5px;
}

img {
    max-width: 100%;
    height: auto;
}

body {
    background-color: #dedede;
}

关于html - 在 CSS 中使用 Flexbox/Grid,使图像在移动设备上堆叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50027590/

相关文章:

javascript - <a> 标签在带有 window.location 的 div 中 - 为什么?

html - 如何在悬停时突出显示多个跨度标签?

javascript - 固定背景附件 CSS/XHTML/jQuery

css - 带有一些 LESS 魔法的花式媒体查询

CSS float 左图像列响应

javascript - 在循环中使用 xhr 对象进行 html5 分块上传

javascript - 运行代码后出现 Uncaught Error

html - 在父元素中间居中文本

css - 如何使悬停在非相邻元素或子元素上工作

html - 让 Chrome 中的 **所有** 网站拉伸(stretch)其内容以实际适应屏幕宽度(如 : remove all horizontal space on left/right of ALL pages)