CSS border Property

Beginner
⏱️ 5 min read
📚 Updated: Jun 2026
🎯 4 Examples
Box Model & Design

What You’ll Learn

The border property is a CSS shorthand for drawing a frame around an element. With one line of CSS you can set the border width, style, and color to define boundaries and improve visual structure.

01

Shorthand

Width, style, color.

02

Border Width

px, em, thin, thick.

03

Border Style

solid, dashed, dotted.

04

Border Color

Names, hex, rgb, hsl.

05

Box Model

Border sits outside padding.

06

Side Borders

border-top and friends.

Definition and Usage

The border CSS property is a shorthand that sets an element’s border width, border style, and border color in a single declaration. Borders help separate content, highlight cards and buttons, and make layouts easier to scan.

Borders are an essential part of web design because they define the edges of elements and add structure to a page. You can apply the same border to all four sides, or use individual properties such as border-top when you need more control.

💡
Beginner Tip

A border only appears when border-style is not none. The most common pattern is border: 1px solid #cbd5e1; for a light outline around a card or input field.

📝 Syntax

The border shorthand accepts one, two, or all three border parts: width, style, and color.

syntax.css
selector {
  border: width style color;
}

Basic Example

border.css
div {
  border: 2px solid black;
  padding: 10px;
}

Syntax Rules

  • The usual order is width, then style, then color.
  • You can omit values, but border-style must not be none for the border to show.
  • Longhand properties include border-width, border-style, and border-color.
  • Side-specific shorthands include border-top, border-right, border-bottom, and border-left.
  • Borders sit between an element’s padding and margin in the CSS box model.

⚡ Quick Reference

QuestionAnswer
Initial valuemedium none currentColor (no visible border)
Applies toAll elements
InheritedNo
AnimatablePartially (color and width in supporting browsers)
Common useCards, buttons, inputs, tables, and layout separation

Default Value

If the border property is not set, the default is effectively medium none currentColor. That means the border style is none, so no border is visible even if width or color values exist elsewhere.

💎 Property Values

The border shorthand combines three parts: width, style, and color.

Border Width

Defines how thick the border is. Use length units or keywords.

ValueExampleMeaning
Lengthborder: 2px solid black;Exact thickness in px, em, rem, and other units
thinborder: thin solid black;Browser-defined thin border
mediumborder: medium solid black;Default medium thickness
thickborder: thick solid black;Browser-defined thick border

thin

medium

thick

Border Style

Determines how the border line looks. none hides the border.

previewsolid
previewdotted
previewdashed
previewdouble
previewgroove
previewridge
previewinset
previewoutset
  • none — No border (default style).
  • solid — A continuous line.
  • dotted — A series of dots.
  • dashed — A series of dashes.
  • double — Two solid lines.
  • groove, ridge, inset, outset — 3D-style border effects.

Border Color

Sets the border color using any valid CSS color value.

FormatExample
Named colorborder: 2px solid red;
Hexborder: 2px solid #ff0000;
RGBborder: 2px solid rgb(255, 0, 0);
HSLborder: 2px solid hsl(0, 100%, 50%);
red #2563eb rgb(5,150,105) hsl purple

Border Shorthand vs Longhand

Besides the main border shorthand, CSS provides longhand and side-specific properties for finer control.

PropertyPurpose
border-widthSets thickness on all sides or individual sides
border-styleSets line style on all sides or individual sides
border-colorSets color on all sides or individual sides
border-top, border-right, etc.Shorthand for one side only

👀 Live Preview

Three boxes showing common border styles with different colors:

solid
dashed
dotted

All boxes use a 3px border width with different styles and colors.

Examples Gallery

Try the border shorthand with solid lines, dashed outlines, custom colors, and single-side borders.

📚 Basic Borders

Start with the most common border shorthand: width, style, and color together.

Example 1 — Solid Black Border

Create a 2px solid black border around a div, matching the classic beginner example.

border-solid.html
<style>
  div {
    border: 2px solid black;
    padding: 10px;
  }
</style>

<div>
  This div element has a 2px solid black border.
</div>
Try It Yourself

How It Works

The shorthand sets a visible 2px solid line on all four sides. Padding keeps the text from touching the border edge.

Example 2 — Dashed and Dotted Borders

Change only the style keyword to create different outline effects.

border-styles.css
.dashed { border: 2px dashed #2563eb; }
.dotted { border: 2px dotted #dc2626; }
.double { border: 4px double #059669; }
Try It Yourself

How It Works

Width and color stay the same while the style keyword changes the line pattern. double needs enough width to show two lines clearly.

🎨 Custom Borders

Use colors and single-side borders to highlight cards, alerts, and navigation items.

Example 3 — Colored Border with Hex Value

Apply a brand color to a card border using a hex code.

border-color.css
.card {
  border: 1px solid #7c3aed;
  border-radius: 0.75rem;
  padding: 1rem;
  background: #faf5ff;
}
Try It Yourself

How It Works

The border color can use any CSS color format. Pairing a subtle background with a colored border creates a polished card look.

Example 4 — Border on One Side Only

Use a side shorthand when you only want a bottom or left accent line.

border-side.css
.nav-item {
  border-bottom: 3px solid #2563eb;
  padding-bottom: 0.35rem;
}
Try It Yourself

How It Works

border-bottom is shorthand for just the bottom edge. This pattern is common for active tabs and section dividers.

🧠 How border Works

1

You choose width, style, and color

Write one shorthand rule such as border: 2px solid black;.

CSS rule
2

The browser draws the box edge

If style is not none, a border appears between padding and margin on each side.

Box model
3

Side rules override when needed

Use border-top or other side shorthands to style individual edges.

Fine control
=

Clear element boundaries

Borders separate content, highlight controls, and make layouts easier to understand.

Universal Browser Support

The border property is supported in all browsers, including very old ones. It has been part of CSS since the earliest versions.

Baseline · All browsers

Reliable styling on every platform

Chrome, Firefox, Safari, Edge, and Opera all support border width, style, and color consistently.

100% Universal support
Google Chrome1+ · Desktop & Mobile
Full support
Mozilla Firefox1+ · Desktop & Mobile
Full support
Apple Safari1+ · macOS & iOS
Full support
Microsoft EdgeAll versions
Full support
OperaAll versions
Full support
border property 100% supported

Bottom line: Use border freely in any project. It is one of the most dependable CSS properties.

Conclusion

The border property is a versatile CSS shorthand for adding frames around elements. By combining width, style, and color, you can define boundaries, highlight important content, and improve the visual structure of any webpage.

Experiment with different border styles, widths, and colors to see how they transform cards, buttons, inputs, and layout sections. Mastering borders is a core step toward polished web design.

💡 Best Practices

✅ Do

  • Use border: 1px solid with a subtle color for cards and inputs
  • Include border-style whenever you want a visible border
  • Pair borders with padding so content does not touch the edge
  • Use side borders for tabs, alerts, and accent highlights
  • Consider box-sizing: border-box for predictable layout sizing

❌ Don’t

  • Set border width and color without a visible style
  • Use very thick borders on small text elements without reason
  • Mix too many different border styles in one UI
  • Forget that borders add to total element size by default
  • Rely on low-contrast border colors that are hard to see

Key Takeaways

Knowledge Unlocked

Five things to remember about border

Use these points when styling element outlines.

5
Core concepts
📏02

Width

px or thin/medium/thick.

Size
03

Style

solid, dashed, dotted.

Look
🎨04

Color

Any CSS color value.

Theme
📦05

Box Model

Sits outside padding.

Layout

❓ Frequently Asked Questions

The border property is a shorthand that sets an element's border width, border style, and border color in one declaration.
If no border is set, the initial border-style value is none, which means the border is not visible even if a width or color is specified.
The usual order is width, then style, then color. For example: border: 2px solid black;
solid, dashed, and dotted are the most common. double, groove, ridge, inset, and outset create 3D-style effects.
By default, border adds to the element's total size unless you use box-sizing: border-box, which includes border in the width and height calculation.

Practice in the Live Editor

Open the HTML editor, try different border styles, and preview the results instantly.

HTML Editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful