CSS Buttons

Buttons are the most clicked elements on a webpage. A well-styled button communicates its purpose clearly, responds to user interaction, and fits the visual design of the page. CSS gives you complete control over every aspect of a button's appearance and behavior.

The Default Button Problem

Browser-default buttons look different in Chrome, Firefox, Safari, and Edge. They have system-specific padding, borders, and background colors. Resetting and rebuilding them gives you consistency and creative freedom.

DIAGRAM — Default vs Styled button:

Default (Chrome):  [ Submit ] ← gray gradient, OS-specific border
Default (Safari):  [ Submit ] ← slightly rounded, different gray
Styled button:     [  Submit  ] ← your colors, your shape, your brand

The Base Button Reset

button {
  display: inline-block;
  padding: 10px 24px;
  font-size: 1rem;
  font-family: inherit;  /* match your page font */
  font-weight: 600;
  line-height: 1;
  text-align: center;
  text-decoration: none;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  outline: none;
  background: none;
  -webkit-appearance: none;
}

Primary Button

A primary button is the main call to action — the most important clickable element on a section.

.btn-primary {
  background-color: #3498db;
  color: white;
  padding: 12px 28px;
  border: none;
  border-radius: 6px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.2s, transform 0.1s;
}

.btn-primary:hover {
  background-color: #2980b9;
}

.btn-primary:active {
  transform: scale(0.97);
}

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

Secondary / Outline Button

.btn-outline {
  background-color: transparent;
  color: #3498db;
  border: 2px solid #3498db;
  padding: 10px 26px;
  border-radius: 6px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.2s, color 0.2s;
}

.btn-outline:hover {
  background-color: #3498db;
  color: white;
}
DIAGRAM — Button types:

Primary:   [  Save Changes  ]   ← filled, high emphasis
Outline:   [  Cancel        ]   ← border only, lower emphasis
Ghost:     [  Learn More    ]   ← no border, text only
Danger:    [  Delete        ]   ← red, destructive action

Button Sizes

.btn-sm {
  padding: 6px 14px;
  font-size: 0.85rem;
}

.btn-md {
  padding: 10px 24px;
  font-size: 1rem;
}

.btn-lg {
  padding: 14px 36px;
  font-size: 1.1rem;
}

.btn-xl {
  padding: 18px 48px;
  font-size: 1.25rem;
}

Hover, Focus, and Active States

Every button needs three states: hover (mouse over), focus (keyboard navigation), and active (click moment). Skipping any of these harms usability.

DIAGRAM — Button state transitions:

Normal  →  [  Submit  ]  ← default appearance
            ↓ hover
Hover   →  [  Submit  ]  ← slightly darker background
            ↓ click
Active  →  [  Submit  ]  ← brief scale-down press effect
            ↓ tab key
Focus   →  [  Submit  ]  ← visible outline ring around button
.btn {
  background: #3498db;
  color: white;
  transition: background 0.2s, box-shadow 0.2s, transform 0.1s;
}

.btn:hover {
  background: #2980b9;
  box-shadow: 0 4px 12px rgba(52,152,219,0.4);
}

.btn:active {
  transform: translateY(1px);
  box-shadow: none;
}

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

Disabled Button

.btn:disabled,
.btn[disabled] {
  opacity: 0.5;
  cursor: not-allowed;
  pointer-events: none;
}

Full-Width Button

.btn-block {
  display: block;
  width: 100%;
}

Pill-Shaped Button

.btn-pill {
  border-radius: 9999px; /* any large number creates a full pill */
}
DIAGRAM — Pill vs square vs rounded:

Square:   [ Submit ]
Rounded:  ( Submit )   ← border-radius: 6px
Pill:    ╭ Submit ╮    ← border-radius: 9999px

Icon Button

.btn-icon {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

/* Text-only icon button (e.g., close button) */
.btn-icon-only {
  width: 40px;
  height: 40px;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
}

Loading State Button

.btn-loading {
  position: relative;
  color: transparent; /* hide text during loading */
  cursor: wait;
}

.btn-loading::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  width: 18px;
  height: 18px;
  margin: -9px 0 0 -9px;
  border: 2px solid white;
  border-top-color: transparent;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

Gradient Button

.btn-gradient {
  background: linear-gradient(135deg, #667eea, #764ba2);
  color: white;
  border: none;
  padding: 12px 28px;
  border-radius: 6px;
  transition: opacity 0.2s;
}

.btn-gradient:hover {
  opacity: 0.9;
}

3D Press Effect Button

.btn-3d {
  background: #3498db;
  color: white;
  border: none;
  border-bottom: 4px solid #2471a3; /* dark bottom border = shadow */
  border-radius: 6px;
  padding: 12px 28px;
  transition: border-bottom-width 0.1s, transform 0.1s;
}

.btn-3d:active {
  border-bottom-width: 1px;
  transform: translateY(3px); /* presses down */
}

Accessibility Checklist

  • Always use <button> for actions and <a> for navigation — do not use a styled <div> as a button.
  • Keep :focus-visible styles — keyboard users navigate with Tab and need to see where they are.
  • Disabled buttons should still be perceivable — use opacity, not display: none.
  • Ensure sufficient contrast between button text and background (4.5:1 minimum ratio).
  • Button labels must describe the action — "Submit Form" is better than just "Submit."

A great button system has a primary, secondary, and destructive variant in multiple sizes, with consistent hover, focus, active, and disabled states. Building this system once saves styling effort on every subsequent page.

Leave a Comment

Your email address will not be published. Required fields are marked *