Home >Web Front-end >CSS Tutorial >Why Doesn\'t \'display: table-column\' Work as Expected in CSS Table Layout?
Why Doesn't "display: table-column" Work in My CSS Layout?
Given the HTML and CSS code provided, you might expect to see a layout with two columns ("colLeft" and "colRight") and three rows ("row1", "row2", and "row3"). However, in browsers like Chrome and IE, the entire layout collapses to zero size.
This behavior arises from a fundamental misunderstanding of how the CSS table model works. While it resembles the HTML table model, there are crucial differences:
Correct HTML and CSS Structure:
To create a valid CSS table layout, you should use the following structure:
<code class="html"><div class="mytable"> <div class="mycolumn1"></div> <div class="mycolumn2"></div> <div class="myrow"> <div class="mycell">Contents of first cell in row 1</div> <div class="mycell">Contents of second cell in row 1</div> </div> <div class="myrow"> <div class="mycell">Contents of first cell in row 2</div> <div class="mycell">Contents of second cell in row 2</div> </div> </div></code>
<code class="css">.mytable { display: table; } .mycolumn1 { display: table-column; background-color: green; } .mycolumn2 { display: table-column; background-color: red; } .myrow { display: table-row; } .mycell { display: table-cell; }</code>
The above is the detailed content of Why Doesn\'t \'display: table-column\' Work as Expected in CSS Table Layout?. For more information, please follow other related articles on the PHP Chinese website!