Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
541 views
in Technique[技术] by (71.8m points)

html - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...