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.
| Part | Longhand | Values | Default |
|---|---|---|---|
| Width | border-width | 1px, 0.2em, thin, medium, thick | medium |
| Style | border-style | solid, dashed, dotted, double, and more | none |
| Color | border-color | Any color | currentColor (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.
| Value | Result |
|---|---|
border-radius: 8px | All corners rounded by 8px |
border-radius: 50% | A circle on a square element, an ellipse on a rectangle |
border-radius: 999px | A pill: the short sides become full semicircles |
border-radius: 8px 0 | Top-left and bottom-right 8px, the other two square |
border-top-left-radius: 12px | One 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.
| Border | Outline | |
|---|---|---|
| Takes up space | Yes, part of the box size | No |
| Per side | Yes (border-top, ...) | No, all around |
Follows border-radius | Yes | Yes, in current browsers |
| Offset from the box | No | Yes, outline-offset |
| Typical use | Design | Focus indicators, debugging |
Common mistakes
- No border style.
border: 1px #111827;andborder-color: red;draw nothing. Includesolid(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 undercontent-box. Usebox-sizing: border-box. border-radiuswith a child poking out of the corners. Addoverflow: hiddento 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.