What is -webkit-focus-ring-color?
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 -webk开发者_开发百科it-focus-ring-color
here but could not find it anywhere.
-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)
-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.
Use this jsFiddle. I got rgb(229, 151, 0)
in Chrome 14 on Windows 7.
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 */
}
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;
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.
精彩评论