CSS outline-offset Property
The CSS outline-offset property specifies the distance between an element's outline and its border edge.
Positive values move the outline outward, while negative values move it inward. This property is commonly used with focus indicators to create visible spacing around buttons, links, and form controls without affecting their layout dimensions.
outline-offset Syntax
The outline-offset property accepts a CSS length value. Positive, negative, and zero values are allowed.
Basic Syntax
selector {
outline-offset: 5px;
}
Positive Offset
.box {
outline: 3px solid blue;
outline-offset: 5px;
}
Negative Offset
.box {
outline: 3px solid blue;
outline-offset: -5px;
}
Zero Offset
.box {
outline: 3px solid blue;
outline-offset: 0;
}
Relative Length
.box {
outline: 3px solid blue;
outline-offset: 0.25em;
}
CSS-Wide Keywords
.box {
outline-offset: initial;
}
.box {
outline-offset: inherit;
}
.box {
outline-offset: unset;
}
.box {
outline-offset: revert;
}
.box {
outline-offset: revert-layer;
}
The initial value is 0, meaning the outline is not intentionally offset from its normal position.
Property Values
| Value | Description |
|---|---|
<length> |
Specifies the distance between the outline and the element's border edge. Positive and negative values are permitted. |
0 |
Uses no additional outline offset. |
initial |
Resets the property to 0. |
inherit |
Uses the computed offset value of the parent element. |
unset |
Resets the property to 0 because it is not inherited. |
revert |
Reverts the property according to cascade origin rules. |
revert-layer |
Reverts the property according to cascade layer rules. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | 0 |
| Inherited | No |
| Applies to | All elements |
| Computed value | Absolute length |
| Animation type | By computed value type |
| Affects layout | No |
| Accepts negative values | Yes |
| Related properties | outline, outline-width, outline-style, outline-color |
Valid Values
.box {
outline-offset: 0;
}
.box {
outline-offset: 4px;
}
.box {
outline-offset: -3px;
}
.box {
outline-offset: 0.25em;
}
.box {
outline-offset: 1rem;
}
Invalid Values
.box {
outline-offset: 10%;
}
.box {
outline-offset: auto;
}
.box {
outline-offset: solid;
}
Percentages, auto, and outline style keywords are not valid values for outline-offset.
How It Works
The outline-offset property moves the outline relative to the element's border edge without moving or resizing the element itself.
Outline Without an Offset
.box {
border: 2px solid #64748b;
outline: 3px solid #2563eb;
outline-offset: 0;
}
The outline is drawn at its normal position outside the border.
Outline with an Offset
.box {
border: 2px solid #64748b;
outline: 3px solid #2563eb;
outline-offset: 6px;
}
The outline is moved six pixels outward from its normal position, creating visible space between the border and outline.
Offset Does Not Affect Layout
.box {
width: 200px;
padding: 20px;
outline: 4px solid #2563eb;
outline-offset: 10px;
}
The element's calculated box dimensions remain unchanged. The outline may extend into surrounding space or overlap nearby content.
Outline Rendering
The exact shape of an outline can depend on the element's geometry and browser rendering. Outlines around wrapped inline content may not form a simple rectangle.
Important Consideration
The property affects only the outline's position. It does not change the outline's width, style, or color.
Positive Offset Values
Positive values move an outline outward, increasing the visible distance between the outline and the element's border edge.
Small Positive Offset
.box {
outline: 3px solid #2563eb;
outline-offset: 2px;
}
Medium Positive Offset
.box {
outline: 3px solid #2563eb;
outline-offset: 6px;
}
Large Positive Offset
.box {
outline: 3px solid #2563eb;
outline-offset: 12px;
}
Using Positive Offsets with Borders
.box {
border: 2px solid #64748b;
outline: 3px solid #2563eb;
outline-offset: 5px;
}
The positive offset creates a visible gap between the border and outline.
Common Uses
- Separating focus indicators from button borders.
- Creating additional visual emphasis around interactive elements.
- Adding space between an outline and a decorative border.
- Improving the visibility of keyboard focus indicators.
Important Consideration
Large positive offsets can cause outlines to overlap adjacent elements or become clipped by ancestor containers.
Negative Offset Values
Negative values move an outline inward toward the element's content area.
Small Negative Offset
.box {
outline: 3px solid #2563eb;
outline-offset: -2px;
}
Medium Negative Offset
.box {
outline: 3px solid #2563eb;
outline-offset: -6px;
}
Large Negative Offset
.box {
outline: 3px solid #2563eb;
outline-offset: -12px;
}
Inset-Looking Outline
.box {
border: 2px solid #64748b;
outline: 3px solid #2563eb;
outline-offset: -8px;
}
The outline moves inward and may overlap the element's background or content.
Common Uses
- Creating an inset-looking highlight without changing box dimensions.
- Keeping a focus indicator inside a tightly constrained component.
- Adding a decorative inner line to an element.
Important Consideration
Negative offsets can cause outlines to overlap text, icons, or other content. Check the result carefully, especially when the outline is used as a focus indicator.
Zero Offset
The initial value of outline-offset is 0, which means no additional spacing is applied.
Explicit Zero Offset
.box {
outline: 3px solid #2563eb;
outline-offset: 0;
}
Resetting an Offset
.box {
outline: 3px solid #2563eb;
outline-offset: 8px;
}
.box.reset {
outline-offset: 0;
}
The reset class removes the additional eight-pixel offset.
Using initial
.box {
outline-offset: initial;
}
This also resets the offset to zero.
Important Consideration
A zero offset does not remove the outline. Use outline-style: none or another appropriate declaration to control whether the outline is drawn.
Length Units
The outline-offset property accepts CSS length units, including absolute and relative lengths.
Pixels
.box {
outline-offset: 5px;
}
Pixels provide a straightforward way to define a consistent offset.
em Units
.box {
outline-offset: 0.25em;
}
The offset is relative to the element's font size.
rem Units
.box {
outline-offset: 0.3rem;
}
The offset is relative to the root element's font size.
Calculated Values
.box {
outline-offset: calc(2px + 0.1em);
}
The calc() function combines compatible length values.
Using CSS Variables
:root {
--focus-outline-offset: 3px;
}
button:focus-visible {
outline: 3px solid #2563eb;
outline-offset: var(--focus-outline-offset);
}
Custom properties help maintain consistent outline spacing across multiple components.
Percentages Are Not Supported
.box {
outline-offset: 10%;
}
The declaration is invalid because outline-offset does not accept percentage values.
Outline vs. Border Spacing
Outlines and borders both draw lines around elements, but their spacing and layout behavior differ.
| Feature | Outline | Border |
|---|---|---|
| Part of box model dimensions | No | Yes |
Can be moved with outline-offset |
Yes | No |
| Affects layout size | No | Yes, depending on box sizing |
| Can overlap nearby content | Yes | Normally participates in box sizing |
| Common use | Focus indicators and highlights | Element boundaries and decoration |
Border with Padding
.box {
padding: 20px;
border: 3px solid #2563eb;
}
Padding creates space inside the border and contributes to the element's box dimensions.
Outline with Offset
.box {
outline: 3px solid #2563eb;
outline-offset: 10px;
}
The outline is drawn farther away from the element without changing its box dimensions.
Using Both
.box {
border: 2px solid #64748b;
outline: 3px solid #2563eb;
outline-offset: 5px;
}
The border defines the element's boundary, while the outline provides an additional visual highlight.
Using outline and outline-offset
The outline shorthand controls the outline's width, style, and color. The outline-offset property controls its distance from the element.
Combined Example
.box {
outline: 3px solid #2563eb;
outline-offset: 5px;
}
Equivalent Longhand Outline Properties
.box {
outline-width: 3px;
outline-style: solid;
outline-color: #2563eb;
outline-offset: 5px;
}
Both examples produce the same outline appearance and offset.
Changing the Offset Independently
.box {
outline: 3px solid #2563eb;
outline-offset: 2px;
}
.box.highlighted {
outline-offset: 8px;
}
The highlighted element keeps the same outline width, style, and color but moves the outline farther outward.
Shorthand Does Not Reset the Offset
.box {
outline-offset: 6px;
outline: 3px solid #2563eb;
}
The offset remains six pixels because outline-offset is not part of the outline shorthand.
Important Consideration
Always declare outline-offset separately. It cannot be included as an additional value in the outline shorthand.
Focus Indicators
One of the most useful applications of outline-offset is creating separation between an interactive element and its keyboard focus indicator.
Basic Focus Outline
button:focus-visible {
outline: 3px solid #2563eb;
outline-offset: 3px;
}
Focus Outline for Links
a:focus-visible {
outline: 2px solid #2563eb;
outline-offset: 4px;
}
Focus Outline for Form Controls
input:focus-visible,
select:focus-visible,
textarea:focus-visible {
outline: 3px solid #2563eb;
outline-offset: 2px;
}
Consistent Focus Spacing
:root {
--focus-color: #2563eb;
--focus-offset: 3px;
}
button:focus-visible,
a:focus-visible,
input:focus-visible {
outline: 3px solid var(--focus-color);
outline-offset: var(--focus-offset);
}
Custom properties provide consistent focus styling across different interactive elements.
Important Consideration
Focus indicators should remain clearly visible and should not be clipped by surrounding containers.
Accessibility Considerations
Outline spacing can improve the visibility of keyboard focus indicators by separating them from an element's border and background.
Provide a Visible Focus Indicator
button:focus-visible {
outline: 3px solid #2563eb;
outline-offset: 3px;
}
The offset creates a gap that helps distinguish the focus indicator from the button.
Avoid Excessive Negative Offsets
button:focus-visible {
outline: 3px solid #2563eb;
outline-offset: -12px;
}
A large negative offset may place the outline over text or icons, reducing readability.
Avoid Clipped Outlines
Outlines with large positive offsets may be clipped by ancestor containers that restrict overflow. Test focus indicators inside cards, dialogs, navigation menus, and other constrained layouts.
Forced Colors Mode
@media (forced-colors: active) {
:focus-visible {
outline: 2px solid Highlight;
outline-offset: 2px;
}
}
System colors can help preserve visible focus indicators in forced-colors environments.
Important Consideration
Use the Tab and Shift+Tab keys to verify that focused elements remain clearly identifiable throughout the interface.
Transitions and Animations
The outline-offset property can be animated between compatible length values.
Hover Transition
.box {
outline: 3px solid #2563eb;
outline-offset: 2px;
transition: outline-offset 200ms ease;
}
.box:hover {
outline-offset: 8px;
}
The outline moves outward smoothly when the element is hovered.
Focus Transition
button {
outline: 3px solid transparent;
outline-offset: 0;
transition: outline-offset 150ms ease, outline-color 150ms ease;
}
button:focus-visible {
outline-color: #2563eb;
outline-offset: 4px;
}
The outline becomes visible and moves outward when the button receives visible focus.
Keyframe Animation
@keyframes outline-expand {
from {
outline-offset: 0;
}
to {
outline-offset: 8px;
}
}
.box {
outline: 3px solid #2563eb;
animation: outline-expand 600ms ease-in-out alternate infinite;
}
The outline repeatedly moves between zero and eight pixels from its normal position.
Reduced Motion
@media (prefers-reduced-motion: reduce) {
.box {
animation: none;
transition: none;
}
}
Respecting reduced-motion preferences helps avoid unnecessary animation for users who prefer less motion.
Important Consideration
Keep focus indicators visible even when transitions and animations are disabled.
outline-offset Example
This example compares zero, positive, and negative outline offsets. Each box uses the same outline width, style, and color.
HTML Example
<div class="outline-offset-demo">
<div class="offset-box offset-zero">0px</div>
<div class="offset-box offset-positive">6px</div>
<div class="offset-box offset-large">12px</div>
<div class="offset-box offset-negative">-6px</div>
</div>
CSS Example
.outline-offset-demo {
display: flex;
flex-wrap: wrap;
gap: 32px;
padding: 24px;
background-color: #f1f5f9;
}
.offset-box {
display: flex;
align-items: center;
justify-content: center;
width: 100px;
height: 70px;
border: 2px solid #64748b;
background-color: #fff;
color: #334155;
font-weight: 700;
outline: 3px solid #2563eb;
}
.offset-zero {
outline-offset: 0;
}
.offset-positive {
outline-offset: 6px;
}
.offset-large {
outline-offset: 12px;
}
.offset-negative {
outline-offset: -6px;
}
The positive values move the outlines outward, while the negative value moves the outline inward. The boxes retain their original layout dimensions.
Interactive Focus Example
This example uses outline-offset to separate a keyboard focus indicator from a button's border.
<button class="offset-focus-button" type="button">Focus Me</button>
.offset-focus-button {
padding: 12px 20px;
border: 2px solid #2563eb;
background-color: #fff;
color: #1e40af;
cursor: pointer;
}
.offset-focus-button:focus-visible {
outline: 3px solid #f97316;
outline-offset: 5px;
}
Use the Tab key to focus the button. The orange outline appears five pixels away from its normal position outside the blue border.
Common Mistakes
Using outline-offset Without a Visible Outline
.box {
outline-offset: 5px;
}
The declaration is valid, but it does not create an outline. A visible outline style and width are also needed.
Using Percentage Values
.box {
outline-offset: 10%;
}
The declaration is invalid because percentages are not supported.
Using auto
.box {
outline-offset: auto;
}
The property does not accept auto.
Including the Offset in the outline Shorthand
.box {
outline: 3px solid blue 5px;
}
This declaration is invalid. The offset must be specified separately.
Expecting Layout Changes
Changing outline-offset does not affect an element's box dimensions or position.
Using Excessive Negative Offsets
Large negative offsets may place the outline over text or other content.
Ignoring Overflow Clipping
Outlines moved outward may be clipped by ancestor elements that restrict overflow.
Removing Focus Indicators
Do not remove focus outlines without providing a visible replacement for keyboard users.
Assuming the Offset Changes Outline Thickness
The offset changes the outline's position, not its thickness. Use outline-width to control thickness.
Important Considerations
Keep the following points in mind when using outline-offset:
- The property controls the distance between an outline and an element's border edge.
- Its initial value is
0. - The property is not inherited.
- It applies to all elements.
- It accepts CSS length values.
- Positive values move the outline outward.
- Negative values move the outline inward.
- Zero removes any additional offset.
- Percentage values are not supported.
- The
autokeyword is not supported. - Pixels,
em,rem, and calculated lengths can be used. - The property does not affect layout dimensions.
- The property does not change outline width, style, or color.
- A visible outline must be defined separately.
outline-offsetis not included in theoutlineshorthand.- A later
outlineshorthand declaration does not reset the offset. - Positive offsets may overlap nearby elements.
- Negative offsets may overlap the element's content.
- Outlines may be clipped by ancestor containers.
- The property is useful for keyboard focus indicators.
:focus-visiblecan be used to apply focus styling.- Focus indicators should remain visible and distinguishable.
- The property supports transitions and animations.
- Reduced-motion preferences should be respected when animating outlines.
- Test outlines in different browsers and layout conditions.
Browser Support
The CSS outline-offset property is Baseline - Widely available and is supported in modern versions of major browsers.
Chrome, Edge, Firefox, Safari, and other modern browsers support positive and negative outline offset values.
Rendering details may vary slightly depending on the browser, element geometry, and outline style.
For current compatibility information, visit Can I Use? and search for outline-offset.
Specifications
The outline-offset property is defined in CSS Basic User Interface Module Level 4.
CSS Basic User Interface Module Level 4: outline-offset
The specification defines how outline offsets affect the position of outlines relative to element borders.
For additional documentation and examples, consult MDN Web Docs.
