Sub topic 13
Outline and box-shadow: drawn outside the box model
This chapter assumes you know the four box layers (content, padding, border, margin), how box-sizing changes width calculations, and how to write CSS rules with selectors and declarations. It covers two properties that paint around an element without changing its size or its neighbours' positions: outline and box-shadow.
Why these properties matter for layout
Padding, border, and margin all take up space. If you add border: 4px solid to an element, the element grows by 8px in each dimension (under content-box), and its neighbours may move. That makes borders awkward for effects like focus rings or hover highlights. Adding a border on hover makes the page jump.
outline and box-shadow solve this. Both are painted, but neither is part of the box model's size calculation. They do not change the element's width, height, or the position of any other element.
| Property | Affects element size | Affects neighbour position | Follows `border-radius` |
|---|---|---|---|
| `border` | Yes | Yes | Yes |
| `padding` | Yes | Yes | Not applicable |
| `margin` | No (outside the box) | Yes | Not applicable |
| `outline` | No | No | Yes in current browsers |
| `box-shadow` | No | No | Yes |
Outline
An outline is a line drawn around the border edge of an element. It has three parts, like a border: width, style, and color.
.button:focus-visible {
outline: 3px solid #1a73e8;
outline-offset: 2px;
}The shorthand is outline: <width> <style> <color>. The style must be set (for example solid, dashed, or dotted). The default style is none, so an outline with a width and color but no style draws nothing.
What makes an outline different from a border
- It takes no space. The outline is drawn on top of whatever is next to the element. If a neighbour sits within the outline's reach, the outline overlaps it.
- It is the same on all sides. You cannot set
outline-topor give each side a different width. There is no per-side outline property. - It starts at the border edge. By default, the outline's inner edge touches the outer edge of the border. It never goes inside the padding or content.
- It can be offset.
outline-offsetmoves the outline away from the border edge. A positive value leaves a gap. A negative value pulls the outline inward, over the border and padding. - It may not be rectangular in older browsers. The specification allows an outline to be non-rectangular, for example when it wraps an inline element that spans several lines. Modern browsers also follow
border-radius, but you should not rely on exact shape in older ones.
The following diagram shows where each layer sits relative to the box model.
Outlines and accessibility
Browsers use outlines for keyboard focus indicators. Keyboard users rely on the focus ring to see where they are on the page. A common mistake is removing it:
/* Avoid: removes the focus indicator for everyone */
button:focus {
outline: none;
}If you remove the default outline, you must supply a visible replacement. The better approach is to restyle it, and to use :focus-visible so the ring appears for keyboard focus but not for most mouse clicks.
button:focus-visible {
outline: 3px solid #1a73e8;
outline-offset: 2px;
}Windows High Contrast mode
In forced-colors modes such as Windows High Contrast, the browser overrides most colors but keeps outlines. A focus style built only from box-shadow can vanish in these modes, because the browser removes box shadows when forced colors are active. An outline survives. This is a reason to prefer outline for focus indicators.
If you want a shadow-based look and still need to support forced colors, include a transparent outline:
.input:focus-visible {
box-shadow: 0 0 0 3px rgba(26, 115, 232, 0.5);
outline: 2px solid transparent;
}In normal mode the transparent outline is invisible and the shadow shows. In forced-colors mode the browser replaces the transparent color with a visible system color.
box-shadow
box-shadow paints one or more shadows around the element's border box. Like an outline, it takes no space and does not move other elements.
The syntax is:
box-shadow: <offset-x> <offset-y> <blur-radius> <spread-radius> <color>;| Value | Meaning | Default |
|---|---|---|
| `offset-x` | Horizontal shift. Positive moves right. | Required |
| `offset-y` | Vertical shift. Positive moves down. | Required |
| `blur-radius` | How soft the edge is. Cannot be negative. | `0` (sharp edge) |
| `spread-radius` | Grows (positive) or shrinks (negative) the shadow before blurring. | `0` |
| `color` | Shadow color. | `currentcolor` |
| `inset` | Keyword that draws the shadow inside the border box. | Outer shadow |
A typical card shadow:
.card {
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
}This has no horizontal offset, a 2px downward offset, an 8px blur, and a semi-transparent black color.
Spread and the ring trick
With zero offsets and zero blur, a positive spread draws a solid band around the element. This acts like an outline that follows the shape and can be stacked:
.badge {
box-shadow: 0 0 0 4px #ffd54f;
}You can also use it to fake a second border without changing layout:
.avatar {
border: 2px solid white;
box-shadow: 0 0 0 2px #333;
}The result looks like a white border with a dark ring around it, and the avatar's size stays the same.
Multiple shadows
Separate shadows with commas. The first shadow in the list is painted on top.
.panel {
box-shadow:
0 1px 2px rgba(0, 0, 0, 0.2),
0 8px 24px rgba(0, 0, 0, 0.12);
}Layering a tight, dark shadow with a wide, light one usually looks more natural than a single shadow.
Inset shadows
Adding inset draws the shadow inside the border, above the background but below the content:
.field {
box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.2);
}An inset shadow creates a recessed look, useful for text inputs. Outer and inset shadows can be mixed in one declaration.
Things a shadow does not do
- It does not respond to clicks. A shadow is not part of the element's hit area. Clicking on the shadow does nothing to the element.
- It does not cause scrolling. A shadow that extends past the viewport or a scroll container does not add scrollable overflow.
- It is clipped by ancestors with overflow. If a parent has
overflow: hidden,auto, orscroll, a child's shadow that extends past the parent's padding box is cut off. This is a common bug with card grids. - It is hidden behind later siblings. Shadows are painted as part of the element's background layer, so a following sibling with a background can cover a shadow that extends into it.
Comparing the three ways to draw an edge
Choosing between border, outline, and box-shadow depends on whether you need layout space and what shape you need.
| Need | Best choice | Reason |
|---|---|---|
| A permanent visible edge that is part of the design | `border` | Counts toward size and can differ per side |
| Keyboard focus indicator | `outline` | Survives forced-colors mode and is the browser convention |
| Hover highlight that must not shift layout | `outline` or `box-shadow` | Neither affects size |
| Soft depth or elevation | `box-shadow` | Supports blur and multiple layers |
| Recessed or inner edge | `box-shadow` with `inset` | The only one of the three that draws inside |
| Different widths per side | `border` | Outline is uniform |
Worked example: a hover state that does not jump
This example compares two hover states. The first uses a border and shifts the layout. The second uses an outline and does not.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Hover without layout shift</title>
<style>
*,
*::before,
*::after {
box-sizing: border-box;
}
.row {
display: flex;
gap: 16px;
padding: 16px;
}
.tile {
width: 120px;
height: 80px;
background: #e8f0fe;
}
.bad:hover {
border: 4px solid #1a73e8;
}
.good:hover {
outline: 4px solid #1a73e8;
outline-offset: -4px;
}
</style>
</head>
<body>
<div class="row">
<div class="tile bad">Border hover</div>
<div class="tile good">Outline hover</div>
</div>
</body>
</html>In .bad, the 4px border appears on hover. Because the tile uses border-box and has a fixed width, its outer size does not change, but its content area shrinks by 8px per axis, so text inside can shift or rewrap. With content-box, or with an auto width, the whole tile would grow and push neighbours. In .good, a negative outline-offset draws the ring inside the border edge, and nothing inside the box moves.
Common mistakes
- Setting
outlinewithout a style.outline: 3px #1a73e8draws nothing because the style defaults tonone. - Expecting an outline to push neighbours away. It overlaps them. Leave room with
marginorgapif the ring must not touch anything. - Using a shadow as the only focus indicator. It disappears in forced-colors mode.
- Clipped shadows. A parent with
overflow: hiddencuts off the child's shadow. Add padding to the parent, or move the overflow rule to another element. - Heavy blur on many elements. Large blur radii on long lists can slow painting. Keep blur values modest and test on lower-powered devices.
- Assuming the shadow gets the hit area. Pointer events use the border box, not the shadow.
Exercise
A card has width: 200px; padding: 10px; border: 2px solid; box-sizing: content-box;. You add outline: 6px solid red; and box-shadow: 0 0 0 6px blue;. What is the card's border-box width, and what happens to a sibling 4px to its right?
Show answer
The border-box width is 200 + 2×10 + 2×2 = 224px. The outline and shadow do not change it. Both are painted outside the border edge and take no space, so the sibling does not move. Because both extend 6px but the gap is only 4px, the outline and shadow overlap the sibling visually by 2px. Which one appears on top depends on paint order, so the sibling's background may cover the shadow while the outline is drawn above it.
Exercise
You want a focus style for a text input that uses a soft blue glow and still works in Windows High Contrast mode. Write the CSS.
Show answer
Combine a shadow with a transparent outline: input:focus-visible { box-shadow: 0 0 0 3px rgba(26, 115, 232, 0.5); outline: 2px solid transparent; } In normal mode the glow shows. In forced-colors mode the shadow is removed and the browser draws the outline in a system color.