CSS Outline
In this page:
selector {
outline: width style color;
outline-offset: length;
}
outline-style
outline-style sets the line pattern of an outline, such as solid, dashed, or dotted, following the exact same style keywords as border-style. Unlike border, outline never affects an element's box size or layout, since it's drawn outside the box entirely.
- solid is the most common outline style, especially for custom focus indicators.
- Accepted values:
- none — no outline (initial value)
- auto — browser's default focus-ring style
- solid — one continuous line
- dashed — a series of dashes
- dotted — a series of dots
- double — two parallel lines
- groove / ridge / inset / outset — 3D looks
- hidden is not allowed for outline-style
- inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Example: outline-style
<style>
.box {
outline-style: dashed;
}
</style>
<div class="box">Outline never affects layout size</div>
outline-width and outline-color
outline-width sets how thick the outline is, and outline-color sets its color, working just like border-width and border-color but for the separate outline layer. These are most commonly customized to make a focus outline match a site's branding instead of the browser default.
- Keep outline-width visible enough to notice, at least 2px, since a focus ring is only useful if it's easy to see.
- Accepted values:
- length — e.g. 2px (outline-width)
- thin / medium / thick — width keywords, medium is the initial value
- any color — for outline-color
- invert — legacy value with limited support
- outline shorthand — width, style and color may appear in any order
- inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Example: outline-width and outline-color
<style>
button:focus {
outline-style: solid;
outline-width: 3px;
outline-color: orange;
}
</style>
<button>Tab to me</button>
outline-offset
outline-offset adds space between the element's edge and its outline, pushing the outline further out without changing the element's own size or layout. This is useful when you want visible separation between an element and its focus ring without changing anything else about the box.
- A small positive outline-offset, like 2px to 4px, often makes a focus ring look cleaner by separating it slightly from the element's own border.
- Accepted values:
- length — e.g. 4px, spacing from the border edge
- 0 — outline touches the border edge (initial value)
- negative length — draws the outline inside the border edge
- inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Example: outline-offset
<style>
button:focus {
outline: 2px solid blue;
outline-offset: 4px;
}
</style>
<button>Focus ring pushed outward</button>
Shorthand outline Property
Just like border, outline has a shorthand property that combines width, style, and color into a single declaration. Removing outlines entirely with 'outline: none' is a common accessibility mistake, since it strips keyboard users of visible focus feedback.
- The shorthand keeps focus-state CSS short and readable, especially when defining it on a :focus selector.
- Accepted values:
- outline-width — thin, medium, thick or a length
- outline-style — same values as border-style, except hidden
- outline-color — any color value
- values can appear in any order
- inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Example: Shorthand outline Property
<style>
a:focus {
outline: 2px solid orange;
}
</style>
<a href="#">Width, style, color in one line</a>
Outline vs Border: Key Differences
Border is part of the box model and adds to an element's layout size (unless box-sizing: border-box is used); outline sits entirely outside the box model and never affects layout, even when it visually overlaps neighboring content.
Example: Outline vs Border: Key Differences
<style>
.bordered {
border: 4px solid black;
}
.outlined {
outline: 4px solid black;
}
</style>
<div class="bordered">Border adds to layout size</div>
<div class="outlined">Outline never affects layout, even overlapping</div>
- Setting outline: none to 'clean up' a design without providing any other visible focus indicator, breaking keyboard accessibility.
- Expecting outline to affect an element's layout size the way border does, it doesn't, since it's drawn outside the box entirely.
- Forgetting outline-offset exists, which is often a better fix than fighting with border for spacing around a focus ring.
- Outline draws a line around an element but never affects its layout size, unlike border.
- outline-offset adds space between an element's edge and its outline.
- Outlines are essential for accessibility, since browsers use them to show keyboard focus; never remove them without replacing them.
All standard CSS outline properties covered here are supported by every modern browser.
Chapter Quiz — Complete all 22 topics to unlock
0/22 topics done
Complete these topics first:
- CSS Combinators
- CSS Pseudo-classes
- CSS Pseudo-elements
- CSS Opacity
- CSS Navigation Bar
- CSS Vertical Navbar
- CSS Horizontal Navbar
- CSS Dropdowns
- CSS Advanced Dropdowns
- CSS Image Gallery
- CSS Image Sprites
- CSS Attribute Selectors
- CSS Forms
- CSS Counters
- CSS Specificity
- CSS Specificity Hierarchy
- CSS Variables
- CSS !important
- CSS Box Sizing
- CSS Media Queries
- CSS Pointer Events
- CSS Outline