2013-08-07 51 views
-4

我有主容器940像素股利中心问题

用的宽度250像素每

与主容器我想这些框为中心对准主容器内的div 2。然而问题是第二个div动态添加。所以如果第二个div不在那里,第一个div应该居中。当第二个div被添加时,两者都应该按照940 px主div进行聆听。

我尝试了很多,但无法找到解决方案。请帮忙!

下面是简化的代码

CSS

div.box { 
    background: #EEE; 
    height: 400px; 
    width: 940px; 
} 
div.left { 
    background: #999; 
    float: left; 
    height: 390px; 
    width: 250px; 
} 
div.right { 
    background: #666; 
    height: 390px; 
    width: 250px; 
    float: left; 
} 
div.clear { 
    clear: both; 
} 

HTML

<div class="box"> 

     <div class="left">Tree</div> 
     <div class="right">View</div> (this div will dynamically add) 
     <div class="clear" /> 

    </div> 

感谢

+0

你的源代码在哪里? here image not display – falguni

+0

我无法发布图片,所以我编辑了我的评论。添加了参考代码。 –

回答

0

使这个removeclear:both;

http://jsfiddle.net/jWVxd/4/

CSS

div.box { 
    width: 940px; 
    background: #EEE; 
    height: 400px; 
    display: table; 
    padding:10px; 
} 
div.left { 
    background: #999; 
    display: table-cell; 
    width: 250px; 
} 
div.right { 
    background: #666; 
    display: table-cell; 
    width: 250px; 
} 
+1

OP要div在中心,检查了你的jsfiddle它没解决 –

+0

非常感谢。但是有没有一种解决方案可以在IE7中运行此代码。 –

0

试试这个在您的div.box CSS

margin:0px auto; 

对于演示CHK这一点JSFIDDLE DEMO

0

只需添加margin:auto;div.box,它会在中心

JS FIDDLE DEMO

编辑: 这里有隐藏的第二个DIV( “.right”)和它的做工精细

2nd Demo现在按Ctrl-(减号)查看其中心

+0

感谢您的回复。但是当你尝试删除第二个盒子时。第一个盒子不在你的例子中。 –