标签:居中 color 50% 300px height 垂直 width background
垂直居中
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. 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>
7. 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>
标签:居中,color,50%,300px,height,垂直,width,background 来源: https://blog.csdn.net/weixin_51692246/article/details/113833036
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。