Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

How do you set border width, style and color in CSS?

CSS
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedDec 3, 2026
5 min read

To add a css border, set three things: a width, a style and a color. The shorthand border: 2px solid #333 does all three at once, and the style is the only required part because the default style is none, which draws nothing. This guide shows the shorthand, the longhand properties, per-side borders and how borders affect element size.

Table of Contents

  • The border shorthand
  • Border width
  • Border style
  • Border color
  • Borders on one side only
  • Borders and the box model
  • Border versus outline
  • A complete example
  • Frequently Asked Questions
    • Why is my CSS border not showing?
    • How do I add a border to only one side?
    • Does a border change the size of an element?
    • What is the difference between border and outline?
  • Summary
  • Related Articles

The border shorthand

css

The order of the three values does not matter, but width style color is the convention. If you omit the color, the border uses the element's color value through currentcolor. If you omit the width, it defaults to medium. If you omit the style, no border appears at all, which is the most common reason for an "invisible border".

Border width

border-width accepts a length such as 1px or 0.125rem, or the keywords thin, medium and thick. Percentages are not allowed.

css

With two values the first is top and bottom, the second is left and right. With four values they run clockwise from the top.

Border style

ValueLook
noneNo border (default)
solidContinuous line
dashedDashes
dottedDots
doubleTwo lines (needs at least 3px width)
groove, ridge, inset, outset3D effects, rarely used today
hiddenLike none, but wins conflicts in table borders
css

Border color

border-color takes any color value and follows the same one to four value pattern as width.

css

Without a color value the border follows the text color, which makes themes easy: change color and the border updates.

Borders on one side only

Each side has its own shorthand and longhands.

css

Longhands such as border-top-width, border-right-style and border-bottom-color exist for every combination. For languages that run right to left or vertically, logical properties like border-inline-start and border-block-end follow the writing direction instead of the physical side.

Borders and the box model

By default (box-sizing: content-box) the border is added to the element's width and height, so a 200px wide box with a 5px border becomes 210px wide. Most projects set a global reset:

css

With border-box the width includes padding and border, which makes layouts predictable. Borders also take up space, unlike outline, which is drawn outside the box without affecting layout.

Border versus outline

Featureborderoutline
Affects layoutYesNo
Per-side controlYesNo
Follows border-radiusYesIn current browsers, yes
Typical useDecorationFocus indicators

Never remove the focus outline without providing a visible replacement, because keyboard users depend on it.

A complete example

html

The general border rule sets all sides, and the later border-top rule overrides just the top. Order matters: if border came second, it would reset the top again.

Frequently Asked Questions

Why is my CSS border not showing?

Most often the style is missing. Add solid (or another style), because the default border-style is none even when you set a width and color.

How do I add a border to only one side?

Use border-top, border-right, border-bottom or border-left, or the logical versions like border-inline-start.

Does a border change the size of an element?

With content-box yes, it adds to the total size. With box-sizing: border-box the border is included inside the declared width and height.

What is the difference between border and outline?

A border is part of the box and takes layout space. An outline is drawn outside it and does not move anything, which is why it is used for focus rings.

Summary

A border needs a style to appear, usually with a width and color given in the shorthand border: 2px solid color. Control each side with the side shorthands, use border-box sizing to keep dimensions predictable, and prefer outlines for focus indicators.

Related Articles

  • How do radial and conic gradients work in CSS?
  • How do you create rounded corners with border-radius in CSS?
  • Learn CSS: Complete Study Roadmap from Beginner to Advanced
Share

Similar posts

  • How do you create rounded corners with border-radius in CSS?
  • How do radial and conic gradients work in CSS?
  • How do you create a linear gradient in CSS?
  • How do background-size, position and repeat work in CSS?
  • How do you set a background color and image in CSS?
  • How does the color-mix() function work in CSS?
  • What are modern CSS color spaces like oklch and lab?
  • How do you add transparency to colors with alpha in CSS?