CSS Outline
Outline looks like border's cousin — but it behaves very differently. It draws outside the element without affecting layout, making it perfect for focus indicators. Understanding outline is also critical for building accessible web interfaces.
outline-style, outline-width, outline-coloroutline shorthandoutline-offset — positive and
negative valuesWhat is CSS Outline?
An outline is a line drawn outside the element's border. Unlike border, it does not take up space in the layout — it does not push surrounding elements, does not increase element dimensions, and simply draws on top of the surrounding area.
outline-top or outline-left.
border-radius — rounded elements have rounded outlines.Outline Sub-Properties
Outline has three sub-properties that work the same as border. outline-style is required for the outline to render.
.focused {
outline-width: 3px; /* thickness — px, rem, thin/medium/thick */
outline-style: solid; /* ← required! same values as border-style */
outline-color: #264de4; /* any CSS colour */
}
/* outline-color: invert — high contrast on any background */
.high-contrast {
outline: 3px solid;
outline-color: invert;
}
/* Remove outline — use carefully! */
.custom-focus { outline: none; }
.custom-focus { outline: 0; } /* equivalent */
outline-style — All Values
outline-style accepts the same values as border-style. solid and dashed are most common for focus rings.
The outline Shorthand
The outline shorthand uses the same width
→ style → color order as border:
/* outline: width style color */
button:focus { outline: 3px solid #264de4; }
input:focus { outline: 2px solid #264de4; }
a:focus { outline: 3px dashed #264de4; }
.debug * { outline: 1px solid red; } /* layout debug */
/* :focus-visible — keyboard focus only, not mouse click */
button:focus-visible {
outline: 3px solid #264de4;
outline-offset: 3px;
}
/* currentColor matches the element's own text colour */
a:focus { outline: 2px solid currentColor; }
outline-offset — The Gap
outline-offset controls the distance between the
element's border edge and the outline. Positive values push outward; negative values pull inward for an
inset effect.
/* Positive — gap between border and outline */
button:focus-visible {
outline: 3px solid #264de4;
outline-offset: 4px; /* 4px gap around the button */
}
/* Negative — inset outline inside the element */
.card:focus-visible {
outline: 3px solid #264de4;
outline-offset: -8px; /* draws inside the card */
}
/* Double ring: border + outline + gap */
.double-ring {
border: 2px solid #264de4;
outline: 2px solid #264de4;
outline-offset: 4px;
}
Outline vs Border — Complete Comparison
| Feature | border | outline |
|---|---|---|
| Affects layout / takes up space | Yes | No |
| Increases element size | Yes (content-box) | Never |
| Individual sides (top/right etc.) | Yes | No — all 4 only |
| Works with border-radius | Yes | Yes (modern browsers) |
| Gap from element (offset) | No | Yes — outline-offset |
| Negative offset (inset) | No | Yes |
| Shown on :focus by default | No | Yes |
| Clipped by overflow: hidden | Yes | No |
| Part of the box model | Yes | No |
Accessibility — Never Remove Outline Without Replacement
The browser's default focus outline is critical for keyboard navigation. Removing it without a replacement is a WCAG 2.1 Level AA accessibility violation.
* { outline: none; } /* destroys keyboard accessibility */
button:focus { outline: none; } /* unless you add a custom visible focus style */
Focus Style Examples — Live Demos
outline: none with no replacement.
outline: 3px solid #264de4
outline + outline-offset: 3px
Works on any background colour
outline-offset: -4px (inset)
Custom branded accessible ring
/* Pattern 1: Simple branded ring */
:focus-visible {
outline: 3px solid #264de4;
outline-offset: 3px;
}
/* Pattern 2: Works on light AND dark backgrounds */
:focus-visible {
outline: 2px solid #ffffff;
outline-offset: 0;
box-shadow: 0 0 0 4px #264de4;
}
/* Pattern 3: Keyboard only (hide on mouse click) */
:focus:not(:focus-visible) { outline: none; }
:focus-visible {
outline: 3px solid #264de4;
outline-offset: 3px;
}
/* Pattern 4: Input — border glow instead of outline */
input:focus {
outline: none;
border-color: #264de4;
box-shadow: 0 0 0 3px rgba(38,77,228,.15);
}
:focus fires on any focus event (including mouse clicks). :focus-visible fires only when the browser determines a visible ring should
be shown — typically keyboard navigation only. Use :focus-visible to
avoid showing rings on mouse clicks while still supporting keyboard users fully.
Practical Uses of Outline
* { outline: 1px solid red }
outlines every element without moving anything — fastest layout debugging tool.
/* Layout debug — never ship to production */
* { outline: 1px solid red !important; }
/* Image gallery selection */
.gallery-img { cursor: pointer; outline: none; }
.gallery-img.selected {
outline: 3px solid #264de4;
outline-offset: 3px;
}
/* Double ring button */
.btn-double-ring {
border: 2px solid #264de4;
outline: 2px solid #264de4;
outline-offset: 4px;
}
/* Card inset focus */
.card:focus-visible {
outline: 3px solid #264de4;
outline-offset: -8px;
}
Try It Yourself
Click or Tab through the elements in the preview to see focus outlines in action. Try
changing outline-offset values.
Quick Check — Chapter 14
Other key differences: outline cannot be applied to individual sides (all four or nothing); outline supports
outline-offset including negative values; outline appears
by default on focused elements.
It violates WCAG 2.1 Success Criterion 2.4.7 (Focus Visible, Level AA). Always replace
outline: none with a custom visible focus style — either a custom
outline, a box-shadow ring, or a border colour change.
outline-offset controls the gap between the element's border edge
and the outline.Positive values push the outline outward, creating a gap. Example:
outline-offset: 4px on a button focus ring gives a nice breathing
space.Negative values pull the outline inward, drawing it inside the element. Example:
outline-offset: -8px on a card creates a subtle inset focus
ring visible inside the card border.
:focus matches whenever an element receives focus — including mouse
clicks on buttons. This can show focus rings on mouse interactions which some find visually
noisy.:focus-visible matches only when the browser determines a visible
focus ring should be shown — typically keyboard navigation (Tab key). Mouse clicks on buttons do NOT
show the ring. This is the modern best practice.
:focus-visible {outline: 2px solid #ffffff;outline-offset: 0;box-shadow: 0 0 0 4px #264de4;}The white ring is visible on dark backgrounds; the blue shadow ring is visible on light backgrounds. At least one layer is always visible regardless of the background colour.
- Outline draws outside the border and takes up no layout space — no element size change, no reflow.
- Three sub-properties:
outline-width,outline-style(required),outline-color. Shorthand:outline: width style color. outline-offsetcontrols the gap. Positive = outward. Negative = inset (draws inside the element).- Outline cannot be applied to individual sides — all four simultaneously only.
- Never remove outline without a visible replacement. Keyboard accessibility and WCAG 2.1 compliance require a visible focus indicator.
:focus-visibleis best practice — shows ring for keyboard, hides for mouse clicks.- Universal focus pattern:
outline: 2px solid #fff; box-shadow: 0 0 0 4px #264de4— visible on any background. - Outline is ideal for: focus indicators, layout debugging, double-ring effects, and image selection states.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.