• Designing Websites for the iPhone X
  • Timothy Horton
  • The Nuggets translation Project
  • Permanent link to this article: github.com/xitu/gold-m…
  • Translator: Hyde Song
  • Proofreader: Larry Vernon

On the full screen of the newly released iPhone X, Safari beautifully displays existing websites. Content is automatically embedded in a secure area of the display, so as not to be obscured by the space of the rounded corners and original deep view camera system.

The grooves fill the background-color of the page (such as the background color specified for a or < HTML > element) and blend in with the rest of the page. For many sites, that’s enough. If your page has only text and images on the background color, the default groove will look great too.

For other pages — especially those with full-width horizontal navigation bars, like the one below — choose to go a little deeper and take advantage of the new display. The iPhone X human interface guide details some general design principles, and the UIKit documentation discusses specific mechanisms native apps can employ to make sure they look good. Your site can take advantage of edge-to-edge features by taking advantage of some webKit-like apis introduced in iOS 11.

While reading this article, you can click on any image to access the corresponding Demo page and view the source code:

Safari’s default insetting behavior

Default embedding behavior in Safari.

Use the entire screen

The first new feature is an extension of the existing ViewPort meta tag, called ViewPort-Fit, which provides control over the embedded behavior. Viewport-fit is available in iOS 11.

The default value of viewport-fit is auto, which causes the effect of automatic embedding. To disable this behavior and make the page full screen, you can set viewport-fit:cover to cover. After doing so, our ViewPort meta tag looks like this:

<meta name='viewport' content='initial-scale=1, viewport-fit=cover'>Copy the code

After reloading, the navigation bar appears edge-to-edge and looks much better. However, it’s clear why it’s important to pay attention to the embedding of the system’s security zones: the content of some pages is obscured by the space of the original deep camera system, and the navigation bar at the bottom is very difficult to use.

viewport-fit=cover

Use viewport-fit=cover to fit the full screen.

Pay attention to safe areas

In order for the page to be usable after viewport-fit=cover, the next step is to selectively apply padding to elements that contain important content to ensure that the elements are not obscured by the shape of the screen. The resulting page will take advantage of the added screen real estate on the iPhone X, while dynamically adjusting to avoid the four corners of the original deep camera system near the home screen.

Safe and Unsafe Areas

Safe and unsafe for iPhone X landscape (with default embedded values)

To achieve this, WebKit in iOS 11 adds a new CSS function, constant(), and a set of four predefined constants: Safe-area-inset-left, safe-area-inset-right, safe-area-inset-top and safe-area-inset-bottom. When combined, styles are allowed to use the size of the security zone in each direction.

Keep in mind that the CSS working group recently decided to add this feature, but under a different name.

The constant() function is similar to var(), as in the following example, using the padding property:

.post {
    padding: 12px;
    padding-left: constant(safe-area-inset-left);
    padding-right: constant(safe-area-inset-right);
}Copy the code

For browsers that do not support constant(), styles that include constant() are ignored. Therefore, it is important to use alternative styles in addition to styles that use constant().

Safe area constants

Pay attention to the embedding of the security zone, so that important content is visible.

Combine it all together using min() and Max ()

This section describes the features not implemented in iOS 11.

If you use constant() to set the security zone in your website design, you may notice that it is difficult to specify the minimum padding when setting the security zone. In the page above, we replaced the 12 px left padding with constant(safe-area-inset-left), and when we went back to portrait, the left safe area became 0 px, and the text immediately sat close to the edge of the screen.

No margins

Security zone embedding is not a substitute for margins.

To solve this problem, we need to specify that the padding should be either the default padding or the larger one in the security zone. This can be done with the new CSS functions min() and Max (), which will be supported in a future Safari preview release. Both functions take an arbitrary number of arguments and return a minimum or maximum value. They can be used in calc(), or nested together, and both of these functions allow mathematical computations like calc().

For example, in an example like this, Max () can be used like this:

@supports(padding: max(0px)) { .post { padding-left: max(12px, constant(safe-area-inset-left)); padding-right: max(12px, constant(safe-area-inset-right)); }}Copy the code

It is important to use @supports to detect min and Max because it is not supported by any browser. Do not specify variables in @supports queries according to CSS invalid variable handling.

In the example page, constant(safe-area-inset-left) resolves to 0 px in portrait, so Max () resolves to 12 px. Setting constant(safe-area-inset-left) becomes larger in landscape mode due to sensor space, and the Max () function parses this size to ensure that important content is always visible.

max() with safe area insets

Max () combines safe zone inlays with traditional margins

Experienced Web developers have probably encountered CSS locking mechanisms before, typically used to set CSS properties in a specific range of values. Using min() and Max () together makes things easier and will help achieve effective responsive design in the future.

Feedback and questions

You can now start using ViewPort-fit and security zone nesting in Safari of iPhone X emulator in Xcode 9. We’d love to hear about all features being adopted, feel free to send feedback and questions to [email protected] or @webKit on Twitter, and submit any bugs to WebKit’s bug tracker.


The Nuggets Translation Project is a community that translates quality Internet technical articles from English sharing articles on nuggets. The content covers Android, iOS, React, front-end, back-end, product, design and other fields. If you want to see more high-quality translation, please continue to pay attention to the Project, official Weibo, Zhihu column.