一、绝对定位
在CSS中,position: absolute;可以让我们将一个元素相对于它的最近非static的祖先元素进行定位。在下面的示例中,我们将一个具有固定宽高的元素在其父元素中央进行居中显示。首先需要让父元素相对定位(position: relative;),然后给要定位的元素设置绝对定位,再通过设置top: 50%和left: 50%将其移动到父元素的中心位置,最后通过负距离调整元素的位置。
.parent {
position: relative;
width: 400px;
height: 400px;
border: 1px solid black;
}
.child {
position: absolute;
width: 100px;
height: 100px;
margin-top: -50px;
margin-left: -50px;
top: 50%;
left: 50%;
background-color: red;
}
原创文章,作者:OZSL,如若转载,请注明出处:https://www.506064.com/n/134729.html
微信扫一扫
支付宝扫一扫