CSS Buttons
In this page:
.button {
background-color: color;
color: color;
padding: length;
border: none;
border-radius: length;
}
.button:hover {
background-color: color;
}
Basic Button Styling
Styling a button starts with removing the browser default appearance (border, background) and applying your own padding, background-color, and border-radius.
Setting cursor: pointer explicitly is good practice even though buttons already show a pointer cursor by default, since it becomes necessary the moment you style a link as a button instead.
- Accepted values:
- border: none — removes the default border
- padding: <length> — inner spacing
- background-color: <color> — fill
- border-radius: <length> — corner rounding
- cursor: pointer — pointer on hover
Example: Basic Button Styling
<style>
button {
border: none;
background: royalblue;
color: white;
padding: 10px 20px;
border-radius: 6px;
cursor: pointer;
}
</style>
<button>Click Me</button>
Hover and Active States
A button needs visible feedback for the states a user actually interacts with: :hover for mouse-over (usually a slightly darker or lighter background) and :active for the moment it is being clicked (often a slightly darker shade still, or a small scale-down transform to simulate a physical press).
- Accepted values:
- :hover — mouse over
- :active — while pressed
- :focus-visible — keyboard focus
- transition: background-color 0.2s — smooth state change
- transform: translateY(1px) — pressed feel
Example: Hover and Active States
<style>
button {
background: royalblue;
color: white;
border: none;
padding: 10px 20px;
}
button:hover {
background: #3b5fd6;
}
button:active {
background: #2a49b0;
transform: scale(0.98);
}
</style>
<button>Press Me</button>
Focus State for Keyboard Users
A visible :focus (or :focus-visible) outline is essential for keyboard users tabbing through a page — removing it entirely with outline: none without providing a replacement is a common accessibility failure.
A custom focus ring using box-shadow or a styled outline keeps buttons both good-looking and accessible.
- Accepted values:
- :focus-visible — keyboard focus only
- outline: <width> <style> <color> — the focus ring
- outline-offset: <length> — space between element and ring
- outline: none — only with a visible replacement
Example: Focus State for Keyboard Users
<style>
button:focus-visible {
outline: 3px solid orange;
outline-offset: 2px;
}
</style>
<button>Tab to me</button>
Disabled Button Styling
The disabled attribute already prevents clicks and changes the cursor to not-allowed in most browsers by default, but a disabled button should also look visually distinct, typically with reduced opacity and a muted color, so users understand at a glance that it cannot be interacted with.
- Accepted values:
- :disabled — matches disabled buttons
- opacity: <number> — dimmed look
- cursor: not-allowed — signals unavailable
- pointer-events: none — ignores clicks
Example: Disabled Button Styling
<style>
button:disabled {
opacity: 0.5;
cursor: not-allowed;
}
</style>
<button disabled>Unavailable</button>
Button Size and Shape Variants
Real interfaces usually need several button variants: a primary filled button, a secondary outlined button (transparent background, colored border), and size variants (small/large) controlled by adjusting padding and font-size together so the button scales proportionally.
- Accepted values:
- padding: <length> — controls the size
- font-size: <length> — text size
- border-radius: 999px — pill shape
- border-radius: 0 — square corners
- width: 100% — full-width button
Example: Button Size and Shape Variants
<style>
.primary { background: royalblue; color: white; padding: 10px 20px; border: none; }
.secondary { background: transparent; border: 2px solid royalblue; color: royalblue; padding: 10px 20px; }
.small { font-size: 12px; padding: 4px 10px; }
</style>
<button class="primary">Primary</button>
<button class="secondary">Secondary</button>
<button class="primary small">Small</button>
- Removing the default
outlineon focus without adding a replacement, so keyboard users cannot see focus. - Styling
<div>as buttons instead of<button>, which loses keyboard and accessibility behavior. - Forgetting
cursor: pointerand a:hoverstate.
Supported in all modern browsers: Chrome, Firefox, Safari, Edge and Opera.
Chapter Quiz — Complete all 22 topics to unlock
0/22 topics done
Complete these topics first:
- CSS Gradients
- CSS Shadows
- CSS Filters
- CSS Blend Modes
- CSS Transforms
- CSS 2D Transforms
- CSS 3D Transforms
- CSS Transitions
- CSS Animations
- CSS Image Styling
- CSS Image Effects
- CSS Hover Overlays
- CSS Image Modal
- CSS Image Centering
- CSS Image Shapes
- CSS Buttons
- CSS Columns
- CSS Clip Path
- CSS Shapes
- CSS Scroll Snap
- CSS Architecture: BEM
- CSS Custom Properties