html - 子 div 显示表格单元格固定宽度不起作用

标签 html css

我试图通过在子项上使用 display: table-cell 将子项 div 拉伸(stretch)到父项的全高,但我希望子项宽度保持在容器内并且子项忽略宽度属性,并且始终为 100% 宽度。

我尝试将 table-layout: fixed 添加到父级,为子级设置 max-width: 100px 但它似乎并不影响子级 div .

我有以下布局:

<div class="parent">

  <div class="child">

    //Content Here

  </div>

</div>

和下面的 CSS

.parent {
  display: table;
  width: 100%;
}

.child {
  display: table-cell;
  width: 100px;
}

示例:Fiddle

************** 更新 **************

我能够通过在 .parent 上使用 display: flex 和 flex-direction: column 以及在 .child 上使用 flex: 1 来实现所需的行为,但是由于浏览器兼容性,我不确定这是否是最佳解决方案。

查看更新 Fiddle

最佳答案

添加一个孙子 div

这是一个片段:

.parent {
    display: table;
    width: 100%;
    background: blue;
    min-height: 500px;
}
.child {
    display: table-cell;
    padding: 5% /* demo */
}
.grand-child {
    background: white;
    width: 100px;
}
<div class="parent">
    <div class="child">
        <div class="grand-child">Blue Background not visible due to .child taking width 100%</div>
    </div>
</div>

更新(基于 OP 评论)

Thank you, but then the child/grand-child div is not full height of the parent

所以这是一个解释/解决方案:

说明:

min-height 不适用于 table elements

In CSS 2.1, the effect of min-width and max-width on tables, inline tables, table cells, table columns, and column groups is undefined.

来自 MDN :

Applies to all elements but non-replaced inline elements, table columns, and column groups

browser compatibility

因此,您可以将 min-height 替换为 height,因为 table 总是拉伸(stretch)。

解决方案:

/* Both Single and Multiple Cells */

.parent {
  display: table;
  box-sizing:border-box; /*see vendor-prefixes */
  width: 100%;
  background: blue;
  height: 500px;
}
.child {
  display: table-cell;
}
.child .parent{
  width: 100px;
  background: white;
}


/* Multiple "Cells" extra */

  /*if you want to have more than one .child.parent display in same .row either you set them float:left or .display:inline-block (maybe with some margin-left/right) - added multiple justfor demo puposes */

.parent .multiple {
  float:left;
  margin-right:6%;
}
<h1> single "Cell" </h1>

<hr />

<div class="parent">
  <div class="child">
    <div class="parent">Blue Background not visible due to .child taking width 100%</div>
  </div>
</div>


<!--

HOW THIS CODE WORKS:

<table> // parent
    <tr> -- ommited in the code above --
        <td> //child
            <table> // child parent
            </table>
       </td>
    </tr>
</table>

-->

<h1> Multiple "Cells" </h1>

<hr />

<div class="parent">
  <div class="child">
    <div class="parent multiple">Blue Background not visible due to .child taking width 100%</div>
    <div class="parent multiple">Blue Background not visible due to .child taking width 100%</div>
    <div class="parent multiple">Blue Background not visible due to .child taking width 100%</div>
  </div>
</div>

<!--

HOW THIS CODE WORKS:

<table> // parent
    <tr> -- ommited in the code above --
        <td> //child
            <table> // child parent
            </table>
            <table> // child parent
            </table>
            <table> // child parent
            </table>
       </td>
    </tr>
</table>

-->

关于html - 子 div 显示表格单元格固定宽度不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31529726/

相关文章:

javascript - 当页面变大时滚动变慢

javascript - 元素之间的间距相等,同时保持第一个和最后一个与相对边缘对齐

javascript - 如何在没有 JQuery 的情况下使 Navbar 下拉选项工作

css - 样式化无序列表

php - 索引文件与包含文件冲突

html表格底部对齐问题

javascript - 更改 HTML 表格中特定列的背景颜色

html - 如何在 bootstrap 中创建一个 2 列的完美方形图像网格?

php - 如何在Controller Codeigniter中调用Modal Bootstrap?

javascript - 如何在 td 中 float div