html - CSS:响应式 3x3 网格在调整窗口大小时搞砸了

标签 html css responsive-design

我正在尝试制作一个响应式 3x3 网格来放置我的零点和十字游戏。 当我有一个最大化的屏幕时,它对我有用,但是一旦我缩小窗口以检查响应能力,一切都会变得困惑。 这是怎么回事?我应该使用 vh 以外的东西吗? http://codepen.io/apswak/pen/dXdjvW

<h3>Noughts and Crosses</h3>
<div class="gameGrid">
  <div class="cell"></div>
  <div class="cell"></div>
  <div class="cell"></div>
  <div class="cell"></div>
  <div class="cell"></div>
  <div class="cell"></div>
  <div class="cell"></div>
  <div class="cell"></div>
  <div class="cell"></div>
</div>

样式.css

body {
  background-color: wheat;
  margin: 0;
  padding: 0;
}

.gameGrid {
  margin: 0 auto;
  padding: 0;
  width: 62vh;
  height: 62vh;
}

.cell {
  margin-bottom: -5px;
  display: inline-block;
  width: 18.82vh;
  height: 18.82vh;
  border: 5px solid green;
  background-color: blue;
}

最佳答案

我可能会选择 flexbox路线。我保留了您的 gameGrid 类,但添加了一个行类,在每一行内,我添加了 3 个单元格。不用担心 naming除非你愿意。

.gameGrid {
  margin: 0 auto;
  padding: 0;
  width: 62vh;
  height: 62vh;
  background: green;
}
.gameGrid--row {
  display: flex;
  flex-flow: row;
  justify-content: space-around;
}
.gameGrid--cell {
  background: blue;
  margin: 5px;
  flex: 1 0 auto;
}
.gameGrid--cell:before {
  content: '';
  float: left;
  padding-top: 100%;
}
<div class="gameGrid">
  <div class="gameGrid--row">
    <div class="gameGrid--cell"></div>
    <div class="gameGrid--cell"></div>
    <div class="gameGrid--cell"></div>
  </div>
  <div class="gameGrid--row">
    <div class="gameGrid--cell"></div>
    <div class="gameGrid--cell"></div>
    <div class="gameGrid--cell"></div>
  </div>
  <div class="gameGrid--row">
    <div class="gameGrid--cell"></div>
    <div class="gameGrid--cell"></div>
    <div class="gameGrid--cell"></div>
  </div>
</div>

关于html - CSS:响应式 3x3 网格在调整窗口大小时搞砸了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38457272/

相关文章:

javascript - JQuery 验证消息奇怪地出现在两个单选按钮之间

html - 将填充插入 Bootstrap 列网格

javascript - 子菜单的问题

JQuery 不在 $(document).change() 上执行

html - 如何制作 1,2,3 行网格的响应式

html - 如何设置已由其父项设置样式的嵌套元素的样式

html - @media 删除顶部 :0; from style

html - 在响应式布局中使用 CSS 排列具有交替元素计数的行

javascript - 使 Highcharts.js 图表在移动和桌面上看起来不错

javascript - 响应式菜单打开