Skip to main content

The border Property

The border property is the structural line that surrounds an element, situated between the padding and the margin. It is the visual frame of the element.

A border requires three components to be visible: a width, a style, and a color.


The border Shorthand​

The most common and efficient way to define a border is using the border shorthand property, which accepts the width, style, and color in any order.

styles.css
selector {
border: <border-width> <border-style> <border-color>;
}

Example​

styles.css
.highlight-box {
/* 2px wide, solid line, indigo color */
border: 2px solid #5C6BC0;
}

Longhand Properties​

For more granular control, especially when dealing with different styles or colors on specific sides, you can use the longhand properties.

1. border-width​

Sets the thickness of the border. Values can be length units (px, em, rem) or keywords (thin, medium, thick).

SyntaxDescription
border-width: 5px;All four sides are 5px5\text{px}.
border-width: 1px 3px;Top/Bottom 1px1\text{px}, Left/Right 3px3\text{px}.
border-top-width: 10px;Only the top border is 10px10\text{px}.

2. border-style (Mandatory)​

This is the most critical propertyβ€”if the style is omitted or set to none, the border will not be displayed, regardless of its width or color.

StyleDescription
solidA single, continuous line (most common).
dottedA series of dots.
dashedA series of short lines.
doubleTwo parallel solid lines.
grooveCreates a 3D grooved effect.
ridgeCreates a 3D raised effect (opposite of groove).
noneNo border (default).

3. border-color​

Sets the color of the border, using color names, hex codes, or RGB/HSL values.

styles.css
.special-border {
border-style: double;
border-width: 4px;
border-color: #FFB300; /* Amber */
}

Side-Specific Borders​

You can define all three properties (width, style, and color) for a single side:

PropertyDescription
border-topShorthand for top width, style, and color.
border-rightShorthand for right width, style, and color.
border-bottomShorthand for bottom width, style, and color.
border-leftShorthand for left width, style, and color.
/* Example: Border only on the left side */
.sidebar-note {
border-left: 6px solid #4CAF50; /* Thick green line */
}

Shaping Corners with border-radius​

While technically not part of the Box Model layers (width, padding, margin), the border-radius property is essential for styling the border and corner shapes. It allows you to round the corners of the element's border.

SyntaxDescription
border-radius: 10px;Rounds all four corners equally.
border-radius: 10px 0;Top-Left/Bottom-Right 10px10\text{px}, Top-Right/Bottom-Left 0px0\text{px}.
styles.css
.rounded-button {
border: 1px solid #7986CB;
border-radius: 8px; /* Smooth corners */
}

Interactive border Demo​

Use the live editor to experiment with different border styles and colors. Note how the border sits between the inner content/padding and the outer margin.