600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > html设置div内容垂直居中 css让DIV元素或文字水平垂直居中的五种方法

html设置div内容垂直居中 css让DIV元素或文字水平垂直居中的五种方法

时间:2019-02-18 02:08:34

相关推荐

html设置div内容垂直居中 css让DIV元素或文字水平垂直居中的五种方法

div元素或文字水平垂直居中的5中方法

第一种方法:使用top: 50%/left: 50%和translateX(-50%) translateY(-50%);具体代码如下:

.container {

position: absolute;

top: 50%;

left: 50%;

transform: translateX(-50%) translateY(-50%);

}

实例:

如何让元素或文字水平垂直居中

.container {

position: absolute;

top: 50%;

left: 50%;

transform: translateX(-50%) translateY(-50%);

}

文字水平垂直居中

方法二:使用display: flex; align-items: center; justify-content: center;属性

html, body, .container {

height: 100%;

}

.container {

display: flex;

align-items: center;

justify-content: center;

}

实例:

如何让元素或文字水平垂直居中

html, body, .container {

height: 100%;

}

.container {

display: flex;

al

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。