不固定和固定宽高的盒子垂水平直居中的几种方法
1.不固定宽高让盒子垂直居中有以下方法:
第一种:使用css 方法
<!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>Document</title>
<style>
.father {
display: table-cell;
text-align: center;
vertical-align: middle;
height: 400px;
width: 400px;
background: red;
}
/*子盒子宽高不固定*/
.son {
display: inline-block;
vertical-align: middle;
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
</body>
</html>
第二种:使用定位和位移
<!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>Document</title>
<style>
.father {
position: relative;
height: 400px;
width: 400px;
background: red;
}
/* 子盒子宽高不固定*/
.son {
position: absolute;
transform: translate(-50%, -50%);
top: 50%;
left: 50%;
/*background: pink;*/
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
</body>
</html>
第三种使用弹性模型
<!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>Document</title>
<style>
.father {
display: flex;
justify-content: center;
align-items: center;
height: 400px;
width: 400px;
background: red;
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
</body>
</html>
2.宽高固定让盒子垂直水平居中
第一种 使用绝对定位和 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>Document</title>
<style>
.father {
position: relative;
height: 400px;
width: 400px;
background: red;
}
.son {
height: 250px;
width: 250px;
position: absolute;
left: 0px;
top: 0px;
bottom: 0px;
right: 0px;
margin: auto;
background: #000;
}
</style>
</head>
<body>
<div class="father">
<div class="son"> </div>
</div>
</body>
</html>
第二种:使用定位和外边距负值
<!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>Document</title>
<style>
.father {
position: relative;
height: 400px;
width: 400px;
background: red;
}
.son {
height: 200px;
width: 200px;
position: absolute;
/* 定位父盒子的50% */
left: 50%;
top: 50%;
/* 子盒子宽高各一半 */
margin-left: -100px;
margin-top: -100px;
background: #000;
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
</body>
</html>
第三种使用弹性盒子
<!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>Document</title>
<style>
.father {
display: flex;
height: 100%;
flex-flow: row wrap;
align-items: center;
justify-content: center;
background: red;
}
.son {
height: 200px;
width: 200px;
background: #000;
}
</style>
</head>
<body style="height: 100%">
<div class="father">
<div class="son"></div>
</div>
</body>
</html>