CSS Outline

The CSS Outline is used to draw a line around an element without affecting its layout. Unlike borders, which are part of the element’s box model, outlines do not occupy space and can extend beyond the element without shifting other elements.

This property is useful for:

  • Highlighting elements (e.g., when they are focused).
  • Debugging layout issues.
  • Creating visual effects like glowing or floating outlines.
  1. Non-Intrusive – Unlike borders, outlines do not take up space or affect the element’s size.
  2. Outside the Element’s Border – The outline is always drawn outside the border.
  3. No Individual Sides – You cannot specify different outlines for top, bottom, left, or right (unlike border).
  4. Customizable – You can adjust its width, style, color, and offset.

The outline property consists of four main sub-properties:

  1. outline-style
  2. outline-color
  3. outline-width
  4. outline-offset

Each of these properties allows you to control different aspects of the outline.

This property defines the visual style of the outline. It is required when using outline, meaning you cannot define an outline without specifying a style.

Available Outline Styles:

StyleDescription
noneNo outline (default).
solidA continuous solid line.
dottedA series of dots.
dashedA series of dashes.
doubleTwo solid lines.
grooveA 3D groove effect (depends on color).
ridgeA 3D ridge effect.
insetA 3D inset effect.
outsetA 3D outset effect.

Example: Different outline-style Values

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Outline Styles</title>
    <style>
        .box {
            width: 150px;
            height: 50px;
            margin: 10px;
            text-align: center;
            line-height: 50px;
            font-weight: bold;
        }
        .solid { outline: 3px solid red; }
        .dotted { outline: 3px dotted blue; }
        .dashed { outline: 3px dashed green; }
        .double { outline: 5px double purple; }
        .groove { outline: 5px groove orange; }
        .ridge { outline: 5px ridge brown; }
        .inset { outline: 5px inset pink; }
        .outset { outline: 5px outset cyan; }
    </style>
</head>
<body>
    <div class="box solid">Solid</div>
    <div class="box dotted">Dotted</div>
    <div class="box dashed">Dashed</div>
    <div class="box double">Double</div>
    <div class="box groove">Groove</div>
    <div class="box ridge">Ridge</div>
    <div class="box inset">Inset</div>
    <div class="box outset">Outset</div>
</body>
</html>
CSS

output:

Solid
Dotted
Dashed
Double
Groove
Ridge
Inset
Outset

Defines the color of the outline.

Possible Values

  • Named colors: red, blue, green, etc.
  • Hex values: #ff0000
  • RGB values: rgb(255, 0, 0)
  • HSL values: hsl(120, 100%, 50%)
  • Transparent: Makes the outline invisible.

Example: Applying Different Colors

.box {
    outline-style: solid;
    outline-width: 3px;
}
.red { outline-color: red; }
.blue { outline-color: blue; }
.green { outline-color: green; }
.rgb { outline-color: rgb(255, 165, 0); } /* Orange */
.hex { outline-color: #800080; } /* Purple */
.hsl { outline-color: hsl(120, 100%, 25%); } /* Dark Green */
CSS

Specifies the thickness of the outline.

Possible Values:

ValueDescription
thinA thin outline.
mediumA medium outline (default).
thickA thick outline.
<length>Custom value (e.g., 2px, 5px, 10px).

Example: Different Outline Widths

.thin { outline-width: thin; }
.medium { outline-width: medium; }
.thick { outline-width: thick; }
.custom { outline-width: 8px; }
CSS

Defines the space between the outline and the element’s border.

Possible Values

  • Any length unit (e.g., 5px, 1em, 10%).
  • Negative values are allowed (outline moves closer to the element).

Example: Using outline-offset

.offset-0 { outline-offset: 0px; }
.offset-5 { outline-offset: 5px; }
.offset-10 { outline-offset: 10px; }
.offset-negative { outline-offset: -5px; }
CSS

Instead of setting outline-width, outline-style, and outline-color separately, you can define them in a single line:

Example: Shorthand Syntax

.box {
    outline: 3px dashed blue;
    outline-offset: 10px;
}
CSS

This is equivalent to:

.box {
    outline-width: 3px;
    outline-style: dashed;
    outline-color: blue;
    outline-offset: 10px;
}
CSS

Useful for accessibility (:focus pseudo-class).

input:focus {
    outline: 3px solid blue;
}
CSS

button:hover {
    outline: 4px dashed red;
}
CSS

.glow {
    outline: 5px solid rgba(0, 255, 0, 0.7);
    outline-offset: 5px;
    box-shadow: 0 0 10px rgba(0, 255, 0, 0.7);
}
CSS

PropertyDescriptionExample
outline-styleDefines the style (solid, dashed, etc.).outline-style: dashed;
outline-colorSets the color of the outline.outline-color: red;
outline-widthDefines the thickness.outline-width: 5px;
outline-offsetMoves the outline away from the element.outline-offset: 10px;
outline (shorthand)Combines width, style, and color.outline: 3px solid blue;
  • outline is useful for focus styles, hover effects, and debugging.
  • It does not affect the element’s size.
  • outline-offset allows for cool floating effects.
  • Can be animated using CSS transitions for modern UI effects.