CSS Outline
An outline is a line drawn around an element, outside its border. It looks similar to a border but behaves very differently: an outline takes up no space and does not affect layout. Browsers use outlines to show keyboard focus, which makes them important for accessibility.
Outline vs border
| Border | Outline | |
|---|---|---|
| Affects element size/layout | Yes | No |
| Can differ per side | Yes (border-left…) |
No — same on all sides |
Follows border-radius |
Yes | Yes (in modern browsers) |
| Can be offset from the edge | No | Yes, with outline-offset |
| Default use | Decoration | Focus indicator |
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; padding: 20px; }
.box { width: 160px; padding: 10px; background: #eef; display: inline-block; margin: 10px; }
.b { border: 8px solid tomato; }
.o { outline: 8px solid tomato; }
</style>
</head>
<body>
<div class="box">plain</div><div class="box b">border</div><div class="box o">outline</div>
<p>The outlined box keeps its original size; the outline just overlaps the space around it.</p>
</body>
</html>Outline properties
| Property | Example values |
|---|---|
outline-style |
none, solid, dotted, dashed, double, groove, ridge, inset, outset |
outline-color |
any colour, or invert (in older browsers) |
outline-width |
thin, medium, thick, or a length like 4px |
outline-offset |
distance between border edge and outline, e.g. 6px (can be negative) |
outline |
shorthand: width style color in any order |
Like borders, an outline is invisible unless outline-style is something other than none.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; display: flex; flex-wrap: wrap; gap: 28px; padding: 20px; }
div { padding: 10px 14px; background: #f4f4f4; }
.s1 { outline: 3px dotted #e91e63; }
.s2 { outline: 3px dashed #3f51b5; }
.s3 { outline: 6px double #009688; }
.s4 { outline: 6px groove #ff9800; }
.s5 { outline: 3px solid #673ab7; outline-offset: 6px; }
.s6 { outline: 3px solid #000; outline-offset: -8px; }
</style>
</head>
<body>
<div class="s1">dotted</div><div class="s2">dashed</div><div class="s3">double</div>
<div class="s4">groove</div><div class="s5">offset 6px</div><div class="s6">offset -8px</div>
</body>
</html>Outlines and focus
When you press Tab to move through links, buttons and inputs, the browser draws a focus ring — usually an outline. Removing it with outline: none makes your site unusable for keyboard users. Instead, restyle it, ideally with :focus-visible so it only shows for keyboard navigation, not mouse clicks.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; padding: 20px; }
button {
font-size: 16px; padding: 10px 18px; margin-right: 10px;
border: none; border-radius: 8px; background: #2962ff; color: white; cursor: pointer;
}
button:focus-visible {
outline: 3px solid #ffab00;
outline-offset: 3px;
}
input { font-size: 16px; padding: 8px; border: 2px solid #999; border-radius: 6px; }
input:focus { outline: 3px solid #00c853; outline-offset: 2px; border-color: transparent; }
</style>
</head>
<body>
<p>Click in the frame, then press <b>Tab</b> to move focus:</p>
<button>First</button><button>Second</button>
<p><input placeholder="Type here"></p>
</body>
</html>Tip: Never write
*:focus { outline: none; }without providing a visible replacement. A strong, high-contrast focus style is one of the simplest accessibility wins.
Debugging with outlines
Because outlines don't change layout, they are perfect for debugging. Temporarily adding * { outline: 1px solid red; } reveals every box on the page without shifting anything.
Test yourself
What is the main layout difference between outline and border?
- Outline is always dashed
- Outline does not take up space
- Outline is inside the padding
- Outline only works on inputs
Which property moves an outline away from the border edge?
- outline-margin
- outline-gap
- outline-offset
- outline-padding
What is the best practice for focus outlines?
- Remove them with outline: none
- Restyle them, e.g. with :focus-visible
- Replace them with a title attribute
- Only show them on hover