1.为父元素设置相对定位,为子元素设置绝对定位,
子元素在设置left right top bottom 都为0
margin:auto;
2.为父元素设置一个定位,为子元素设置绝对定位
子元素设置top: calc(50% - 子元素高度的一半);
left:calc(50% - 子元素宽度的一半);
3.为父元素设置定位,子元素设置绝对定位
left,top为父元素50%
margin-left margin-top 子元素的-的一半
4.为父元素设置弹性盒布局
display:flex;
align-items: center;
justify-content: center;
5.为父元素设置弹性盒布局
display:flex;
子元素设置margin:auto;
6.为父元素设置定位,子元素设置绝对定位
为子元素设置以下
position:absolute;
left:50%;
top:50%;
transform:translate(-50%,-50%);
translate设置的百分数是以自己的宽高为准
css块级元素居中
最新推荐文章于 2024-04-24 11:24:38 发布