How to make the wrapping div element with relative position match child elements height?

前端 未结 2 1423
名媛妹妹
名媛妹妹 2021-02-06 05:02

I have a simple problem where I have 2 divs, 1 is relative positioned. The child element is absolute positioned. This child has varying height.

The code so far:

2条回答
  •  旧巷少年郎
    2021-02-06 05:58

    Absolute positionned elements are outside the flow so parents can't adjust their height.

    But you can simply use:

    #wrap {
        width: 100%; /* useless */
        background: #ccc;
        overflow:hidden; /* establish a new formatting context */
        min-height: 20px;
    }
    
    #inner {
        width:30%;
        background: #000;
        float:right;
    }
    

提交回复
热议问题