html file:
<body>
<div class=‘parentelement‘>
<div class=‘childelement‘>
想让子元素在父元素内水平、垂直居中
</div>
</div>
</body>
css file:
.parentelement{
width:200px;
height:200px;
background-color:red;
position:relative;
}
.childelement{
width:50%;
height:50%;
background-color:green;
position:absolute;
top:50%;
left:50%;
-webkit-transform: translate(-50%,-50%);
-moz-transform: translate(-50%,-50%);
-o-transform: translate(-50%,-50%);
transform: translate(-50%,-50%);
}
时间: 2024-10-23 09:14:10