Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is a CSS "display: table-column" supposed to work?

Given the following HTML and CSS, I see absolutely nothing in my browser (Chrome and IE latest at time of writing). Everything collapses down to 0x0 px. Why?

<!DOCTYPE html> <html> <head>     <style type="text/css">         section { display: table; height: 100%; background-color: grey; }          #colLeft { display: table-column; height: 100%; background-color: green; }         #colRight { display: table-column; height: 100%; background-color: red; }          #row1 { display: table-row; height: 100%; }         #row2 { display: table-row; height: 100%; }         #row3 { display: table-row; height: 100%; }          #cell1 { display: table-cell; height: 100%; }         #cell2 { display: table-cell; height: 100%; }         #cell3 { display: table-cell; height: 100%; }     </style> </head> <body>     <section>         <div id="colLeft">             <div id="row1">                 <div id="cell1">                     AAA                 </div>             </div>             <div id="row2">                 <div id="cell2">                     BBB                 </div>             </div>         </div>         <div id="colRight">             <div id="row3">                 <div id="cell3">                     CCC                 </div>             </div>         </div>     </section> </body> </html> 
like image 832
Eliot Avatar asked Oct 01 '11 00:10

Eliot


People also ask

What does display table cell do in CSS?

Setting display to table makes the element behave like a table. So you can make a replica of an HTML table without using the table element and corresponding elements such as tr and td . For example, in HTML, you can make a table with the <table> element and also a <div> , or any container of your choice.

What is table-layout fixed?

When table-layout: fixed is applied, the content no longer dictates the layout, but instead, the browser uses any defined widths from the table's first row to define column widths. If no widths are present on the first row, the column widths are divided equally across the table, regardless of content inside the cells.


1 Answers

The CSS table model is based on the HTML table model http://www.w3.org/TR/CSS21/tables.html

A table is divided into ROWS, and each row contains one or more cells. Cells are children of ROWS, they are NEVER children of columns.

"display: table-column" does NOT provide a mechanism for making columnar layouts (e.g. newspaper pages with multiple columns, where content can flow from one column to the next).

Rather, "table-column" ONLY sets attributes that apply to corresponding cells within the rows of a table. E.g. "The background color of the first cell in each row is green" can be described.

The table itself is always structured the same way it is in HTML.

In HTML (observe that "td"s are inside "tr"s, NOT inside "col"s):

<table ..>   <col .. />   <col .. />   <tr ..>     <td ..></td>     <td ..></td>   </tr>   <tr ..>     <td ..></td>     <td ..></td>   </tr> </table> 

Corresponding HTML using CSS table properties (Note that the "column" divs do not contain any contents -- the standard does not allow for contents directly in columns):

.mytable {    display: table;  }  .myrow {    display: table-row;  }  .mycell {    display: table-cell;  }  .column1 {    display: table-column;    background-color: green;  }  .column2 {    display: table-column;  }
<div class="mytable">    <div class="column1"></div>    <div class="column2"></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>


OPTIONAL: both "rows" and "columns" can be styled by assigning multiple classes to each row and cell as follows. This approach gives maximum flexibility in specifying various sets of cells, or individual cells, to be styled:

//Useful css declarations, depending on what you want to affect, include:    /* all cells (that have "class=mycell") */  .mycell {  }    /* class row1, wherever it is used */  .row1 {  }    /* all the cells of row1 (if you've put "class=mycell" on each cell) */  .row1 .mycell {  }    /* cell1 of row1 */  .row1 .cell1 {  }    /* cell1 of all rows */  .cell1 {  }    /* row1 inside class mytable (so can have different tables with different styles) */  .mytable .row1 {  }    /* all the cells of row1 of a mytable */  .mytable .row1 .mycell {  }    /* cell1 of row1 of a mytable */  .mytable .row1 .cell1 {  }    /* cell1 of all rows of a mytable */  .mytable .cell1 {  }
<div class="mytable">    <div class="column1"></div>    <div class="column2"></div>    <div class="myrow row1">      <div class="mycell cell1">contents of first cell in row 1</div>      <div class="mycell cell2">contents of second cell in row 1</div>    </div>    <div class="myrow row2">      <div class="mycell cell1">contents of first cell in row 2</div>      <div class="mycell cell2">contents of second cell in row 2</div>    </div>  </div>

In today's flexible designs, which use <div> for multiple purposes, it is wise to put some class on each div, to help refer to it. Here, what used to be <tr> in HTML became class myrow, and <td> became class mycell. This convention is what makes the above CSS selectors useful.

PERFORMANCE NOTE: putting class names on each cell, and using the above multi-class selectors, is better performance than using selectors ending with *, such as .row1 * or even .row1 > *. The reason is that selectors are matched last first, so when matching elements are being sought, .row1 * first does *, which matches all elements, and then checks all the ancestors of each element, to find if any ancestor has class row1. This might be slow in a complex document on a slow device. .row1 > * is better, because only the immediate parent is examined. But it is much better still to immediately eliminate most elements, via .row1 .cell1. (.row1 > .cell1 is an even tighter spec, but it is the first step of the search that makes the biggest difference, so it usually isn't worth the clutter, and the extra thought process as to whether it will always be a direct child, of adding the child selector >.)

The key point to take away re performance is that the last item in a selector should be as specific as possible, and should never be *.

like image 86
ToolmakerSteve Avatar answered Oct 03 '22 00:10

ToolmakerSteve