- CSS Tutorial
- CSS Introduction
- CSS Syntax
- CSS Comments
- CSS Selectors
- CSS Fonts
- CSS Colors
- CSS Backgrounds
- CSS Box Model
- CSS Borders
- CSS Margins
- CSS Padding
- CSS Text
- CSS Images
- CSS Links
- CSS Lists
- CSS Tables
- CSS Outline
- CSS Icons
- CSS Display
- CSS max-witdh
- CSS Position
- CSS z-index
- CSS Overflow
- CSS Float
- CSS Align
- CSS Opacity
- CSS Navigation Bar
- CSS Dropdowns
- CSS Forms
- CSS Units
- CSS !important
- CSS Specificity
- CSS Combinators
- CSS inline-block
- CSS Hover
- CSS Cursors
- CSS Selectors
- CSS Type Selector
- CSS Class Selector
- CSS ID Selector
- CSS Attribute Selector
- CSS Pseudo-class Selector
- CSS Pseudo-element Selector
- CSS Universal Selector
- CSS Advanced
- CSS Text Formatting
- CSS Gradients
- CSS Shadow
- CSS Rounded Corners
- CSS Text Effects
- CSS 2D Transform
- CSS 3D Transform
- CSS Border Images
- CSS Inherit
- CSS Transitions
- CSS Animations
- CSS Box Sizing
- CSS Tooltip
- CSS Masking
- CSS Pagination
- CSS Styling Images
- CSS object-fit
- CSS object-position
- CSS Buttons
- CSS Multiple Columns
- CSS Variables
- CSS Flexbox
- CSS Grid
- CSS Media Queries
CSS Outline
In CSS, the outline property is a powerful tool used to draw a line around an element, outside its border edge. While it may look similar to a border, there is one critical difference: outlines do not take up space in the box model. Because they are drawn "on top" of the element's area, adding or removing an outline will never cause the layout to shift or jump.
Outlines are primarily used to provide visual feedback for accessibility (like showing which element has keyboard focus) or to highlight specific items without affecting the positioning of surrounding elements.
outline: 1px solid red; instead of a border.
- Basic Outline: You can specify the color, style, and width of the outline using the outline property. The basic syntax follows the pattern: outline: [width] [style] [color];
/* Example: Highlighting a selected card */
.card-selected {
outline: 4px solid #3498db;
}
- Outline Color (outline-color): Specifies the color of the line. Beyond standard hex codes and RGB, you can also use the keyword
invert(though support varies), which performs a color inversion of the pixels behind the line to ensure visibility regardless of background.
.element {
outline-color: #e74c3c; /* A vibrant red */
}
- Outline Style (outline-style): Defines the look of the line. This is the only required property if you are using the shorthand; if the style is not set, the outline will not appear. Common values include solid, dotted, dashed, double, groove, ridge, inset, and outset.
.button-focused {
outline-style: dashed;
outline-width: 2px;
}
- Outline Width (outline-width): Sets the thickness of the outline. You can use specific units like px, em, or rem, or use predefined keywords: thin (1px), medium (3px), or thick (5px).
.heavy-border {
outline-width: thick;
}
- Outline Shorthand (outline): To keep your CSS clean and readable, you can combine color, style, and width into a single line. The order of the values does not strictly matter, but [width] [style] [color] is the industry standard.
/* A clean, professional focus state */
.input-field:focus {
outline: 3px solid rgba(66, 153, 225, 0.6);
}
- Outline Offset (outline-offset): This is a unique property that borders do not have. It adds space between the outline and the edge/border of the element. This space is transparent, allowing the background of the parent element to show through.
.promo-box {
outline: 2px solid black;
outline-offset: 5px; /* Creates a "floating" frame effect around the box */
}
outline-offset to make focus states more visible. Adding a 2px offset prevents the outline from blending directly into the element's border, making it much easier for keyboard users to see where they are on the page.
- Removing Outlines: While you can remove outlines using
outline: none;oroutline: 0;, you must do so with extreme caution. This is often done to remove the default "blue ring" in Chrome or Safari for aesthetic reasons.
/* Warning: This can break accessibility */
button:focus {
outline: none;
}
The outline property is a fundamental tool for both design and accessibility. By using it correctly—especially in combination with outline-offset—you can create interactive elements that look great and remain usable for everyone, including those using screen readers or keyboard navigation.