Position absolute but relative to parent

前端 未结 4 1475
小鲜肉
小鲜肉 2020-11-21 05:33

I have two divs inside another div, and I want to position one child div to the top right of the parent div, and the other child div to the bottom of the parent div using cs

4条回答
  •  闹比i
    闹比i (楼主)
    2020-11-21 05:45

    #father {
       position: relative;
    }
    
    #son1 {
       position: absolute;
       top: 0;
    }
    
    #son2 {
       position: absolute;
       bottom: 0;
    }
    

    This works because position: absolute means something like "use top, right, bottom, left to position yourself in relation to the nearest ancestor who has position: absolute or position: relative."

    So we make #father have position: relative, and the children have position: absolute, then use top and bottom to position the children.

提交回复
热议问题