Chapter 11 of 33

Borders & Outlines

In The Box Model we saw that every element is a box made of four layers: content, padding, border and margin. In Margin & Padding we controlled the space inside and outside the box. Now it's time for the layer in between: the border.

Borders are everywhere in real designs: card outlines, input fields, table lines, dividers, round profile pictures (yes, those use border-radius). In this chapter we will also meet the border's close cousin, the outline, which looks similar but behaves very differently — and is essential for keyboard users.


What is a Border?

A border is a line drawn around an element, between its padding and its margin. It has three properties: a width, a style and a colour.

.card {
  border-width: 2px;
  border-style: solid;
  border-color: #333333;
}

The three properties answer three questions:

PropertyQuestionInitial value
border-widthHow thick is the line?medium (3px)
border-styleWhat does the line look like?none
border-colorWhat colour is it?currentcolor (the element's text colour)

Look closely at the initial values. The style starts as none, which means no border is drawn until you set a style. Width and colour on their own do nothing. This is the number one reason beginners say "my border isn't showing".


The border Shorthand

Writing three properties every time is tedious, so we almost always use the shorthand:

.card {
  border: 2px solid #333333;   /* width style color */
}

The three values can be written in any order, and any of them can be left out. Missing values are reset to their initial values:

.a { border: solid; }          /* 3px (medium), solid, text colour */
.b { border: 1px solid; }      /* 1px, solid, text colour */
.c { border: 2px red; }        /* style = none  →  NO border at all! */

Rule .c is the classic mistake. Because the style is missing, it is reset to none. In fact, when the style is none the browser also treats the border width as 0: if you check the element in DevTools, its computed border-top-width is 0px, so the border takes up no space at all.

Width keywords

Besides lengths like 1px or 0.25rem, border-width accepts three keywords. The current CSS specification fixes their sizes, and all modern browsers follow it:

KeywordWidth
thin1px
medium3px (the default)
thick5px

Percentages are not allowed for border widths.


Border Styles

border-style has ten possible values:

ValueLooks like
noneNo border (width treated as 0)
hiddenSame as none, except in tables with collapsed borders, where it wins conflicts
solidA single continuous line
dashedA series of short dashes
dottedA series of dots (round in most browsers)
doubleTwo parallel solid lines with a gap between them
grooveLooks carved into the page (3D effect)
ridgeLooks raised out of the page (opposite of groove)
insetThe whole box looks pressed in
outsetThe whole box looks raised

In practice you will use solid 95% of the time, with dashed and dotted for things like drop zones or "coupon" boxes. The 3D styles look dated and are rarely used today.

One detail about double: it needs room for two lines and a gap, so it only looks double at 3px or more. At 1px it looks like a plain solid line.

.upload-zone {
  border: 2px dashed #888888;
}

.receipt {
  border: 4px double black;   /* two 1-2px lines with a gap */
}

Borders on Individual Sides

Each of the four sides has its own set of properties. You can target a side with a shorthand:

.heading {
  border-bottom: 3px solid #0a7cff;   /* underline-style divider */
}

.quote {
  border-left: 4px solid #cccccc;     /* classic quote bar */
}

Or you can set one property for all four sides using the 1–4 value pattern you already know from Margin & Padding (top, right, bottom, left — clockwise):

.box {
  border-style: solid;
  border-width: 1px 2px 3px 4px;    /* top right bottom left */
  border-color: red blue;          /* top/bottom red, left/right blue */
}

The full family looks like this:

border                     (all sides: width style color)
├── border-top / -right / -bottom / -left       (one side: width style color)
├── border-width / border-style / border-color   (one property, 1–4 sides)
└── border-top-width, border-left-color, ...     (one property, one side)

A very common pattern is "set everything, then remove one side":

.tab {
  border: 1px solid #cccccc;
  border-bottom: none;
}

Modern CSS also has logical versions such as border-inline-start and border-block-end, which follow the writing direction instead of physical sides. We will cover them in Modern CSS: :has(), Nesting, clamp() & Logical Properties.


Borders Change the Size of the Box

Remember from The Box Model: with the default box-sizing: content-box, width is only the content width. Borders are added on top.

.box {
  width: 200px;
  padding: 10px;
  border: 5px solid #333333;
}

Step by step, the total width is:

  content          200px
+ padding left      10px
+ padding right     10px
+ border left        5px
+ border right       5px
  ---------------------
  total            230px

We checked this in Chrome: the element's offsetWidth is exactly 230. With box-sizing: border-box the total would stay 200px and the content would shrink to 200 − 20 − 10 = 170px.

This matters when you add a border on hover:

/* ❌ The button grows by 2px on hover and the layout "jumps" */
.btn:hover {
  border: 1px solid black;
}

/* ✅ Reserve the space with a transparent border */
.btn {
  border: 1px solid transparent;
}
.btn:hover {
  border-color: black;
}

The second version always has a 1px border; only its colour changes, so nothing moves.


How Corners Are Drawn (and the CSS Triangle)

When two sides have different colours, the browser joins them with a diagonal line at the corner. You rarely notice this with thin borders, but with thick ones it is obvious:

.corners {
  width: 60px;
  height: 60px;
  border: 20px solid;
  border-color: tomato gold teal navy;   /* top right bottom left */
}

Each side becomes a trapezoid. Now shrink the content to zero:

.triangle-up {
  width: 0;
  height: 0;
  border: 40px solid transparent;
  border-bottom-color: teal;
}

With no content, each border becomes a triangle meeting at the centre. Three of them are transparent, so only the bottom one is visible — a teal triangle pointing up, 80px wide and 40px tall. (The element itself is 80×80, because the transparent top border still takes space.) This "CSS triangle" trick is still used for tooltip arrows, although today clip-path or an inline SVG is often cleaner.


Rounded Corners with border-radius

border-radius rounds the outer corners of an element's border box. It works even if the element has no border — the background is clipped to the rounded shape.

.card {
  border-radius: 8px;
}

Like margin, it takes 1 to 4 values, but they go around the corners, starting top-left and moving clockwise:

border-radius: 10px 20px 30px 40px;

   top-left (10px) ┌──────────┐ top-right (20px)
                   │          │
bottom-left (40px) └──────────┘ bottom-right (30px)

border-radius: 10px 20px;   →  top-left & bottom-right = 10px,
                               top-right & bottom-left = 20px

Each corner also has its own property, such as border-top-left-radius.

Circles, ellipses and pills

A radius can be a percentage of the box's size: horizontal radius as a percentage of the width, vertical radius as a percentage of the height.

.avatar {
  width: 120px;
  height: 120px;
  border-radius: 50%;     /* perfect circle */
}

.oval {
  width: 200px;
  height: 100px;
  border-radius: 50%;     /* 100px × 50px radii → an ellipse */
}

.pill {
  width: 200px;
  height: 50px;
  border-radius: 9999px;  /* fully rounded ends */
}

Why does 9999px give a pill and not something weird? If the radii on one side add up to more than that side's length, the browser scales all radii down proportionally until they fit. For a 50px-tall box, the radii shrink to 25px — exactly half the height — which makes perfect semicircle ends. That's why designers use a huge number for "fully rounded" buttons: it works whatever the height. 50% would not give a pill here; it would give an ellipse (as with .oval).

Elliptical corners

A slash separates horizontal radii from vertical radii:

.blob {
  border-radius: 10px 40px / 20px 60px;
}

Before the slash are horizontal radii (top-left & bottom-right = 10px, top-right & bottom-left = 40px). After it are vertical radii (20px and 60px in the same pattern). So the top-left corner is a 10px × 20px elliptical curve and the top-right is 40px × 60px. Chrome reports the computed top-left radius as 10px 20px, which confirms the pairing.

border-radius and images

The rounding also clips the element's own background and replaced content like images:

img.avatar {
  width: 96px;
  height: 96px;
  object-fit: cover;
  border-radius: 50%;
}

Child elements that stick out are not clipped automatically. If a card has rounded corners and an image inside it pokes into the corner, add overflow: hidden to the card (more on that in Overflow & Scrolling).


Outlines

An outline is a line drawn outside an element's border edge. Unlike a border, it is not part of the box model: it takes up no space and never changes the layout.

.focus-demo {
  outline: 3px solid blue;   /* width style color, like border */
  outline-offset: 4px;       /* gap between border edge and outline */
}

The outline properties mirror the border ones: outline-width, outline-style, outline-color, plus outline-offset. In our Chrome test, an element with width: 100px and a 3px outline still had an offsetWidth of exactly 100 — the outline was simply painted on top of the surrounding space.

Border vs outline

FeatureBorderOutline
Takes up spaceYes, adds to the box sizeNo, never affects layout
Per-side controlYes (border-left, ...)No, always all four sides
Offset from the boxNoYes, outline-offset (can even be negative)
Follows border-radiusYesYes, in all current browsers (Safari since 16.4, 2023)
Can overlap other contentNoYes, it is drawn over neighbours
Main useDesign and decorationFocus indicators, debugging

Older tutorials say "outlines are always rectangular". That was true for years, but every major browser now draws the outline following the rounded corners (Firefox and Chrome since 2021, Safari since version 16.4 in March 2023).

Outlines and keyboard focus

Browsers draw an outline around the element that has keyboard focus (a link, a button, an input). For people who navigate with the Tab key, this ring is the only way to know where they are on the page.

Many sites remove it because it "looks ugly":

/* ❌ Never do this without a replacement */
button:focus {
  outline: none;
}

That makes the site unusable with a keyboard and fails accessibility guidelines (WCAG). The right approach is to style it, usually with :focus-visible from Pseudo-classes & Pseudo-elements, which shows the ring for keyboard focus but not for mouse clicks:

/* ✅ Custom, clearly visible focus ring for keyboard users */
button:focus-visible {
  outline: 3px solid #0a7cff;
  outline-offset: 2px;
}

Because the outline takes no space, the button does not move when the ring appears — which is exactly why outlines are used for focus instead of borders.

Outline as a debugging tool

Since outlines never change layout, they are perfect for seeing boxes while debugging:

* {
  outline: 1px solid red;   /* temporarily see every box on the page */
}

Using border here instead would add 2px to every element and could break the very layout you are trying to inspect.


Borders in Tables

By default, every table cell has its own border, so adjacent cells show double lines with a gap. border-collapse: collapse merges neighbouring cell borders into one:

table {
  border-collapse: collapse;
}

th, td {
  border: 1px solid #cccccc;
  padding: 8px;
}
border-collapseResult
separate (default)Each cell has its own border; border-spacing controls the gap
collapseAdjacent borders merge into a single line; border-spacing is ignored

When collapsed borders conflict, the browser picks a winner: hidden beats everything, then the wider border wins, then the style (double beats solid beats dashed, and so on).


A Quick Look at border-image

border-image paints the border with an image or gradient instead of a colour. It slices the image into 9 pieces (4 corners, 4 edges, 1 middle) and stretches or repeats the edges.

.fancy {
  border: 10px solid;   /* a style and width are still required */
  border-image: linear-gradient(45deg, purple, orange) 1;
}

The final 1 is the slice value. The main limitation: border-image ignores border-radius, so you cannot get rounded gradient borders this way. You will see gradients properly in the next chapter, Backgrounds & Gradients.


Complete Example

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Borders &amp; Outlines</title>
  <style>
    body {
      font-family: system-ui, sans-serif;
      padding: 24px;
    }
    .card {
      width: 260px;
      padding: 16px;
      border: 1px solid #dddddd;
      border-top: 4px solid #0a7cff;
      border-radius: 8px;
    }
    .avatar {
      width: 64px;
      height: 64px;
      border-radius: 50%;
      border: 3px solid white;
      outline: 2px solid #0a7cff;
      background: #ffd27a;
    }
    .btn {
      margin-top: 12px;
      padding: 8px 20px;
      border: 1px solid transparent;
      border-radius: 9999px;
      background: #0a7cff;
      color: white;
    }
    .btn:hover {
      border-color: #003f88;
    }
    .btn:focus-visible {
      outline: 3px solid #ff9800;
      outline-offset: 2px;
    }
  </style>
</head>
<body>
  <div class="card">
    <div class="avatar"></div>
    <h3>Priya Sharma</h3>
    <p>Final-year CSE student</p>
    <button class="btn">Follow</button>
  </div>
</body>
</html>

Result: a card with a thin grey border, rounded corners and a thick blue strip along the top (notice that the top border also curves into the rounded corners). Inside is a circular yellow avatar ringed by a white border and then a blue outline that follows the circle, and a blue pill-shaped "Follow" button. Pressing Tab shows an orange focus ring around the button without moving anything.


Common Mistakes

  • Forgetting the style. border: 2px red draws nothing because the style defaults to none.

  • Layout jumps on hover. Adding a border only on :hover changes the box size. Use a transparent border from the start, or use an outline / box-shadow.

  • Forgetting borders in width calculations. A width: 50% box with a 1px border is wider than 50% (unless box-sizing: border-box), so two of them won't fit side by side.

  • Using 50% for a pill. On a non-square box it gives an ellipse. Use a very large length like 9999px.

  • Removing the focus outline. outline: none without a visible replacement breaks keyboard navigation.

  • Expecting border-image to respect border-radius. It doesn't.


Interview / Exam Questions

1. What is the difference between border and outline?

A border is part of the box model: it adds to the element's size and can be set per side. An outline is drawn outside the border edge, takes up no space, never affects layout, always covers all four sides, and can be moved with outline-offset. Outlines are mainly used for focus indicators.

2. Why does border: 3px blue; show no border?

The shorthand resets the missing border-style to its initial value none. With style none no border is drawn and its used width becomes 0.

3. A div has width: 300px; padding: 20px; border: 4px solid; (content-box). What is its total width?

300 + 20 + 20 + 4 + 4 = 348px. With box-sizing: border-box it would be 300px, with a content width of 300 − 40 − 8 = 252px.

4. How do you make a circle with CSS?

Give an element equal width and height and set border-radius: 50%. On a non-square element, 50% gives an ellipse.

5. What does border-radius: 10px 30px; do?

Top-left and bottom-right corners get 10px; top-right and bottom-left get 30px.

6. Why is outline: none considered bad practice?

The outline is the browser's default focus indicator. Removing it without a replacement means keyboard users can't see which element is focused. If you want to change it, style it with :focus-visible instead of deleting it.

7. What is the default border-color?

currentcolor — the value of the element's color property. So border: 1px solid on red text gives a red border.


Summary

PropertyUseExample
borderWidth, style, colour on all sidesborder: 1px solid #ccc
border-top etc.One side onlyborder-left: 4px solid teal
border-radiusRounded cornersborder-radius: 50%
outlineLine outside the box, no layout effectoutline: 2px solid blue
outline-offsetGap between border and outlineoutline-offset: 3px
border-collapseMerge table cell bordersborder-collapse: collapse
  • A border needs a style; without it nothing is drawn.

  • Borders add to the box size (in content-box); outlines never do.

  • border-radius goes clockwise from top-left; 50% = circle on a square, 9999px = pill.

  • Keep focus outlines visible; customise them with :focus-visible.

In the next chapter, Backgrounds & Gradients, we will fill these boxes with colours, images and gradients.