1. CSS3.0弹性布局flex, 无需知道父子元素宽高
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>弹性布局flex, 无需知道父子元素宽高</title>
<style>
html,
body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
body {
display: flex;
align-items: center;
/*定义body的子元素垂直居中*/
justify-content: center;
/*定义body的子元素水平居中*/
}
.content {
width: 300px;
height: 300px;
background: orange;
}
</style>
</head>
<body>
<div class="content"></div>
</body>
</html>
2. vertical-align:middle垂直居中, text-align: center水平居中, 无需知道父子元素宽高
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>vertical-align:middle垂直居中, text-align: center水平居中, 无需知道父子元素宽高</title>
<style>
.big-box {
width: 500px;
height: 400px;
background: green;
text-align: center;
}
.box {
line-height: 400px;
}
img {
vertical-align: middle;
}
</style>
</head>
<body>
<div class="big-box">
<span class="box">
<img src="ermao.jpg" />
</span>
</div>
</body>
</html>
3. position: absolute加margin: auto, 无需知道父子元素宽高
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>position: absolute加margin: auto, 无需知道父子元素宽高</title>
<style>
.center-vertical {
width: 100px;
height: 100px;
background: orange;
margin: auto;
/* 一般margin: auto只能实现水平居中, 垂直方向设置无效;
设置以下5行样式和设置元素高度, 会有一种特殊场景, margin: auto会垂直居中。
*/
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
</style>
</head>
<body>
<div class="center-vertical"></div>
</body>
</html>
4. position: absolute加transform: translate( -50%, -50%), 无需知道父子元素宽高
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>position: absolute加transform: translate( -50%, -50%), 无需知道父子元素宽高</title>
<style>
.content{
background: orange;
position: absolute;
top: 50%;
left: 50%;
/* transform: translate( -50%, -50%)是根据自身元素的宽高的百分比 */
/* 这里不能用margin-top: -50%; margin-left: -50%; 因为不论margin水平方向还是垂直方向的百分比, 都是根据父元素的宽的百分比 */
transform: translate( -50%, -50%);
}
</style>
</head>
<body>
<div class="content">你复合1233肥反反复复</div>
</body>
</html>
5. position: relative加transform: translateY(-50%)实现垂直居中, margin: 0 auto实现水平居中, 无需知道父子元素宽高
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>position: relative加transform: translateY(-50%)实现垂直居中, margin: 0 auto实现水平居中, 无需知道父子元素宽高</title>
<style>
html,body{
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
.content{
width: 300px;
height: 300px;
background: orange;
margin: 0 auto;/*水平居中*/
position: relative;/*设置position*/
top: 50%; /*偏移*/
transform: translateY(-50%);
}
</style>
</head>
<body>
<div class="content"></div>
</body>
</html>
6. display:table实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>display: table-cell;</title>
<style>
.parent{
width: 300px;
height: 300px;
background: orange;
text-align: center;
display: table;
}
.son{
display: table-cell;
background-color: yellow;
vertical-align: middle;
}
</style>
</head>
<body>
<div class="parent">
<div class="son">nihaosssss</div>
</div>
</body>
</html>