Tutorials CSS Outline
Ch 14 / 60
Chapter 14 Beginner 5 min read CSS

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.

What you'll learn
What outline is and how it differs from border
outline-style, outline-width, outline-color
The outline shorthand
outline-offset — positive and negative values
Accessibility — why removing outline is dangerous
Custom focus styles that are beautiful and accessible

What 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.

border (affects layout)
Box A
Box B
Each border adds to rendered size and pushes neighbours
outline (no layout impact)
Box A
Box B
Outline paints outside — boxes are same size as if no outline existed
Does not affect layout
Adding or removing an outline does not change element dimensions, does not trigger reflow, and does not push neighbouring elements.
Cannot style individual sides
Unlike border, outline applies to all four sides simultaneously. There is no outline-top or outline-left.
Follows border-radius
In modern browsers, outline follows the element's border-radius — rounded elements have rounded outlines.
Primary purpose: focus indicator
Browsers use outline automatically to show which element has keyboard focus. This is a critical accessibility feature.

Outline Sub-Properties

Outline has three sub-properties that work the same as border. outline-style is required for the outline to render.

CSS — outline sub-properties
.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.

solid
solid
dashed
dashed
dotted
dotted
double
double
groove
groove
ridge
ridge
inset
inset
outset
outset

The outline Shorthand

The outline shorthand uses the same width → style → color order as border:

CSS — outline shorthand
/* 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.

inset
Inset
offset: -6px
flush
Flush
offset: 0
gap
Small gap
offset: 3px
gap
Large gap
offset: 8px
CSS — outline-offset
/* 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.

Never do this
* { outline: none; } /* destroys keyboard accessibility */ button:focus { outline: none; } /* unless you add a custom visible focus style */

Focus Style Examples — Live Demos

❌ No outline (dangerous)
Keyboard focus invisible!

outline: none with no replacement.

✅ Simple ring
Focused

outline: 3px solid #264de4

✅ Ring with gap
Focused

outline + outline-offset: 3px

✅ White + shadow ring
Focused

Works on any background colour

✅ Inset outline
Focused

outline-offset: -4px (inset)

✅ Brand colour
Focused

Custom branded accessible ring

CSS — Accessible custom focus styles
/* 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 vs :focus-visible
: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

Focus ring
The primary purpose — shows which element has keyboard focus on buttons, links and inputs.
Debug layout
* { outline: 1px solid red } outlines every element without moving anything — fastest layout debugging tool.
Double border ring
Combine border + outline + outline-offset for a double-ring design effect with a visible gap.
Selection indicator
Mark selected gallery images with outline — no layout jump unlike adding a border.
CSS — Practical patterns
/* 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;
}
Free Resource
CSS3 Cheat Sheet — All Properties & Selectors
All CSS3 properties including outline, border and box model reference.
Download PDF

Try It Yourself

Click or Tab through the elements in the preview to see focus outlines in action. Try changing outline-offset values.

Live Editor
✎ Editor
👁 Preview — Tab through elements
Featured Course — Enroll Now
Modern Web Development With AI
Master HTML, CSS, JavaScript, React and Node.js with real projects and placement support.
120+
Chapters
40 hrs
Content
10 Real
Projects
4.9 ⭐
Rating
HTML5CSS3JavaScriptReact.jsNode.jsTailwind CSSAI Tools

Quick Check — Chapter 14

Questions & Answers
The biggest difference is that outline does not affect layout. Border is part of the CSS Box Model — it takes up space and pushes neighbours. Outline is drawn outside the element but takes zero space. Adding or removing it causes no reflow, no dimension change, no neighbour displacement.

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.
The browser's focus outline tells keyboard users which element currently has focus. Without it, pressing Tab shows no visible indicator of where you are on the page. This affects users with motor disabilities, screen reader users, and keyboard power users.

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.
Combine a white outline with a dark box-shadow ring:

: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.
Multiple Choice Questions
Question 1 of 5
Does CSS outline affect the layout or dimensions of an element?
Question 2 of 5
Which selector shows focus rings only for keyboard navigation, not mouse clicks?
Question 3 of 5
What does a negative value for outline-offset do?
Question 4 of 5
Which is TRUE about outline compared to border?
Question 5 of 5
What is the primary built-in purpose of CSS outline in browsers?
Chapter Summary
  • 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-offset controls 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-visible is 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.
~5 min read
Beginner level
Chapter 14 of 60

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel