21

I have a button with a box-shadow that makes it look like it's floating, and I would like to make a pressing effect when I click on it:

Code(CSS):

#startBtn
{
    font-family: OpenSans;
    color: #FFFFFF;
    background-color: #00FF7C;
    border: 1px solid #00FF7C;
    border-radius: 5px;
    box-shadow: 0px 5px 0px #00823F;
}

Code(HTML):

<input type="button" id="startBtn" value="Let's begin">

Screenshot:

Button

m4n0
  • 27,411
  • 26
  • 71
  • 84
Jojo01
  • 1,189
  • 3
  • 12
  • 33
  • Just look at this example: [https://www.w3schools.com/code/tryit.asp?filename=GJKQEYQ8AXA8](https://www.w3schools.com/code/tryit.asp?filename=GJKQEYQ8AXA8). –  Oct 10 '20 at 17:06

3 Answers3

45

Use :active pseudo class and change the box-shadow vertical offset value. Adjust the top value for the activated element with respect to the relatively positioned input with absolute parent.

.button {
  position: absolute;
}
#startBtn {
  font-family: OpenSans;
  color: #FFFFFF;
  background-color: #00FF7C;
  border: 1px solid #00FF7C;
  border-radius: 5px;
  box-shadow: 0px 5px 0px #00823F;
  position: relative;
  top: 0px;
  transition: all ease 0.3s;
}
#startBtn:active {
  box-shadow: 0 3px 0 #00823F;
  top: 3px;
}
<div class="button">
  <input type="button" id="startBtn" value="Let's begin">
</div>
m4n0
  • 27,411
  • 26
  • 71
  • 84
6

IF the buttoms have a fixed height (as it looks) I would wrap the buttom into a div with that height and set the button to position absolute to create the right effect (moving it down) with:

#startBtn:active {
  box-shadow: 0 1px 0 #00823F;
    bottom:-4px;
}

JSFIDDLE

Alvaro Menéndez
  • 8,499
  • 3
  • 34
  • 54
0

You can use the CSS hover property:

#startBtn:hover { 
  /* your css code here */ 
}

Another option is to use button generators like buttongarage.in to generate your code for the button and the hover effect.

Sebastian Simon
  • 16,564
  • 7
  • 51
  • 69
Siddu
  • 1