一、直接及算法
1. margin =(父盒子宽度 - 子盒子宽度)/ 2
<!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>
.parent {
width: 600px;
height: 600px;
background-color: darkcyan;
overflow: hidden;
}
.child {
width: 300px;
height: 300px;
background-color: darkgreen;
}
.center {
margin: 150px;
}
</style>
</head>
<body>
<div class="parent">
<div class="child center"></div>
</div>
</body>
</html>
二、绝对定位法
2. 四边距0 和 margin自适应
<!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>
body {
margin: 0;
}
div {
width: 300px;
height: 300px;
background-color: green;
}
.box {
margin: auto;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
3. 绝对定位百分比 和 负margin
<!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>
div {
width: 300px;
height: 300px;
background-color: orange;
}
.box {
position: absolute;
left: 50%;
top: 50%;
margin-top: -150px;
margin-left:-150px;
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
4. 绝对定位 和 translate
<!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>
div {
width: 300px;
height: 300px;
background-color: cyan;
}
.box {
position: absolute;
transform: translate(-50%, -50%);
left: 50%;
top: 50%;
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
三、Flex布局
5. 父盒子flex 和 justify-content,align-items属性
<!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>
body,
html {
height: 100%;
width: 100%;
margin: 0;
}
body {
display: flex;
justify-content: center;
align-items: center;
}
div {
width: 300px;
height: 300px;
background-color: red;
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
6.父盒子flex 和 子盒子margin自适应
<!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>
body,
html {
height: 100%;
width: 100%;
margin: 0;
}
div {
width: 300px;
height: 300px;
background-color: purple;
}
body {
display: flex;
}
.box {
margin: auto;
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
四、table-cell
7. 父盒子设置table-cell 和 vertical-align,text-align属性
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
.parent {
width: 500px;
height: 500px;
background-color: cyan;
}
.center {
display: table-cell;
vertical-align: middle;
text-align: center;
}
.child {
width: 200px;
height: 200px;
display: inline-block;
background-color: red;
}
</style>
</head>
<body>
<div class="parent center">
<div class="child"></div>
</div>
</body>
</html>