第三种解决方案

来源:3-5 垂直居中布局的第二种解决方案优点与缺点

杨超越赐我offer

2019-11-10 11:10

#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回答

  • 慕田峪5397708
    2020-07-17 16:14:21

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

    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 01:44:06

    共 1 条回复 >

  • 请再努力一下
    2019-12-26 14:58:16

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

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


  • 往事随风_遇见最好的你
    2019-12-10 17:10:18

    第三种解决方案

    <!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://img4.mukewang.com/5def60f6000123b601600658.jpg

  • 前端是我菜
    2019-11-15 15:16:52
    #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;

一课全面掌握主流CSS布局

一网打尽CSS主流布局方案,一步掌握页面布局技巧.

5276 学习 · 60 问题

查看课程

相似问题