一、定位
1、盒子宽高已知
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.box {
position: relative;
width: 400px;
height: 400px;
border: 10px solid #f0f;
background-color: steelblue;
}
.demo {
position: absolute;
top: 50%;
left: 50%;
width: 200px;
height: 200px;
margin-left: -100px;
margin-top: -100px;
background-color: thistle;
}
</style>
</head>
<body>
<div class="box">
<div class="demo"></div>
</div>
</body>
</html>
2、盒子宽高未知
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.box {
position: relative;
width: 400px;
height: 400px;
border: 10px solid #ff0;
background-color: tomato;
}
.demo {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
width: 200px;
height: 200px;
margin: auto;
background-color: turquoise;
}
</style>
</head>
<body>
<div class="box">
<div class="demo"></div>
</div>
</body>
</html>
3、平移+定位
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.box {
position: relative;
width: 400px;
height: 400px;
border: 10px solid #0ff;
background-color: violet;
}
.demo {
position: absolute;
top: 50%;
left: 50%;
width: 200px;
height: 200px;
background-color: yellowgreen;
transform: translate(-50%,-50%);
}
</style>
</head>
<body>
<div class="box">
<div class="demo"></div>
</div>
</body>
</html>
二、margin+padding
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.box {
width: 400px;
padding: 100px 0;
border: 10px solid #00f;
}
.demo {
width: 200px;
height: 200px;
margin: 0 auto;
background-color: springgreen;
}
</style>
</head>
<body>
<div class="box">
<div class="demo"></div>
</div>
</body>
</html>
三、平移
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.box {
width: 400px;
height: 400px;
border: 10px solid #0ff;
background-color: violet;
}
.demo {
width: 200px;
height: 200px;
margin-left: 50%;
margin-top: 50%;
background-color: yellowgreen;
transform: translate(-50%,-50%);
}
</style>
</head>
<body>
<div class="box">
<div class="demo"></div>
</div>
</body>
</html>
四、table-cell布局
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.box {
display: table-cell;
vertical-align: middle;
width: 400px;
height: 400px;
border: 10px solid rgb(0, 255, 98);
background-color: rgb(238, 130, 197);
}
.demo {
width: 200px;
height: 200px;
margin: 0 auto;
background-color: rgb(50, 187, 205);
}
</style>
</head>
<body>
<div class="box">
<div class="demo"></div>
</div>
</body>
</html>
五、flex布局
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.box {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
width: 400px;
height: 400px;
border: 10px solid rgb(200, 255, 0);
background-color: rgb(234, 130, 238);
}
.demo {
width: 200px;
height: 200px;
background-color: rgb(50, 205, 146);
}
</style>
</head>
<body>
<div class="box">
<div class="demo"></div>
</div>
</body>
</html>