2013-02-19 66 views
2

我想放在一起有一个网格的四个方格在中心的网页。这就是我所拥有的,我认为你将能够看到我想要发生的事情。我在这里错过了什么让定位工作,因为它应该?居中2x2网格

http://jsfiddle.net/LKfd4/

HTML

<div id='main-container'> 
<div class='box' id='topleft'></div> 
<div class='box' id='topright'></div> 
<div class='box' id='bottomleft'></div> 
<div class='box' id='bottomright'></div> 

</div> 

CSS

#main-container{ 
    border:1px solid black; 
    width:980px; 
    height:700px; 
    margin:0 auto; 
    position:relative; 
    top:20px; 
} 

.box { 
    width:100px; 
    height:100px; 
    background:gray; 
    border:2px solid #696969; 
    -webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */ 
    -moz-box-sizing: border-box; /* Firefox, other Gecko */ 
    box-sizing: border-box; 
} 

#topleft{ 
    position:relative; 
    margin:0 auto; 
    top:50%; 
    left:50%; 
    margin-top:-110px; 
    margin-left:-110px; 
} 
#topright{ 
    position:relative; 
    margin:0 auto; 
    top:50%; 
    left:50%; 
    margin-top:-110px; 
    margin-left:10px; 
} 
#bottomleft{ 
    position:relative; 
    margin:0 auto; 
    top:50%; 
    left:50%; 
    margin-top:10px; 
    margin-left:10px; 
} 
#bottomright{ 
    position:relative; 
    margin:0 auto; 
    top:50%; 
    left:50%; 
    margin-top:10px; 
    margin-left:-110px; 
} 
+0

可以将标记的变化,也可以只改变CSS? – 2013-02-19 21:34:39

+0

任何事情都可以改变,这只是概念 – ejfrancis 2013-02-19 21:35:02

回答

5

#topleft#topright#bottomleft#bottomright删除position: relative;。并且添加position: absolute;.box

的jsfiddle:http://jsfiddle.net/G85fM/

udpated的jsfiddle(至于@Neils评论):http://jsfiddle.net/G85fM/1/

+0

Bah的第一稿,你打败了我,但当你在这里时,我不妨建议将顶部和左侧样式移动到.box。 – Neil 2013-02-19 21:38:57

+0

啊,是的。除了位置风格以外,没有太注意任何事情。注意到并关注更新的小提琴。 – 2013-02-19 21:43:41

+0

你们很棒,谢谢! – ejfrancis 2013-02-19 21:55:13