Tillitsdone
down Scroll to discover
CSS outline-offset Control Element Outlines
CSS outline-offset controls the space between an element's outline and its border.

Use it for focus indicators and decorative purposes.

Set with pixels, ems, or percentages.
thumbnail

Introduction

The outline-offset property in CSS is a handy tool for controlling the space between an element’s outline and its border. It’s great for enhancing the visual appeal and user experience of your web designs without affecting the page layout.

Specification

The outline-offset property is part of the CSS Basic User Interface Module Level 4. This module provides properties and values for controlling the user interface aspects of web elements, including outlines and their offsets.

Description

The outline-offset property specifies the space between an element’s outline and its border. Unlike borders, outlines don’t affect the layout of the page and can overlap other elements. This makes them ideal for focus indicators and decorative purposes.

Syntax

The syntax for the outline-offset property is straightforward:

outline-offset: <length>;

Where <length> can be any valid CSS length value like pixels (px), ems (em), or other units. You can also use global CSS values like inherit, initial, revert, revert-layer, and unset.

Values

The primary value for outline-offset is a length, which can be specified in various units:

  • Pixels (px): Specifies the space in pixels.
  • Ems (em): Specifies the space relative to the font size of the element.
  • Percentages (%): Specifies the space as a percentage of the element’s size.

You can also use negative values to place the outline inside the element:

outline-offset: -5px;

A value of 0 means there is no space between the element and its outline:

outline-offset: 0;

Global values like inherit, initial, revert, revert-layer, and unset can also be used:

outline-offset: inherit;
outline-offset: initial;
outline-offset: revert;
outline-offset: revert-layer;
outline-offset: unset;

Formal Definition

  • Initial Value: The default value for outline-offset is 0.
  • Applies To: All elements.
  • Inherited: No.
  • Animatable: Yes, as a length.

Examples

Here are some examples to illustrate how outline-offset works:

Setting Outline Offset in Pixels

HTML:

<p>Gallia est omnis divisa in partes tres.</p>

CSS:

p {
outline: 1px dashed red;
outline-offset: 10px;
background: yellow;
border: 1px solid blue;
margin: 15px;
}

This creates a paragraph with a dashed red outline 10 pixels away from the element’s border.

Using Ems for Outline Offset

HTML:

<div class="example">This is an example with ems.</div>

CSS:

.example {
outline: 2px solid green;
outline-offset: 0.5em;
background: lightgray;
border: 1px solid black;
margin: 20px;
padding: 10px;
}

This creates a div with a solid green outline 0.5em away from the element’s border.

Negative Outline Offset

HTML:

<div class="negative-offset">This is an example with a negative offset.</div>

CSS:

.negative-offset {
outline: 2px solid purple;
outline-offset: -5px;
background: lightblue;
border: 1px solid darkblue;
margin: 20px;
padding: 10px;
}

This creates a div with a solid purple outline 5 pixels inside the element’s border.

Zero Outline Offset

HTML:

<div class="zero-offset">This is an example with zero offset.</div>

CSS:

.zero-offset {
outline: 2px solid orange;
outline-offset: 0;
background: lightyellow;
border: 1px solid brown;
margin: 20px;
padding: 10px;
}

This creates a div with a solid orange outline right next to the element’s border.

Using outline-offset

The outline-offset property is a versatile tool for enhancing the visual appeal of your web designs. Here are some practical tips:

Understanding the Basics

  1. Default Behavior: By default, the outline is rendered right outside the element’s border.
  2. Positive and Negative Values: Use positive values to add space between the border and the outline, and negative values to place the outline inside the element.
  3. Zero Value: A value of 0 means no space between the element and its outline.

Practical Applications

  • Focus Indicators: Use outlines with offsets for focus indicators to improve accessibility.
  • Decorative Purposes: Create visually appealing elements without affecting the page layout.

By using the outline-offset property effectively, you can create visually distinct and user-friendly web designs.

Using Outline and Outline-Offset in CSS

  1. Focus Indicators: Outlines help users navigate through interactive elements like buttons, links, and form fields. Adjust outline-offset to make these indicators more visible and user-friendly.

    a:focus {
    outline: 2px solid blue;
    outline-offset: 5px;
    }
  2. Decorative Elements: Outlines can highlight important sections of your web page. Use outline-offset to create visually appealing designs.

    .highlight {
    outline: 3px dashed red;
    outline-offset: 10px;
    }
  3. Responsive Design: Use relative units like em or rem with outline-offset to make your designs responsive.

    .responsive-outline {
    outline: 2px solid green;
    outline-offset: 0.5em;
    }

Best Practices

  1. Consistency: Keep your use of outlines and their offsets consistent for a cohesive look.
  2. Accessibility: Ensure outlines are visible and easy to distinguish, using high-contrast colors and appropriate offsets.
    .accessible-outline {
    outline: 2px solid #000;
    outline-offset: 5px;
    }
  3. Performance: Avoid excessive values or complex styles that could impact performance.
  4. Testing: Test designs across different browsers and devices for consistent behavior.

Combining Outline and Outline-Offset

HTML:

<div class="custom-outline">
This is an element with a custom outline and offset.
</div>

CSS:

.custom-outline {
border: 1px solid steelblue;
outline: 5px solid lightblue;
outline-offset: 10px;
padding: 25px;
margin: 20px;
background: lightgray;
}

Browser Compatibility

The outline-offset property is widely supported across modern browsers:

BrowserVersionRelease Date
Chrome4.0Jan 2010
Firefox3.5Jun 2009
IE/Edge15.0Apr 2017
Opera10.5Mar 2010
Safari3.1Mar 2008

See Also

To further enhance your designs, explore these related properties:

  • [outline]WebsiteUrl: Shorthand for setting all outline properties.
  • [outline-width]WebsiteUrl: Specifies the width of the outline.
  • [outline-style]WebsiteUrl: Specifies the style of the outline.
  • [outline-color]WebsiteUrl: Specifies the color of the outline.
  • [border]WebsiteUrl: Property for setting the border of an element.

By understanding and using these properties, you can create highly customized and visually appealing web designs.

Get in Touch
Thank you for your interest in Tillitsdone! Whether you have a question about our services, want to discuss a potential project, or simply want to say hello, we're here and ready to assist you.
We'll be right here with you every step of the way.
Contact Information
rick@tillitsdone.com+66824564755
Find All the Ways to Get in Touch with Tillitsdone - We're Just a Click, Call, or Message Away. We'll Be Right Here, Ready to Respond and Start a Conversation About Your Needs.
Address
9 Phahonyothin Rd, Khlong Nueng, Khlong Luang District, Pathum Thani, Bangkok Thailand
Visit Tillitsdone at Our Physical Location - We'd Love to Welcome You to Our Creative Space. We'll Be Right Here, Ready to Show You Around and Discuss Your Ideas in Person.
Social media
Connect with Tillitsdone on Various Social Platforms - Stay Updated and Engage with Our Latest Projects and Insights. We'll Be Right Here, Sharing Our Journey and Ready to Interact with You.
We anticipate your communication and look forward to discussing how we can contribute to your business's success.
We'll be here, prepared to commence this promising collaboration.
Frequently Asked Questions
Explore frequently asked questions about our products and services.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.