ComparisonComparisons

border vs outline

border is part of the box model and enlarges the element, while outline draws a ring outside it, with no effect on the page layout or size.

On this page

Side-by-side comparison

#
Aspectborderoutline
Affects the element’s sizeYes, it enlarges it (outside of border-box)No, it’s drawn outside without taking up space
Can be styled on each side separatelyYes, e.g. border-topNo, it’s a single rectangle around the whole element
Rounding via border-radiusDefines itYes, modern browsers round the outline together with the element
Offsetting from the elementNot possible without an extra elementYes, with outline-offset
Default use by the browserNoneKeyboard focus indicator
Typical useFrames for cards, fields and buttons:focus-visible indicator, highlighting while debugging

Example

#
CSS
.card {
  border: 2px solid #3f3f46;
}

.card:focus-visible {
  outline: 3px solid #a78bfa;
  outline-offset: 3px;
}
Preview

Definition and usage

#

The border property is a full-fledged part of the box model. It adds to the element’s size unless you turn on box-sizing: border-box, and you can style each side separately with border-top, border-right and so on. Rounding the corners with border-radius applies to this very border.

outline is drawn outside the border and never affects the size or layout of neighboring elements. You can’t style it separately on each side: it’s always a single rectangle around the whole element. Modern browsers round it together with border-radius, and outline-offset lets you push it away from the edge.

Browsers use outline by default to show keyboard focus, so it’s a natural choice for your own focus indicators and for debugging layout, since it never shifts the content. Save border for the visible frames of cards, fields and buttons that are part of the design.

Syntax

#
Syntax
border: <thickness> <style> <color>;
outline: <thickness> <style> <color>;

More examples

#
Outline doesn’t take up space in the layout
CSS
.box-outline { outline: 6px solid #f59e0b; }
Preview

Despite the thick outline, items 1 and 3 stay in the same place as they would without it.

The same thickness of border pushes neighbors apart
CSS
.box-border { border: 6px solid #f59e0b; }
Preview

The same element with a border of the same thickness pushes neighbors 1 and 3 away to make room for itself.

Good practices

#
  • For a keyboard focus indicator, use outline, ideally with :focus-visible, so you don’t shift the layout.
  • If you need a different border thickness on one side, it has to be border. Outline is only ever a single rectangle.
  • outline-offset pushes the outline away from the element without changing its size.

Related entries

#

Spotted a mistake or missing an example? Write to us.