Safari: VH units applied to parent element doesn&#

2019-02-13 16:18发布

I have a very simple situation, where I want to set a container element to 80vh and then have the inner div to be 100% of that height. On Chrome this will render correctly, however on Safari, the inner element doesn't have 100% of the 80vh height.

.container {
    background-color: red;
    width: 100%;
    height: 80vh;
}

.inner {
    height: 100%;
    background-color: blue;
}

Here is a fiddle showing this issue: http://jsfiddle.net/neilff/24hZQ/

On Chrome the element is blue, in Safari it is red. Is there a work around for this issue without applying 80vh to the height of the .inner div?

3条回答
Luminary・发光体
2楼-- · 2019-02-13 16:56

This is a known bug with vh and vw in Safari. You can fix it by setting height: inherit on the inner element:

.inner {
    height: inherit;
}

http://jsfiddle.net/24hZQ/47/

查看更多
混吃等死
3楼-- · 2019-02-13 16:57

You'll need to set position: absolute; to the .inner element.

.container {
    background-color: red;
    width: 100%;
    height: 80vh;
    position: relative;
}

.inner {
    height: 100%;
    background-color: blue;
    position: absolute;
    width: 100%;
}   
查看更多
老娘就宠你
4楼-- · 2019-02-13 17:15

You can achieve this using flexbox (tested in Safari 7.0.6, iOS 7 and iOS 8.0 simulator) -

http://jsfiddle.net/clintioo/zkmnomab/

.container {
    background-color: red;
    width: 100%;
    height: 80vh;  
    display: -webkit-box;
    display: -moz-box;
    display: -ms-flexbox;
    display: -webkit-flex;
    display: flex;
    -ms-flex-direction: column;
    -webkit-flex-direction: column;
    flex-direction: column;
}

.inner {
    -ms-flex: 1;
    -webkit-flex: 1;
    -webkit-box-flex: 1.0;
    flex: 1;
    background-color: blue;
}
查看更多
登录 后发表回答