How to Overlap Parent Div From Child Div

前端 未结 5 1348
隐瞒了意图╮
隐瞒了意图╮ 2021-01-02 23:40

I have some divs at my HTML and one of them is loading image div so I want it overlap its parent div. Here is my code:

<
5条回答
  •  醉话见心
    2021-01-03 00:26

    Sajjan's the closest from what I can tell, but his has a few flaws:

    1. Position: absolute requires its parent to have a non-static position (this is often done with position: relative, which effectively works the same way as static).
    2. You don't need to set the height and width of the child, just the parent.

    Here's my Fiddle for it to demonstrate.

    #parent {
        border: 5px solid gray;
        position: relative;
        height: 100px;
        width: 100px;
        margin-left: 50px;
    }
    
    #child {
        position: absolute;
        top:0;
        left: 0;
        right: 0;
        bottom: 0;
        background: red;
    }
    

    The key here is the position: relative on the parent.

    I am curious, though, what exactly you're trying to achieve with this. I have a feeling that whatever it is, there's a better way.

提交回复
热议问题