How exactly does absolute position element behaves inside a static parent

后端 未结 2 1274
既然无缘
既然无缘 2021-01-06 04:06

I ask this question because yesterday I had to style several divs as it\'s shown in this JSFiddle example.

The tricky part for me was to position

2条回答
  •  Happy的楠姐
    2021-01-06 04:32

    CSS position

    • position: static is default, and places the elements in order, one after another, according to their display property. There's little practical purpose on manually setting an element to static.
    • position: relative works much like static, however the elements inside a relative parent have their top, left, bottom and right values relative to the first non-static parent. This can be seen when you get the offset position of an element via JavaScript, or set it with either CSS or JavaScript.
    • position: absolute makes the element be placed relative to or the first relatively or absolutely positioned parent element. This means its position should be set with top, left, bottom, right CSS properties, that are counted starting from the first relative or absolute ancestor element.

    Your mistake was not setting top nor left CSS properties to the

    . Hope this little explanation lit you up! Best of luck.

提交回复
热议问题