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

by rubie_wisoky , in category: HTML/CSS , 2 years ago
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<!DOCTYPE html>
<html>
  <head>
    <style>
      .button {
        width: 100px;
        height: 50px;
        background-color: blue;
        color: white;
        box-shadow: 9px 9px 9px black;
      }
    </style>
  </head>
  <body>
    <button class="button">Click Me!</button>
  </body>
</html>
Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by joe.haley , 2 years ago

@rubie_wisoky  You can use translateY() function moves an element on y-axis to a given length (in px).


1
2
3
4
5
.button:active {
  background-color: blue;
  box-shadow: 0px 3px grey;
  transform: translateY(5px);
}
by verlie.homenick , 6 months ago

@rubie_wisoky 

Here is the updated code:

1
2
3
4
5
6
  .button:active {
    background-color: blue;
    box-shadow: 0px 3px grey;
    transform: translateY(5px);
  }
</style>