CSS outline Property
The CSS outline property is a shorthand for setting the width, style, and color of an outline drawn around an element.
Unlike borders, outlines do not occupy space in the CSS box model and do not affect an element's dimensions or surrounding layout. They are commonly used to highlight focused controls, links, buttons, and other interface elements.
outline Syntax
The outline shorthand accepts an outline width, style, and color. These components may be written in any order, and omitted components are reset to their initial values.
Basic Syntax
selector {
outline: 2px solid blue;
}
Style Only
.element {
outline: solid;
}
The outline uses the default width and color.
Width and Style
.element {
outline: 3px dashed;
}
Style and Color
.element {
outline: dotted #2563eb;
}
Width, Style, and Color
.element {
outline: 4px double #16a34a;
}
Removing an Outline
.element {
outline: none;
}
Removing a focus outline without providing an accessible replacement can make keyboard navigation difficult.
CSS-Wide Keywords
.element {
outline: initial;
}
.element {
outline: inherit;
}
.element {
outline: unset;
}
.element {
outline: revert;
}
.element {
outline: revert-layer;
}
Because outline is a shorthand, these keywords affect all three associated longhand properties.
Property Values
The outline shorthand combines three longhand properties.
| Property | Description |
|---|---|
outline-width |
Specifies the thickness of the outline. |
outline-style |
Specifies the line style of the outline. |
outline-color |
Specifies the color of the outline. |
Accepted Value Types
| Value | Description |
|---|---|
<line-width> |
A nonnegative length or one of thin, medium, or thick. |
<line-style> |
A line style such as solid, dashed, dotted, or double. |
<color> |
A CSS color, including named colors, hexadecimal values, and color functions. |
auto |
Allows the browser to use a platform-dependent outline style, commonly for focus indicators. |
none |
Prevents an outline from being drawn. |
initial |
Resets all outline longhands to their initial values. |
inherit |
Inherits all outline longhand values from the parent. |
unset |
Resets all outline longhands to their initial values because they are not inherited. |
revert |
Reverts the outline longhands according to cascade origin rules. |
revert-layer |
Reverts the outline longhands according to cascade layer rules. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | See individual longhand properties |
| Inherited | No |
| Applies to | All elements |
| Computed value | See individual longhand properties |
| Animation type | See individual longhand properties |
| Affects layout | No |
| Shorthand for | outline-color, outline-style, and outline-width |
The initial outline style is none, so elements do not normally display an outline unless one is specified or supplied by browser styles.
How It Works
An outline is drawn around the outside of an element's border edge. It provides a visible highlight without adding to the element's box dimensions.
Basic Outline
.box {
padding: 20px;
outline: 3px solid #2563eb;
}
The outline appears around the box without changing its width or height.
Outline with a Border
.box {
border: 2px solid #64748b;
outline: 3px solid #2563eb;
}
The border is part of the box model, while the outline is drawn outside it.
Outlines Do Not Reserve Space
.box {
width: 200px;
padding: 15px;
outline: 5px solid #dc2626;
}
The outline does not increase the box's calculated dimensions. It may visually overlap nearby content when insufficient space is available.
Outlines and Element Shapes
Outlines generally follow the outer shape of an element, but their exact rendering can depend on the browser and the element's geometry. An outline around an inline element that wraps across lines may have a nonrectangular shape.
Important Consideration
Unlike borders, outlines cannot be configured independently on individual sides. The outline shorthand defines one outline around the element.
Outline Style
The outline style determines the appearance of the line. A visible style is normally required for the outline to appear.
| Value | Description |
|---|---|
none |
No outline is drawn. |
hidden |
No outline is drawn. |
dotted |
A series of dots. |
dashed |
A series of short dashes. |
solid |
A continuous line. |
double |
Two parallel lines. |
groove |
A line with a grooved appearance. |
ridge |
A line with a raised appearance. |
inset |
A line with an inset appearance. |
outset |
A line with an outset appearance. |
auto |
A browser-defined outline style. |
Solid Outline
.box {
outline: 3px solid #2563eb;
}
Dashed Outline
.box {
outline: 3px dashed #16a34a;
}
Dotted Outline
.box {
outline: 3px dotted #c2410c;
}
Double Outline
.box {
outline: 5px double #7c3aed;
}
Automatic Outline
button:focus-visible {
outline: auto;
}
The browser chooses the outline's appearance. Its color, width, and shape may vary between browsers and operating systems.
Outline Width
The outline width determines the thickness of the outline.
Keyword Widths
.thin-outline {
outline: thin solid #2563eb;
}
.medium-outline {
outline: medium solid #2563eb;
}
.thick-outline {
outline: thick solid #2563eb;
}
The exact thickness represented by thin, medium, and thick is browser-dependent.
Pixel Width
.box {
outline: 4px solid #2563eb;
}
Relative Width
.box {
outline: 0.2em solid #2563eb;
}
The width scales relative to the element's font size.
Invalid Negative Width
.box {
outline: -2px solid #2563eb;
}
Negative outline widths are invalid.
Important Consideration
The width has no visible effect when the outline style is none or hidden.
Outline Color
The outline color specifies the color used to draw the outline. It accepts standard CSS color values.
Named Color
.box {
outline: 3px solid blue;
}
Hexadecimal Color
.box {
outline: 3px solid #2563eb;
}
RGB Color
.box {
outline: 3px solid rgb(37 99 235);
}
Transparent Color
.box {
outline: 4px solid rgb(37 99 235 / 50%);
}
The outline is partially transparent.
Using currentColor
.box {
color: #2563eb;
outline: 2px solid currentColor;
}
The outline uses the element's current text color.
Important Consideration
The outline must have a visible style and sufficient contrast against surrounding content to be useful as a visual indicator.
Combining Shorthand Values
The outline shorthand allows its width, style, and color components to appear in any order.
Equivalent Declarations
.box {
outline: 3px solid blue;
}
.box {
outline: solid blue 3px;
}
.box {
outline: blue 3px solid;
}
These declarations produce equivalent outlines.
Equivalent Longhand Properties
.box {
outline-width: 3px;
outline-style: solid;
outline-color: blue;
}
The three longhand declarations are equivalent to outline: 3px solid blue.
Omitting the Width
.box {
outline: solid blue;
}
The outline uses the initial medium width.
Omitting the Color
.box {
outline: 3px solid;
}
The outline uses its initial color behavior, which ordinarily follows the element's current color.
Resetting Earlier Values
.box {
outline-color: red;
outline: 2px solid;
}
The shorthand resets the previously specified outline-color because no color is included in the shorthand declaration.
Important Consideration
When combining shorthand and longhand declarations, remember that declaration order and CSS cascade rules determine which values apply.
Outline vs. Border
Outlines and borders can look similar, but they behave differently.
| Feature | outline |
border |
|---|---|---|
| Part of box model dimensions | No | Yes |
| Affects layout size | No | Yes, depending on box sizing |
| Can style individual sides | No | Yes |
| Supports offset spacing | Yes, with outline-offset |
No equivalent border-offset property |
| Common use | Focus and visual highlighting | Element boundaries and decoration |
Border Example
.box {
border: 4px solid #2563eb;
}
The border contributes to the element's box dimensions.
Outline Example
.box {
outline: 4px solid #2563eb;
}
The outline is drawn without changing the element's box dimensions.
Using Both
.box {
border: 2px solid #64748b;
outline: 3px solid #2563eb;
}
The border and outline can appear together.
Important Consideration
Use borders for structural or decorative boundaries and outlines for highlighting elements without changing their layout dimensions.
Using outline-offset
The outline-offset property controls the distance between an outline and the element's border edge.
Positive Offset
.box {
outline: 3px solid #2563eb;
outline-offset: 5px;
}
The outline is drawn farther away from the element.
Negative Offset
.box {
outline: 3px solid #2563eb;
outline-offset: -4px;
}
The outline is shifted inward toward the element.
Focus Outline with Spacing
button:focus-visible {
outline: 3px solid #2563eb;
outline-offset: 3px;
}
The spacing helps separate the focus indicator from the button's border.
Important Consideration
outline-offset is not included in the outline shorthand. It must be declared separately.
Focus Indicators
One of the most important uses of outlines is identifying the element that currently has keyboard focus.
Basic Focus Outline
button:focus {
outline: 3px solid #2563eb;
}
The button receives an outline whenever it has focus.
Keyboard Focus Indicator
button:focus-visible {
outline: 3px solid #2563eb;
outline-offset: 3px;
}
The :focus-visible pseudo-class applies focus styling when the browser determines that a visible focus indicator is appropriate, commonly during keyboard navigation.
Links and Form Controls
a:focus-visible,
button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible {
outline: 3px solid #2563eb;
outline-offset: 2px;
}
Automatic Focus Outline
:focus-visible {
outline: auto;
}
The browser chooses a suitable outline appearance.
Important Consideration
Focus indicators should be clearly visible against the surrounding interface. Avoid using colors or widths that make the outline difficult to distinguish.
Accessibility Considerations
Outlines are important accessibility features because they help keyboard users identify which interactive element currently has focus.
Avoid Removing Focus Outlines
button:focus {
outline: none;
}
This removes the outline without providing another visible focus indicator. Avoid this pattern unless a clear replacement is supplied.
Providing a Replacement
button:focus-visible {
outline: 3px solid #2563eb;
outline-offset: 3px;
}
A visible outline provides an easily recognizable focus indicator.
Focus Indicator Contrast
Choose outline colors that contrast with adjacent backgrounds and element boundaries. A focus indicator that blends into the page may be difficult to see.
High Contrast and Forced Colors
CSS outlines can adapt well to forced-colors environments. System colors may be used to provide more predictable results.
@media (forced-colors: active) {
:focus-visible {
outline: 2px solid Highlight;
outline-offset: 2px;
}
}
Do Not Rely on Color Alone
A visible outline changes an element's boundary as well as its color, making it more distinguishable than a color-only change.
Important Consideration
Test focus indicators using the keyboard, including the Tab and Shift+Tab keys, to confirm that the active element remains clearly identifiable.
outline Example
This example compares four outline styles. Each box uses the same width and color so the difference between the line styles is easy to see.
HTML Example
<div class="outline-demo">
<div class="outline-box outline-solid">Solid</div>
<div class="outline-box outline-dashed">Dashed</div>
<div class="outline-box outline-dotted">Dotted</div>
<div class="outline-box outline-double">Double</div>
</div>
CSS Example
.outline-demo {
display: flex;
flex-wrap: wrap;
gap: 20px;
padding: 20px;
background-color: #f1f5f9;
}
.outline-box {
display: flex;
align-items: center;
justify-content: center;
width: 100px;
height: 70px;
background-color: #fff;
color: #334155;
font-weight: 700;
}
.outline-solid {
outline: 4px solid #2563eb;
}
.outline-dashed {
outline: 4px dashed #2563eb;
}
.outline-dotted {
outline: 4px dotted #2563eb;
}
.outline-double {
outline: 6px double #2563eb;
}
Each box has an outline drawn outside its border edge. The outlines do not change the boxes' layout dimensions.
Interactive Focus Example
This example demonstrates an outline that appears when a button receives visible keyboard focus.
<button class="focus-button" type="button">Focus Me</button>
.focus-button {
padding: 12px 20px;
border: 2px solid #2563eb;
background-color: #fff;
color: #1e40af;
cursor: pointer;
}
.focus-button:focus-visible {
outline: 3px solid #f97316;
outline-offset: 4px;
}
Use the Tab key to focus the button. An orange outline appears outside the blue border, separated by a four-pixel offset.
Common Mistakes
Forgetting the Outline Style
.box {
outline: 3px blue;
}
The declaration is valid, but it resets the outline style to none, so no outline is drawn.
Using Negative Widths
.box {
outline: -3px solid blue;
}
Negative outline widths are invalid.
Expecting an Outline to Affect Layout
Outlines do not reserve space or increase an element's box dimensions.
Trying to Style Individual Outline Sides
CSS does not provide separate outline-top, outline-right, outline-bottom, or outline-left properties.
Including outline-offset in the Shorthand
.box {
outline: 3px solid blue 5px;
}
This declaration is invalid. Set outline-offset separately.
Removing Focus Indicators
Avoid removing browser focus outlines without providing an accessible alternative.
Using Low-Contrast Outline Colors
An outline that closely matches the background may not provide a useful visual indicator.
Confusing Outline and Border Behavior
Borders affect the box model, while outlines are drawn without affecting layout dimensions.
Forgetting Shorthand Resets
The shorthand resets omitted outline longhand values. A previously declared outline color or width may be replaced by its initial value.
Important Considerations
Keep the following points in mind when using outline:
- The property is a shorthand for
outline-width,outline-style, andoutline-color. - Its components can appear in any order.
- Omitted components are reset to their initial values.
- The property is not inherited.
- It applies to all elements.
- Outlines do not contribute to box model dimensions.
- Outlines do not change an element's layout position.
- Outlines may visually overlap nearby content.
- The initial outline style is
none. - A visible outline normally requires a style other than
noneorhidden. - Outline widths must not be negative.
- Outline colors accept standard CSS color values.
- The
currentColorkeyword can match the element's text color. - Outlines cannot be styled independently on individual sides.
outline-offsetcontrols the spacing between the outline and border edge.outline-offsetis not included in the shorthand.- Outlines are commonly used for keyboard focus indicators.
:focus-visibleis useful for focus styling.- Removing focus outlines can create accessibility problems.
- Focus outlines should contrast clearly with surrounding content.
- Forced-colors environments may affect outline rendering.
- Browser-defined
autooutlines can vary in appearance. - Use borders when individual side styling or layout dimensions matter.
Browser Support
The CSS outline property is Baseline - Widely available and has been supported in major browsers for many years.
Modern versions of Chrome, Edge, Firefox, Safari, and other major browsers support the outline shorthand and its standard width, style, and color components.
Some rendering details, particularly outline: auto, may vary between browsers and operating systems.
For current compatibility information, visit Can I Use? and search for outline.
Specifications
The outline shorthand is defined in CSS Basic User Interface Module Level 4.
CSS Basic User Interface Module Level 4: Outline Properties
The specification describes outline styling, widths, colors, and related behavior.
For additional documentation and examples, consult MDN Web Docs.
