Skip to content

CSS Outline

Understand how to use CSS outlines to highlight elements without affecting layout.

An outline is a line drawn outside an element’s border. It’s often used to make an element “stand out”, typically for accessibility reasons like indicating focus.

Key difference from borders: Outlines do not take up space. Adding an outline won’t affect the element’s dimensions or cause other elements to reflow. They are drawn over the surrounding content.

The CSS outline properties let you control the style, color, width, and offset of this line.

You can set the outline-style, outline-color, and outline-width individually, or use the shorthand outline property.

/* Individual properties */
.element-with-outline {
outline-style: dashed;
outline-color: blue;
outline-width: 2px;
}
/* Shorthand: width style color */
.another-element {
outline: 3px solid red;
}

The outline-offset property adds space between the border edge and the outline. It can accept positive or negative values.

.element-with-offset-outline {
border: 1px solid black;
outline: 2px dotted green;
outline-offset: 5px; /* Outline is 5px away from the border */
}

Consider two identical divs. One has a border, the other an outline. Notice how the border pushes content away, while the outline does not affect the layout.

.box {
width: 100px;
height: 50px;
margin: 10px;
background-color: lightgray;
display: inline-block; /* For side-by-side comparison */
}
.has-border {
border: 5px solid black; /* Adds 10px to width and height */
}
.has-outline {
outline: 5px solid black; /* Does NOT add to width/height */
}

Outlines are crucial for showing keyboard users which element currently has focus. Browsers provide default focus outlines, but you can customize them for better visibility, ensuring they contrast well with the background.

a:focus {
/* Using outline for focus - doesn't disrupt layout */
outline: 3px solid dodgerblue;
outline-offset: 2px;
/* Optional: Remove default browser outline if customizing */
/* outline: none; /* Only do this if you provide a replacement! */ */
}
/* Modern alternative for focus styling */
a:focus-visible {
outline: 3px solid dodgerblue;
outline-offset: 2px;
}

Avoid removing outlines (outline: none;) without providing a clear alternative focus indicator, as this harms accessibility.

PropertyDescriptionExample Values
outlineShorthand for setting outline-width, outline-style, and outline-color.e.g., 2px dotted red
outline-colorSets the color of the outline.e.g., blue, #00FF00, rgb(255, 0, 0), currentColor
outline-styleSets the style of the outline.auto (browser default), none, dotted, dashed, solid, double, groove, ridge, inset, outset
outline-widthSets the thickness of the outline.e.g., thin, medium, thick, 2px, 0.1em
outline-offsetSets the space between the outline and the border edge.e.g., 5px, -2px, 0.5em
For more details, refer to the MDN Web Docs on CSS Outline: [https://developer.mozilla.org/en-US/docs/Web/CSS/outline](https://developer.mozilla.org/en-US/docs/Web/CSS/outline)