CSS Outline
CSS Outlines
Section titled “CSS Outlines”Understand how to use CSS outlines to highlight elements without affecting layout.An outline is a line drawn outside an element’s border. It’s often used to make an element “stand out”, typically for accessibility reasons like indicating focus.
Key difference from borders: Outlines do not take up space. Adding an outline won’t affect the element’s dimensions or cause other elements to reflow. They are drawn over the surrounding content.
The CSS outline properties let you control the style, color, width, and offset of this line.
Core Concepts & Examples
Section titled “Core Concepts & Examples”Styling an Outline:
Section titled “Styling an Outline:”You can set the outline-style, outline-color, and outline-width individually, or use the shorthand outline property.
/* Individual properties */.element-with-outline { outline-style: dashed; outline-color: blue; outline-width: 2px;}
/* Shorthand: width style color */.another-element { outline: 3px solid red;}Outline Offset:
Section titled “Outline Offset:”The outline-offset property adds space between the border edge and the outline. It can accept positive or negative values.
.element-with-offset-outline { border: 1px solid black; outline: 2px dotted green; outline-offset: 5px; /* Outline is 5px away from the border */}Outline vs. Border Layout Impact:
Section titled “Outline vs. Border Layout Impact:”Consider two identical divs. One has a border, the other an outline. Notice how the border pushes content away, while the outline does not affect the layout.
.box { width: 100px; height: 50px; margin: 10px; background-color: lightgray; display: inline-block; /* For side-by-side comparison */}
.has-border { border: 5px solid black; /* Adds 10px to width and height */}
.has-outline { outline: 5px solid black; /* Does NOT add to width/height */}Accessibility Use Case: Focus Indicator
Section titled “Accessibility Use Case: Focus Indicator”Outlines are crucial for showing keyboard users which element currently has focus. Browsers provide default focus outlines, but you can customize them for better visibility, ensuring they contrast well with the background.
a:focus { /* Using outline for focus - doesn't disrupt layout */ outline: 3px solid dodgerblue; outline-offset: 2px; /* Optional: Remove default browser outline if customizing */ /* outline: none; /* Only do this if you provide a replacement! */ */}
/* Modern alternative for focus styling */a:focus-visible { outline: 3px solid dodgerblue; outline-offset: 2px;}Avoid removing outlines (outline: none;) without providing a clear alternative focus indicator, as this harms accessibility.
CSS Outline Properties Summary
Section titled “CSS Outline Properties Summary”| Property | Description | Example Values |
|---|---|---|
| outline | Shorthand for setting outline-width, outline-style, and outline-color. | e.g., 2px dotted red |
| outline-color | Sets the color of the outline. | e.g., blue, #00FF00, rgb(255, 0, 0), currentColor |
| outline-style | Sets the style of the outline. | auto (browser default), none, dotted, dashed, solid, double, groove, ridge, inset, outset |
| outline-width | Sets the thickness of the outline. | e.g., thin, medium, thick, 2px, 0.1em |
| outline-offset | Sets the space between the outline and the border edge. | e.g., 5px, -2px, 0.5em |
For more details, refer to the MDN Web Docs on CSS Outline: [https://developer.mozilla.org/en-US/docs/Web/CSS/outline](https://developer.mozilla.org/en-US/docs/Web/CSS/outline)