2014-03-27 165 views
3

This jsfiddle說明了我在爲自己設計頁面時遇到的問題。具有幾乎相同CSS位置的兩個div不對齊

我有兩個div(.list和.piece),每個都在一個較大的包含div(.list-bkg和.piece-bkg)中,以創建半透明背景,覆蓋整個屏幕,當其中一個列表項被點擊並且.piece div滑落。 .list和.piece的CSS在它們的定位方面是相同的(至少是垂直方向),並且兩個-bkg div也具有相同的垂直位置。

儘管如此,出於某種原因,兩個內部divs的頂部並不對齊。我嘗試過玩弄每種遊戲的浮動效果,以及玩具箱的大小( as recommended here),以防萬一出現輕微的數學問題,但似乎沒有任何效果。這是一個相對/絕對定位的簡單問題嗎?我真的很擔心發生了什麼事情。所有幫助讚賞!

這裏是受影響的HTML代碼:

<div class="list-bkg"> 
    <div class="list nav"> 
     <span>serious 1</span><br /> 
     <span>serious 2</span><br /> 
     <span>serious 3</span><br /> 
    </div> 
</div> 

<div class="piece-bkg"> 
    <div class="piece"> 
     <iframe width="560" height="315" src="http://www.youtube.com/embed/7TEq3iyifpQ" frameborder="0" allowfullscreen></iframe> 
     <br /> <br /> 
     important info<br/> 
     helpful<br /> 
     cool<br /> 
    </div> 
</div> 

和CSS:

html, body { 
margin: 0px; 
padding: 0px; 
width: 100%; 
height: 100%; 
min-width: 1024px; 
min-height: 768px; 
} 
.list-bkg { 
float: left; 
position: absolute; 
top: 0px; 
z-index: 75; 
width: 36.29%; 
padding-top: 10%; 
height: 90%; 
background-color: rgba(0, 0, 0, 0.65); 
font-family: monospace; 
color: white; 
text-align: center; 
} 
.piece-bkg{ 
float: left; 
position: absolute; 
top: 0px; 
z-index: 75; 
width: 63.71%; 
padding-top: 10%; 
height: 90%; 
background-color: rgba(0, 0, 0, 0.65); 
font-family: monospace; 
color: white; 
left: 36.29%; 
display: none; 
} 
.piece { 
position: relative; 
margin-top: 5.8%; 
height: 74%; 
left: 2%; 
padding-top: 2%; 
padding-right: 2%; 
font-size: 18px; 
background-color: rgba(211, 58, 146, 0.8); 
float: left; 
text-align: center; 
max-width: 600px; 
padding-left: 2%; 
} 
.list { 
position: relative; 
margin-top: 5.8%; 
height: 74%; 
left: 8.97%; 
width: 82%; 
padding-top: 2%; 
padding-right: 2%; 
font-size: 18px; 
text-align: right; 
background-color: rgba(211, 58, 146, 0.8); 
} 
+0

您的子元素將其樣式設置爲其容器的百分比,但容器的大小並不相同。沒有理由期望孩子的身材相同。 –

+0

@StephenThomas他們的身高是相同的,所以他們身高的百分比應該是相同的,對嗎?或者它是由div的總面積的百分比? – qwentl

+0

'margin-top:5.8%;''padding-top:2%;'根據**寬度計算** –

回答

1

在你們的款式中,div s pricelistmargin-top:5.8%。但list-bkgwidth 36.29%和piece-bkgwidth 63.71%。所以margin-top爲div price將爲width 63.71%和margin-top 5.8%的列表將爲寬度36.39%的5.8%的5.8%。因此,這兩個div的margin-top值將會不同(因爲padding基於寬度和高度),這就是爲什麼兩個div頂部不對齊的原因。爲了解決這個問題要麼降低.pricemargin-top或增加.list

.price{ 
    margin-top:3.3%;// change this as you need 
} 

OR

.list{ 
    margin-top:10.1%; // change this as you need 
} 

同其他的解決辦法是增加margin-top以像素爲單位,那麼無論是div旨意正確對齊。

+0

究竟如何根據寬度計算邊距?我希望能夠使用數學精確地排列它們。 – qwentl

+0

是的邊距和填充是基於包含塊的寬度計算的。這些鏈接可能會有所幫助http://stackoverflow.com/questions/11003911/why-are-margin-padding-percentages-in-css-always-calculated-against-width,http://mattsnider.com/css-using -Percent換容限和填充/ – James

0

我們根據您的百分比幅度最高,但2容器是不同的(因爲填充是基於寬度和高度),如果將相同的邊界放在像素中,則兩個容器將對齊

 html, body { 
     margin: 0px; 
     padding: 0px; 
     width: 100%; 
     height: 100%; 
     min-width: 1024px; 
     min-height: 768px; 
    } 
    .list-bkg { 
     float: left; 
     position: absolute; 
     top: 0px; 
     z-index: 75; 
     width: 36.29%; 
     padding-top: 10%; 
     height: 90%; 
     background-color: rgba(0, 0, 0, 0.65); 
     font-family: monospace; 
     color: white; 
     text-align: center; 
    } 

    .piece-bkg{ 
     float: left; 
     position: absolute; 
     top: 0px; 
     z-index: 75; 
     width: 63.71%; 
     padding-top: 10%; 
     height: 90%; 
     background-color: rgba(0, 0, 0, 0.65); 
     font-family: monospace; 
     color: white; 
     left: 36.29%; 
     display: none; 
    } 

    .piece { 
     position: relative; 
     margin-top: 10px; 
     height: 74%; 
     left: 2%; 
     padding-top: 2%; 
     padding-right: 2%; 
     font-size: 18px; 
     background-color: rgba(211, 58, 146, 0.8); 
     float: left; 
     text-align: center; 
     max-width: 600px; 
     padding-left: 2%; 
    } 

    .list { 
     position: relative; 
     margin-top: 10px; 
     height: 74%; 
     left: 8.97%; 
     width: 82%; 
     padding-top: 2%; 
     padding-right: 2%; 
     font-size: 18px; 
     text-align: right; 
     background-color: rgba(211, 58, 146, 0.8); 
    }