Course contentsShow
← Web Development
Lesson 1056 of 2,98219. Keyboard Navigation and FocusFree lesson

The outline Property

Using CSS outline to create custom focus indicators without affecting layout.

The outline Property

What you'll learn: How to use the CSS outline property to create visible, accessible focus indicators that don't disrupt your page layout.

What Is outline and Why Use It?

When an element receives keyboard focus, browsers typically display a focus ring around it—that's the outline. Unlike border, which affects the element's box model and can push other content around, outline is drawn outside the element without taking up any space. This makes it perfect for creating custom focus indicators that won't cause your layout to shift or jump when elements receive focus.

How outline Works

The outline property is a shorthand that combines three values:

button:focus {
  outline: 2px solid blue;
  /* width | style | color */
}

You can also set these individually:

button:focus {
  outline-width: 3px;
  outline-style: dashed;
  outline-color: orange;
  outline-offset: 2px; /* Adds space between element and outline */
}

Why outline Is Better Than border for Focus

Think of outline like a hologram projected around an element—it's visible but doesn't occupy physical space. A border, on the other hand, is like adding a frame that increases the element's dimensions. When you add a 2px border on focus, your button grows by 4px total (2px on each side), potentially shifting everything around it. With outline, nothing moves.

The Golden Rule

Never use outline: none without providing an alternative focus indicator. Users navigating with keyboards rely on these visual cues to know where they are on the page.

Key Takeaway: The outline property creates focus indicators that enhance accessibility without affecting layout—use it instead of border to avoid unwanted shifting when elements receive focus.