Why should I use display:table instead of table

前端 未结 4 1751
说谎
说谎 2020-12-09 04:09

What\'s the benefits of structuring my site with divs and apply the display:table property ( display:tr, display:tr). Doesn\'t this mean that the divs will behave exactly li

相关标签:
4条回答
  • 2020-12-09 04:18

    The following explains why to use DIV over TABLE elements.

    Pros of Table Element: Most designers use table for a consistent look. Tables are also easy to maintain. Another advantage of table is that it is compatible with the most browsers.

    Cons of Table Element: All this comes with a cost: Too many nested tables increase page size and download time. More table elements push important content down so search spiders are less likely to add content to search engines.

    Pros of DIV Element: div with CSS we can achieve the same table based page structure and reduce the number of elements on the page, which allows the page to load faster. It also makes page more compatible with search engine spiders.

    Cons of DIV Element: The major drawback of this is not all CSS elements are not browser compatible. Because of this we have to write some custom CSS to resolve issues.

    full article : http://www.codeproject.com/KB/HTML/DIVwebsite.aspx

    display: table tells the element to display as a table. Nested elements should be displayed as table-row and table-cell, mimicking the good old TR's and TD's. There's also a display: table-column but it should show nothing at all, only serving for style information like a COL does. I'm not sure exactly how this works.

    more about div display style : http://www.quirksmode.org/css/display.html

    0 讨论(0)
  • 2020-12-09 04:22

    What's the benefits of structuring my site with divs and apply the display:table property ( display:tr, display:tr).

    None whatsoever in my opinion, except that you take away compatibility with older browsers. The idea that using DIVs with display: table-* is somehow better than <table>s is idiotic IMO, and the result of totally misguided hysteria against table elements. (Not attacking you @Nimo, just criticizing some people who have taken the "tables are evil" meme too far.)

    Tables are supposed to be used to represent tabular data, not to be misused for layouting.

    There are, however, certain abilities that tables have that are still very hard to simulate using pure CSS. You either need massive hacks and sometimes even JavaScript based workarounds to make those things work.

    You should design your layouts in a way that don't rely on those abilities.

    In some rare cases, you do need them. But then, it doesn't matter whether you use a proper <table><tr> or a brain-dead <div style="display: table"><div style="display: table-row"> (which one is more semantic and better readable by the way?)

    If you need display: table-* for your layout, you have one of those rare cases at hand, or you have painted yourself in a corner anyway. Either way, with a <table>, you at least get consistent browser support.

    0 讨论(0)
  • 2020-12-09 04:26

    Tables should ONLY be used to represent tabular data. Divs are containers to group content. Simple as that. display: table only gives you the layout properties of tables. But it's not compatible with <IE7 so for the sake of graceful degradation, avoid it.

    0 讨论(0)
  • 2020-12-09 04:45

    display: table; doesn't mean you turn divs into tables, it just makes that certain properties (like vertical-align) work where they normally wouldn't. It's not anything like using table for layout.

    0 讨论(0)
提交回复
热议问题