“绝对定位”和 “固定定位” 下的居中【解决办法】

在常规流中,居中有好几种办法,但是一旦使用了绝对定位和固定定位之后,脱离了文档流,就无法居中了,针对此问题,以下为解决办法!

## 定位下的居中(常规流块盒)

1. 定宽(高),定了宽高之后,将左右上下距离设置为0,将左右上下margin设置为auto !(水平居中+垂直居中)

css代码:

position: absolute;
left: 0;
right: 0;
bottom: 0;
top: 0;
margin: auto;

 此代码为完美的居中方法!无瑕疵!话不多说!

如果,只想水平居中,那就把left和right都设置为0,margin为auto, 然后top和bottom自己设定想要的尺寸!

如果,只想垂直居中,那就把top和bottom都设置为0, margin为auto, 然后left和right自己设定想要的尺寸!

发布了42 篇原创文章 · 获赞 68 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_36732046/article/details/103688295