当前位置:Gxlcms > html代码 > 三行css代码实现垂直居中_html/css_WEB-ITnose

三行css代码实现垂直居中_html/css_WEB-ITnose

时间:2021-07-01 10:21:17 帮助过:20人阅读

html结构为:

css代码为:

#div2{	height:300px;width:300px;	border:1px red solid;	text-align:center;	margin:auto;}#div1{      height:50px;width:50px;      position: relative;      top: 50%;      transform: translateY(-50%);      border:1px red solid;      margin:auto;}

其中最核心的css是这三行

      position: relative;      top: 50%;      transform: translateY(-50%);

为了浏览器兼容性,需要在transform属性前添加浏览器前缀

人气教程排行