Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How (and why) to use display: table-cell (CSS)

I have a site with a very active background (I'm talking 6 or so different z-indexes here 2 with animations). I wanted a in the foreground that had content but wanted a "window" through to the background in it. Some problems I had:

  1. you can't "punch a hole" in a background, so...

    • I built a containing div, lets call it "srminfo"
    • Inside that I had a "top", "left", "window", "right" and "bottom"
    • the top, left, right, bottom all had opaque white backgrounds
    • while the srminfo and window divs had background:none;
  2. No matter how hard I tried, the "right" div wouldn't fill the space between the "top" and "bottom" divs, I tried a lot of different things. The reason it had to be dynamic is that the text in the "left" div was dynamic based on the background colour, which was itself generated randomly with JavaScript.

How is display: table; and all the other related CSS code like tables? And how can it be used?

like image 248
Jim Avatar asked Mar 24 '15 09:03

Jim


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 display property used for?

The Display property in CSS defines how the components(div, hyperlink, heading, etc) are going to be placed on the web page. As the name suggests, this property is used to define the display of the different parts of a web page. It is used to displays an element as an inline 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. See the W3C standard for more information about the CSS table model. * And a few other properties like borders, backgrounds.

What is the use of display inline-block in CSS?

The display: inline-block Value Compared to display: inline , the major difference is that display: inline-block allows to set a width and height on the element. Also, with display: inline-block , the top and bottom margins/paddings are respected, but with display: inline they are not.


2 Answers

After days trying to find the answer, I finally found

display: table;

There was surprisingly very little information available online about how to actually getting it to work, even here, so on to the "How":

To use this fantastic piece of code, you need to think back to when tables were the only real way to structure HTML, namely the syntax. To get a table with 2 rows and 3 columns, you'd have to do the following:

<table>     <tr>         <td></td>         <td></td>         <td></td>     </tr>     <tr>         <td></td>         <td></td>         <td></td>     </tr> </table> 

Similarly to get CSS to do it, you'd use the following:

HTML

<div id="table">     <div class="tr">         <div class="td"></div>         <div class="td"></div>         <div class="td"></div>     </div>     <div class="tr">         <div class="td"></div>         <div class="td"></div>         <div class="td"></div>     </div> </div> 

CSS

#table{      display: table;  } .tr{      display: table-row;  } .td{      display: table-cell; } 

As you can see in the example below, the divs in the 3rd column have no content, yet are respecting the auto height set by the text in the first 2 columns. WIN!

#table {     display: table;     padding: 5px; } .tr {     display: table-row;     padding: 5px; } .td {     display: table-cell;     padding: 5px;     width: 150px;     border: #000000 solid 1px;     margin: 5px; }
<div id="table">     <div class="tr">         <div class="td">Row 1,             <br />Column 1</div>         <div class="td">Row 1, Column 2</div>         <div class="td" style="background:#888888;"></div>     </div>     <div class="tr">         <div class="td">Row 2,             <br />Column 1</div>         <div class="td">Row 2, Column 2</div>         <div class="td" style="background:#888888;"></div>     </div> </div>

It's worth noting that display: table; does not work in IE6 or 7 (thanks, FelipeAls), so depending on your needs with regards to browser compatibility, this may not be the answer that you are seeking.

like image 186
Jim Avatar answered Oct 08 '22 21:10

Jim


It's even easier to use parent > child selector relationship so the inner div do not need to have their css classes to be defined explicitly:

.display-table {      display: table;   }  .display-table > div {       display: table-row;   }  .display-table > div > div {       display: table-cell;      padding: 5px;  }
<div class="display-table">      <div>          <div>0, 0</div>          <div>0, 1</div>      </div>      <div>          <div>1, 0</div>          <div>1, 1</div>      </div>  </div>
like image 42
Dmitriy Sintsov Avatar answered Oct 08 '22 20:10

Dmitriy Sintsov