Skip to content
Tutorials Center
My learning

Topics

Lesson 90 of 139 ยท Lists, Tables and Forms

Styling Buttons and Links

Style buttons and links that look good and work for everyone. Cover hover, focus-visible, active and disabled states, cursors, transitions and readable links.

Buttons and links are how people get things done on your page. They need to look clickable, react when someone points at them, show clearly where keyboard focus is, and make it obvious when they can't be used. Getting all of that right takes a handful of states and a little care, and once you have a good button style you will reuse it everywhere.

This lesson builds a button from scratch, adds every interactive state, creates a few variants, and then turns to links: how to style them in body text without making them hard to find.

Before any CSS, pick the right element. The rule is simple:

  • A link (<a href>) goes somewhere: another page, a section of this page, a file.
  • A button (<button>) does something: submits a form, opens a menu, adds an item to the cart.

The two behave differently. Links are activated with Enter and show up in the browser's link lists; buttons are activated with Enter or Space and announce themselves as buttons to screen readers. CSS can make a link look like a button or a button look like a link, and that's fine, as long as the element underneath matches what it does. Textarea and Buttons and Links in the HTML course cover the markup.

A solid base button

Browsers give buttons a small, gray, platform-specific look. Start by resetting the bits that cause trouble, then add your own design:

CSS
.btn {
  /* Reset browser quirks */
  font: inherit;
  border: 1px solid transparent;
  cursor: pointer;

  /* Layout: icon and text side by side, centered */
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5em;

  /* Look */
  padding: 0.6em 1.2em;
  border-radius: 8px;
  background: #4f46e5;
  color: white;
  font-weight: 600;
  line-height: 1.2;
  text-decoration: none;
}

A few of these deserve a comment:

  • font: inherit: buttons don't inherit the page font by default (see Styling Forms).
  • border: 1px solid transparent: a transparent border means variants with a visible border stay exactly the same size.
  • cursor: pointer: buttons show the arrow cursor by default. The hand is a convention for links, but most sites use it for buttons too, and users expect it.
  • display: inline-flex with gap: lines up an icon and a label neatly, and works the same on <a> and <button>.
  • text-decoration: none: removes the underline when the class is used on a link.

Sizes are in em, so a button with a larger font-size grows all its padding in proportion.

The interactive states

A button has five states worth styling. Each one answers a question for the user.

State Selector The user is asking
Hover :hover "Is this clickable?"
Focus :focus-visible "Where am I with the keyboard?"
Active :active "Did my press register?"
Disabled :disabled "Why can't I use this?"
Default .btn "What is this?"
CSS
.btn:hover {
  background: #4338ca;
}

.btn:focus-visible {
  outline: 3px solid #f59e0b;
  outline-offset: 2px;
}

.btn:active {
  transform: translateY(1px);
}

.btn:disabled {
  background: #a5b4fc;
  cursor: not-allowed;
}

Focus is the one people most often get wrong. Never remove the outline without replacing it. :focus-visible shows the ring when someone is using a keyboard but not after a mouse click, so you no longer have to choose between good looks and accessibility. An outline-offset leaves a gap so the ring stands out against the button's own color. Outlines covers focus rings in depth.

Disabled buttons ignore clicks automatically. They're exempt from contrast requirements, but make them look clearly inactive without making the label unreadable. Note that :hover rules still match a disabled button, so scope your hover to enabled buttons:

CSS
.btn:hover:not(:disabled) { background: #4338ca; }
A button with every state

Disabled buttons can confuse people. A disabled button can't receive focus, so screen reader users may not even know it exists, and nobody is told why it is disabled. Often it's better to leave the button enabled and show a helpful error when it's pressed. If you must show an unavailable action that stays focusable, use aria-disabled="true" and style [aria-disabled="true"] the same way, then block the action in JavaScript.

Smooth state changes

The demo above includes a transition. It makes the color change ease in over 150 milliseconds instead of snapping. Keep button transitions short (100 to 200 ms) so the interface feels responsive, and only transition the properties you change: background-color, color, border-color, box-shadow and transform are all cheap to animate. Avoid transition: all, which can animate things you didn't intend. The full details are in Transitions.

Variants with custom properties

Most designs need several kinds of button: a strong primary action, a quieter secondary one, a subtle "ghost" button and a red one for destructive actions. Rather than repeating every state for each variant, drive the colors with custom properties. Each variant only changes the variables:

Button variants and a link styled as a button

The last item is an <a> with the same classes. It looks like a button but behaves like a link, which is correct, because it navigates. Adding a new variant now takes one line.

Size and touch targets

The buttons above have min-height: 44px. Fingers are much less precise than a mouse pointer, so small targets lead to mistaken taps. Accessibility guidelines set a minimum of 24 by 24 CSS pixels (including spacing around the target), and 44 by 44 is a comfortable size for primary touch controls. Icon-only buttons are the usual offenders. Give them enough padding, and a text label for screen readers with aria-label or visually hidden text (see Accessible CSS).

Links inside paragraphs have a different job from buttons. They sit in the middle of running text, so they must stand out from the words around them without shouting.

The single most important rule: keep the underline for links in body text. Color alone isn't enough, because many people can't tell the link color from the text color. Modern text-decoration properties let you make underlines look refined instead of removing them:

CSS
.prose a {
  color: #1d4ed8;
  text-decoration-thickness: 1px;
  text-underline-offset: 0.2em;
  text-decoration-color: color-mix(in srgb, currentColor 40%, transparent);
}
.prose a:hover {
  text-decoration-thickness: 2px;
  text-decoration-color: currentColor;
}

text-underline-offset moves the line away from the letters so descenders like "g" and "y" don't collide with it. A softer underline color looks lighter, and on hover it becomes solid and thicker. Styling Text covers these properties.

Links have two extra states: :link (not yet visited) and :visited. When you style several states, write them in this order so later ones win when more than one matches:

CSS
a:link    { color: #1d4ed8; }
a:visited { color: #7e22ce; }
a:hover   { color: #1e40af; }
a:focus-visible { outline: 2px solid currentColor; outline-offset: 2px; }
a:active  { color: #172554; }

A memory aid is "LoVe Hate Focus Active": link, visited, hover, focus, active. All of these selectors have the same specificity, so the last matching rule wins, which is why the order matters.

For privacy, browsers only let :visited change color properties (color, background-color, border-color, outline-color, the decoration color and a few others). Anything else, such as font-weight or a background image, is ignored, so that pages can't detect your browsing history.

Readable links in a paragraph

Navigation bars, footers and card titles are different. There, the position and layout make it obvious that something is a link, so removing the underline is acceptable, provided the hover and focus states are clear. A common pattern makes a whole card clickable by stretching the title link's ::after pseudo-element over the card with position: absolute; inset: 0, keeping just one link in the markup. Pseudo-elements and Positioning explain the pieces.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which element should you use for a control that opens a menu on the same page?

    Opening a menu is an action, so it's a button. Links are for navigating somewhere. A div or span is neither focusable nor announced correctly by default.
  2. 2. Why use :focus-visible instead of :focus for the focus ring?

    The browser decides when focus should be visible, which is normally for keyboard navigation, so you can keep a strong ring without it appearing on every click.
  3. 3. A disabled button still turns darker on hover. What is the fix?

    :hover matches disabled buttons too. Excluding them with :not(:disabled) keeps the hover effect for usable buttons only.
  4. 4. What is the correct order for styling link states?

    The selectors have equal specificity, so later rules win. Putting hover, focus and active last lets them override the link and visited colors.
  5. 5. Why should links in body text keep an underline?

    Many people can't distinguish the link color from the surrounding text. An underline is a second, non-color cue that works for everyone.

Summary

  • Use <a href> for going somewhere and <button> for doing something; CSS can make either look like the other.
  • A base button resets font, border and cursor, and uses inline-flex with gap for icons and labels.
  • Style :hover, :focus-visible, :active and :disabled, and never remove focus outlines without a clear replacement.
  • Keep transitions short and limited to specific properties; build variants by changing custom properties.
  • Make touch targets at least 24px, ideally 44px for main controls.
  • Keep underlines on links in text, refine them with text-underline-offset and text-decoration-thickness, and order link states link, visited, hover, focus, active.

Next: Icons in CSS.