Menu

CSS Border: Width, Style, Color and Border Radius

The border property draws a line around an element's padding. Learn the border shorthand, every border style, colors and single sides, rounded corners with border-radius, and how borders differ from outlines.

This page includes runnable editors - edit, run, and see output instantly.

The border shorthand

border draws a line around an element, between its padding and its margin. The shorthand takes three parts in any order: a width, a style and a color.

PartLonghandValuesDefault
Widthborder-width1px, 0.2em, thin, medium, thickmedium
Styleborder-stylesolid, dashed, dotted, double, and morenone
Colorborder-colorAny colorcurrentColor (the text color)

The style is the one you cannot skip. Its default is none, so border: 2px #2563eb; draws nothing at all. If you leave out the color, the border takes the element's text color.

Border styles

double needs at least 3px to show two lines. groove, ridge, inset and outset shade the color to look 3D and differ slightly between browsers. hidden looks like none; the two only differ inside tables with collapsed borders, which the HTML table border page covers.

One side at a time

Every side has its own shorthand (border-top, border-right, border-bottom, border-left), and every part has per-side longhands such as border-top-color. The logical versions, border-inline-start and border-block-end, follow the text direction:

border-color, border-width and border-style accept one to four values in the same clockwise order as padding: top, right, bottom, left.

Rounded corners with border-radius

border-radius rounds the corners of the border and the background, so it works on elements with no border at all.

ValueResult
border-radius: 8pxAll corners rounded by 8px
border-radius: 50%A circle on a square element, an ellipse on a rectangle
border-radius: 999pxA pill: the short sides become full semicircles
border-radius: 8px 0Top-left and bottom-right 8px, the other two square
border-top-left-radius: 12pxOne corner only
border-radius: 50% / 25%Elliptical corners: horizontal radius / vertical radius

A child with a background can poke out past a rounded parent's corners. overflow: hidden on the parent clips it to the curve.

Border vs outline

An outline looks like a border but is drawn outside it and takes no space, so adding one never moves anything. That is why browsers use outlines for focus rings.

BorderOutline
Takes up spaceYes, part of the box sizeNo
Per sideYes (border-top, ...)No, all around
Follows border-radiusYesYes, in current browsers
Offset from the boxNoYes, outline-offset
Typical useDesignFocus indicators, debugging

Common mistakes

  • No border style. border: 1px #111827; and border-color: red; draw nothing. Include solid (or another style).
  • A border that makes the layout jump on hover. Reserve the space with a transparent border, or use an outline or box-shadow.
  • Forgetting the border in the width. A width: 100% element with a border overflows its container under content-box. Use box-sizing: border-box.
  • border-radius with a child poking out of the corners. Add overflow: hidden to the rounded parent.
  • Removing focus outlines with outline: none. Keyboard users need them; restyle the outline instead of deleting it.

Frequently Asked Questions

How do I add a border in CSS?

Use the shorthand with a width, a style and a color: border: 2px solid #2563eb;. The style is the part that matters most: without one, the border style is none and no border is drawn.

Why is my CSS border not showing?

Almost always because no border style is set. border: 2px #2563eb; or border-color: red; alone draw nothing, since the default border-style is none. Add solid, dashed or another style.

How do I make rounded corners in CSS?

Use border-radius: border-radius: 8px; rounds all four corners. border-radius: 50%; on a square makes a circle, and a large value like 999px makes a pill shape. It rounds the background too, even with no border.

How do I add a border to only one side?

Use a side property: border-bottom: 2px solid #e5e7eb;, or border-inline-start for the side where text starts, which also works for right-to-left languages.

What is the difference between border and outline?

A border is part of the box: it takes up space and changes the element's size. An outline is drawn outside the border without taking space, cannot be set per side, and is what browsers use for focus rings. Both follow border-radius in current browsers.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED