Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 139 ยท The Box Model

Borders

Learn how CSS borders work, from border-style, width and color to single sides, logical properties and the border shorthand, with live examples to edit.

A border is the line drawn around an element's padding. It sits between padding and margin in the box model, and it is one of the most common ways to separate content: cards, input fields, table cells, dividers and highlighted notes all lean on it.

Borders look simple, and mostly they are. But there are a few surprises worth knowing: a border is invisible until it has a style, each side can be different, and borders take up real space unless you plan for them. This lesson walks through all of it.

The three parts of a border

Every border is made of three values:

  • border-style: the kind of line, such as solid, dashed or dotted.
  • border-width: how thick the line is.
  • border-color: what color it is.
CSS
.card {
  border-style: solid;
  border-width: 2px;
  border-color: #94a3b8;
}

The style is the important one. Its initial value is none, and a border with style none has a computed width of zero, whatever width you set. So if you write only border-width: 4px and border-color: red, nothing appears. This is the number one reason a border "doesn't show up".

The other two have sensible defaults. The width defaults to medium (3px in all current browsers), and the color defaults to currentColor, which means the element's text color.

Border styles

CSS has ten keyword styles. Two of them, none and hidden, draw nothing; the rest draw something visible.

Every border style

A few notes on what you just saw:

  • solid covers the vast majority of real designs. dashed and dotted are handy for drop zones, placeholders and "draft" states.
  • double draws two lines with a gap between them. It needs a width of at least 3px to show both lines.
  • groove, ridge, inset and outset fake a 3D effect by shading parts of the border lighter or darker. They look dated today, so use them sparingly.
  • The exact spacing of dashes and dots is up to the browser, and you cannot control it with border-style. If you need precise dashes, use a background gradient or an SVG instead.
  • hidden behaves like none, except in tables with border-collapse: collapse, where it wins over any neighboring border. You will see that in Styling Tables.

Border width

border-width takes any length, like 1px, 0.25rem or 0.1em, or one of three keywords: thin, medium and thick. Browsers map those to 1px, 3px and 5px. Percentages are not allowed.

Hairline borders of 1px are by far the most common. On high-density screens, browsers still draw a 1 CSS-pixel border as a crisp line; you rarely need anything thinner.

Widths in em scale with the font size, which is useful for components like buttons that come in several sizes:

CSS
.button {
  font-size: 1rem;
  border: 0.125em solid currentColor; /* 2px at 16px, grows with the font */
}
.button.large { font-size: 1.5rem; }

Border color and currentColor

border-color accepts any color. Its default, currentColor, is more useful than it sounds: a border that follows the text color updates automatically when the text color changes, for example on hover.

Borders that follow the text color

You only ever set color, and the border keeps up. The same trick works for icons that use fill: currentColor.

One side at a time

Each side has its own set of properties. You can set the full border of a single side, or one part of it:

CSS
.note {
  border-left: 4px solid #2563eb;     /* one side, all three parts */
  border-bottom-color: #cbd5e1;       /* one side, one part */
}

The border-style, border-width and border-color properties also work as shorthands with one to four values, in the same clockwise order as margin and padding (top, right, bottom, left):

CSS
.box {
  border-style: solid dashed;        /* top/bottom solid, left/right dashed */
  border-width: 1px 4px 1px 4px;
  border-color: #64748b #2563eb;
}

Logical sides

The physical names (top, right, bottom, left) are tied to the screen. Logical properties are tied to the direction of the text instead:

  • border-block-start and border-block-end: the start and end of the block direction (top and bottom in English).
  • border-inline-start and border-inline-end: the start and end of a line (left and right in English, but right and left in Arabic or Hebrew).
  • border-block and border-inline set both sides of an axis at once.
CSS
blockquote {
  border-inline-start: 4px solid #a855f7;
  padding-inline-start: 1rem;
}

In a right-to-left page, that quote bar moves to the right side automatically. Logical properties are covered fully in Writing Modes and Logical Properties.

The border shorthand

The border shorthand sets all three parts on all four sides in one go. The values can come in any order, and you can leave out width or color:

CSS
.a { border: 1px solid #e2e8f0; }
.b { border: dashed 2px;        } /* color: currentColor */
.c { border: solid;             } /* medium width, currentColor */
.d { border: none;              } /* remove a border */

Like every shorthand, border resets any part you leave out. If you set border-color: red and later write border: 2px solid, the color goes back to currentColor. A good habit is to set the shorthand first, then override single parts:

CSS
.input {
  border: 1px solid #94a3b8;
}
.input:focus-visible {
  border-color: #2563eb; /* only the color changes */
}
.input.error {
  border-bottom-width: 3px;
  border-color: #dc2626;
}

border cannot set different values for different sides. For that, combine it with side properties, or use the four-value form of border-width, border-style and border-color.

Borders take up space

With the default box-sizing: content-box, a border is added to the element's width and height. A 200px box with a 4px border is 208px wide. With border-box, the border is taken out of the width instead. That difference is explained in box-sizing.

A related trap: adding a border on hover makes the element grow, and everything around it jumps. Two easy fixes are to reserve the space with a transparent border, or to draw the line with an outline or box-shadow, which take up no space.

Avoiding the hover jump

In the first row, the neighbors shift by 6 pixels whenever you hover. In the second row, the transparent border already reserves the space, so only the color changes.

A few practical patterns

Dividers between list items. Put a border on every item except the first, using the next-sibling combinator:

CSS
.menu li + li {
  border-top: 1px solid #e2e8f0;
}

Callout bar. A thick border on one side and a light background is a classic way to highlight a note:

CSS
.callout {
  border-inline-start: 4px solid #0ea5e9;
  background: #f0f9ff;
  padding: 0.75rem 1rem;
}

Triangle from borders. Older CSS drew arrows with zero-size boxes and thick, partly transparent borders. It works, but clip-path (covered in Clip Paths and Masks) or an inline SVG is clearer today. Recognize the trick when you see it; don't reach for it first.

Want a border made of an image or gradient? The border-image property can slice an image into the border area. It is powerful but awkward, and it ignores border-radius. For a gradient border with rounded corners, layering backgrounds with background-clip is usually easier; see Multiple Backgrounds and background-clip.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. You write border-width: 4px; border-color: red; but no border appears. Why?

    border-style defaults to none, which draws nothing and computes the width to zero. Add a style such as solid.
  2. 2. What color is the border in .tag { color: green; border: 2px solid; }?

    When you leave out the color, it falls back to currentColor, the element's text color.
  3. 3. Which of these is NOT a valid border-width value?

    Border widths accept lengths and the keywords thin, medium and thick, but not percentages.
  4. 4. A card shifts its neighbors when a border appears on hover. What is the simplest fix?

    A transparent border reserves the space, so changing only the color on hover does not change the size.
  5. 5. In a right-to-left page, where does border-inline-start appear?

    Logical properties follow the text direction. Lines start on the right in right-to-left text, so the border moves there.

Summary

  • A border has a style, a width and a color; without a style other than none, it is invisible.
  • Ten styles exist; solid, dashed and dotted cover almost every modern design.
  • Widths accept lengths and thin/medium/thick, never percentages; the color defaults to currentColor.
  • Use side properties (border-top, border-inline-start) or the one-to-four value forms for different sides.
  • The border shorthand sets all sides at once and resets anything you leave out.
  • Borders add to an element's size under content-box; reserve space with a transparent border to avoid layout jumps.

Next: Rounded Corners, where those sharp border corners get softened.