元素的水平垂直居中和多行文本的垂直居中

339 阅读1分钟

元素在其父元素中水平居中和垂直居中的方式

方法一:

通过使用flex布局,来设置justify-content:center;和align-items:center;就可实现元素水平垂直居中。

image.png 方法二:

通过使用position:absolute;来定位元素,left,top的值设置为50%,然后再设置负的margin(margin的值为自身大小的一半)就可实现元素水平垂直居中。

image.png 方法三;

通过使用position:absolute;来定位元素,left,top的值设置为50%,然后设置tranform:translate(50%,50%)就可实现元素水平垂直居中。

image.png 方法四: 通过使用position:absolute;来定位元素,将top、right、bottom、left都设置为0,然后设置margin:auto;就可实现水平垂直居中

image.png

多行文本的垂直居中

通过给父元素添加dispaly:table;属性,然后使用display:table-cell;和vertical-align:middle;来实现多行文本垂直居中

image.png