5

Meta Theme Color and Trickery | CSS-Tricks

 3 years ago
source link: https://css-tricks.com/meta-theme-color-and-trickery/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Meta Theme Color and Trickery

Manuel Matuzovic on

Jul 13, 2021 (Updated on Jul 14, 2021)

Learn Development at Frontend Masters

Starting with Version 15, Safari supports the theme-color <meta> tag both on macOS and iOS. That’s exciting news because now the first desktop browser supports this <meta> tag and it also supports the media attribute and the prefers-color-scheme media feature.

I never really took much note of the theme-color meta tag, but now is a good time to learn about its features and limitations and try to discover some interesting use cases.

Heads up! Safari removed support for the theme-color meta tag in the current release of Safari Technology Preview (127). This seems to be a temporary thing.

Features and limitations

Here’s how I’ve been using the theme-color meta tag for the past few years: just a good ‘ol hex code for the content attribute.

<meta name="theme-color" content="#319197">
themecolor_1.jpg?resize=575%2C346&ssl=1

According to tests I made earlier this year, this works in Chrome, Brave and Samsung Internet on Android, installed PWAs in Chrome and now also in Safari Technology Preview.

s_2BD09FE6F5E7D3412F4AEEF0B1BEE0CE1D56F7023E30B804529E2685536D0955_1625073641591_hex.png?resize=2072%2C896&ssl=1Hex color support is great in all supported browsers.

CSS color support

One of the first questions that came to my mind was “Can we use color keywords, hsl(), rgb(), too?” According to the HTML spec, the value of the attribute can be any CSS color. I’ve created this theme-color testing CodePen to verify that.

<meta name="theme-color" content="hsl(24.3, 97.4%, 54.3%)">
Blank webpage with orange header.The theme-color meta tags supports CSS colors in any form: keywords, rgb(), hsl() or hex code.
Blank webpage with a hot pink header. There are controls to the right of the webpage for browser testing.
Looking at Chrome 90 on an Android Galaxy S20

All supported browsers also support hsl() and rgb(). This is awesome because it allows us to do some pretty cool stuff with JavaScript. We’ll talk about that later, but first let’s look at some limitations.

Transparency

HEX codes, rbg(), hsl() and keywords are well and consistently supported, but colors that include transparency: not so much. Actually, they are supported in most browsers, but the results aren’t very consistent and sometimes unexpected.

transparent is a CSS color and used in the theme-color meta tag most browsers do what you’d expect. All regular mobile browsers don’t change color and display the default tab bar, but Safari on macOS and the Chrome Canary PWA on macOS turn the tab bar black. The PWA on Android falls back to theme-color defined in the manifest.json, which we’ll talk about in a bit.

Examples of the same white webpage with either white or dark headers with the browser vendor labeled above each one.
Browser with a transparent theme-color meta tag

All browsers interpret hsla() and rgba(), but they set the alpha value to 1. The only exception is Safari on macOS; it interprets the transparency, but it seems like the transparent color has a black baseline. This has the effect that the light orange color looks like dark orange.

Same browser comparison but all with orange headers, except Safari which is a darker brown.hsla() applied to the theme-color meta tag

New color functions

Safari 15 is the first browser to support lab(), lch(), and hwb() color functions. These functions work if you use them in CSS, but not if you use them in the theme-color meta tag.

All three declarations work fine in Safari 15:

body {
  background-color: hwb(27 10% 28%);
  background-color: lch(67.5345% 42.5 258.2);
  background-color: lab(62.2345% -34.9638 47.7721);
}

If you use any of the new color functions in the theme-color meta tag, Safari doesn’t interpret them and falls back to its own algorithm of picking the color. It’s likely that Safari uses the background color of your <body> for the theme-color, which means that you might get the expected result without defining the theme-color explicitly.

<meta name="theme-color" content="lab(29.2345% 39.3825 20.0664)">
Green webpage with green header.

Please be aware that at the time of writing Safari 15 is the only browser to support these new colors functions.

currentColor

If CSS colors are supported, currentColor should work, too, right? No, unfortunately not in any browser. It’s probably an uncommon use case, but I would expect that we can set the theme-color to the current color of the <body> or <html> element.

<style>
  body {
    color: blue;
  }
</style>

<meta name="theme-color" content="currentColor">

I found a ticket in the WebKit bug tracker titled <meta name="theme-color" content="..."> should also support CSS currentcolor.” Support might change in the future, if someone picks the ticket up.

Prohibited colors

When I was testing CSS color keywords, I used the color red and it didn’t work. First, I thought that keywords weren’t supported, but blue, hotpink, and green worked fine. As is turns out, there’s a narrow range of colors that Safari doesn’t support, colors that would get in the way of using the interface. red doesn’t work because it’s visually too close to the background color of the close button in the tab bar. This limitation is specific to Safari, in all other supported browsers any color seem to work fine.

Wbite webpage with a color picker set to red. The header of the browser is white.If you set the theme-color to red, Safari uses any color it deems appropriate.

Custom properties

I don’t know enough about the internals of browsers and custom properties and if it’s even possible to access custom properties in the <head>, but I tried it anyway. Unfortunately, it didn’t work in any browser.

<style>
  :root {
    --theme: blue;
  }
</style>

<meta name="theme-color" content="var(--theme)">

That’s pretty much everything I wanted to know about basic support of the theme-color meta tag. Next, let’s see how to and how not to implement dark mode for the tab bar.

Dark mode

Safari 15 is the first desktop browser to support the media attribute and the prefers-color-scheme media feature on theme-color meta tags. Starting with version 93, Chrome supports it too, but only for installed progressive web apps.

According to the web app manifest page on web.dev, if you define multiple theme-color meta tags, browsers pick the first tag that matches.

<meta name="theme-color" content="#872e4e" media="(prefers-color-scheme: dark)">

I was eager to find out what happens in browsers that don’t support the media attribute. I’ve created a demo page for testing dark mode that includes the meta tags above and also allows you to install the site as a PWA. The webmanifest.json includes another color definition for the theme-color.

{
  "name": "My PWA",
  "icons": [
    {
      "src": "https://via.placeholder.com/144/00ff00",
      "sizes": "144x144",
      "type": "image/png"
    }
  ],
  "start_url": "/theme-color-darkmode.html",
  "display": "standalone",
  "background_color": "hsl(24.3, 97.4%, 54.3%)",
  "theme_color": "hsl(24.3, 97.4%, 54.3%)"
}

Here’s how supported browsers display the tab bar in light mode. It doesn’t matter if a browser supports the media attribute or not, it will interpret the first meta tag regardless.

s_2BD09FE6F5E7D3412F4AEEF0B1BEE0CE1D56F7023E30B804529E2685536D0955_1624815621789_lightmode.png?resize=1024%2C443&ssl=1

Here’s how the tab bar on the same page looks like in dark mode. These results are more interesting because they vary a bit. The Canary PWA and Safari support and show the dark color. All mobile browsers use their default dark tab bar styling, except for Samsung Internet, which uses the light styling because it doesn’t support the prefers-color-scheme media feature. (TIL: This should change in the near future.)

s_2BD09FE6F5E7D3412F4AEEF0B1BEE0CE1D56F7023E30B804529E2685536D0955_1624817862906_darkmode-1.png?resize=2072%2C896&ssl=1

I did one last test. I wanted to see what happens if I only define a theme color for dark mode, but access the page in light mode.

<meta name="theme-color" content="#872e4e" media="(prefers-color-scheme: dark)">
s_2BD09FE6F5E7D3412F4AEEF0B1BEE0CE1D56F7023E30B804529E2685536D0955_1624818342759_darkmodeonly.png?resize=2072%2C896&ssl=1

These results surprised me the most because I expected all mobile browsers to ignore the media attribute and just use the dark color in the meta tag regardless, but ordinary Chrome Canary completely ignores the whole meta tag, even though it doesn’t support the media attribute. As expected, both Canary PWAs fall back to the color defined in the manifest file.

The other interesting thing is that Safari displays a theme-color even though I haven’t defined one for light mode. That’s because Safari will pick a color on its own, if you don’t provide a theme-color. In this case, it uses the background color of the page, but it also might use the background color of the <header> element, for example.

If you want to define a theme color for light and dark mode, your best bet is to define both colors and use the first meta tag as a fallback for browsers that don’t support the media feature.

<meta name="theme-color" content="#319197" media="(prefers-color-scheme: light)">
<meta name="theme-color" content="#872e4e" media="(prefers-color-scheme: dark)">

Safari has proven that theme-color works great on desktop browsers, too. I’m sure that designers and developers will find many creative ways to use this meta tag, especially considering that the value can be changed via JavaScript. I’ve collected and created some interesting demos for your inspiration.

Demos and use cases

Theming s_2BD09FE6F5E7D3412F4AEEF0B1BEE0CE1D56F7023E30B804529E2685536D0955_1624819758353_Screenshot2021-06-27at20.48.55.png?resize=2804%2C1786&ssl=1s_2BD09FE6F5E7D3412F4AEEF0B1BEE0CE1D56F7023E30B804529E2685536D0955_1625077477167_max.png?resize=1669%2C1079&ssl=1 Page theming s_2BD09FE6F5E7D3412F4AEEF0B1BEE0CE1D56F7023E30B804529E2685536D0955_1624898056250_dave.png?resize=1660%2C1212&ssl=1 Gradients s_2BD09FE6F5E7D3412F4AEEF0B1BEE0CE1D56F7023E30B804529E2685536D0955_1624899029183_Screenshot2021-06-28at18.50.05.png?resize=2804%2C1786&ssl=1 Form validation s_2BD09FE6F5E7D3412F4AEEF0B1BEE0CE1D56F7023E30B804529E2685536D0955_1624820499882_fomr.png?resize=1386%2C647&ssl=1

 email  document
 themeColor  document
 msg  document
 color  
 message  

document   
  e

  email
  email 

   emailvalidityvalid 
    color  
    message  
    email 
  

  msgtextContent  message
  themeColor color

Disco mode



 motion  window


 motionmatches 
   scheme  document
   hue  
   color

    
    color  
    documentbodystylebackground  color
    scheme color
   

Scrolling

     
   meta  document
   meta 
    meta color
  


   window 
   observer     
      entries  
          isIntersecting target   entry
         isIntersecting 
           color  windowtarget
          color
        
      
   
    root document
    rootMargin 
    treshold 
  
  
  document  
    observersection
  

Extracting color s_2BD09FE6F5E7D3412F4AEEF0B1BEE0CE1D56F7023E30B804529E2685536D0955_1624902669606_dominantcolor.png?resize=2804%2C1786&ssl=1

That is just a handful of ideas, but I already like where this is going and I’m sure that you’ll come up with even more creatives ways of using the theme-color meta tag.

Resources


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK