🇯🇵 日本語 | 🇺🇸 English | 🇪🇸 Español | 🇵🇹 Português | 🇹🇭 ไทย | 🇨🇳 中文

CSS Template: Button with Hover Color Change (:hover)

📝 Use Case

This template is useful when you want to provide visual feedback by changing the button's color on hover. It's a common technique to improve user experience.

📘 Explanation

We use the :hover pseudo-class to change the background-color when the user hovers over the button. The transition property adds a smooth effect.

🔹 Partial Code

<button class="hover-button">Hover Me</button>

<style>
  .hover-button {
    background-color: #007bff;
    color: white;
    padding: 10px 20px;
    border: none;
    border-radius: 8px;
    font-size: 1rem;
    cursor: pointer;
    transition: background-color 0.3s;
  }

  .hover-button:hover {
    background-color: #0056b3;
  }
</style>

🔸 Full HTML Code

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Button with Hover Color Change</title>
  <link rel="stylesheet" href="/assets/css/template-common.css?v=1">
  <style>
    .hover-button {
      background-color: #007bff;
      color: white;
      padding: 10px 20px;
      border: none;
      border-radius: 8px;
      font-size: 1rem;
      cursor: pointer;
      transition: background-color 0.3s;
    }

    .hover-button:hover {
      background-color: #0056b3;
    }
  </style>
</head>
<body>
  <button class="hover-button">Hover Me</button>
</body>
</html>
Copied!

🎨 Practical Design Techniques for Hover Effects

Hover effects on buttons are crucial UI elements that visually convey user interactions. Using the :hover pseudo-class to change colors intuitively signals that an element is interactive.

🌈 Color Design Variations

While the default blue (#007bff#0056b3) is common, it's best to adjust it to match your site’s color palette. For example, green buttons can transition from #28a745 to #218838, and red ones from #dc3545 to #c82333. Maintaining a contrast ratio of at least 4.5:1 enhances accessibility.

✨ Enhanced Interactivity

Adding transition: all 0.3s ease allows not only smooth color changes but also subtle adjustments in size and shadows. Applying transform: scale(1.05) makes the button appear slightly larger, enhancing its interactivity. However, avoid excessive animations; transitions under 0.3 seconds are ideal.

📱 Responsive Design Tips

For touch devices, it's better to combine :hover with :active. On mobile, slightly increasing padding (12px 24px) improves tap target size. Use @media (prefers-color-scheme: dark) to set appropriate colors for dark mode support.

Hover effects enhance user experience but should be used consistently across your site. Limit them to key action buttons and avoid excessive decorative use.