热门关键字:
jquery > jquery教程 > jquery教程 > html5文字上下居中

html5文字上下居中

415
作者:管理员
发布时间:2021/6/21 18:31:05
评论数:0
转载请自觉注明原文:http://www.jq-school.com/Show.aspx?id=4987
  设置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;


  总结


  如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。




如果您觉得本文的内容对您的学习有所帮助:支付鼓励



关键字:jquery
友荐云推荐