垂直居中
1. display:flex
- 通过
display:flex
实现CSS垂直居中的方法是给父元素display:flex
;而子元素align-self:center;
- 代码实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box{
background-color: yellow;
width: 300px;
height: 300px;
display: flex;
}
.in{
background-color: green;
width: 50%;
height: 50%;
align-self: center;
}
</style>
</head>
<body>
<div class="box">
<div class="in"></div>
</div>
</body>
</html>
2. :before
- 为父元素添加伪元素
:before
,使得子元素实现垂直居中。 - 代码实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box{
background-color: yellow;
width: 300px;
height: 300px;
}
.box:before{
content: "";
display: inline-block;
vertical-align: middle;
height: 100%;
}
.in{
background-color: green;
width: 50%;
height: 50%;
display: inline-block;
vertical-align: middle;
}
</style>
</head>
<body>
<div class="box">
<div class="in"></div>
</div>
</body>
</html>
3. 隐藏节点
- 创建一个隐藏节点
.hide
,使得隐藏节点的height
值为剩余高度的一半 - 代码实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box{
background-color: yellow;
width: 300px;
height: 300px;
}
.hide{
width: 50%;
height: 25%;
}
.in{
background-color: green;
width: 50%;
height: 50%;
}
</style>
</head>
<body>
<div class="box">
<div class="hide"></div>
<div class="in"></div>
</div>
</body>
</html>
4. transform(已知父元素高度)
- 给子元素的
position:relative
,再通过translateY
即可定位到垂直居中的位置。 - 必须知道父元素高度
- 代码实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box{
background-color: yellow;
width: 300px;
height: 300px;
}
.in{
background-color: green;
width: 50%;
height: 50%;
position: relative;
top: 50%;
transform: translateY(-50%);
}
</style>
</head>
<body>
<div class="box">
<div class="in"></div>
</div>
</body>
</html>
5. transform(未知父元素高度)
- 先给父元素
position:relative
,再给子元素position:absolute
,通过translateY
即可定位到垂直居中的位置 - 无需知道父元素高度
- 代码实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box{
background-color: yellow;
width: 300px;
height: 300px;
position: relative;
}
.in{
background-color: green;
width: 50%;
height: 50%;
position: absolute;
top: 50%;
transform: translateY(-50%);
}
</style>
</head>
<body>
<div class="box">
<div class="in"></div>
</div>
</body>
</html>
6. line-height
- 适用于文本
- 设置子元素的
line-height
值等于父元素的height
,这种方法适用于子元素为单行文本的情况 - 代码实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box{
background-color: yellow;
width: 300px;
height: 300px;
position: relative;
}
.in{
width: 50%;
height: 50%;
line-height: 300px;
}
</style>
</head>
<body>
<div class="box">
<div class="in">hello world</div>
</div>
</body>
</html>
水平居中
1. margin
- 给子元素添加
margin: 0 auto;
- 代码实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box{
background-color: yellow;
width: 300px;
height: 300px;
}
.in{
background-color: green;
width: 50%;
height: 50%;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="box">
<div class="in"></div>
</div>
</body>
</html>
2. position
- 子绝父相,设置子元素的
left:50%
- 设置绝对子元素的
margin-left:
元素宽度的一半px; 或者设置transform: translateX(-50%);
- 代码实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box{
background-color: yellow;
width: 300px;
height: 300px;
position: relative;
}
.in{
width: 100px;
height: 100px;
background-color: green;
position: absolute;
left: 50%;
margin-left: -50px;
}
</style>
</head>
<body>
<div class="box">
<div class="in"></div>
</div>
</body>
</html>
3. display:flex
- 为父元素添加属性 两个属性即可
display: flex;
justify-content: center;
- 代码实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box{
background-color: yellow;
width: 300px;
height: 300px;
display: flex;
justify-content: center;
}
.in{
width: 100px;
height: 100px;
background-color: green;
}
</style>
</head>
<body>
<div class="box">
<div class="in"></div>
</div>
</body>
</html>
水平垂直居中
display
- 子绝父相,添加几个属性即可
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
- 代码实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box{
background-color: yellow;
width: 300px;
height: 300px;
position: relative;
}
.in{
width: 100px;
height: 100px;
background-color: green;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
}
</style>
</head>
<body>
<div class="box">
<div class="in"></div>
</div>
</body>
</html>
display
- 子绝父相
left: 50%;
top: 50%;
margin-left: --元素宽度的一半px;
margin-top: --元素高度的一半px;
- 代码实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box{
background-color: yellow;
width: 300px;
height: 300px;
position: relative;
}
.in{
width: 100px;
height: 100px;
background-color: green;
position: absolute;
left: 50%;
top: 50%;
margin-left: -50px;
margin-top: -50px;
}
</style>
</head>
<body>
<div class="box">
<div class="in"></div>
</div>
</body>
</html>