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 px and rem units. Here you will learn two properties that paint around an element without changing its layout: outline and box-shadow.
Painted, not laid out
border is part of the box model. It adds to the element's size, or takes space from the content area under border-box. outline and box-shadow are different. They are drawn on top of the page after layout is finished, so they take up no space.
In practice this means:
- They do not change the element's width or height.
- They do not push neighbouring elements away.
- They do not affect margin collapsing or the size of a parent container.
- They can overlap neighbours and can be clipped by an ancestor with
overflow: hidden.
Because nothing moves, they are the right tools for effects that appear and disappear, such as focus rings and hover highlights.
Outline
outline is shorthand for outline-width, outline-style, and outline-color. It is drawn just outside the border edge. Unlike border, it has no per-side values, so you cannot outline only the left side.
The outline-offset property moves the outline away from the border edge. A positive value adds a gap. A negative value pulls the outline inward.
.button {
border: 2px solid #333;
padding: 8px 16px;
}
.button:focus-visible {
outline: 3px solid #0a66c2;
outline-offset: 2px;
}When the button receives keyboard focus, the ring appears with no layout shift. Had you used border: 3px solid for the ring, the button would grow by 6px in each direction, and the surrounding content would jump.
Outlines follow the shape of the border, including rounded corners (border-radius) in current browsers.
box-shadow
box-shadow draws one or more shadows around the border box. The syntax is:
box-shadow: offset-x offset-y blur-radius spread-radius color;offset-xandoffset-ymove the shadow right and down. Negative values move it left and up.blur-radiussoftens the edge. The default is 0, which gives a sharp edge.spread-radiusgrows (positive) or shrinks (negative) the shadow before blurring.colorsets the shadow colour.
Add inset at the start to draw the shadow inside the border edge, over the background but under the content.
.card {
width: 300px;
padding: 20px;
border: 1px solid #ccc;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}
.card:hover {
box-shadow: 0 6px 16px rgba(0, 0, 0, 0.3);
}The card stays 342px wide in content-box (300 + 40 + 2) in both states, and nothing around it moves on hover.
Shadows as fake borders
Setting blur to 0 and using only spread gives a solid ring. Unlike an outline, a shadow ring follows the shape exactly and you can stack several in one declaration, separated by commas.
.field:focus {
box-shadow:
0 0 0 2px #fff,
0 0 0 4px #0a66c2;
}This draws a white ring, then a blue ring outside it. In forced-colors modes such as Windows High Contrast, shadows are removed, but outlines are kept. For focus styles, pair a shadow with a transparent outline, such as outline: 2px solid transparent, so a ring still appears in those modes.
Comparison
| Feature | border | outline | box-shadow |
|---|---|---|---|
| Takes layout space | Yes | No | No |
| Per-side control | Yes | No | Through offsets |
| Multiple layers | No | No | Yes |
| Can be inset | Not applicable | No | Yes, with `inset` |
| Shown in forced-colors mode | Yes | Yes | No |
Common errors
- Using a border for hover or focus effects, which shifts the layout.
- Expecting a shadow to be counted in the parent's height. It is not, so leave room with margin or padding.
- A shadow cut off at the edge of a container with
overflow: hidden. - Removing the outline without a replacement focus style.
Exercise
A link has padding: 10px; border: 2px solid transparent;. On hover you want a 3px visible ring that does not move the surrounding text. Which property would you use, and why not change the border width?
Show answer
Use outline: 3px solid <colour> (optionally with outline-offset) or a spread-only box-shadow such as 0 0 0 3px <colour>. Both are painted after layout and take no space. Increasing the border width from 2px to 3px would enlarge the box and shift neighbouring content.
Exercise
What does box-shadow: inset 0 0 0 2px #333 draw, and does it change the element's size?
Show answer
It draws a sharp 2px ring inside the border edge, over the background and under the content. It does not change the element's size, because box-shadow takes no layout space.