Create Hoverable Buttons with CSS

The CSS :hover pseudo-selector is used to apply styles to an element when a user hovers their mouse over it. This creates interactive hoverable buttons that change appearance on mouse hover.

Syntax

selector:hover {
    property: value;
}

Example: Basic Hoverable Button

The following example creates a hoverable button that changes color and border style when hovered −

<!DOCTYPE html>
<html>
<head>
<style>
    .btn {
        background-color: yellow;
        color: black;
        text-align: center;
        font-size: 15px;
        padding: 20px;
        border-radius: 15px;
        border: 3px dashed blue;
        cursor: pointer;
        transition: all 0.3s ease;
    }
    
    .btn:hover {
        background-color: orange;
        color: white;
        border: 3px solid blue;
        transform: scale(1.05);
    }
</style>
</head>
<body>
    <h2>Hoverable Button</h2>
    <p>Hover over the button below:</p>
    <button class="btn">Hover Me!</button>
</body>
</html>
A yellow button with dashed blue border appears. When hovered, it smoothly transitions to orange background with white text, solid blue border, and slightly scales up.

Example: Multiple Hover Effects

You can create different hover effects for various button styles −

<!DOCTYPE html>
<html>
<head>
<style>
    .button-container {
        display: flex;
        gap: 20px;
        flex-wrap: wrap;
    }
    
    .hover-btn {
        padding: 12px 24px;
        border: none;
        border-radius: 5px;
        cursor: pointer;
        font-size: 16px;
        transition: all 0.3s ease;
    }
    
    .fade-btn {
        background-color: #3498db;
        color: white;
    }
    
    .fade-btn:hover {
        background-color: #2980b9;
        opacity: 0.8;
    }
    
    .shadow-btn {
        background-color: #e74c3c;
        color: white;
    }
    
    .shadow-btn:hover {
        background-color: #c0392b;
        box-shadow: 0 4px 8px rgba(0,0,0,0.3);
    }
    
    .slide-btn {
        background-color: #2ecc71;
        color: white;
        position: relative;
        overflow: hidden;
    }
    
    .slide-btn:hover {
        color: #2ecc71;
        background-color: white;
        border: 2px solid #2ecc71;
    }
</style>
</head>
<body>
    <h2>Different Hover Effects</h2>
    <div class="button-container">
        <button class="hover-btn fade-btn">Fade Effect</button>
        <button class="hover-btn shadow-btn">Shadow Effect</button>
        <button class="hover-btn slide-btn">Color Slide</button>
    </div>
</body>
</html>
Three buttons appear with different hover effects: blue button with fade effect, red button with shadow effect, and green button with color slide effect.

Conclusion

The :hover pseudo-selector is essential for creating interactive buttons. Combined with CSS transitions, it provides smooth visual feedback that enhances user experience and makes interfaces more engaging.

Updated on: 2026-03-15T12:55:54+05:30

451 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements