诗和远方需要路费,星辰大海需要门票
在工作中跟面试的时候经常会问居中,下面总结几种居中的用法
display:table-cell 垂直居中
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
.container {
width: 400px;
height: 400px;
background-color: red;
display: table-cell;
vertical-align: middle;
}
.box {
width: 50px;
height: 50px;
background-color: aqua;
}
</style>
</head>
<body>
<div class="container">
<div class="box">
</div>
</div>
</body>
</html>
效果图
absolute+margin
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
.container {
width: 400px;
height: 400px;
background-color: red;
position: relative;
}
.box {
width: 50px;
height: 50px;
background-color: aqua;
position: absolute;
margin: auto;
top: 0;
bottom: 0;
}
</style>
</head>
<body>
<div class="container">
<div class="box">
</div>
</div>
</body>
</html>
效果图
absolute+负margin
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
.container {
width: 400px;
height: 400px;
background-color: red;
position: relative;
}
.box {
width: 100px;
height: 100px;
background-color: aqua;
position: absolute;
margin-top: -50px;
top: 50%;
}
</style>
</head>
<body>
<div class="container">
<div class="box">
</div>
</div>
</body>
</html>
效果图
absolute+calc
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
.container {
width: 400px;
height: 400px;
background-color: red;
position: relative;
}
.box {
width: 100px;
height: 100px;
background-color: aqua;
position: absolute;
top: calc(50% - 50px);
}
</style>
</head>
<body>
<div class="container">
<div class="box">
</div>
</div>
</body>
</html>
效果图
absolute + transform
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
.container {
width: 400px;
height: 400px;
background-color: red;
position: relative;
}
.box {
width: 100px;
height: 100px;
background-color: aqua;
position: absolute;
top: 50%;
transform: translateY(-50%);
}
</style>
</head>
<body>
<div class="container">
<div class="box">
</div>
</div>
</body>
</html>
效果图
line-height
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
.container {
width: 400px;
height: 400px;
line-height: 400px;
background-color: red;
}
.box {
width: 100px;
height: 100px;
background-color: aqua;
display: inline-block;
}
</style>
</head>
<body>
<div class="container">
<div class="box">
</div>
</div>
</body>
</html>
效果图
flex布局
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
.container {
width: 400px;
height: 400px;
background-color: red;
display: flex;
align-items: center;
}
.box {
width: 100px;
height: 100px;
background-color: aqua;
}
</style>
</head>
<body>
<div class="container">
<div class="box">
</div>
</div>
</body>
</html>
效果图
grid布局
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
.container {
width: 400px;
height: 400px;
background-color: red;
display: grid;
}
.box {
width: 100px;
height: 100px;
background-color: aqua;
align-self: center;
}
</style>
</head>
<body>
<div class="container">
<div class="box">
</div>
</div>
</body>
</html>
效果图
伪类after或before
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
.container {
width: 400px;
height: 400px;
background-color: red;
}
.container::after {
content: '';
display: inline-block;
vertical-align: middle;
height: 100%;
}
.box {
width: 100px;
height: 100px;
background-color: aqua;
display: inline-block;
vertical-align: middle;
}
</style>
</head>
<body>
<div class="container">
<div class="box">
</div>
</div>
</body>
</html>