目录
display:table-cell、vertical-align:middle属性详解
HTML文本
<div class="parent">
<div class="child"></div>
</div>
显示效果

方式1
设置position:absolute,top、bottom、right、left=0,margin:auto。
通过设置相对定位4个方向为0并将margin设置为auto使其自动平分上下左右的间距。
.parent {
width: 300px;
height: 300px;
background-color: #ddd;
position:relative;
}
.child {
position: absolute;
background-color:red;
/*竖直方向居中*/
top: 0;
bottom: 0;
/*水平方向居中*/
left: 0;
right:0;
margin: auto;
/*行内文本居中*/
width: 100px;
height: 100px;
line-height: 100px;
}

本文介绍了五种常用的网页元素居中布局技巧,包括绝对定位、Flex布局、Grid布局等方法,并提供了详细的代码示例。
&spm=1001.2101.3001.5002&articleId=113930746&d=1&t=3&u=18a2fc1e1068437094862b7a47697eaa)
588

被折叠的 条评论
为什么被折叠?



