How to Add Shadow to a Button in CSS

Introduction

Adding shadows to buttons can create depth and make buttons stand out, enhancing the overall user interface design. The box-shadow property in CSS allows you to add shadow effects around the button, giving it a more 3D-like appearance.

In this tutorial, you'll learn how to apply shadows to a button using the box-shadow property. We will also include a hover effect to further enhance the button’s interactivity.

Problem Statement

Create a CSS code that:

  • Styles a button with a shadow.
  • Adds hover effects to make the shadow more interactive.

Example:

  • Input: A button element with the text "Shadow Button".
  • Output: A button with shadow that changes when hovered.

Solution Steps

  1. Use the <button> Element: Create the button in HTML.
  2. Apply Shadow to the Button with CSS: Use the box-shadow property to add shadow.
  3. Add a Hover Effect: Use the :hover pseudo-class to change the shadow on hover.

HTML Structure

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Button with Shadow</title>
    <style>
        /* Step 1: Style the button with shadow */
        .shadow-button {
            font-size: 1.5rem;
            color: white;
            background-color: #3498db; /* Initial background color */
            padding: 10px 25px;
            border: none;
            border-radius: 5px; /* Rounded corners */
            cursor: pointer;
            transition: box-shadow 0.3s ease; /* Smooth shadow transition */
            box-shadow: 2px 4px 6px rgba(0, 0, 0, 0.2); /* Initial shadow */
        }

        /* Step 2: Add hover effect to change shadow */
        .shadow-button:hover {
            box-shadow: 4px 8px 12px rgba(0, 0, 0, 0.3); /* Larger shadow on hover */
        }

        /* Center the button */
        .container {
            text-align: center;
            margin-top: 100px;
        }
    </style>
</head>
<body>

    <div class="container">
        <button class="shadow-button">Shadow Button</button>
    </div>

</body>
</html>

Output

You can play with the above HTML in Online HTML Editor and Compiler. Here is the output of the above HTML page.:

Explanation

Step 1: Apply Shadow to the Button

The .shadow-button class is used to style the button and add a shadow effect using the box-shadow property.

.shadow-button {
    font-size: 1.5rem;
    color: white;
    background-color: #3498db; /* Blue background */
    padding: 10px 25px;
    border: none;
    border-radius: 5px; /* Rounded corners */
    cursor: pointer;
    transition: box-shadow 0.3s ease; /* Smooth shadow transition */
    box-shadow: 2px 4px 6px rgba(0, 0, 0, 0.2); /* Initial shadow */
}
  • box-shadow: 2px 4px 6px rgba(0, 0, 0, 0.2): This property creates a shadow behind the button. The shadow is defined as:
    • 2px: Horizontal offset of the shadow.
    • 4px: Vertical offset of the shadow.
    • 6px: Blur radius of the shadow.
    • rgba(0, 0, 0, 0.2): Shadow color in RGBA format (a slight black shadow with 20% opacity).

Step 2: Add Hover Effect

When the button is hovered, the shadow becomes larger and more prominent.

.shadow-button:hover {
    box-shadow: 4px 8px 12px rgba(0, 0, 0, 0.3); /* Larger shadow on hover */
}
  • box-shadow: 4px 8px 12px rgba(0, 0, 0, 0.3): On hover, the shadow expands, making the button appear lifted. The offsets and blur radius increase for a more pronounced effect.

Step 3: Center the Button

To center the button in the middle of the page, use the following CSS in the .container class:

.container {
    text-align: center;
    margin-top: 100px;
}
  • text-align: center: Centers the button horizontally.
  • margin-top: 100px: Adds space above the button to create separation from the top of the page.

Conclusion

Adding a shadow to a button using the box-shadow property is a simple way to make buttons stand out. By adjusting the shadow’s size and blur radius, you can control how pronounced the effect is. Adding a hover effect with a larger shadow further enhances the interactivity and makes the button more visually appealing.

Comments