css 完美垂直居中解决方案兼容ie8以上等其他浏览器

<pre>
<!DOCTYPE html>
<html>
<head>
<title>DIV水平垂直居中 </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
#outer {
display:table;
height:400px;
#position:relative;
overflow:hidden;
}
#middle {
width:300px;
height: 400px;;
display:table-cell;
vertical-align:middle;
#position:absolute;
#top:50%;
background: #F0E68C;
}
#inner {
#position:relative;
#top:-50%;

}
</style>
</head>
<body>
<div >
//显示浏览器版本
document.getElementById('ver').innerHTML = navigator.userAgent;
</script>
</pre>


移动端的写法

<pre>
<!DOCTYPE html>
<html>
<head>
<title>DIV水平垂直居中 </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">

#middle {
width:30%;
height: 10rem;
display:table-cell;
vertical-align:middle;
background: #F0E68C;
}

</style>
</head>
<body>
<div >
水平居中</br>
水平居中</br>
水平居中</br>
水平居中</br>
</div>
</body>
</html>
</pre>


ps:不能用绝对定位否则失效(可以加到外面一层)

还有个绝对定位居中的方法
<pre>
.element {
width: 600px; height: 400px;
position: absolute; left: 50%; top: 50%;
transform: translate(-50%, -50%); /* 50%为自身尺寸的一半 */
}
</pre>

 

相关文章:

  • 2021-12-04
  • 2022-12-23
  • 2022-12-23
  • 2021-12-06
  • 2022-12-23
  • 2021-12-25
  • 2021-06-09
猜你喜欢
  • 2021-09-29
  • 2022-02-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-05
  • 2022-03-04
相关资源
相似解决方案