Skip to main content

CSS Box Model

CSS Outline

Written by Updated CSS Outline exercises

Draw non-layout outlines for focus and emphasis, control their offset, and preserve accessible keyboard focus indicators.

An outline is a line drawn just outside an element's border. Unlike a border, it takes no space, so adding one never shifts the layout. That makes it the standard way to show keyboard focus.

Syntax

css

selector {
    outline: width style color;
}

Example

css

p {
    outline: 2px solid red;
}

That draws a 2px red line around each paragraph, outside any border it already has.

Properties

PropertyWhat it setsExample
outlineWidth, style and color at onceoutline: 2px solid black;
outline-widthThicknessoutline-width: 3px;
outline-styleLine type, such as solid or dashedoutline-style: dashed;
outline-colorColoroutline-color: red;
outline-offsetThe gap between the border and the outlineoutline-offset: 5px;

Outline or border?

  • An outline does not take up space; a border does.
  • An outline is the same on all four sides; a border can differ per side.
  • Modern browsers follow border-radius with the outline, so rounded buttons get rounded focus rings.

Compare with CSS Border.

Example

Complete CSS Outline Example

html

<!DOCTYPE html>
<html>
<head>
    <title>CSS Outline Example</title>
    <style>
        div {
            border: 2px solid blue;
            outline: 3px dashed red;
            outline-offset: 5px;
            padding: 10px;
        }

        p {
            outline: 2px solid green;
        }
    </style>
</head>
<body>

    <div>This div has both border and outline.</div>

    <p>This paragraph has an outline.</p>

</body>
</html>

The div has a blue border and a red dashed outline 5px outside it. The paragraph has a green outline. Notice that neither outline moves anything on the page.

Browser Support

Feature
Chrome
Edge
Firefox
Safari
outline-style11211.2

Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8.

Do not remove the focus outline

outline: none on buttons and links is one of the most harmful lines in CSS. People who use a keyboard rely on that ring to see where they are. If the default ring does not match your design, replace it instead of removing it:

A clear focus ring for keyboard users

css

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

:focus-visible shows the ring for keyboard users but not after a mouse click, which is usually what designers wanted when they removed it. More on this in Accessible CSS.

CSS Outline exercises

An outline

Give the button an outline of 2px solid blue.

The HTML your code works on
<button class="btn">Save</button>
<a class="link" href="#">Help</a>

Result

Check your answer to see it here.