Using CSS transform property In CSS

We can use the CSS transform property to add a pressed effect on the button when it is active. CSS transform property allows us to scale, rotate, move, and skew an element.

Example: In this example, we have used CSS transform property

html




<!DOCTYPE html>
<html>
 
<head>
    <style>
        /* Adding some basic styling to button */
         
        .btn {
            text-decoration: none;
            border: none;
            padding: 12px 40px;
            font-size: 16px;
            background-color: green;
            color: #fff;
            border-radius: 5px;
            box-shadow: 7px 6px 28px 1px rgba(0, 0, 0, 0.24);
            cursor: pointer;
            outline: none;
            transition: 0.2s all;
        }
        /* Adding transformation when the button is active */
         
        .btn:active {
            transform: scale(0.98);
            /* Scaling button to 0.98 to its original size */
            box-shadow: 3px 2px 22px 1px rgba(0, 0, 0, 0.24);
            /* Lowering the shadow */
        }
    </style>
</head>
 
<body>
 
    <!-- Button with a class 'btn' -->
    <button class="btn">Button</button>
 
</body>
 
</html>


Output:

How to add a pressed effect on button click in CSS?

In this tutorial, we are going to learn how to add a pressed effect on a button using CSS. This effect is a part of modern UI design and is used on many websites. This effect allows the user to experience an interaction with the button element as compared to the normal behavior.

We’ll take advantage of the active pseudo class. This class is added to an HTML element automatically when it is clicked.

Table of Content

  • Using CSS transform property
  • Using translate function

Similar Reads

Method 1: Using CSS transform property

We can use the CSS transform property to add a pressed effect on the button when it is active. CSS transform property allows us to scale, rotate, move, and skew an element....

Method 2: Using translate function

...