I recently noticed that it is possible to use display: table-cell;
on an element without surrounding it with an element using display: table-row;
Example (working in IE, Chrome, FF, Safari, Opera):
<div style="display: table;"> <div style="display: table-cell;"> Text </div> </div>
Is this considered bad style? Should the table-cell be surrounded by a table-row or is it not necessary?
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.
A hidden attribute on a <tr> tag hides the table row. Although the table row is not visible, its position on the page is maintained.
The primary method of making a responsive table is to add a class table table-responsive inside the table tag. To make a table responsive, we wrap the whole table inside a container tag like div with property overflow-x equals to auto .
You can use center tag to allign the table at center inside the div.
No. It does not need to be used with display: table-row
. Read here.
table-cell
just represents a <td>
or <th>
:
Specifies that an element represents a table cell.
And specifically regarding table-cell
:
For example, an image that is set to 'display: table-cell' will fill the available cell space, and its dimensions might contribute towards the table sizing algorithms, as with an ordinary cell.
From Everything You Know About CSS Is Wrong :
Anonymous Table Elements
CSS tables happily abide by the normal rules of table layout, which enables an extremely powerful feature of CSS table layouts: missing table elements are created anonymously by the browser. The CSS2.1 specification states:
Document languages other than HTML may not contain all the elements in the CSS 2.1 table model. In these cases, the “missing” elements must be assumed in order for the table model to work. Any table element will automatically generate necessary anonymous table objects around itself, consisting of at least three nested objects corresponding to a “table”/“inline-table” element, a “table-row” element, and a “table-cell” element.
What this means is that if we use
display: table-cell;
without first containing the cell in a block set todisplay: table-row;
, the row will be implied—the browser will act as though the declared row is actually there.
So no, there's nothing wrong with using display:table-cell
without containing display:table-row
. The CSS it produces is perfectly valid and has some very interesting use cases. For more info on using anonymous table Elements, see the article I'm quoting from.
The fact that it produces valid CSS doesn't mean that the behavior of display:table-cell
without display:table-row
or display:table-row
is stable. Even today (February 2016), the behavior of anoymous table elements remain inconsistent across browsers. See Inconsistent behavior of display: table and display: table-cell for more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With