← Back to CSS Course | Chapter 8: Animations & Effects | Lesson 16 of 22

CSS Buttons

A native browser button looks dated the moment your design has any personality. Turning a plain button or anchor tag into something that matches your site takes just a handful of properties.
Syntax
css
.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.

Note:
  • 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

css
<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).

Note:
  • 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

css
<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.

Note:
  • 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

css
<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.

Note:
  • Accepted values:
  • :disabled — matches disabled buttons
  • opacity: <number> — dimmed look
  • cursor: not-allowed — signals unavailable
  • pointer-events: none — ignores clicks

Example: Disabled Button Styling

css
<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.

Note:
  • 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

css
<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>
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Removing the default outline on focus without adding a replacement, so keyboard users cannot see focus.
  2. Styling <div> as buttons instead of <button>, which loses keyboard and accessibility behavior.
  3. Forgetting cursor: pointer and a :hover state.
Browser Support

Supported in all modern browsers: Chrome, Firefox, Safari, Edge and Opera.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.