html - 显示宽度为 : table. 的等高列

标签 html css

试图找出列宽不同的原因,以及如何使它们等宽。

.col-container {
  display: table;
  width: 100%;
  margin: 0 auto;
}

.col {
  display: table-cell;
}
<div class="col-container">
  <div class="col col1">
    <h2>Column 1</h2>
    <p>Hello World</p>
  </div>

  <div class="col col2">
    <h2>Column 2</h2>
    <p>Hello World!</p>
  </div>

  <div class="col col3">
    <h2>Column 3</h2>
    <p>Some other text..</p>
  </div>
</div>

最佳答案

您可以使用 table-layout:fixed; 属性,它应该均匀地喷洒列(问题关键字: 等于宽度)。

您还可以添加一个 width 来保护它。 border-spacing 如果您也使用它,则应包含在 width 计算中。

https://www.w3.org/TR/CSS2/tables.html#propdef-table-layout

17.5.2.1 Fixed table layout

With this (fast) algorithm, the horizontal layout of the table does not depend on the contents of the cells; it only depends on the table's width, the width of the columns, and borders or cell spacing.

.col-container {
  display: table;
  width: 100%;
  margin: 0 auto;
  table-layout: fixed;
}

.col {
  display: table-cell;
  border: solid;
}

.w {
  margin-top: 1em;
  border-spacing: 2px;
}
/* add width and border-spacing */
.w.col {
  width: 33.33%;
  box-sizing: border-box;
}
<div class="col-container">
  <div class="col col1">
    <h2>Column 1</h2>
    <p>Hello World</p>
  </div>

  <div class="col col2">
    <h2>Column 2</h2>
    <p>Hello World!</p>
  </div>

  <div class="col col3">
    <h2>Column 3</h2>
    <p>Some other text.. Some other text.. Some other text.. Some other text..</p>
  </div>
</div>

<div class="col-container w">
  <div class="col col1">
    <h2>Column 1</h2>
    <p>Hello World</p>
  </div>

  <div class="col col2">
    <h2>Column 2</h2>
    <p>Hello World!</p>
  </div>

  <div class="col col3">
    <h2>Column 3</h2>
    <p>Some other text.. Some other text.. Some other text.. v Some other text..</p>
  </div>
</div>

关于html - 显示宽度为 : table. 的等高列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47143751/

相关文章:

html - CSS 计数器在 div 内的子项中不起作用

javascript - 使用 jQuery 在 DIV 中包装两个 HTML 元素

javascript - 单击链接后需要更改 td 的背景图像大小

html - 当内容改变时动态改变元素的宽度

css - DIV 中控件的定位

jquery - 停止对子元素说 anchor 标记的父 div onclick 函数

html - 菜单悬停在 Windows 8 Phone 和 Windows Surface 上不起作用

html - 如何使用ajax设置图片的src?

html - 在其他容器和东西上创建小标签

html - 使用 css 不显示图片