How to make a div responsive height between header and footer with CSS only?

前端 未结 9 1076
隐瞒了意图╮
隐瞒了意图╮ 2021-01-18 15:15

I have an HTML page with fixed height header and footer and a element in between.

I want the element to have always the total height of the screen (excluding the h

9条回答
  •  一整个雨季
    2021-01-18 15:29

    Here is a quick tip for creating responsive elements that retain their aspect ratio as they scale.

    However, when we specify a padding-bottom value for the same element, the padding measurement is calculated relative to the element’s width:

    .rect {
        width: 100%;
        height: 0;
        padding-bottom: 50%;
    }
    

    Correct aspect ratio

    You will notice that I have set the element’s height property to 0, to ensure that the height of any child elements will not be added to the padding value when the visible height is calculated.

    A note about child elements

    If you also want to specify percentage based heights for a child element, you will need to assign an absolute or relative position value to both the parent and the child:

    .rect {
        position: relative;
        width: 100%;
        height: 0;
        padding-bottom: 50%;
     }
    .rect p {
        position: absolute;
        margin: 0;
        height: 100%;
        }
    

    View A Demo

提交回复
热议问题