Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Safari CSS rule vh-units?

Does anybody know if there is a fix for Safari vh rule?

#what{
 min-height:70vh;
}

All working ok, in all browsers, but only in Safari it is not recognized? Is there a fix for safari, that we can use VH rule in css?

like image 490
Schneider Avatar asked Apr 22 '14 07:04

Schneider


People also ask

Is VH supported in Safari?

Viewport units: vw, vh, vmin, vmax is Fully Supported on Safari 15, which means that any user who'd be accessing your page through Safari 15 can see it perfectly.

What are VH units CSS?

What are VH Units? The full form of VH is viewport height. It works like the percentage unit as well. Specifying 10vh is equivalent to occupying 10% of entire visible screen height. Look at this demo to see how it works:👇 .text { display: none; } .box { width: 300px; height: 50vh; /* display: none; */ }

Can I use VH VW?

vh and vw in actionTo use vh and vw values, just type “Nvh” or “Nvw” (where “N” represents the percentage of the viewport you'd like to cover) into any width or height field. So to cover 100% of the viewport, you'd set 100% for the width and 100vh for the height.

How is VH calculated CSS?

Viewport Height (vh). This unit is based on the height of the viewport. A value of 1vh is equal to 1% of the viewport height.


1 Answers

Instead of Vw/Vh, use rem with this JavaScript. The "run code snippet" might create confusion, cause its window "resizes" by zooming. To test this, just copy this code into some html file and run it in Safari and other browsers (or see "Full Page").

<!DOCTYPE html>
<head>
<script language="javascript" type="text/javascript">
(function (doc, win) {
        var docEl = doc.documentElement,
            recalc = function () {
                var clientWidth = docEl.clientWidth;
                if (!clientWidth) return;

                docEl.style.fontSize = clientWidth + 'px';
                docEl.style.display = "none";
                docEl.clientWidth; // Force relayout - important to new Android devices
                docEl.style.display = "";
            };
 
        // Abort if browser does not support addEventListener
        if (!doc.addEventListener) return;

        // Test rem support
        var div = doc.createElement('div');
        div.setAttribute('style', 'font-size: 1rem');

        // Abort if browser does not recognize rem
        if (div.style.fontSize != "1rem") return;

        win.addEventListener('resize', recalc, false);
        doc.addEventListener('DOMContentLoaded', recalc, false);
    })(document, window);
</script>
<style>
    @charset "utf-8";
    *{padding: 0;margin: 0;}
    div {position:fixed;width:40%;height:30%;background-color:yellow;color:blue;font-size:0.02rem;}
</style>
</head>

<body>
    <div>in this case 0.01 rem == 1vw . You need to remove body margins.</div>
</body>

</html>

For more explenation, see this article that I ve found. http://css-tricks.com/theres-more-to-the-css-rem-unit-than-font-sizing

like image 71
Andrej Ilic Avatar answered Sep 19 '22 16:09

Andrej Ilic