Position absolutely positioned DIV relative to (also absolutely positioned) parent DIV?

前端 未结 3 545
梦毁少年i
梦毁少年i 2021-01-23 14:23

This is one problem I always have when i\'m fixing layouts. I\'ve got a absolutely positioned DIV, I place a child-DIV inside which also needs to be absolutely positioned. But I

3条回答
  •  囚心锁ツ
    2021-01-23 14:49

    If you're already absolutely positioning one div, can't you just add the two offsets together for the child? e.g. for #one to be at 10,10 and #two at 10,10 relative to #one,

    #one {
        position: absolute;
        left: 10px;
        top: 10px;
    }
    
    #two {
        position: absolute;
        left: 20px;
        top: 20px;
    }
    

    Or is it something fancier, like #one locking to top-left corner and #two to bottom-right of #one, in which case basic math doesn't work that way? In that case, an extra div is probably necessary.

    Or am I just tired and missing something?

提交回复
热议问题