CSS Outline Shorthand
The CSS outline shorthand property lets you define several outline settings in a single CSS declaration. It combines outline width, outline style, and outline color.
Outlines are commonly used to highlight interactive elements such as buttons, links, form fields, and other controls. They are especially important for keyboard accessibility, because browsers often use outlines to show which element currently has focus.
A basic example looks like this:
button {
outline: 2px solid blue;
}
Here:
2pxis the outline width.solidis the outline style.blueis the outline color.
Instead of writing three separate properties, the shorthand keeps the CSS shorter and easier to maintain.
What Is the CSS outline Property?
The outline property draws a line around an element.
Unlike a border, an outline is drawn outside the element’s border edge and normally does not take up space in the CSS box model. Therefore, adding an outline usually does not change the size or position of surrounding elements.
For example:
.box {
outline: 3px solid red;
}
This creates a solid red outline around the element.
The shorthand is equivalent to:
.box {
outline-width: 3px;
outline-style: solid;
outline-color: red;
}
The outline shorthand therefore controls three longhand properties:
| Shorthand Component | CSS Property | Purpose |
|---|---|---|
| Width | outline-width | Controls outline thickness |
| Style | outline-style | Controls the appearance of the line |
| Color | outline-color | Controls the outline color |
CSS Outline Shorthand Syntax
The general syntax is:
selector {
outline: width style color;
}
For example:
div {
outline: 4px dashed green;
}
The declaration creates a:
4pxwide outlinedashedoutline style- green outline color
The formal idea can be represented as:
outline: <outline-width> || <outline-style> || <outline-color>;
The || notation means these components can generally appear in any order, with each component appearing at most once.
For example, these declarations represent the same basic settings:
outline: 2px solid red;
outline: solid 2px red;
outline: red solid 2px;
For readability, however, this order is often easiest to understand:
outline: width style color;
Components of the outline Shorthand
The shorthand combines the following properties:
outline-width
outline-style
outline-color
Consider:
.element {
outline: 5px dotted purple;
}
This is equivalent to:
.element {
outline-width: 5px;
outline-style: dotted;
outline-color: purple;
}
Using the shorthand is particularly useful when all three characteristics need to be defined together.
1. Outline Width
The first common component controls the thickness of the outline.
Example:
.box {
outline: 3px solid black;
}
Here, 3px specifies the width.
You can use length values such as:
outline: 1px solid black;
outline: 0.2em solid black;
outline: 0.1rem solid black;
CSS also provides keyword values for outline-width:
thin
medium
thick
Examples:
.box1 {
outline: thin solid red;
}
.box2 {
outline: medium solid blue;
}
.box3 {
outline: thick solid green;
}
The exact thickness represented by these keywords is determined by the browser.
For more predictable visual results, explicit lengths such as 2px are often preferred.
2. Outline Style
The outline style determines how the outline line is drawn.
For example:
input {
outline: 2px dashed blue;
}
The available styles include:
| Value | Description |
|---|---|
none | No outline is drawn |
hidden | Generally produces no visible outline |
dotted | Series of dots |
dashed | Series of short dashes |
solid | Single solid line |
double | Two solid lines |
groove | Carved or grooved appearance |
ridge | Raised ridge appearance |
inset | Makes the outline appear embedded |
outset | Makes the outline appear raised |
auto | Allows the browser/platform to choose an appropriate outline style |
The exact appearance of some styles can vary between browsers and platforms.
Solid Outline
.box {
outline: 3px solid red;
}
Dashed Outline
.box {
outline: 3px dashed blue;
}
Dotted Outline
.box {
outline: 3px dotted green;
}
Double Outline
.box {
outline: 5px double purple;
}
Automatic Outline
button:focus {
outline: auto;
}
auto is particularly relevant to focus indicators because it allows the browser to use its native focus presentation.
3. Outline Color
The color component controls the color of the outline.
Example:
.box {
outline: 2px solid orange;
}
CSS supports the usual color formats.
Named Color
outline: 2px solid red;
Hexadecimal
outline: 2px solid #0066ff;
RGB
outline: 2px solid rgb(0 102 255);
RGB with Transparency
outline: 3px solid rgb(0 102 255 / 50%);
HSL
outline: 2px solid hsl(220 100% 50%);
Current Text Color
outline: 2px solid currentColor;
currentColor is especially useful when you want an outline to automatically match the element’s color property.
For example:
button {
color: darkblue;
outline: 2px solid currentColor;
}
The outline will use the same computed color as the button text.
Using Only Some Outline Values
You do not have to explicitly provide all three components.
For example:
.box {
outline: solid;
}
The browser supplies initial values for the omitted outline components.
Another example:
.box {
outline: 3px solid;
}
Here, the width and style are explicitly provided while the color is omitted.
You can also write:
.box {
outline: solid red;
}
However, specifying all relevant components is often easier for developers reading the stylesheet:
.box {
outline: 2px solid red;
}
Why outline-style Matters
An important detail is that an outline requires a drawable style to become visible.
For example:
.box {
outline: 5px red;
}
This does not provide an outline style. Because the initial value of outline-style is none, you should not expect a normal visible outline.
Use:
.box {
outline: 5px solid red;
}
Therefore, when creating a visible custom outline, including the style is usually essential.
Complete CSS Outline Example
HTML:
<div class="example">
CSS Outline Example
</div>
CSS:
.example {
width: 250px;
padding: 20px;
border: 1px solid #333;
outline: 4px dashed #0066cc;
}
The element has both a border and an outline.
The border participates in the box model, while the outline is drawn around the outside without normally affecting layout.
Outline vs Border
Beginners often confuse outline with border. They look similar, but they serve different purposes.
| Feature | Outline | Border |
|---|---|---|
| Property | outline | border |
| Part of box model | No | Yes |
| Normally affects layout | No | Yes |
| Can be offset | Yes | Not with border itself |
| Individual sides | No | Yes |
| Common accessibility use | Focus indication | General visual styling |
| Follows border edge | Usually | Defines border edge |
Example border:
.box {
border: 3px solid blue;
}
Example outline:
.box {
outline: 3px solid blue;
}
A border is normally used as part of an element’s visual box.
An outline is especially useful as a highlight or focus indicator.
Using Border and Outline Together
An element can have both.
.card {
border: 2px solid black;
outline: 3px solid orange;
}
The outline is drawn outside the border.
You can make the separation more obvious with outline-offset:
.card {
border: 2px solid black;
outline: 3px solid orange;
outline-offset: 5px;
}
This produces space between the border edge and the outline.
Is outline-offset Part of the outline Shorthand?
No.
This is an important distinction.
The outline shorthand includes:
outline-width
outline-style
outline-color
It does not include:
outline-offset
Therefore, write outline-offset separately:
button {
outline: 3px solid blue;
outline-offset: 4px;
}
You cannot rely on something such as this to define an offset:
outline: 3px solid blue 4px;
Instead, use two declarations.
Positive Outline Offset
A positive value moves the outline farther away from the element.
.box {
outline: 2px solid blue;
outline-offset: 6px;
}
This can create a visually separated focus ring.
Negative Outline Offset
outline-offset can also accept negative lengths.
For example:
.box {
outline: 2px solid red;
outline-offset: -4px;
}
This moves the outline inward relative to its normal position.
Use negative offsets carefully because the outline can overlap the element’s content or other visual features.
CSS Outline for Focus States
One of the most important uses of outlines is showing keyboard focus.
For example:
button:focus {
outline: 3px solid #005fcc;
}
When the button receives focus, the outline makes the focused element easier to identify.
A more refined approach is often:
button:focus-visible {
outline: 3px solid #005fcc;
outline-offset: 3px;
}
:focus-visible allows browsers to display the custom focus treatment when a visible focus indicator is appropriate, commonly during keyboard navigation.
:focus vs :focus-visible
Both selectors relate to focus, but they are not identical.
:focus
Matches whenever the element has focus.
input:focus {
outline: 2px solid blue;
}
:focus-visible
Matches focused elements when the browser determines that a visible focus indicator should be presented.
button:focus-visible {
outline: 3px solid blue;
}
For modern interfaces, :focus-visible can provide a cleaner experience while maintaining keyboard accessibility.
Never Remove Focus Outlines Without a Replacement
You may encounter CSS such as:
button:focus {
outline: none;
}
This removes the focus outline.
Doing this without providing another clear focus indicator can make a website difficult to navigate for keyboard users.
If you remove the browser’s default focus outline, provide a strong alternative:
button:focus-visible {
outline: 3px solid #005fcc;
outline-offset: 3px;
}
The replacement should be clearly visible against surrounding colors.
Outline Shorthand with Links
Outlines are also useful for links.
a:focus-visible {
outline: 2px solid currentColor;
outline-offset: 3px;
}
Using currentColor allows the focus outline to follow the link’s current text color.
Outline Shorthand with Form Fields
Form elements benefit from clear focus indicators.
input:focus-visible,
textarea:focus-visible,
select:focus-visible {
outline: 3px solid #0066cc;
outline-offset: 2px;
}
This helps users identify which field is active.
For example:
<label for="email">Email</label>
<input id="email" type="email">
input {
padding: 10px;
border: 1px solid #777;
}
input:focus-visible {
outline: 3px solid #0066cc;
outline-offset: 2px;
}
Outline Shorthand with Buttons
A practical button style might look like:
.button {
padding: 10px 18px;
border: none;
border-radius: 6px;
background: #1d4ed8;
color: white;
}
.button:focus-visible {
outline: 3px solid #111;
outline-offset: 3px;
}
The outline does not require additional layout space, making it useful for focus rings.
Outline and Rounded Corners
Modern browsers generally render outlines in a way that follows rounded element shapes reasonably well.
For example:
.button {
border-radius: 12px;
}
.button:focus-visible {
outline: 3px solid blue;
outline-offset: 3px;
}
However, exact outline rendering can still vary depending on the browser, element shape, and platform.
If highly customized ring geometry is required, designers sometimes use other CSS techniques such as box-shadow, but accessibility should remain the priority.
Outline vs Box Shadow
A box-shadow can also create a ring-like effect.
For example:
button:focus-visible {
box-shadow: 0 0 0 4px lightblue;
}
Compare it with:
button:focus-visible {
outline: 3px solid blue;
outline-offset: 2px;
}
The two approaches are not identical.
An outline is semantically and traditionally associated with focus indication, while box-shadow is a general visual effect.
For robust focus styling, an outline is often a strong foundation.
You can even combine them:
button:focus-visible {
outline: 2px solid #000;
outline-offset: 2px;
box-shadow: 0 0 0 5px #fff;
}
This can improve visibility across complicated backgrounds when designed carefully.
Using CSS Variables with Outline Shorthand
Custom properties make outline styles easier to reuse.
:root {
--focus-color: #005fcc;
--focus-width: 3px;
}
button:focus-visible {
outline: var(--focus-width) solid var(--focus-color);
outline-offset: 3px;
}
You can reuse the same values across multiple components:
a:focus-visible,
button:focus-visible,
input:focus-visible {
outline: var(--focus-width) solid var(--focus-color);
outline-offset: 3px;
}
This makes maintaining a consistent design system easier.
Using currentColor
currentColor is particularly convenient with outlines.
.button {
color: purple;
}
.button:focus-visible {
outline: 2px solid currentColor;
}
Because currentColor represents the computed value of the color property, changing the text color also changes the outline.
For example:
.danger {
color: red;
}
.success {
color: green;
}
.danger:focus-visible,
.success:focus-visible {
outline: 2px solid currentColor;
}
Each element automatically gets a focus outline matching its own text color.
Transparent Outline
CSS also permits transparent outline colors.
.box {
outline: 3px solid transparent;
}
The outline exists conceptually but is not visibly colored.
Transparent outlines can be useful in certain component designs, but they should not be used as the only focus indicator when users need a visible indication of focus.
Outline with CSS Transitions
Some outline-related visual changes can be combined with transitions.
For example:
button {
outline: 2px solid transparent;
outline-offset: 2px;
transition: outline-color 0.2s ease;
}
button:focus-visible {
outline-color: blue;
}
Here, the outline structure is already defined, while its color changes when focus becomes visibly indicated.
Keep accessibility in mind when adding animation. A focus indicator should become apparent quickly rather than being delayed by a long transition.
Global Outline Styling
A site-wide keyboard focus rule can be created using:
:focus-visible {
outline: 3px solid #005fcc;
outline-offset: 3px;
}
This provides a consistent baseline focus style across many interactive elements.
Individual components can then override it when necessary.
Outline Shorthand and the CSS Box Model
The standard CSS box model consists of:
Content
Padding
Border
Margin
An outline is not another box-model layer that contributes to an element’s calculated width or height.
For example:
.box {
width: 200px;
padding: 20px;
border: 5px solid black;
outline: 10px solid red;
}
The 10px outline does not add another 20 pixels to the element’s calculated box width in the way a border could.
This is one reason outlines are convenient for highlighting elements without causing layout shifts.
Does an Outline Affect Element Size?
Normally, no.
Consider:
.card:hover {
outline: 4px solid blue;
}
Adding the outline does not normally cause nearby elements to move.
By comparison, changing a border from zero to 4px can affect dimensions depending on the box-sizing and sizing rules.
This makes outlines useful for temporary visual highlighting.
Outlines Can Overlap Other Content
Because an outline does not reserve layout space, a large outline may overlap nearby content.
For example:
.box {
outline: 20px solid rgb(255 0 0 / 40%);
}
If another element is very close, the visual outline may extend into its area.
Therefore, very thick outlines should be used carefully.
Global CSS Values
Like many CSS properties, outline can use global values.
Examples include:
outline: inherit;
outline: initial;
outline: revert;
outline: revert-layer;
outline: unset;
initial
.element {
outline: initial;
}
Resets the shorthand’s longhand properties to their initial values.
inherit
.element {
outline: inherit;
}
Uses the corresponding computed outline values from the parent.
unset
.element {
outline: unset;
}
Applies the standard unset behavior to the shorthand’s longhands.
revert
.element {
outline: revert;
}
Reverts the property according to the CSS cascade’s earlier origin behavior.
revert-layer
.element {
outline: revert-layer;
}
Reverts according to cascade-layer rules.
These values are particularly useful in resets, component systems, and advanced cascade management.
Common CSS Outline Shorthand Examples
Thin Solid Outline
.box {
outline: thin solid black;
}
Medium Dashed Outline
.box {
outline: medium dashed blue;
}
Thick Dotted Outline
.box {
outline: thick dotted red;
}
Pixel-Based Outline
.box {
outline: 3px solid green;
}
Hex Color Outline
.box {
outline: 2px solid #ff6600;
}
RGB Outline
.box {
outline: 2px solid rgb(30 100 200);
}
HSL Outline
.box {
outline: 3px dashed hsl(280 70% 45%);
}
Current Color Outline
.box {
outline: 2px solid currentColor;
}
Practical Hover Example
An outline can highlight a card without changing its dimensions:
.card {
padding: 20px;
border: 1px solid #ddd;
}
.card:hover {
outline: 2px solid #2563eb;
}
Since the outline does not normally participate in layout, the card’s neighbors do not need to move when the hover effect appears.
For clickable cards, however, make sure hover is not the only interaction state. Keyboard focus should also be visible.
.card:hover,
.card:focus-visible {
outline: 2px solid #2563eb;
outline-offset: 2px;
}
Practical Form Example
<form>
<label for="name">Name</label>
<input id="name" type="text">
<button type="submit">Submit</button>
</form>
input,
button {
padding: 10px;
}
input:focus-visible,
button:focus-visible {
outline: 3px solid #0066cc;
outline-offset: 2px;
}
The outline clearly indicates the active control during keyboard navigation.
Common Mistakes
Mistake 1: Removing Every Outline
* {
outline: none;
}
This is generally a poor accessibility practice because it can remove important browser-provided focus indicators.
A better approach is to create a clear custom focus style:
:focus-visible {
outline: 3px solid #005fcc;
outline-offset: 2px;
}
Mistake 2: Forgetting the Style
.box {
outline: 3px red;
}
Without a drawable style, the outline may not appear as intended.
Use:
.box {
outline: 3px solid red;
}
Mistake 3: Putting Offset Inside the Shorthand
Incorrect:
.box {
outline: 2px solid blue 5px;
}
Correct:
.box {
outline: 2px solid blue;
outline-offset: 5px;
}
Mistake 4: Using Very Low Contrast
button:focus-visible {
outline: 1px solid #eeeeee;
}
On a white or similarly light background, this may be difficult to see.
Choose an outline that is clearly distinguishable from adjacent colors.
Mistake 5: Assuming Outline Works Exactly Like Border
An outline cannot be controlled per side using properties such as:
outline-top
outline-right
outline-bottom
outline-left
Those are not standard outline properties.
If individual side control is needed, use borders or another appropriate styling technique.
Accessibility Best Practices
Outlines play an important role in accessible interfaces.
A useful focus style might be:
:focus-visible {
outline: 3px solid #005fcc;
outline-offset: 3px;
}
When designing focus indicators:
- Keep them clearly visible.
- Use sufficient contrast against surrounding colors.
- Do not remove browser focus styling without providing an equivalent or better replacement.
- Test navigation with the
Tabkey. - Check focus states on both light and dark backgrounds.
- Ensure the focus indicator is not hidden behind other elements.
- Avoid making the outline so subtle that users cannot identify the focused control.
Shorthand vs Longhand Properties
The shorthand version:
.element {
outline: 3px dashed blue;
}
The longhand version:
.element {
outline-width: 3px;
outline-style: dashed;
outline-color: blue;
}
Both can produce the same visual result.
When to Use Shorthand
Use shorthand when the outline settings belong together:
outline: 3px solid blue;
It is concise and readable.
When to Use Longhand
Longhand properties can be useful when changing only one component:
.element {
outline: 2px solid transparent;
}
.element:focus-visible {
outline-color: blue;
}
Here, only the color changes when the element receives visible focus.
Important Shorthand Reset Behavior
CSS shorthand properties do more than save typing. They also set the longhand properties they represent.
For example:
.box {
outline-color: red;
outline-width: 10px;
outline: solid;
}
The final outline declaration does not simply change outline-style. It sets the complete shorthand and causes omitted components to use their appropriate initial values.
If you only want to change the style, use:
.box {
outline-style: solid;
}
This distinction is important when maintaining larger stylesheets.
Browser Support
The core CSS outline shorthand has been supported by modern browsers for many years.
It works across major desktop and mobile browsers.
Basic syntax such as:
outline: 2px solid blue;
is therefore suitable for normal web development.
Exact visual rendering can vary slightly across browsers and operating systems, especially for styles such as auto, groove, ridge, inset, and outset.
Quick Reference Table
| Feature | Details |
|---|---|
| Property | outline |
| Type | Shorthand property |
| Controls | Width, style and color |
| Includes offset? | No |
| Common syntax | outline: 2px solid blue; |
| Affects normal layout? | No |
| Part of box model? | No |
| Individual sides supported? | No |
| Common use | Focus indicators and highlighting |
| Important related property | outline-offset |
Outline Shorthand Examples Table
| Purpose | CSS |
|---|---|
| Basic outline | outline: 2px solid black; |
| Red outline | outline: 2px solid red; |
| Thick outline | outline: 5px solid blue; |
| Dashed outline | outline: 3px dashed green; |
| Dotted outline | outline: 3px dotted purple; |
| Double outline | outline: 5px double orange; |
| Current text color | outline: 2px solid currentColor; |
| Browser-selected style | outline: auto; |
| Remove outline | outline: none; |
Best Practices for CSS Outline Shorthand
For clean and maintainable CSS:
- Use the conventional
width style colororder for readability.
outline: 2px solid blue;
- Use
outline-offsetseparately.
outline: 2px solid blue;
outline-offset: 3px;
- Preserve visible keyboard focus.
:focus-visible {
outline: 3px solid #005fcc;
outline-offset: 3px;
}
- Prefer clear, high-contrast focus indicators.
- Use
currentColorwhen the outline should follow an element’s text color.
outline: 2px solid currentColor;
- Remember that an outline does not reserve layout space and can therefore overlap nearby content.
- Remember that the shorthand resets omitted outline components instead of simply leaving all previous longhand values untouched.
Frequently Asked Questions
What is CSS outline shorthand?
The CSS outline shorthand lets you set outline-width, outline-style, and outline-color in one declaration.
outline: 2px solid blue;
What is the correct syntax?
A common and readable syntax is:
outline: width style color;
Example:
outline: 3px dashed red;
Does CSS outline affect element size?
No. An outline normally does not participate in the CSS box model and therefore does not change the element’s calculated dimensions.
Is outline the same as border?
No. Borders are part of the box model, while outlines are drawn around an element without normally taking up layout space.
Is outline-offset included in the shorthand?
No. It must be declared separately:
outline: 2px solid blue;
outline-offset: 4px;
Can an outline have different styles on different sides?
No. CSS outlines do not provide separate top, right, bottom, and left outline properties like borders do.
Can I remove the outline?
Yes:
outline: none;
However, removing focus outlines without providing another clearly visible focus indicator can create serious accessibility problems.
What does outline: auto mean?
It lets the browser or platform choose an appropriate outline appearance. It is commonly associated with native focus indication.
Can I use currentColor with an outline?
Yes:
outline: 2px solid currentColor;
The outline then uses the element’s computed text color.
Which is better for focus, outline or border?
An outline is usually more convenient because it does not normally alter the element’s dimensions or cause layout changes.
Conclusion
The CSS outline shorthand property is a simple but important tool for styling elements and creating accessible focus indicators. It combines outline-width, outline-style, and outline-color into one declaration.
A typical rule is:
outline: 3px solid blue;
For focus styling, it is often paired with outline-offset:
button:focus-visible {
outline: 3px solid #005fcc;
outline-offset: 3px;
}
The key point is that outline is different from border. It sits outside the normal box model, does not normally affect layout, cannot be independently styled on each side, and is particularly valuable for showing keyboard focus.
Used carefully, the outline shorthand keeps CSS concise while improving both visual clarity and accessibility.