CSS Outline

Learn how to use CSS outline properties for focus indicators and decorative effects

What is CSS Outline?

CSS outline is a line that is drawn around elements, outside the borders, to make the element stand out. Unlike borders, outlines do not take up space and do not affect the layout of the page.

Outline Properties:

  • outline - Shorthand for all outline properties
  • outline-width - Sets the thickness of the outline
  • outline-style - Sets the style of the outline
  • outline-color - Sets the color of the outline
  • outline-offset - Sets the space between the outline and the element
Margin
Border
Padding
Content
Outline

Outline in Action

Example Code

/* Outline Properties */
.element {
  /* Shorthand outline property */
  outline: 3px solid red;
  
  /* Individual outline properties */
  outline-width: 3px;
  outline-style: solid;
  outline-color: red;
  
  /* Outline offset */
  outline-offset: 5px;
  
  /* Removing outline */
  outline: none;
}

/* Outline styles */
.styles {
  outline-style: solid;     /* Solid line */
  outline-style: dashed;    /* Dashed line */
  outline-style: dotted;    /* Dotted line */
  outline-style: double;    /* Double line */
  outline-style: groove;    /* 3D grooved */
  outline-style: ridge;     /* 3D ridged */
  outline-style: inset;     /* 3D inset */
  outline-style: outset;    /* 3D outset */
}

/* Focus styles for accessibility */
button:focus {
  outline: 2px solid blue;
  outline-offset: 2px;
}

/* Removing default focus outline (use with caution) */
button:focus {
  outline: none;
  /* Always provide alternative focus indicator */
  box-shadow: 0 0 0 3px rgba(0, 120, 255, 0.5);
}

Outline vs Border: Key Differences

Borders

  • Part of the box model
  • Affects layout and element dimensions
  • Can be applied to individual sides
  • Can have rounded corners with border-radius
  • Participates in box-sizing calculation
  • Renders inside the element
This is a border

Outlines

  • Not part of the box model
  • Does not affect layout or dimensions
  • Always applies to all sides
  • Cannot have rounded corners
  • Not included in box-sizing
  • Renders outside the element
This has an outline

When to use each: Use borders for structural styling and outlines for focus indicators or temporary highlighting without affecting layout.

Outline Properties in Detail

Outline Width

The outline-width property sets the thickness of the outline. It accepts length values or keywords.

outline-width: 1px
outline-width: 2px
outline-width: 4px
outline-width: 8px
/* Keyword values */
outline-width: thin;
outline-width: medium;
outline-width: thick;

Outline Style

The outline-style property sets the style of the outline. It accepts the same values as border-style.

solid
dashed
dotted
double

Outline Color

The outline-color property sets the color of the outline. It accepts all CSS color values.

outline-color: red
outline-color: blue
outline-color: green
outline-color: purple

Outline Shorthand

The outline shorthand property combines width, style, and color in one declaration.

/* outline: width style color */
outline: 2px solid #000;
outline: medium dashed red;
outline: thick double rgba(0, 0, 0, 0.5);
outline: 2px dashed teal;

Outline Offset

The outline-offset property sets the amount of space between an outline and the edge or border of an element. This property accepts positive and negative values.

Positive Values

Positive values push the outline outward, creating space between the element and its outline.

outline-offset: 1px
outline-offset: 2px
outline-offset: 4px
outline-offset: 8px

Negative Values

Negative values pull the outline inward, causing it to overlap the element.

outline-offset: -1px
outline-offset: -2px
outline-offset: -4px
outline-offset: -8px

Accessibility and Focus Indicators

Outlines are crucial for accessibility as they provide visual indicators for focused elements, helping keyboard users navigate through interactive elements on a page.

Default Focus Styles

Browsers provide default focus outlines for interactive elements. These are essential for accessibility.

/* Browser default */
button:focus {
outline: 2px solid Highlight;
outline: 2px solid -webkit-focus-ring-color;
}

Custom Focus Indicators

If you remove the default outline, always provide an alternative focus indicator for accessibility.

button:focus {
outline: none;
box-shadow: 0 0 0 3px rgba(0, 120, 255, 0.5);
}

Important: Never remove focus indicators without providing an alternative visible focus style. This is essential for accessibility and required by WCAG guidelines.

Practical Outline Examples

Interactive Elements

button, a:focus {
outline: 2px solid #4CAF50;
outline-offset: 2px;
}
Link with outline

Form Validation

input:invalid {
outline: 2px solid #F44336;
}

input:valid {
outline: 2px solid #4CAF50;
}
Try entering an invalid email address to see the outline change.

Highlighting Elements

.highlight {
outline: 3px dashed #FF9800;
outline-offset: 5px;
}
This element is highlighted with a dashed orange outline.
< Previous (Borders)Next (Display Property) >