简体   繁体   English

将子 div 扩展到表 div 父级的高度

[英]Expand child div to heigh of table div parent

I know this "expanding child div to parent height"-thing is old and today we can solve things with flexbox and grids.我知道这个“将子 div 扩展到父级高度”是旧的,今天我们可以用 flexbox 和网格来解决问题。 But my problem is a bit different.但我的问题有点不同。 I need the format of a table with dynamic width values, depending on there content.我需要具有动态宽度值的表格格式,具体取决于内容。 So each row should decide it's height and each column it's width based on there contents.所以每一行都应该根据那里的内容决定它的高度和每一列的宽度。 Also the number of columns and rows is variable.列数和行数也是可变的。

That's why I can't use flexbox or grid based layout.这就是为什么我不能使用 flexbox 或基于网格的布局。 They are not dependent on each others width or height just of the height/width of there own children.它们不依赖于彼此的宽度或高度,仅依赖于自己孩子的高度/宽度。

Also: the table has some other tags inside.另外:表格里面还有一些其他的标签。 Not just text!不仅仅是文字!

Because of that I can't use a normal <table> tag.因此,我不能使用普通的<table>标签。 It's not allowed to use other html tags inside a table cell tag.不允许在表格单元标签内使用其他 html 标签。

So I decided to use <div> tags and set them to tables via css.所以我决定使用<div>标签并通过 css 将它们设置为表格。

 .table { background: #bbbb; display: table; }.table-row { display: table-row; }.table-cell { border: 1px solid black; display: table-cell; }.content { background: #f005; }
 <div class="table"> <div class="table-row"> <div class="table-cell"><div class="content">1 / 1</div></div> <div class="table-cell"><div class="content">1 / 2</div></div> <div class="table-cell"><div class="content">1 / 3</div></div> </div> <div class="table-row"> <div class="table-cell"><div class="content">2 / 1</div></div> <div class="table-cell"> <div class="content">very long content <br /> with line break and all the stuff</div> </div> <div class="table-cell"><div class="content">2 / 3</div></div> </div> <div class="table-row"> <div class="table-cell"><div class="content">Another very long <br /> line <br /> breaking <br /> content and ither <span>html tags</span></div></div> <div class="table-cell"><div class="content">3 / 2</div></div> <div class="table-cell"><div class="content">3 / 3</div></div> </div> </div >

The challenge now is to expand all the <div class="content"> tags to the full width and height of the parent/table-cell.现在的挑战是将所有<div class="content">标签扩展到父/表格单元格的完整宽度和高度。

Note: I can't use flexbox here because display: is already used to show the div as a table cell.注意:我不能在这里使用 flexbox 因为display:已经用于将 div 显示为表格单元格。

add height:100% to the content, table-cell and tableheight:100%添加到内容、表格单元格和表格中

 .table { background: #bbbb; display: table; height: 100%; }.table-row { display: table-row; }.table-cell { border: 1px solid black; display: table-cell; height: 100%; }.content { background: #f005; height: 100%; }
 <div class="table"> <div class="table-row"> <div class="table-cell"> <div class="content">1 / 1</div> </div> <div class="table-cell"> <div class="content">1 / 2</div> </div> <div class="table-cell"> <div class="content">1 / 3</div> </div> </div> <div class="table-row"> <div class="table-cell"> <div class="content">2 / 1</div> </div> <div class="table-cell"> <div class="content">very long content <br /> with line break and all the stuff</div> </div> <div class="table-cell"> <div class="content">2 / 3</div> </div> </div> <div class="table-row"> <div class="table-cell"> <div class="content">Another very long <br /> line <br /> breaking <br /> content and ither <span>html tags</span></div> </div> <div class="table-cell"> <div class="content">3 / 2</div> </div> <div class="table-cell"> <div class="content">3 / 3</div> </div> </div> </div>

Related: height: 100% for <div> inside <div> with display: table-cell相关: <div> 内 <div> 的高度:100% 显示:table-cell

Grid could also be an option and it would need less markup:网格也可以是一种选择,它需要更少的标记:

 .grid { background: #bbbb; display: grid }.c3 { grid-template-columns: repeat(3, auto) }.shrink { width: max-content; /* behave alike a table */ margin: auto; }.content { border: solid 1px; background: #f005; padding:0.25em; }.grid~.grid.content { margin: 2px; /* mimic border-spacing*/ }
 <div class="grid c3 shrink"> <!--<div class="table-row"> <div class="table-cell">--> <div class="content">1 / 1</div> <!--</div> <div class="table-cell">--> <div class="content">1 / 2</div> <!--</div> <div class="table-cell">--> <div class="content">1 / 3</div> <!--</div> </div> <div class="table-row"> <div class="table-cell">--> <div class="content">2 / 1</div> <!--</div> <div class="table-cell"> --> <div class="content">very long content <br /> with line break and all the stuff</div> <!--</div> <div class="table-cell">--> <div class="content">2 / 3</div> <!--</div> </div> <div class="table-row"> <div class="table-cell">--> <div class="content">Another very long <br /> line <br /> breaking <br /> content and ither <span>html tags</span></div> <!--</div> <div class="table-cell">--> <div class="content">3 / 2</div> <!--</div> <div class="table-cell">--> <div class="content">3 / 3</div> <!--</div> </div>--> </div> <hr> <p>cleaned up html </p> <div class="grid c3 shrink"> <div class="content">1 / 1</div> <div class="content">1 / 2</div> <div class="content">1 / 3</div> <div class="content">2 / 1</div> <div class="content">very long content <br /> with line break and all the stuff</div> <div class="content">2 / 3</div> <div class="content">Another very long <br /> line <br /> breaking <br /> content and ither <span>html tags</span></div> <div class="content">3 / 2</div> <div class="content">3 / 3</div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM