CSS Basics

CSS Borders

CSS Borders are decorative lines that surround an element's content and padding, defined by border-width, border-style, and border-color properties, which can be set individually for each side of an element.

How do you add a border in CSS?

Use the border shorthand with a width, a style and a colour: border: 1px solid #ccc. The style is required; without it no border shows. Round the corners with border-radius, and style one side at a time with properties like border-bottom.

.card   { border: 1px solid #e5e7eb; border-radius: 12px; }
.tab    { border-bottom: 2px solid transparent; }
.tab.active { border-bottom-color: #3b82f6; }
.avatar { border-radius: 50%; }   /* circle */

Border Properties

Width

  • • Thin, medium, thick
  • • Pixels (px)
  • • Ems, rems
  • • Percentages

Style

  • • Solid
  • • Dashed
  • • Dotted
  • • Double

Color

  • • Hex codes
  • • RGB values
  • • Named colors
  • • Transparent

Border Styles Demo

Explore different border styles, effects, and techniques. Click on any box to see dynamic border changes!

Code Editor

<div class="b solid">solid</div>
<div class="b dashed">dashed</div>
<div class="b one-side">bottom only</div>
<div class="b round">rounded</div>
<div class="avatar"></div>

Preview

Border Styles

Basic Styles

solid

A single solid line

dashed

A series of dashes

dotted

A series of dots

double

Two parallel lines

Advanced Styles

groove

3D grooved effect

ridge

3D ridged effect

inset

3D inset effect

outset

3D outset effect

Border Shorthand

Complete Border Shorthand

/* Syntax: border: width style color; */ border: 2px solid #3b82f6; /* Individual sides */ border-top: 1px dashed #10b981; border-right: 2px dotted #f59e0b; border-bottom: 3px double #ef4444; border-left: 4px groove #8b5cf6; /* Individual properties */ border-width: 2px; border-style: solid; border-color: #3b82f6;

Border Radius

/* All corners */ border-radius: 10px; /* Individual corners */ border-top-left-radius: 15px; border-top-right-radius: 5px; border-bottom-right-radius: 15px; border-bottom-left-radius: 5px; /* Shorthand: top-left | top-right | bottom-right | bottom-left */ border-radius: 15px 5px 15px 5px;

Border Image

/* Border image */ border-image: url('border.png') 30 round; /* Gradient borders */ border: 3px solid transparent; background: linear-gradient(white, white) padding-box, linear-gradient(45deg, #ff6b6b, #4ecdc4) border-box;

Your Learning Path

Master CSS borders and visual framing

What You've Learned

  • ▶Border width, style, and color properties
  • ▶Different border styles and effects
  • ▶Border radius for rounded corners
  • ▶Individual border sides control
  • ▶Border shorthand properties

What's Next

  • ▶CSS Margins - Control external spacing
  • ▶CSS Padding - Manage internal spacing
  • ▶CSS Height & Width - Element dimensions
  • ▶CSS Box Model - Complete layout system

Pro Border Tip

Use borders strategically to create visual hierarchy and guide user attention. Combine borders with shadows and gradients for modern, polished designs.

CSS Borders — step by step

1 / 4
1

Border Shorthand and Individual Sides

Use border: 2px solid red as a shorthand, or control each side independently with border-top, border-right, border-bottom, border-left.

1 of 4
💡Use outline for Focus Rings
Never remove outline: none on focusable elements without a replacement — it is the primary visual cue for keyboard users. Use outline-offset to add spacing between the element and the ring.

🎯 Quick Check

Which property creates a border WITHOUT affecting the element's layout size?

CSS Borders: summary

Key points

  • Border styles include solid, dashed, dotted, double and none.
  • border-radius: 50% on a square element makes a circle.
  • Borders add to an element's size unless you use box-sizing: border-box.
  • For a line that doesn't affect layout, use outline or an inset box-shadow.

Frequently asked questions

›Why is my border not showing?

A border-style is missing. border: 1px red shows nothing; border: 1px solid red works.

›What is the difference between border and outline?

A border is part of the box model and takes up space. An outline is drawn outside the border, takes up no space, and can't have per-side styles. Outlines are mainly used for focus indicators.