块级元素水平,垂直居中的两种方式
方式一: 利用margin
<!DOCTYPE html>
<html>
<head>
<title>块级元素水平,垂直居中</title>
<meta charset="utf-8">
<style>
.wrapper {
height: 600px;
border: 1px solid gray;
}
.box {
width: 100px;
height: 100px;
background: gold;
margin: 250px auto 0;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="box"></div>
</div>
</body>
</html>方式二,利用定位及负边距
<!DOCTYPE html>
<html>
<head>
<title>块级元素水平,垂直居中</title>
<meta charset="utf-8">
<style>
.wrapper {
height: 600px;
border: 1px solid gray;
position: relative;
}
.box {
width: 100px;
height: 100px;
background: gold;
position: absolute;
left: 50%;
top: 50%;
margin-left: -50px;
margin-top: -50px;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="box"></div>
</div>
</body>
</html> 相关推荐
drdrsky 2020-06-16
PioneerFan 2020-06-10
HSdiana 2020-03-28
葉無聞 2020-03-23
lyg0 2020-03-07
冰蝶 2020-01-10
玫瑰小妖 2019-12-31
行吟阁 2019-12-08
aiolos 2016-04-15
loverlucky 2016-03-15
WebVincent 2019-07-01
云端漂移 2019-07-01
renpinghao 2019-06-30
kbkiss 2019-06-29