outline-offset sets the distance between an element’s outline and its edge. Positive lengths move the outline outward; negative lengths move it inward. section 8.2.2 section 11.5.1
Use outline-offset to make interactive states clearer or to fit an outline inside a control:
- Add breathing room around a dotted hover outline with outline-offset: 2px. section 8.2.2
- Keep keyboard focus visible with a solid outline and the same 2-pixel outward offset. section 8.2.2
- Inset a share-button outline by 5 pixels with outline-offset: -5px. section 11.5.1
Hover and focus states
A button can use a dotted teal outline on hover and a solid teal outline on focus, with outline-offset: 2px in both states. A share button instead uses :focus-visible and outline-offset: -5px to place its black outline inside the button. section 8.2.2 section 11.5.1
Listing 8.4 Button hover and focus states
button:hover,
.button:hover {
outline: dotted 1px #2c6c69;
outline-offset: 2px;
}
button:focus,
.button:focus {
outline: solid 1px #2c6c69;
outline-offset: 2px;
}
Inspect the complete button hover and focus declarations, including outline style, width, color, and a 2-pixel offset. section 8.2.2
Listing 11.4 Share button hover and focus CSS
.share__button:hover,
.share__button:focus-visible {
cursor: pointer;
outline: solid 1px black;
outline-offset: -5px;
}
Inspect a compact focus-visible example that offsets the outline inward by 5 pixels. section 11.5.1
