aboutsummaryrefslogtreecommitdiff
path: root/build/dv/index.html
blob: 43d3cc904f13b31bcc30eac31a10441e8dcedb88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<!doctype html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<meta name="color-scheme" content="dark light">
	<link rel="icon" href="data:,">
	<title>Dynamic Viewports with CSS</title>
	<link href="/atom.xml" type="application/atom+xml" rel="alternate" title="Atom feed for blog posts" />
	<link href="/rss.xml" type="application/rss+xml" rel="alternate" title="RSS feed for blog posts" />
<style>*{box-sizing:border-box;}body{font-family:sans-serif;line-height:1.33;margin:0 auto;max-width:650px;padding:1rem;}blockquote{background:rgba(0,0,0,0.1);border-left:4px solid;padding-left:5px;}img{max-width:100%;}pre{border:1px solid;overflow:auto;padding:5px;}table{text-align:left;width:100%;}.footnotes{font-size:90%;}</style>
</head>

<nav>
	<a href="#menu">Menu &darr;</a>
</nav>

<main>
<h1 id="dynamic-viewports-with-css">Dynamic Viewports with CSS</h1>
<p>2023-02-08</p>
<p>I think it&#8217;s safe to assume most web designers and developers are familiar with the standard <code>vh</code> and <code>vw</code> parameters in CSS. These parameters are used for setting an element&#8217;s height and&#47;or width, relative to the viewport (v) height (h) or width (w). For example:</p>
<p>If I want my <code>.box</code> element to take up the entire height of a device&#8217;s screen:</p>
<pre><code>.box {
    height: 100vh;
}
</code></pre>
<p>Or I want my <code>.box</code> element to take up the entire width of a device&#8217;s screen:</p>
<pre><code>.box {
    width: 100vw;
}
</code></pre>
<p>These are wonderful options to have - specifically for those of us designing web applications. But there are some <em>minor</em> issues with <code>vh</code> and <code>vw</code>.</p>
<ol>
<li>The setting does not take into account device-specific UI (status bars, toolbars, search fields etc.)</li>
<li>In some instances these will not play nice with <code>box-sizing</code> properties</li>
</ol>
<h2 id="have-no-fear-dynamic-viewport-is-here">Have No Fear, Dynamic Viewport is Here!</h2>
<p>Lucky for us there exists an awesome <em>new-ish</em> CSS API called dynamic viewport-percentage units: <code>dvh</code> &#38; <code>dvw</code>. They are defined as follows:</p>
<blockquote>
<p>The dynamic viewport-percentage units (dv) are defined with respect to the dynamic viewport size: the viewport sized with dynamic consideration of any UA interfaces that are dynamically expanded and retracted. This allows authors to size content such that it can exactly fit within the viewport whether or not such interfaces are present.</p>
</blockquote>
<p>So our examples above would translate into:</p>
<pre><code>.box {
    height: 100dvh;
    width: 100dvw;
}
</code></pre>
<h3 id="what-about-browser-support">What About Browser Support?</h3>
<p><a href="https://caniuse.com/mdn-api_css_dvh">Can I Use Stats</a> &#47; ~67.17% coverage.</p>
<p><strong>Note:</strong> Even though the caniuse page states that Firefox 109+ and iOS Safari 16.3 do not support <code>dvh</code>, in my experiments they do. I&#8217;m not sure what testing was done for those two browsers, so YMMV.</p>
<p>If you want to play it safe, use dynamic viewports with standard &#8220;traditional&#8221; viewports as backup. That way you support all use cases while still taking advantage of newer CSS properties.</p>
<footer role="contentinfo">
    <h2>Menu Navigation</h2>
    <ul id="menu">
        <li><a href="/">Home</a></li>
        <li><a href="/projects">Projects</a></li>
        <li><a href="/uses">Uses</a></li>
        <li><a href="/wiki">Wiki</a></li>
        <li><a href="/resume">Resume</a></li>
        <li><a href="/colophon">Colophon</a></li>
        <li><a href="/now">Now</a></li>
        <li><a href="/donate">Donate</a></li>
        <li><a href="/atom.xml">RSS</a></li>
        <li><a href="#top">&uarr; Top of the page</a></li>
    </ul>
    <small>
        Built with <a href="https://git.sr.ht/~bt/barf">barf</a>. <br>
        Maintained with ♥ for the web. <br>
        Proud supporter of <a href="https://usefathom.com/ref/DKHJVX">Fathom</a> &amp; <a href="https://nextdns.io/?from=74d3p3h8">NextDNS</a>. <br>
        The content for this site is <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>.<br> The <a href="https://git.sr.ht/~bt/bt.ht">code for this site</a> is <a href="https://git.sr.ht/~bt/bt.ht/tree/master/item/LICENSE">MIT</a>.
    </small>
</footer>