由於您使用內嵌塊顯示您的方框,因此父項可能有:text-align:center;
,第二個方塊的負邊距爲自己的寬度,拉中間的第一個盒子。
http://codepen.io/gcyrillus/pen/ADsEx
背景顏色被用來示出其中框站立和標尺被添加到可視地檢查水平中心。
#top-menu {
display : inline-block;
width : 764px;
height : 55px;
background : green;
}
#social {
display : inline-block;
width : 100px;
height : 55px;
margin-right : -100px;
background : yellow;
}
#top-menu-wrapper {
text-align : center;
min-width : 990px;
background : purple;
}
div {
vertical-align : top;
padding : 5px 0;
}
.ruler {
width : 50%;
background : red;
margin : 5px 0 ;
}
這樣,你不介意父母的寬度,除非你想讓他們2總是在一行上。如果是這樣,那麼可以應用最小寬度:足夠寬。
另一種解決方案是增加的boxe 2. http://codepen.io/gcyrillus/pen/hipBl
body {
padding : 2em;
margin : 0;
}
#top-menu {
display : inline-block;
width : 764px;
height : 55px;
background : green;
}
#social, #top-menu-wrapper:before {
display : inline-block;
width : 100px;
height : 55px;
background : yellow;
}
#top-menu-wrapper:before {
content : '';
height : 0;
}
#top-menu-wrapper {
text-align : center;
min-width : 990px;
background : purple;
}
div {
vertical-align : top;
padding : 5px 0;
}
.ruler {
width : 50%;
background : red;
margin : 5px 0 ;
}
相同寬度的僞它爲我,我剛添加的顏色的div所以你可以看到它。 (http://jsfiddle.net/nbatothemax/G7uC9/)可能是問題是你有一個'#header' ID作爲每個這些容器的父代,當你沒有給所有的頭容器時他們。另外,如果你的元素有ID,你不需要使用父選擇器來找到它們,因爲你可以立即得到它們。 –