Margin-top percentage not relative to parent

前端 未结 2 1449
耶瑟儿~
耶瑟儿~ 2021-01-25 08:23

I an having trouble understanding why the \"square\" class isn\'t relative to the parent, \"container\".

i.e - I set the container to have a height of 200px; the \"squa

2条回答
  •  长情又很酷
    2021-01-25 08:53

    Vertical padding and margin are relative to the width of the parent. Top and bottom on the other hand are not.

    Try to place a div inside another. Use the top property (for example: top: 25%) and make sure you specify a position (e.g. position: relative;)

    I've forked and adjusted your code example to demonstrate what I mean: https://codepen.io/anon/pen/Yaqmva

    top: 5%;
    position: relative;
    

提交回复
热议问题