首页 > 开发 > CSS > 正文

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

2024-07-11 08:28:21
字体:
来源:转载
供稿:网友
第一种:将固定大小的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%;
top: 50%;
}
.in{height: 300px;
width: 300px;
position: absolute;
left: -150px;
top: -150px;
background-color: #999;
}
</style>
</head>
<body>
<div class="out">
<div class="in"></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;
}
.in{
background-color: red;
height: auto;
width: auto;
min-width: 0px;
min-height: 0px;
display: inline;
}
</style>
</head>
<body>
<div class="middle">
<div class="in">内容自适应水平垂直居中</div>
</div>
</body>
</html>

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表