Thuta Learning
CSS
IntermediateWeb DevelopmentBeginner

Border

CSSLesson 1125 min

What you'll walk away with

  • Explain why a border shorthand without a style draws nothing
  • Write border-radius for single corners, circles and pills
  • Prevent the layout jump caused by adding a border on hover
  • Replace outline: none with a visible :focus-visible indicator

Explain it simply

Explain it simply

A border is like a picture frame. You choose three things for the frame: its thickness, its style (solid, dotted, dashed) and its color. Cards, inputs, buttons and alerts all use borders to stand apart from what is around them. border-radius rounds the frame's corners, which feels softer than sharp ones.

There is also outline, a border look-alike. It shows keyboard users who press Tab where they currently are on the page, so it is important to understand it.

Problem → Why → Solution

The problem: deleting the focus ring strands keyboard users

A designer dislikes the blue ring that appears when a button is clicked, so the developer adds outline: none. Nothing changes for mouse users, but people who rely on the keyboard alone, including people with motor disabilities, screen reader users and power users filling in forms quickly, can no longer see where focus goes when they press Tab. The card hover border is a second problem: every time the mouse enters, the card grows by 4px and the content around it jumps.

buttons.css
button:focus {
  outline: none;               /* keyboard users lose their place */
}

.card:hover {
  border: 2px solid #2563eb;   /* card had no border: it grows 4px and jumps */
}

Why does this happen?

The browser's default focus outline is to keyboard users what the mouse cursor is to everyone else; if you remove it, you must provide a replacement. A border is part of the box's size, so adding one on hover changes the layout. An outline takes no layout space, so it avoids that problem. Redesign the focus ring with outline, and reserve the hover border in advance as a transparent one.

Core concept: three parts and rounded corners

The shorthand border: 1px solid #d0d7de; sets border-width, border-style and border-color on all four sides at once. The style is the part that matters most: border-style starts as none, and with style none the computed width is 0 no matter what width you wrote. That is why border: 3px red; draws nothing. If you leave out the color, the border uses the text color, currentColor.

Most interfaces only ever use solid. dashed is common for drop zones such as a file upload area, while dotted and double are rare. You can also change one longhand at a time: switching only border-color on hover leaves the width and style exactly as they were.

To control one side, use side properties such as border-top or border-left. The logical version, border-inline-start, means the side where text starts: the left in left-to-right Myanmar and English, and automatically the right in right-to-left languages such as Arabic. For borders tied to the text, like an alert's accent bar, the logical property is the better fit.

border-radius rounds a box's corners, and it clips the background even when there is no border. One value rounds all four corners equally; four values go clockwise starting from the top-left. 50% on a square makes a circle and on a rectangle makes an ellipse. A large value such as 999px gives a pill with fully round ends, because the browser scales the radii down so neighbouring corners never overlap.

Note that radius only rounds the box's own background and border. An image inside a card can still poke out of the rounded corners with square edges; overflow: hidden on the card clips it. Finally, a border is part of the box's size, while outline and box-shadow take no layout space.

Mental model: how a frame gets drawn
  1. Is there a style? (none draws nothing)
  2. Width takes space in the box
  3. Color paints the frame
  4. Radius rounds the corners

An outline is drawn outside the frame but never changes the box's size.

General pattern

css
selector {
  border: width style color;          /* all four sides */
  border-inline-start: width style color; /* one logical side */
  border-radius: all-corners;         /* or: tl tr br bl */
}

selector:focus-visible {
  outline: width style color;
  outline-offset: gap;
}

Always include a style in the shorthand. Four radius values go top-left, top-right, bottom-right, bottom-left. Write the focus indicator as an outline on :focus-visible, with outline-offset to keep a gap from the box.

Example: a card and an alert

This example shows two ways to use a border. .card gets a subtle border on all four sides with rounded corners, while .alert gets a thick, bright border on its left side only.

.card {
  border: 1px solid #d0d7de;
  border-radius: 12px;
  padding: 16px;
}

.alert {
  border-left: 4px solid tomato;
}

Code breakdown

  1. Line 2border: 1px solid #d0d7de;

    All three parts are present. #d0d7de has only about 1.45:1 contrast on white, fine for a decorative card but not enough for an input border that users must be able to see.

  2. Line 3border-radius: 12px;

    Rounds all four corners with a 12px radius and clips the background to the same shape.

  3. Line 4padding: 16px;

    Keeps the text away from the border; without it the text would touch the frame.

  4. Line 8border-left: 4px solid tomato;

    Only the left side is set, so the other three keep border-style: none. Write border-inline-start if you support other writing directions. Note that .alert has no padding, so its text sits right against the bar.

You should see
.card is a box with a light grey 1px border, 12px rounded corners and 16px of padding. .alert shows a 4px tomato (orange-red) bar on its left side only, with no border on the other sides.

Hover and focus styles that do not shift the layout

If hover should only change the border color, reserve a transparent border in the normal state, and the size never changes. For the focus ring use :focus-visible: browsers show it when focus arrives by keyboard and usually not when a button is clicked with the mouse. That satisfies both the designer's complaint and the keyboard user's need.

interactive.css
.card {
  border: 2px solid transparent;   /* space reserved from the start */
  border-radius: 12px;
}
.card:hover {
  border-color: #2563eb;           /* only the color changes: no jump */
}

button:focus-visible {
  outline: 3px solid #2563eb;
  outline-offset: 2px;
}

Contrast for input borders

WCAG 2.1's non-text contrast rule (1.4.11) asks for at least 3:1 contrast against the background for borders users need in order to identify a component, such as an input. On white, #6b7280 reaches about 4.8:1, while #d0d7de manages only 1.45:1.

In real projects

Where this shows up in real projects

Products like GitHub and Stripe separate cards, tables and dropdowns with subtle 1px borders and use shadows sparingly. Design systems usually limit border colors to two or three tokens such as --border and --border-strong, and radius to a few steps like small, medium and full (pill), so the whole app feels consistent.

A missing focus indicator is one of the most frequent findings in accessibility audits. Most teams define a single :focus-visible outline style in the global stylesheet and reuse it in every component. In form validation, a red border on the invalid input is common, but it must come with an error message in text, because not every user can tell colors apart.

Common mistakes

Common mistakes

1. Forgetting the style in the shorthand

border: 2px #2563eb; leaves the style at none, so no border appears and DevTools even shows a computed border-width of 0px. Always include a style such as solid or dashed.

2. Writing outline: none with no replacement

*:focus { outline: none; } turns up often in reset stylesheets, and it makes the whole site unusable by keyboard. If you remove the default, add a clearly visible outline on :focus-visible in the same change.

3. Square image corners inside a rounded card

Give a card border-radius: 12px and a full-width image at the top, and the image's square corners cover the card's rounded ones. Add overflow: hidden to the card, or give the image the same radius.

Best practices

Best practices

  • Define the focus style once for :focus-visible in the global stylesheet, then walk the whole page with the Tab key alone.
  • Keep border colors and radius values in a few CSS variables and reuse them in every component.
  • Check borders that users must see, such as inputs and checkboxes, for 3:1 contrast with a contrast checker.

Mini challenge

Mini challenge: borders for a profile form

Write three kinds of border for a small profile section: a circular avatar, an input with a visible keyboard focus ring, and a note bar that follows the writing direction.

  • .avatar: 64×64px, a 3px blue border, perfectly round
  • .field: a 1px border with more than 3:1 contrast on white, 8px radius
  • .field:focus-visible: a 3px outline with a 2px offset (no outline: none)
  • .note: a 4px accent bar using a logical property, not border-left
profile.html
<!doctype html>
<html lang="en">
<style>
  /* TODO: .avatar - 64px square, 3px blue border, make it a circle */

  /* TODO: .field - padding, a 1px border with at least 3:1 contrast, 8px radius */

  /* TODO: .field:focus-visible - a 3px outline, 2px away from the border */

  /* TODO: .note - padding and a 4px bar on the side where text starts */
</style>
<img class="avatar" src="avatar.png" alt="Profile photo of Aye">
<p>
  <label for="name">Name</label>
  <input class="field" id="name" type="text">
</p>
<p class="note">Your name appears on your public profile.</p>
</html>

Expected: The avatar is a circle with a 3px blue frame (its alt text shows if the image file is missing). Pressing Tab to reach the Name input draws a 3px blue outline 2px outside it. The note has a 4px amber bar on its left, which moves to the right under dir="rtl".

Show hint

border-radius: 50% turns a square into a circle. The property for the side where inline text starts is border-inline-start. #6b7280 gives about 4.8:1 contrast on white.

Show solution
profile.html
<!doctype html>
<html lang="en">
<style>
  .avatar {
    width: 64px;
    height: 64px;
    border: 3px solid #2563eb;
    border-radius: 50%;
  }
  .field {
    padding: 8px 12px;
    border: 1px solid #6b7280;
    border-radius: 8px;
  }
  .field:focus-visible {
    outline: 3px solid #2563eb;
    outline-offset: 2px;
  }
  .note {
    padding: 8px 12px;
    border-inline-start: 4px solid #f59e0b;
  }
</style>
<img class="avatar" src="avatar.png" alt="Profile photo of Aye">
<p>
  <label for="name">Name</label>
  <input class="field" id="name" type="text">
</p>
<p class="note">Your name appears on your public profile.</p>
</html>

Quick quiz 1 / 3

You write .box { border: 3px red; }. What appears in the browser?

Quick quiz 2 / 3

What shape does border-radius: 50% give a 160px × 80px element?

Quick quiz 3 / 3

A card with no border gets border: 2px solid on hover, and nearby content jumps. What is the best fix?

What to remember

  • Always include a style in the border shorthand; the initial style is none, which draws nothing.
  • Four border-radius values run clockwise from the top-left; 50% makes a circle or ellipse, 999px a pill.
  • Borders add to the box size, so reserve a transparent border for hover; outline and box-shadow take no space.
  • Never write outline: none without a replacement; give :focus-visible a clearly visible outline.

CSS reference — MDN Web Docs

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Border | Thuta Learning