小盒子在大盒子里水平垂直居中
<!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>
*{
margin: 0;
padding: 0;
}
.parent{
background-color: #ccc;
width: 400px;
height: 400px;
text-align: center;
display: table-cell;
vertical-align: middle;
}
.child{
background-color: pink;
width: 250px;
height: 250px;
display: inline-block; /*用 margin: 0 auto; 也可以 */
}
</style>
</head>
<body>
<div class="parent">
<div class="child">水平垂直</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">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
.parent{
background-color: #ccc;
width: 400px;
height: 400px;
position: relative;
}
.child{
background-color: pink;
width: 250px;
height: 250px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
</style>
</head>
<body>
<div class="parent">
<div class="child">水平垂直</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">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
.parent{
background-color: #ccc;
width: 400px;
height: 400px;
display:flex;
justify-content:center;
align-items:center;
}
.child{
background-color: pink;
width: 250px;
height: 250px;
}
</style>
</head>
<body>
<div class="parent">
<div class="child">水平垂直</div>
</div>
</body>
</html>