Search code examples
htmlbox-shadow

Howto make an HTML box with shadow and pressable


I would like to make an HTML widget like this one: https://jsfiddle.net/api/mdn/

.simple {
  box-shadow: 5px 5px 5px rgba(0,0,0,0.7);
}

But I would like it to be pressable so that it flattens for 1 second and redirects to a different URL.

Edit:

I tried the following:

button {
  width: 150px;
  font-size: 1.1rem;
  line-height: 2;
  border-radius: 10px;
  border: none;
  background-image: linear-gradient(to bottom right, #777, #ddd);
  box-shadow: 5px 5px 5px rgba(0,0,0,0.7);
}

button:focus, button:hover {
  background-image: linear-gradient(to bottom right, #888, #eee);
}

button:active {
  box-shadow: inset 2px 2px 1px black,
              inset 2px 3px 5px rgba(0,0,0,0.3),
              inset -2px -3px 5px rgba(255,255,255,0.5);
}

<button>Press me!</button>

https://jsfiddle.net/z7a0v8uv/

But I don't know how to translate the content of the button to make it look like it's being pressed down.


Solution

  • I found an answer here: https://www.w3schools.com/howto/howto_css_animate_buttons.asp

    I was looking for:

    translateX(...) translateY(...)
    

    As we can see in this working example:

    https://jsfiddle.net/z7a0v8uv/1/

    Now I just need to integrate this with the right style.

    Edit:

    Here is the complete CSS code:

    button {
      width: 100%;
      border: none;
      box-shadow: 5px 5px 5px rgba(0,0,0,0.7);
    }
    
    button:focus, button:hover {
      background-image: linear-gradient(to bottom right, #888, #eee);
    }
    
    button:active {
      box-shadow: 2px 2px 2px rgba(0,0,0,0.7);
      transform: translateY(3px) translateX(3px);
    }