Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding display:table-cell; functioning

Tags:

Please note that I am not trying to resolve any specific issue, but trying to understand what's causing this issue.

I have set the width, height and display of some divs, but the height/width settings are not being honored. The text is also being pushed downward.

http://jsfiddle.net/k7esv/

1) Why does it push the text downward when height is set in table-row then BUT when height is removed, it places text at the top?

2) Why are the width/height settings not honored?

3) Why doesn't setting the margin property have any effect on them either?

http://jsfiddle.net/k7esv/1/

like image 226
Shawn Taylor Avatar asked Feb 25 '12 09:02

Shawn Taylor


People also ask

What does display table cell do?

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 TD table cell?

The <td> tag defines a standard data cell in an HTML table. An HTML table has two kinds of cells: Header cells - contains header information (created with the <th> element) Data cells - contains data (created with the <td> element)

What is display table column?

The "table-column" display type means it acts like the <col> tag in HTML - i.e. an invisible element whose width* governs the width of the corresponding physical column of the enclosing table.


2 Answers

1) This seems to be a rendering issue specific to Firefox. Setting the vertical-align property on the divs fixes it. top, middle, or bottom all seem to work. I don't understand myself what FF is doing when there is a height but no vertical-align set; it might be a bug.

2) The width and height are honored, but they are subject to table sizing rules. When a table does not have enough room to give each of the cells the width they have specified, it will give more room to cells that have more content. This is what was happening with your example. If you look at my example below, you will see that when the parent element is wider than the sum total of the table cells' widths, the cells respect the width. The height should always work (except in the case of the FF rendering issue I mentioned above).

3) Table cells don't have margins. Use border-spacing and display:table on a parent div.

http://jsfiddle.net/chad/k7esv/3/

like image 73
Chad von Nau Avatar answered Oct 13 '22 16:10

Chad von Nau


I will just add (seeing as it seems to have been missed) that setting the heights on individual table cells in a table row can be pointless, as all cells in the same table row will become the same height as the tallest cell in said row.

Having said that, heights may want to be added for when dynamic content is served to different cells, meaning their heights fluctuate. It may be that you want to set a particular cell to never be less than height X, which will only come in to effect when another certain cell has less content.

like image 24
mr_reamer Avatar answered Oct 13 '22 16:10

mr_reamer