CSS两种水平垂直居中示例介绍

网站建设 2023-01-28 20:49www.1681989.com免费网站
第一种将固定大小的div框相对窗口水平垂直居中,改变浏览器窗口大小时,依然保持水平垂直居中;

复制代码
代码如下:

<!doctype html>
<html lang="en">
<head>
<title>水平垂直居中</title>
<meta charset="utf-8">
<style type="text/css">
.out{width: 0px;
height: 0px;
position: absolute;
left: 50%;
: 50%;
}
.{height: 300px;
width: 300px;
position: absolute;
left: -150px;
: -150px;
background-color: #999;
}
</style>
</head>
<body>
<div class="out">
<div class=""></div>
</div>
</body>
</html>

 
第二种让一个自适应大小的div相对浏览器窗口水平垂直居中

复制代码
代码如下:

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>自适应剧中</title>
<style type="text/css">
.middle{
height: 300px;
width: 300px;
border: 1px solid;
display: table-cell;
text-align: center;
vertical-align: middle;
}
.{
background-color: red;
height: auto;
width: auto;
m-width: 0px;
m-height: 0px;
display: le;
}
</style>
</head>
<body>
<div class="middle">
<div class="">内容自适应水平垂直居中</div>
</div>
</body>
</html>

Copyright © 2016-2025 www.1681989.com 推火网 版权所有 Power by