为了账号安全,请及时绑定邮箱和手机立即绑定

第三种解决方案

#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;


正在回答

4 回答

可以解释一下吗?不是很明白原理

0 回复 有任何疑惑可以回复我~
#1

ccyy_阿亮

定位元素在包含块中的方程(垂直方向): top + margin-top + border-top-height + padding-top + height + padding-bottom + border-bottom-height + margin-bottom + bottom = the height of the containing block
2020-12-02 回复 有任何疑惑可以回复我~

测试了下,这样就行啦,当然,思路是你们的,我只是测试了下。。。?

#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;


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>


http://img1.sycdn.imooc.com//5def60f6000123b601600658.jpg

0 回复 有任何疑惑可以回复我~
#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;

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

第三种解决方案

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信