0
非常簡單的概念:自適應設計,垂直和水平對準一個div內一個div
- DIV-1具有90%的寬度和1000像素的高度,
- DIV-2具有的寬度20%(div-1)沒有高度。
我可以使用CSS在div-1內水平和垂直居中div-2嗎?
非常簡單的概念:自適應設計,垂直和水平對準一個div內一個div
我可以使用CSS在div-1內水平和垂直居中div-2嗎?
確實如此。外容器上的內部使用position:relative;
,position:absolute
,和幾個樣式(見下表)
.outer {
width:90%;
height:1000px;
position:relative;
}
.inner {
height:2px; /* To make it visible */
width:20%;
position:absolute;
left:40%;
top:50%;
transform:translateY(-50%); /* Optional; positions it perfectly in center.
Alternatives include using negative margins
or javascript to compensate.Needs some prefixes*/
}
我不知道爲什麼你要一個元素具有的高度0 ...
如果div2是可變的,我不確定你能做到 - 需要JS來計算它的高度,才能夠應用偏移量 – Trent