#parent {
width: 200px;
height: 600px;
background: #ccc;
position: absolute;
}
#child {
width: 200px;
height: 200px;
background: #c9394a;
position: absolute; left: 0; top: 0; right: 0; bottom: 0;
margin: auto 0;
可以解释一下吗?不是很明白原理
测试了下,这样就行啦,当然,思路是你们的,我只是测试了下。。。?
#parent {
width: 200px;
height: 600px;
background: #ccc;
position: relative;
}
#child {
width: 200px;
height: 200px;
background: #c9394a;
position: absolute; top: 0; bottom: 0;
margin: auto 0;
第三种解决方案
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>测试</title>
<style>
.main{
width:100px;
height:500px;
background: #666;
display: flex;
align-items: center;
}
.child {
width:100px;
height:100px;
background: red;
}
</style>
</head>
<body>
<div class="main">
<div class="child">
11111
1111111111
11111111111
11111111
</div>
</div>
</body>
</html>
#parent {width: 200px;height: 600px;background: #ccc;position: relative;}#child {width: 200px;height: 200px;background: #c9394a;position: absolute; left: 0; top: 0; right: 0; bottom: 0;margin: auto;
这里应该是margin:auto ,不是 margin:auto 0;