What is -webkit-focus-ring-color?

CssWebkit

Css Problem Overview


I want to reproduce the outline effect for focused input boxes in webkit to non-webkit browsers. I found here the default CSS used in webkit. The lines of interest are:

:focus {
    outline: auto 5px -webkit-focus-ring-color
}

I tried making a search in the whole code for the definition -webkit-focus-ring-color here but could not find it anywhere.

Css Solutions


Solution 1 - Css

-webkit-focus-ring-color is defined in the WebKit codebase as focusRingColor in each RenderTheme class. That work was performed in June 2009 as part of this changeset by Jeremy Moskovich.

For instance, the default Mac theme (used by Safari) defines the colour in RenderThemeMac.mm (in a roundabout way) as:

[NSColor keyboardFocusIndicatorColor]

(Apple's very light documentation of that property is available online).

There is an override value for the Mac (called WebCore::oldAquaFocusRingColor) to be used for testing (near as I can tell it's for the code to be able to perform comparison between the browser rendering and a reference graphic; it is toggled using WebCore::usesTestModeFocusRingColor). It's defined in ColorMac.mm as the following (which apparently maps to Color(125, 173, 217)):

0xFF7DADD9

Chromium/Chrome defines the colour in RenderThemeChromiumSkia.cpp as:

Color(229, 151, 0, 255)

The default colour (specified in RenderTheme.h) is pure black:

Color(0, 0, 0)

Solution 2 - Css

Edit: As @chharvey notes, Highlight is now a deprecated system color, so disregard this answer.


-webkit-focus-ring-color does not work in Firefox. You can use the system color Highlight as a replacement though.

:focus {
    outline: auto 2px Highlight;
    outline: auto 5px -webkit-focus-ring-color;
}

Also see this site on why resetting outline styles is usually a bad idea.

Solution 3 - Css

Use this jsFiddle. I got rgb(229, 151, 0) in Chrome 14 on Windows 7.

Solution 4 - Css

The following code tries to find the closest solution to the system colors:

*:focus {
    box-shadow: 0 0 1px 3px rgba(59, 153, 252, .7);
    box-shadow: 0 0 0 3px activeborder; /* Blink, Chrome */ 
    box-shadow: 0 0 0 3px -moz-mac-focusring; /* Firefox */
    outline: auto 0 -webkit-focus-ring-color; /* Webkit, Safari */
}

Solution 5 - Css

FWIW: Using Chrome on a Mac, I get a blue outline color when using normal browser mode. When I use the device view, I get a yellow/golden outline color.

Not sure why it changes - was actually very confusing. See examples below.

device-enabled, yellow outline

normal-browser, blue outline

Solution 6 - Css

Nowadays, the revert value has good browser support (yay!), which is used to roll back to the default UA styles, here's my 2020 "please let me have outlines" snippet (usually accompanied with !important declarations):

:focus {
  outline: -webkit-focus-ring-color auto thin;
  outline: revert;
}

Or if you wish to use longhand properties for the first instance of outline:

  outline-color: -webkit-focus-ring-color;
  outline-style: auto;
  outline-width: thin;
  outline: revert;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRandomblueView Question on Stackoverflow
Solution 1 - CssKit GroseView Answer on Stackoverflow
Solution 2 - CsspsanikoView Answer on Stackoverflow
Solution 3 - CssTyler CromptonView Answer on Stackoverflow
Solution 4 - CssHoltwickView Answer on Stackoverflow
Solution 5 - CssFedericoView Answer on Stackoverflow
Solution 6 - CssNullView Answer on Stackoverflow