Skip to content
Tutorials Center
My learning

Topics

Lesson 36 of 139 ยท The Box Model

Outlines

Learn how CSS outlines differ from borders, how outline-offset works, and how to design clear, accessible focus indicators with :focus-visible.

An outline is a line drawn around an element, just outside its border. At first glance it looks like a second border, but it behaves very differently: it takes up no space in the layout, it can't be set per side, and it can be pushed away from the box with outline-offset.

Those properties make outlines the perfect tool for one job above all: showing which element has keyboard focus. Browsers draw focus rings with outlines by default, and a large part of this lesson is about styling them well instead of removing them.

Outline basics

An outline has the same three parts as a border: a style, a width and a color. There are individual properties and a shorthand:

CSS
.highlight {
  outline-style: solid;
  outline-width: 3px;
  outline-color: #f59e0b;
}

/* Same thing */
.highlight {
  outline: 3px solid #f59e0b;
}

The styles are the same as for borders (solid, dashed, dotted, double and so on), with one extra: auto. outline-style: auto asks the browser to draw its own native focus-ring style, which is what you see on many default focus rings.

Just like borders, an outline with style none is invisible. The color defaults to currentColor, the element's text color.

Outline vs border

Here is how the two compare:

Border Outline
Takes up space Yes, changes the element's size No, drawn on top of the layout
Different sides Yes (border-left, โ€ฆ) No, always all four sides
Follows border-radius Yes Yes, in all current browsers
Can be offset from the box No Yes, with outline-offset
Part of the box model Yes No

Because an outline takes no space, adding or removing one never moves anything. Neighbors don't shift, text doesn't reflow. The outline can overlap nearby content instead.

Border vs outline on hover

The border version pushes its neighbors around; the outline version just draws a ring. Notice that the outline also follows the rounded corners.

outline-offset

outline-offset moves the outline away from the border edge. A positive value creates a gap; a negative value pulls the outline inside the element.

CSS
button:focus-visible {
  outline: 2px solid #2563eb;
  outline-offset: 3px;
}

A small gap between the element and its ring is very readable: the ring stands apart from the element's own edges and background, even when both are similar colors.

Playing with outline-offset

The negative offsets draw the line inside the box, a nice way to get a "stitched" look on a card without touching its size.

Focus indicators

When you press Tab to move through links, buttons and form fields, the browser draws a focus indicator on the active element. For keyboard users, it plays the role of the mouse pointer: without it, they cannot tell where they are on the page.

Browsers draw this indicator with an outline. Unfortunately, many old stylesheets contain this line:

CSS
/* Don't do this */
*:focus {
  outline: none;
}

It was usually added because the focus ring appeared after clicking a button with the mouse and a designer didn't like it. But it makes the site unusable for keyboard users. Never remove focus styles without replacing them with something at least as visible.

:focus vs :focus-visible

The modern answer to "I don't want rings on mouse clicks" is the :focus-visible pseudo-class. The browser matches it only when it thinks a focus indicator is helpful: when the user navigates with the keyboard, and always for text fields. A mouse click on a button usually focuses it without matching :focus-visible.

CSS
/* Custom focus ring, shown only when it helps */
:focus-visible {
  outline: 3px solid #2563eb;
  outline-offset: 2px;
}

Browsers' own default focus styles already use :focus-visible logic, so this rule simply replaces the look while keeping the behavior.

Designing a focus ring

With the keyboard, every element gets the amber ring. With the mouse, buttons show only a subtle inset line, while the text field still shows the ring because you're about to type in it.

What makes a good focus indicator

  • Contrast. The ring should contrast clearly with the background around it, at least 3:1. A ring that's the same color as the button disappears.
  • Thickness. 2โ€“3px is a good minimum. A thin 1px dotted line is easy to miss.
  • Offset. A small outline-offset separates the ring from the element, so it reads well even on colored buttons.
  • Consistency. Use one style for the whole site, so users learn what "focused" looks like.

A ring that works on any background. Pair an outline with a contrasting box-shadow: outline: 2px solid white; box-shadow: 0 0 0 4px #1e3a8a;. One of the two colors is always visible, whether the element sits on a light or a dark area.

Outlines and forced colors

Some users turn on a high-contrast mode, called forced colors on Windows. In that mode the browser replaces your colors with the user's own palette and removes all box shadows. Outlines, however, are kept and recolored. That is the best reason to draw focus indicators with outline rather than only box-shadow. If you must hide an outline visually, make it transparent instead of none:

CSS
.fancy-button:focus-visible {
  outline: 2px solid transparent; /* invisible normally, visible in forced colors */
  box-shadow: 0 0 0 3px #a855f7;
}

You will learn more about this in Accessible CSS.

Outlines as a debugging tool

Since outlines don't change layout, they are a handy way to see the boxes on a page while debugging. A temporary rule like this shows every element without shifting anything:

CSS
* {
  outline: 1px solid rgb(255 0 0 / 0.4);
}

Borders would add a pixel on every side and change the layout you're trying to debug. DevTools can highlight boxes too, but this one-liner shows all of them at once.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What happens to the layout when you add a 5px outline to an element?

    Outlines are drawn on top of the layout and do not affect the size or position of anything.
  2. 2. What does outline-offset: -4px do?

    A negative offset pulls the outline inward, into the element.
  3. 3. Why is *:focus { outline: none; } harmful?

    The focus indicator is how keyboard users know where they are. Removing it without a replacement makes the page very hard to use.
  4. 4. When does a button match :focus-visible?

    Browsers match :focus-visible for keyboard focus and for text inputs, but usually not when a button is clicked with the mouse.
  5. 5. Which can an outline NOT do?

    Unlike borders, outlines have no per-side properties; the same outline goes around all four sides.

Summary

  • outline draws a line outside the border with a style, width and color, plus the special auto style.
  • Outlines take up no space, cannot differ per side and follow border-radius in current browsers.
  • outline-offset moves the outline outward (positive) or inward (negative).
  • Focus indicators are outlines; never remove them without a visible replacement.
  • Style focus with :focus-visible, aiming for strong contrast, 2โ€“3px thickness and a small offset.
  • Outlines survive forced colors mode, so use a transparent outline if you style focus with shadows.

Next: Box Shadows, for depth, elevation and glows.