npm package: @hint/hint-meta-theme-color
Description: hint for best practices related to the 'theme-color' meta tag
Latest version: 4.0.23

# Valid `theme-color` (`meta-theme-color`)

`meta-theme-color` hint checks if a single `theme-color` meta tag
is specified in the `<head>` with a valid value supported by all user
agents.

## Why is this important?

The [`theme-color` meta tag][theme-color spec] provides a way to
suggest a color that browsers should use to customize the display
of the page or of the surrounding user interface. For example,
browsers might use the color for the page's title bar or use it
as a color highlight in a tab bar or task switcher.

In the context of [progressive web apps][pwas], for a more app-like
feel, providing a theme color is essential.

Here is an example of browser UI when the `theme-color` meta tag is
not specified and when it is:

![Browser UI when the theme-color meta tag is not specified](images/no_theme-color.png)
&nbsp; ![Browser UI when the theme-color meta tag is specified](images/theme-color.png)

Note that:

* While the [specification][theme-color spec] defines that the theme
  color can be any [valid CSS color][css color]:

  * Values such as [`hex with alpha` are not supported][hex with alpha
    support] everywhere `theme-color` is.

  * Browsers that supported `rgba`, `hsla`, or `hex with alpha` values
    will ignore the alpha value.

* Always specify the theme color using the meta tag. Even though
  it can also be declared in the [web app manifest file][manifest]:

  * Browsers only acknowledge the value from the web app manifest
    file once the user has added the page to their home screen.

  * The `theme-color` meta tag overwrites the value specified in the
    web app manifest file so it allows for better individual page level
    customization.

## What does the hint check?

The hint checks if a single `theme-color` meta tag is specified
in the `<head>` and the value of its `content` attribute is a
[valid CSS color][css color] supported by all the [targeted
browsers][targeted browsers].

### Examples that **trigger** the hint

The `theme-color` meta tag is not specified:

```html
<!doctype html>
<html lang="en">
    <head>
        <title>example</title>
        ...
    </head>
    <body>...</body>
</html>
```

The `theme-color` meta tag is wrongly specified as `<space>theme-color`:

```html
<meta name=" theme-color" content="#f00">
```

The `theme-color` meta tag is specified with an invalid value:

```html
<meta name="theme-color" content="invalid">
```

```html
<meta name="theme-color" content="currentcolor">
```

The `theme-color` meta tag is specified with a value that is not
supported by all the [targeted browsers][targeted browsers] (e.g.:
`Samsung Internet v5` is targeted).

```html
<meta name="theme-color" content="#f00a">
```

```html
<meta name="theme-color" content="#ff0000aa">
```

The `theme-color` meta tag is not specified in the `<head>`:

```html
<!doctype html>
<html lang="en">
    <head>
        <title>example</title>
        ...
    </head>
    <body>
        <meta name="theme-color" content="#f00">
        ...
    </body>
</html>
```

Multiple `theme-color` meta tags are specified:

```html
<!doctype html>
<html lang="en">
    <head>
        <title>example</title>
        <meta name="theme-color" content="#f00">
        <meta name="theme-color" content="#f0f">
        ...
    </head>
    <body>...</body>
</html>
```

### Examples that **pass** the hint

A single `theme-color` meta tag is specified in the `<head>` and
the value of its `content` attribute is a [valid CSS color][css
color] supported by all the [targeted browsers][targeted browsers].

```html
<!doctype html>
<html lang="en">
    <head>
        <title>example</title>
        <meta name="theme-color" content="#f00">
        ...
    </head>
    <body>...</body>
</html>
```

The `content` attribute value can be specified as:

* a color name

  * `<meta name="theme-color" content="red">`

* `hex` using 3 or 6 digits

  * `<meta name="theme-color" content="#f00">`
  * `<meta name="theme-color" content="#ff0000">`

* `hsl` / `hsla`

  * `<meta name="theme-color" content="hsl(0, 50%, 50%)">`
  * `<meta name="theme-color" content="hsla(0, 50%, 50%, 1)">`

* `rgb` / `rgba`

  * `<meta name="theme-color" content="rgb(255, 0, 0)">`
  * `<meta name="theme-color" content="rgba(255, 0, 0, 1)">`

And, depending on the [targeted browsers][targeted browsers]:

* `hex` using 4 or 8 digits

  * `<meta name="theme-color" content="#f000">`
  * `<meta name="theme-color" content="#ff000000">`

## How to use this hint?

To use it you will have to install it via `npm`:

```bash
npm install @hint/hint-meta-theme-color --save-dev
```

**Note:** The recommended way of running webhint is as a `devDependency` of
your project.

And then activate it via the [`.hintrc`][hintrc] configuration file:

```json
{
    "connector": {...},
    "formatters": [...],
    "hints": {
        "meta-theme-color": "error",
        ...
    },
    "parsers": [...],
    ...
}
```

## Further Reading

* [`theme-color` specification][theme-color spec]

<!-- Link labels: -->

[css color]: https://drafts.csswg.org/css-color/#typedef-color
[hex with alpha support]: https://caniuse.com/#feat=css-rrggbbaa
[manifest]: https://www.w3.org/TR/appmanifest/
[pwas]: https://developer.mozilla.org/en-US/Apps/Progressive
[hintrc]: https://webhint.io/docs/user-guide/configuring-webhint/summary/
[targeted browsers]: https://webhint.io/docs/user-guide/configuring-webhint/browser-context/
[theme-color spec]: https://html.spec.whatwg.org/multipage/semantics.html#meta-theme-color