乐趣区

CSS 垂直居中

absolute + transform
<!DOCTYPE html>
<html>
<head>
<meta charset=”utf-8″>
<title></title>
<style>
#parent {
width: 500px;
height: 500px;
background: lightgray;
position: relative;
}
#child {
background: white;
width: 250px;
height: 250px;
position: absolute;
top: 50%;
transform: translate(50%, -50%);
}
</style>
</head>
<body>
<div id=”parent”>
<div id=”child”></div>
</div>
</body>
</html>
felx + align-items: center
<!DOCTYPE html>
<html>
<head>
<meta charset=”utf-8″>
<title></title>
<style>
#parent {
width: 500px;
height: 500px;
background: lightgray;
display: flex;
align-items: center;
}
#child {
background: white;
width: 250px;
height: 250px;
transform: translate(50%, 0);
}
</style>
</head>
<body>
<div id=”parent”>
<div id=”child”></div>
</div>
</body>
</html>

felx + justify-content: center
<!DOCTYPE html>
<html>
<head>
<meta charset=”utf-8″>
<title></title>
<style>
#parent {
width: 500px;
height: 500px;
background: lightgray;
flex-direction: column;
justify-content: center;
}
#child {
background: white;
width: 250px;
height: 250px;
transform: translate(50%, 0);
}
</style>
</head>
<body>
<div id=”parent”>
<div id=”child”></div>
</div>
</body>
</html>

退出移动版