Borders, Radius and Outlines
How borders take up space, how border-radius rounds the corners and clips what's inside, and why outlines are the right tool for focus rings and debugging.
Borders, Radius and Outlines
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A border is part of the box: it adds to the element's size (unless `box-sizing: border-box` absorbs it) and pushes content inward. An outline is drawn outside the box and takes up no layout space.
- `border-radius` rounds the corners of the background and border, and clips scrolling or overflowing content only when `overflow` is not `visible`. Values run top-left, top-right, bottom-right, bottom-left.
- `border-radius: 50%` makes a circle on a square. `border-radius: 999px` makes a pill on any size, because radii that are too large are scaled down to fit.
- `outline` plus `outline-offset` draws a ring around an element without moving anything, which makes it the right tool for focus indicators. Never remove it without putting a visible replacement back.
- A border can be set one side at a time (`border-bottom`), and a style of `none` collapses the width to zero, so a border needs a style to show up.
A border belongs to the box
Every element is a box with content, padding, border and margin, in that order outward. The border is the strip between padding and margin. Because it sits inside the margin edge, it takes up room. A 200px box with a 4px border and default sizing is 208px wide. That arithmetic is the whole reason box-sizing: border-box exists.
The border shorthand sets width, style and color:
.card { border: 1px solid oklch(0.85 0 0); }
.tab { border-bottom: 3px solid currentColor; } /* one side only */
The style is not optional. border-style defaults to none, and a style of none makes the computed width 0. That is why border-width: 2px; border-color: red; alone shows nothing.
border-radius: how corners are rounded
The curve is a quarter of a circle (or ellipse) tucked into each corner. The shorthand goes clockwise from the top-left:
.a { border-radius: 8px; } /* all four */
.b { border-radius: 8px 0 8px 0; } /* TL TR BR BL */
.c { border-radius: 50%; } /* circle if the box is square */
.d { border-radius: 999px; } /* pill, whatever the size */
.e { border-radius: 10px / 40px; } /* elliptical: 10px wide, 40px tall */
The 999px trick works because the browser checks whether adjacent radii would overlap along an edge. If they do, it scales all radii down by the same factor until they fit, so the curve is as round as the box allows. On a short box this gives rounded ends, which is how pill buttons are made.
border-radius shapes the element’s own background and border. It does not clip children by itself. If a child image’s square corners poke out of a rounded card, add overflow: hidden to the card, or give the image the matching radius.
For nested rounded boxes, an inner radius looks right when it equals the outer radius minus the gap between them. A card of border-radius: 16px with 8px of padding wants its inner element at 8px.
Outlines: drawn outside, free of layout
An outline looks like a border, but it is a different mechanism. It is painted around the border edge and takes up no space, so turning one on cannot move anything:
button:focus-visible {
outline: 3px solid oklch(0.6 0.2 250);
outline-offset: 2px; /* gap between the border and the ring */
}
Two properties matter beyond the shorthand. outline-offset sets the gap between the element and the ring. A negative value draws it inside, useful for items inside an overflow: hidden container, where an outside ring would be cut off. And outlines follow border-radius in current browsers, so a rounded button gets a rounded ring.
This is why outlines are the right tool for focus. A keyboard user needs to see where focus is, and that indicator must not shift the page. :focus-visible shows it for keyboard navigation and not for mouse clicks. Writing outline: none removes that signal. If you must restyle it, replace it with something equally visible.
There is one more reason. In forced-colors modes such as Windows High Contrast, the browser removes box-shadow but keeps outlines. In a test with forced colors active in Chromium, the computed box-shadow became none while an outline stayed solid. A focus ring built only from a shadow disappears for those users.
The same trick for debugging
Because an outline takes no space, * { outline: 1px solid red; } shows every element’s edges without shifting a pixel. Adding border: 1px solid red instead would resize every box and distort what you came to inspect.
What the video covers
Kevin Powell’s “CSS Outline vs Border - differences, advantages, and disadvantages” is about ten minutes and dates from January 2021. The properties it covers are unchanged. Its description lists these chapters: the box model, individual sides on borders, border-radius, outline-offset, and a fun use of outline-offset. I could not read a transcript, so I do not describe the demos beyond those chapter titles. The radius clamping, nested radii and forced-colors points above are the prose’s own.
Where this fits
Borders and padding sit inside the box from the box model and box-sizing, and the area they surround is painted by backgrounds and gradients. Where a border draws a hard edge, box shadows and filters draw a soft one. The frontend roadmap’s box model lesson has the shallower version of the sizing arithmetic.
Check yourself
You add `border: 4px solid` to a `width: 200px` box and it grows to 208px wide. Why, and how do you stop it?
With the default `box-sizing: content-box`, `width` sets only the content area, so padding and border add to it. Set `box-sizing: border-box` and `width` then includes the border and padding, so the box stays 200px.
Why is `outline` a better choice than `border` for a hover or focus highlight that shouldn't make the layout jump?
Outlines do not take part in layout. Adding one does not change the element's size or push neighbors around, whereas adding a border makes the box larger (or changes its content position under `border-box`) and can shift the content around it.
A card has `border-radius: 12px` and a child `<img>` at the top. The image's square corners poke out. What fixes it?
`overflow: hidden` on the card clips the child to the rounded shape. Alternatively give the image the same top radius. `border-radius` alone only rounds the element's own background and border, it doesn't clip children.
What does `border-radius: 10px / 40px` mean?
The slash separates horizontal radii from vertical radii. Each corner becomes an elliptical curve 10px wide and 40px tall, instead of a circular arc.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.