CSS Calc Viewport Units Workaround?

前端 未结 4 987
慢半拍i
慢半拍i 2020-12-02 15:22

From what I\'ve seen in other answers, CSS viewport units can\'t be used in calc() statements yet. What I would like to achieve is the following statement:

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-02 15:56

    Before I answer this, I'd like to point out that Chrome and IE 10+ actually supports calc with viewport units.

    FIDDLE (In IE10+)

    Solution (for other browsers): box-sizing

    1) Start of by setting your height as 100vh.

    2) With box-sizing set to border-box - add a padding-top of 75vw. This means that the padding will be part f the inner height.

    3) Just offset the extra padding-top with a negative margin-top

    FIDDLE

    div
    {
        /*height: calc(100vh - 75vw);*/
        height: 100vh;
        margin-top: -75vw;
        padding-top: 75vw;
        -moz-box-sizing: border-box;
        box-sizing: border-box;
        background: pink;
    }
    

提交回复
热议问题