当前位置:网站首页>Two ways to center block level elements

Two ways to center block level elements

2022-06-25 23:45:00 Delusion_ wangx

1、 Without setting absolute positioning , After setting the width of the block level element, use margin:0 auto; In the middle

2、 Set to absolute positioning , Adjust the distance from the left margin to the center according to the width ,
example :

position: absolute;
left: 25%;
width: 50%;

or

position: absolute;
left: 50%;
margin-left: -50px;
width: 100px;

There is no problem in centering these two methods , But with absolute positioning , Reuse margin:0 auto Nine cannot be centered . The specific reason is unknown , I checked several articles , It is said that this is the rule , Just use it . Later, due to limited time, I didn't go deep into it . If you know the reason, please leave a message in the comment area , Thank you for the first ,

原网站

版权声明
本文为[Delusion_ wangx]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206252055329642.html