前端开发——垂直居中的容器布局实现
发布时间
阅读量:
阅读量
本案例所描述的是前端开发中关于容器居中的实现方法。
通过设置 margin:0 auto 的方式,已经能够使子元素在父容器内实现水平居中效果。
方法一:采用定位技术,将子元素设为绝对定位,同时将父容器设为相对定位。
<div style="height: 100px;width:200px;background: lime;position: relative">
<div id="center_method_one">
<p>子布局</p>
</div>
</div>
AI写代码html
通过配置CSS样式,将子布局设定为绝对定位,使其与父级布局左上角保持一半的距离,再向左上方偏移自身尺寸的一半,即可实现所需效果。
#center_method_one{
background-color: red;
position: absolute;
margin: auto;
width: 70px;
top:50%;
left: 5
全部评论 (0)
还没有任何评论哟~
