Is there a disadvantage of using `display:table-cell`on divs?

自闭症网瘾萝莉.ら 提交于 2019-11-26 09:45:36

问题


What I am trying to accomplish is having a fixed-width first div and a fluid second div which will fill up the rest width of the parent div\'s width.

<div class=\'clearfix\'>
  <div style=\'float:left; width:100px;\'>some content</div>
  <div style=\'float:left\'>some more content</div>
</div>

and on this one everything seems alright and fluid.

<div style=\'display:table\'>
  <div style=\'display:table-cell; width:100px;\'>some content</div>
  <div style=\'display:table-cell\'>some more content</div>
</div>

I want to go ahead with the second one but i feel like the second example will give me headaches in the future.

Could you offer some suggestions or insights?


回答1:


display: table-cell is perfectly fine to use, with just one downside..

It doesn't work in IE7 (or IE6, but who cares?): http://caniuse.com/#search=css-table

If you don't need to support IE7, then feel free to use it.

IE7 still has some usage, but you should check your Analytics, and then make a decision.


To answer your specific use case, you can do it without display: table-cell, provided that you don't need the height to adjust based on content:

http://jsfiddle.net/g6yB4/

<div class='clearfix'>
  <div style='float:left; width:100px; background:red'>some content</div>
  <div style='overflow:hidden; background:#ccc'>some more content</div>
</div>

(why overflow: hidden? With: http://jsfiddle.net/g6yB4/3/ vs without: http://jsfiddle.net/g6yB4/4/)




回答2:


You could do something like this. It puts your main content first. You can use a vertically repeating css background image on your main "content" container to create the illusion of a background running all the way down the left column.

<div id="content" style="clear:both;">
    <div id="mainwrap" style="float:left; width:100%;">
        <div id="main" style="margin-left:100px">
            Main content here
        </div>
    </div>
    <div id="leftnav" style="float:left; width:100px; margin-left:-100%;">
        Left content here
    </div>
</div>

To extend to a 3-column with fluid center:

<div id="content" style="clear:both;">
    <div id="mainwrap" style="float:left; width:100%;">
        <div id="main" style="margin-left:100px; margin-right:100px;">
            Main content here
        </div>
    </div>
    <div id="leftnav" style="float:left; width:100px; margin-left:-100%;">
        Left content here
    </div>
    <div id="rightnav" style="float:left; width:100px; margin-left:-100px;">
        Right content here
    </div>
</div>



回答3:


To get the first example working, you should also float the containing div, this will make sure that both of the elements within sit as you would expect within it. Not really sure what you mean by 'is a pain', though?




回答4:


One down side of using table-row (very related to the OP) is that you can't use margin/padding on a row.



来源:https://stackoverflow.com/questions/6307934/is-there-a-disadvantage-of-using-displaytable-cellon-divs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!