Why is this absolutely positioned element not positioning relative to its container?

前端 未结 2 1569
故里飘歌
故里飘歌 2021-01-27 05:40

I have this simple code to place two div #container elements side by side. In each of these there is a child div #child which I would like to position

2条回答
  •  不要未来只要你来
    2021-01-27 06:07

    Absolutely positioned elements are positioned with respect to the edges of their containing block, which is defined as the first ancestor that is not position: static.

    None of the ancestor elements are position: static, so it is with respect to the initial position of the viewport.

    Set position: relative on the .container elements if you really want to absolutely position them.

    That said, it looks like you would be better off doing this instead:

    .child {
        margin-left: 0.2ex;
    }
    

提交回复
热议问题