Parent div not expanding to children's height

前端 未结 2 1436
故里飘歌
故里飘歌 2021-02-01 04:31

As you will see, I have a div (#innerPageWrapper) that wraps around the divs that contain the content. #innerPageWrapper also does act vis

2条回答
  •  执笔经年
    2021-02-01 04:48

    Try setting width and height of the parent element to auto. Simplified example:

    http://jsfiddle.net/kimgysen/8nWDE/

    #innerPageWrapper {
        width:auto;
        height:auto;
        background:red;
    }
    
    #innerPageWrapper p{
        width: 100px;
        height: auto;
        margin: 10px auto;
        background: yellow;
    }
    

    Edit:

    Check this fiddle for a more advanced example, including horizontal and vertical center: http://jsfiddle.net/kimgysen/8nWDE/1/

    The parent element will dynamically adapt to the bounds of the child elements.
    You can also set dynamic width and height on child elements.

    .outerDiv{
        display: table-cell;
        background-color: yellow;
        width: auto; 
        height: auto;
        vertical-align: middle;
        text-align: center;
    }
    
    .innerDiv{
        display: inline-block;
        background-color: red; 
        width: 100px;
        height: 100px;
        margin: 20px;
    }
    

提交回复
热议问题