html元素居中

水平居中:

1、定宽度,需要谁居中,给其设置 margin: 0 auto;
2、子元素设置display: inline-block; 或 display: inline;即将其转换成行内块级/行内元素,给父元素设置 text-align: center;
3、定位,设置父元素为相对定位,再设置子元素为绝对定位,设置子元素的left:50%,margin-left: -元素宽度的一半px

垂直居中

1、设置行高(line-height)等于盒子高(height)(子标签为内联)
2、父元素vertical-align: middle;
3、定位,设置父元素为相对定位,再设置子元素为绝对定位,设置子元素的top:50%,margin-top: -元素高度的一半px

猜你喜欢

转载自blog.csdn.net/qq_43532386/article/details/109558455