← Back to HTML & CSS, page 8

html-css-0153 · Medium mid

How do you keep content out of the notch and the home bar area on phones?

Add viewport-fit=cover to the viewport meta tag to draw edge to edge, then pad with env(safe-area-inset-top, right, bottom, left), combined with max() or calc(). Insets are 0 on screens without a notch, and a plain value before the env line works as a fallback.

Full answer

Some phones have a notch, rounded corners or a home indicator bar. If a page uses the whole screen, parts of the content can sit under these areas. The browser handles this by default: it keeps the page inside the safe area. When you want a design that reaches the screen edges, you must handle it yourself in two steps.

  1. Opt in with the viewport meta tag: <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">. With viewport-fit=cover, the page is drawn over the whole screen, including the unsafe areas.
  2. Add padding with the environment variables: env(safe-area-inset-top), env(safe-area-inset-right), env(safe-area-inset-bottom) and env(safe-area-inset-left). They are the sizes of the unsafe areas, and they are 0 on screens without them.

A fixed bottom bar is the most common case:

css
.bar { padding-bottom: 16px; padding-bottom: max(16px, env(safe-area-inset-bottom)); }

The first line is the fallback for browsers that do not understand env(). The second keeps at least 16px of space, and uses the larger inset when there is one. max() and calc() can combine the values: calc(16px + env(safe-area-inset-bottom)) adds them instead.

env() also takes a fallback value: env(safe-area-inset-bottom, 0px). The variables are only filled when viewport-fit=cover is set, and when the phone is turned sideways the left and right insets matter.

Do not use viewport-fit=cover without handling the insets, because then content can be hidden under the notch. If you do not need edge-to-edge design, skip it.

On a desktop browser the insets are 0, so the bar has 12px of padding on every side. On a phone with a home bar, the bottom padding grows to the height of the bar, so the text stays clear of it.

The notch itself was not tested here. Check on a real device or in a simulator that has a notch.

html
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<style>
  .bar { position: fixed; left: 0; right: 0; bottom: 0; background: #222; color: #fff;
    padding: 12px; padding-bottom: 12px; padding-bottom: max(12px, env(safe-area-inset-bottom));
    padding-left: max(12px, env(safe-area-inset-left)); padding-right: max(12px, env(safe-area-inset-right)); }
</style>
<div class="bar" id="bar">Bottom bar</div>

On a desktop browser the insets are 0. The notch was not tested, so check on a device or a simulator. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • viewport-fit=cover lets the page reach the screen edges.
  • env(safe-area-inset-*) holds the unsafe area sizes.
  • Use max() or calc() to combine with normal padding.
  • Insets are 0 where there is no notch.
  • Test on a device with a notch.

Common mistakes

  • Using viewport-fit=cover and leaving out the env() padding.
  • Forgetting a fallback for browsers that do not support env().
  • Ignoring the left and right insets when the phone is turned sideways.

Follow-ups

  • What value does env(safe-area-inset-bottom) have on a desktop?
  • What does viewport-fit=cover change?

Tips

  • State the two steps: viewport-fit=cover and env() insets.
  • Show max() or calc() as the way to combine with normal padding.