我想通过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