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

后端 未结 4 544
再見小時候
再見小時候 2020-12-13 04:37

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 conten

4条回答
  •  离开以前
    2020-12-13 04:57

    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:

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

    HTML

    CSS

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

    As you can see in the JSFiddle 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!

    http://jsfiddle.net/blyzz/1djs97yv/1/

    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.

提交回复
热议问题