我想通過translateY來定位父DIV中的子DIV。 但是,如果子DIV不是位置:絕對或沒有高度屬性的父DIV,它只是用盡父DIV。鉻翻譯工作不正確
HTML
<div class="parent">
<div class="img-wrap"></div>'
<div class="child"></div>
</div>
CSS
.parent{
/*
height:190px;
*/
background-color:#ccc;
padding:20px;
-webkit-transform-style: preserve-3d;
-moz-transform-style: preserve-3d;
transform-style: preserve-3d;
}
.parent:after{
clear:both;
content:"";
display:block;
}
.img-wrap{
background-color:blue;
float:left;
height:150px;
width:200px;
}
.child{
background-color:red;
height:100px;
/*
position:absolute;
*/
position:relative;
top:50%;
-webkit-transform: translateY(-50%);
-ms-transform: translateY(-50%);
transform: translateY(-50%);
}
這裏有一個樣本 Position middle vertically
感謝@LSSon,原因是我使用position:relative,因爲子DIV內的內容會被推到img wrap的右邊。但是如果我使用position:absolute –
,它會覆蓋img wrap的頂部,如果向父級添加固定高度。它會正確定位小孩,但我不能擁有固定的高度,因爲img-wrap的高度不會相同。 –
@mok_ku我的示例不使用固定高度,因此它將調整大小。 – LGSon