Favorites

4 Cool CSS Buttons

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee

Why style your buttons?

Buttons are where the action happens on a website: signing up, buying, sending a message. A well-designed button stands out, looks clickable and gives visitors confidence to press it. The browser's default button, on the other hand, looks plain and different on every device.

In this tutorial we'll build four cool button styles with simple CSS, each with its own hover effect. Along the way we'll make sure they're readable, work with the keyboard and look good on any screen.

Here are the four buttons we'll build. Hover over them, click and hold the green one, or press Tab to move between them:

Results

Button or link?

Before styling anything, pick the right element. It makes a real difference to how your page works:

  • Use <button> for actions on the current page, such as submitting a form, opening a menu or adding to a cart.
  • Use <a href="..."> for navigation to another page or section.

The styles in this post work on both, so a link can look exactly like a button. But using the right element means the keyboard and screen readers behave the way visitors expect.

The base button

All four designs share the same foundation. We'll put the common styles in a .btn class, then add a second class for each design:

<button class="btn btn-pill">Gradient pill</button>
<button class="btn btn-sweep">Fill sweep</button>
<button class="btn btn-press">3D press</button>
<button class="btn btn-glow">Glowing gradient</button>
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5em;
  min-height: 44px;
  padding: 0.6em 1.4em;
  font: 600 1rem/1.2 system-ui, sans-serif;
  border: 0;
  border-radius: 999px;
  cursor: pointer;
  text-decoration: none;
  transition: box-shadow 0.2s ease, translate 0.15s ease;
}

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

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

Line by line

LineWhat it does
display: inline-flex;Keeps the button inline with text, while centring its contents perfectly
align-items, justify-contentCentre the label vertically and horizontally
gap: 0.5em;Adds space between the text and an icon, if you include one
min-height: 44px;Makes the button easy to tap on a phone
padding: 0.6em 1.4em;Space around the text. Using em means the padding grows with the font size.
fontA clear, semibold 1rem label using the device's system font
border-radius: 999px;Fully rounded ends, whatever the button's width
cursor: pointer;Shows the hand cursor so it's obviously clickable
text-decoration: none;Removes the underline when the class is used on a link

What changed since the old version? The original button had a fixed height: 38px, a tiny 13px font and line-height to centre the text. If the label wrapped onto two lines or someone enlarged their text, the words spilled out. Padding with min-height lets the button grow to fit its content.

Button 1: the gradient pill

This is a modern version of the button from the original post: a rounded pill with a soft gradient and a pale ring around the edge. On hover, the ring turns white and a shadow lifts the button off the page.

.btn-pill {
  --btn-from: #0277b6;
  --btn-to: #0369a1;
  --btn-ring: #bae6fd;
  color: #ffffff;
  background: linear-gradient(var(--btn-from), var(--btn-to));
  box-shadow: 0 0 0 5px var(--btn-ring);
}

.btn-pill:hover {
  box-shadow: 0 0 0 5px #ffffff,
              0 4px 14px rgb(0 0 0 / 0.25);
}
  • linear-gradient() blends from the first colour at the top to the second at the bottom.
  • The ring is a box-shadow with no blur and a spread of 5px. Unlike the original's border, a shadow ring doesn't change the button's size, and it can animate smoothly.
  • On hover, a second shadow is added after a comma, so the button has both a ring and a soft drop shadow.

Four colours with custom properties

The colours are stored in custom properties (CSS variables) such as --btn-from. To make a new colour, you only need to change those three values. That replaces the twelve lines per colour the original needed:

.btn-pill.red {
  --btn-from: #dc2626;
  --btn-to: #b91c1c;
  --btn-ring: #fecaca;
}
.btn-pill.teal {
  --btn-from: #0f766e;
  --btn-to: #115e59;
  --btn-ring: #99f6e4;
}
.btn-pill.grey {
  --btn-from: #64748b;
  --btn-to: #334155;
  --btn-ring: #e2e8f0;
}
Results

Button 2: the fill sweep

This button starts as an outline. On hover, colour sweeps in from the left to fill it, and the text turns white. The trick is a background that starts with zero width and grows to full width:

.btn-sweep {
  --btn-color: #1d4ed8;
  color: var(--btn-color);
  border: 2px solid var(--btn-color);
  background: linear-gradient(var(--btn-color) 0 0) no-repeat left / 0% 100%;
  transition: background-size 0.3s ease, color 0.3s ease;
}

.btn-sweep:hover,
.btn-sweep:focus-visible {
  background-size: 100% 100%;
  color: #ffffff;
}
Results

How it works

  • linear-gradient(var(--btn-color) 0 0) is a handy shortcut for a solid block of colour that can be used as a background image.
  • no-repeat left anchors that block to the left edge.
  • / 0% 100% sets its size: zero width, full height, so it starts invisible.
  • On hover, background-size: 100% 100% grows it across the whole button, and the transition animates the sweep.

Tip: change left to right to sweep the other way, or to center to fill from the middle outwards.

Button 3: the 3D press

A chunky button that looks like it has depth, and physically presses down when clicked. A solid shadow underneath forms the "side" of the button, and on :active (while it's being pressed) the button moves down to meet it:

.btn-press {
  color: #ffffff;
  background: #15803d;
  border-radius: 10px;
  box-shadow: 0 6px 0 #14532d;
}

.btn-press:hover {
  background: #16a34a;
}

.btn-press:active {
  translate: 0 4px;
  box-shadow: 0 2px 0 #14532d;
}
Results
  • box-shadow: 0 6px 0 has no blur, so it looks like a solid edge 6px below the button.
  • On press, translate: 0 4px moves the button down 4px, and the shadow shrinks by 4px to match, so the bottom edge stays in place.

Tip: make the shadow a darker shade of the button colour, not black. It looks much more like a real solid object.

Button 4: the glowing gradient

A vibrant purple-to-pink gradient with a coloured glow. On hover, the gradient slides across and the glow grows stronger:

.btn-glow {
  color: #ffffff;
  background: linear-gradient(90deg, #7c3aed, #db2777, #7c3aed) 0 0 / 200% 100%;
  box-shadow: 0 6px 20px -6px rgb(124 58 237 / 0.6);
  transition: background-position 0.5s ease, box-shadow 0.3s ease;
}

.btn-glow:hover,
.btn-glow:focus-visible {
  background-position: 100% 0;
  box-shadow: 0 10px 28px -6px rgb(219 39 119 / 0.7);
}
Results

How it works

  • The gradient goes purple, pink, then purple again, and / 200% 100% makes it twice as wide as the button. Only half of it is visible at a time.
  • On hover, background-position: 100% 0 slides the gradient to show the other half. Because it starts and ends with the same colour, the movement looks seamless.
  • The glow is a box-shadow in the button's own colours. A negative spread (-6px) tucks it under the button, so it glows mostly from below.

The first button also has an arrow icon that nudges to the right on hover. The base class's gap spaces it from the text, and one small rule moves it:

.btn svg {
  transition: translate 0.2s ease;
}
.btn:hover svg {
  translate: 3px 0;
}

Making buttons accessible

A button that looks great but can't be used by everyone isn't really finished. Here's a quick checklist.

Readable text

Button text needs a contrast ratio of at least 4.5 to 1 against the background, and that includes every part of a gradient. Check both ends with your browser's developer tools or an online contrast checker.

A visible focus style

Keyboard users move between buttons with the Tab key. The :focus-visible rule in our base class draws a clear amber outline around the focused button. Notice that the sweep and glow buttons also show their hover effect on focus, so keyboard users get the same experience.

Big enough to tap

Small buttons are frustrating on a phone. The min-height: 44px in the base class keeps every button comfortably tappable, and padding in em keeps it in proportion as text size changes.

Clear disabled state

When a button can't be used yet, add the disabled attribute. Our base class fades it and shows a "not allowed" cursor:

Results

Respect reduced motion

Sweeps, slides and presses are fun, but some people find movement uncomfortable. Turn off the transitions for visitors who've asked their device to reduce motion:

@media (prefers-reduced-motion: reduce) {
  .btn,
  .btn svg {
    transition: none;
  }
}

No more prefixes: the original code repeated every gradient and transition with -moz-, -webkit- and -o- versions, including the old -webkit-gradient() syntax. Plain linear-gradient() and transition have worked in every browser for years, so you can leave them out.

Wrapping up

With one shared base class and a few lines for each design, you now have four cool buttons: a gradient pill, a fill sweep, a 3D press and a glowing gradient. Use custom properties to change colours quickly, pair every hover effect with :focus-visible, and check your contrast, and your buttons will look great for every visitor.

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee