An outline draws a line around an element without changing its layout dimensions or the space occupied by nearby elements. It is useful for making an interactive element stand out when it receives focus. section 6.8
For keyboard navigation, :focus-visible can show an outline when someone reaches a control. Unlike :focus, it need not show the same styling for mouse interaction. The outline-offset property controls placement: a positive value moves the line outward, while a negative value moves it inward. section 10.9.1 section 11.5.1
Make a card link easy to find
A card link can keep its bright background while gaining a separate focus cue: a:focus { outline: 1px dashed #e69500; outline-offset: 3px; } The dashed line sits 3px away from the link. section 5.4.2
An outline is not the only possible focus cue. Text fields, a drop-down menu, and a text area can use outline: none when their focused borders change from solid to dashed. Keeping an invalid field’s red border preserves its error cue. Radio buttons and checkboxes instead retain a dashed, offset outline. section 10.9.1
Listing 10.22 Styling text fields and drop-down menu when focused
:is(
input:not([type="radio"], [type="checkbox"]),
textarea,
select
):focus-visible {
outline: none; #1
border-bottom: dashed 1px var(--primary);
border-image: none; #2
}
:is( #3
input:not([type="radio"], [type="checkbox"]).dirty:invalid, #3
textarea.dirty:invalid, #3
select.dirty:invalid #3
):focus-visible { #3
border-color: var(--error); #3
} #3
Compare the focused field rule with the invalid-field rule: removing the outline is paired with a dashed border, and the error border color is retained. section 10.9.1
Place an outline inside a button
A share button uses the same outline on hover and visible focus. Setting outline-offset: -5px places the black line 5px inside the button rather than beyond its outer edge. section 11.5.1
