CSS Buttons
Browsers give <button> a plain grey look that differs between operating systems. With a few properties you can create consistent, attractive buttons with clear hover, focus, active and disabled states.
A basic styled button
Start by resetting the border, then add colour, padding, rounded corners and a pointer cursor.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; padding: 20px; }
.btn {
font: inherit;
font-weight: 600;
padding: 12px 24px;
border: none;
border-radius: 8px;
background: #1e88e5;
color: white;
cursor: pointer;
}
</style>
</head>
<body>
<button>Default button</button>
<button class="btn">Styled button</button>
</body>
</html>Tip: Buttons don't inherit the page font by default.
font: inheritfixes that in one line.
Colours, sizes and shapes
Use modifier classes to build a small button system.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; }
.btn { font: inherit; border: 2px solid transparent; border-radius: 8px; padding: 10px 20px; cursor: pointer; margin: 4px; color: white; }
.primary { background: #3949ab; }
.success { background: #43a047; }
.danger { background: #e53935; }
.warning { background: #fdd835; color: #333; }
.outline { background: transparent; border-color: #3949ab; color: #3949ab; }
.sm { padding: 5px 10px; font-size: 13px; }
.lg { padding: 16px 32px; font-size: 20px; }
.pill { border-radius: 999px; }
.round { width: 48px; height: 48px; padding: 0; border-radius: 50%; font-size: 22px; }
.block { display: block; width: 100%; }
</style>
</head>
<body>
<button class="btn primary">Primary</button>
<button class="btn success">Success</button>
<button class="btn danger">Danger</button>
<button class="btn warning">Warning</button>
<button class="btn outline">Outline</button><br>
<button class="btn primary sm">Small</button>
<button class="btn primary lg">Large</button>
<button class="btn success pill">Pill</button>
<button class="btn danger round">+</button>
<button class="btn primary block">Full-width block</button>
</body>
</html>Interactive states
| State | Selector | Typical effect |
|---|---|---|
| Hover | :hover |
Darker or lighter background, lift |
| Keyboard focus | :focus-visible |
Visible outline ring |
| Pressed | :active |
Push down, darker |
| Disabled | :disabled |
Faded, cursor: not-allowed |
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; padding: 20px; }
.btn {
font: inherit; font-weight: 600; padding: 12px 22px; margin-right: 10px;
border: none; border-radius: 10px; background: #7b1fa2; color: white; cursor: pointer;
box-shadow: 0 4px 0 #4a0072;
transition: background .15s, transform .1s, box-shadow .1s;
}
.btn:hover { background: #9c27b0; }
.btn:focus-visible { outline: 3px solid #ffab00; outline-offset: 3px; }
.btn:active { transform: translateY(4px); box-shadow: 0 0 0 #4a0072; }
.btn:disabled { background: #bdbdbd; box-shadow: 0 4px 0 #9e9e9e; cursor: not-allowed; }
</style>
</head>
<body>
<button class="btn">Press me</button>
<button class="btn" disabled>Disabled</button>
<p>Hover, click, and use Tab to see each state.</p>
</body>
</html>Button groups and icon buttons
Group related buttons by removing gaps and rounding only the outer corners.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; padding: 20px; }
.group { display: inline-flex; }
.group button { font: inherit; padding: 10px 16px; border: 1px solid #00796b; background: white; color: #00796b; cursor: pointer; margin-left: -1px; }
.group button:first-child { border-radius: 8px 0 0 8px; margin-left: 0; }
.group button:last-child { border-radius: 0 8px 8px 0; }
.group button:hover, .group .on { background: #00796b; color: white; }
.icon { display: inline-flex; align-items: center; gap: 8px; font: inherit; padding: 10px 18px; margin-top: 16px;
border: none; border-radius: 8px; background: #263238; color: white; cursor: pointer; }
</style>
</head>
<body>
<div class="group"><button>Day</button><button class="on">Week</button><button>Month</button></div>
<br>
<button class="icon"><span aria-hidden="true">⬇️</span> Download</button>
</body>
</html>Animated hover effect
A sliding background built with a gradient and background-size:
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; display: grid; place-items: center; height: 200px; }
.slide {
font: inherit; font-size: 18px; padding: 14px 30px; cursor: pointer;
color: #ff5722; border: 2px solid #ff5722; border-radius: 6px;
background: linear-gradient(#ff5722, #ff5722) no-repeat left / 0 100%;
transition: background-size .35s, color .35s;
}
.slide:hover { background-size: 100% 100%; color: white; }
</style>
</head>
<body>
<button class="slide">Hover to fill</button>
</body>
</html>Note: Use
<button>for actions (submit, open, toggle) and<a>for navigation, even if they look identical.
Test yourself
Why add font: inherit to a button?
- To make it bold
- Buttons don't inherit the page font by default
- To enable hover
- To remove the border
Which selector styles a button that cannot be clicked?
- :inactive
- :disabled
- :readonly
- :blocked
Which state should always have a visible style for keyboard users?
- :hover
- :focus-visible
- :visited
- :empty