Master CSS From Scratch

Clear, interactive, and structured CSS lessons designed for beginners.

CSS Border

CSS border is used to create a visible boundary around an HTML element.

A border appears between the element's padding and margin. It can be customized using different styles, widths, and colors.

Basic Syntax

selector {
    border: width style color;
}

Border Diagram

Border = Visible Boundary Around the Element
CIIT Training Institute

Simple Explanation

Think of an HTML element as a box.

The content is inside the box, padding creates space around the content, and the border creates a visible line around that area.

You can control three main things about a border: width, style, and color.

For example, 4px solid blue means a 4-pixel solid border with blue color.

Example 1: Solid Border

The solid border creates a continuous line around the element.

.box {
    background-color: lightblue;
    border: 4px solid blue;
    padding: 20px;
}

Output

CIIT Training Institute

Example 2: Dashed Border

The dashed style creates a border made of short lines.

.box {
    background-color: lightgreen;
    border: 3px dashed green;
    padding: 20px;
}

Output

Dashed Border Example

Example 3: Dotted Border

The dotted style creates a border made of dots.

.box {
    background-color: lightyellow;
    border: 3px dotted orange;
    padding: 20px;
}

Output

Dotted Border Example

Border Properties

border-width

Controls how thick the border is.

border-style

Defines the appearance of the border, such as solid, dashed, or dotted.

border-color

Controls the color of the border.

Example 4: Border on Individual Sides

You can apply different borders to the top, right, bottom, and left sides of an element.

.box {
    background-color: #e9d5ff;

    border-top: 5px solid purple;
    border-right: 5px solid blue;
    border-bottom: 5px solid green;
    border-left: 5px solid red;

    padding: 20px;
}

Output

Different Border Sides

Border Shorthand

Instead of writing border width, style, and color separately, you can combine them into one property.

.box {
    background-color: #d1fae5;
    border: 2px solid #047857;
    padding: 20px;
}

Output

Border Shorthand Example

Common Border Styles

Style Description Example
solid A continuous line. border: 2px solid blue;
dashed A border made of short lines. border: 2px dashed green;
dotted A border made of dots. border: 2px dotted red;
double A double-line border. border: 4px double purple;
none No visible border. border: none;

Border Width

The border width controls the thickness of the border. You can use values such as pixels or predefined keywords.

.box {
    border-width: 5px;
    border-style: solid;
    border-color: blue;
}

Border Color

The border color can be specified using color names, HEX values, RGB values, and other CSS color formats.

.box {
    border: 3px solid #2563eb;
}
Important: A border normally needs a border style such as solid, dashed, or dotted to become visible. A common shorthand format is: border: width style color;

Summary

CSS border creates a visible boundary around an HTML element. You can control its width, style, and color. Borders can be applied to all four sides together or individually using border-top, border-right, border-bottom, and border-left.