设置position为absolute,相当于把元素变为了inline-block,因此宽度没有占据整行
如果需要的话,可以手动添加width
display:inline-block;
width:200px;
height:100px;
1、自身水平垂直居中
单行文字可以使用line-height
text-align:center;
line-height:100px;
多行文字
display: flex;
justify-content: center;
align-items: center;
这种是将多行文字看做一个整体水平垂直居中,因此不是每一行文字都是水平居中效果
2、在容器内水平垂直居中
position: absolute;
top:50%;
left:50%;
margin-left:-100px;
margin-top:-50px;
指定容器宽高,块元素
1、自身水平垂直居中
单行文字
display:block;
width:200px;
height:100px;
text-align:center;
line-height:100px;
多行文字
display: flex;
justify-content: center;
align-items: center;
2、在容器内水平垂直居中
position: absolute;
top:50%;
left:50%;
margin-left:-100px;
margin-top:-50px;
或者
margin:0 auto;
总结
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
如需转载,请注明文章出处和来源网址:http://www.divcss5.com/html/h62271.shtml