1.元素内容水平居中:
text-align:center;
2.块级元素水平居中:
margin:0 auto
3.一行文字垂直居中:
行高等于高
4.多行内容垂直居中:
margin:20px 0; (加在自己上)
padding:20px 0; (加在父元素上)
5.子元素在父元素中水平垂直居中:
(1)margin:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.box{
width: 300px;
height: 300px;
background-color: palevioletred;
overflow: hidden;
}
.box1{
width: 100px;
height: 100px;
background-color: deepskyblue;
margin: 100px auto;
}
</style>
</head>
<body>
<div class="box">
<div class="box1"></div>
</div>
</body>
</html>
(2)position:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.box{
width: 300px;
height: 300px;
background-color: palevioletred;
position: relative;
}
.box1{
width: 100px;
height: 100px;
background-color: deepskyblue;
position: absolute;
left: 100px;
top: 100px;
}
</style>
</head>
<body>
<div class="box">
<div class="box1"></div>
</div>
</body>
</html>
(3)padding:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.box{
width: 300px;
height: 300px;
background-color: palevioletred;
padding: 100px;
box-sizing: border-box;
}
.box1 {
width: 100px;
height: 100px;
background-color: deepskyblue;
}
</style>
</head>
<body>
<div class="box">
<div class="box1"></div>
</div>
</body>
</html>
(4)表格法:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.box{
width: 300px;
height: 300px;
background-color: palevioletred;
display: table-cell;
vertical-align: middle;
}
.box1 {
width: 100px;
height: 100px;
background-color: deepskyblue;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="box">
<div class="box1"></div>
</div>
</body>
</html>
(5)绝对定位法:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.box{
width: 300px;
height: 300px;
background-color: palevioletred;
position: relative;
}
.box1 {
width: 100px;
height: 100px;
background-color: deepskyblue;
position: absolute;
top: 50%;
left: 50%;
margin-left: -50px;
margin-top: -50px;
}
</style>
</head>
<body>
<div class="box">
<div class="box1"></div>
</div>
</body>
</html>
(6)弹性盒法:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.box{
width: 300px;
height: 300px;
background-color: palevioletred;
display: flex;
justify-content: center;
align-items: center;
}
.box1 {
width: 100px;
height: 100px;
background-color: deepskyblue;
}
</style>
</head>
<body>
<div class="box">
<div class="box1"></div>
</div>
</body>
</html>