css - 显示 :table row 内 div 的水平居中

标签 css html centering

我有一个布局,由显示为表格的 div、3 个表格行和每行最多 5 个表格单元格组成。我的问题是双重的: 1)如果表格中的单元格少于五个,我无法弄清楚如何将表格单元格置于行的中心。,和 2) 当连续少于 5 个时,我不知道如何保持形状的完整性。

一行中的单元格数量是可变的(在 3 到 5 之间),我必须使用 div——我不能使用真正的表格。

这是一个代码笔:http://codepen.io/Jaemaz/full/aCboe

这是来源:

HTML:

<div class="focus-container">
  <div class="focus-row">
    <div class="focus-element  circle">
      <span class="ng-scope">
        Option A
      </span>
      </div>
      <div class="focus-element  circle">
        <span class="ng-scope">
          Option B
        </span>
      </div>
      <div class="focus-element  circle">
        <span class="ng-scope">
          Option C
        </span>
      </div>
      <div class="focus-element  circle">
        <span class="ng-scope">
          Option D
        </span>
      </div>
      <div class="focus-element  circle">
        <span class="ng-scope">
          Option E
        </span>
      </div>
  </div>
  <div class="focus-row">
    <div class="focus-element  circle" ng-class="{unknown: unknown, square: square, circle: circle, selected: selected, shouldHaveBeenSelected: shouldHaveBeenSelected, shouldNotHaveBeenSelected: shouldNotHaveBeenSelected}" ng-click="click()" ng-transclude="" focuschoice="" identifier="F" enabled="true">
      <span class="ng-scope">
        Option F
      </span>
  </div>
  </div>
</div>

CSS:

.focus-container {
width: 560px;
height: auto;
display: table;
background-color: #000;
border-spacing: 15px;
table-layout: fixed;
-moz-user-select: none;
-khtml-user-select: none;
-webkit-user-select: none;
user-select: none;
}

.focus-row {
width: 100%;
height: 100%;
display: table-row;
}

.focus-element {
width: 90px;
height: 90px;
display: table-cell;
color: #fff;
background: #808080;
border: 2px solid #BFBFBF;
vertical-align: middle;
text-align: center;
font-family: "Ubuntu", Arial, Helvetica, sans-serif;
font-size: 14px;
text-shadow: 0px 2px 1px rgba(0, 0, 0, 1);
max-width: 94px;
}

.focus-element.circle {
-moz-border-radius: 94px;
-webkit-border-radius: 94px;
border-radius: 94px;
}

屏幕截图: enter image description here

最佳答案

表格不会这样做,但表格中的表格会。 1) 在外部表中使用单个单元格行,在每个单元格中使用一个新表,并将内部表置于行中。 2) 您可以在内表中的每个单元格上使用固定的(或一些相对的)宽度和高度。

添加: 这将适用于 display:table 以及 old-school table。 最外层的表格每一行应该只有一个单元格,在那个单元格中放置内层表格。

关于css - 显示 :table row 内 div 的水平居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14218572/

相关文章:

javascript - Material UI 中的滑动来自屏幕边缘

html - 在国旗照片上过度使用

javascript onChange 来自另一个元素点击的输入

javascript - 广告不会在 ie 中居中,除非正文是文本对齐 :center

javascript - 将具有子元素(包含 imgs/iframe)的元素绝对居中,同时在调整大小时保持其纵横比

html - 在 CSS 图片库中设置统一的框高度

css - Chrome 浏览器中 css 网格的奇怪行为?

其他元素的 CSS 悬停触发器无法正常工作

javascript - 为什么 jquery 元素在 ipad 上没有响应?

html - 将左对齐的短文本 block 居中