htmlcsstoggle

How to make HTML button look pressed in using css?


How do I style a button, with a shadow, so that it looks like it is pressed in?

I tried using box-shadow: ... ;. But this didn't have any affect.


Solution

  • By creatively styling the :active or :focus pseudo classes using a box-shadow: inset ...;

    Using the :active pseudo class:

    button {
      background: #ededed;
      border: 1px solid #ccc;
      padding: 10px 30px;
      border-radius: 3px;
      cursor: pointer;
    }
    
    button:active {
      background: #e5e5e5;
      -webkit-box-shadow: inset 0px 0px 5px #c1c1c1;
         -moz-box-shadow: inset 0px 0px 5px #c1c1c1;
              box-shadow: inset 0px 0px 5px #c1c1c1;
       outline: none;
    }
    <button>
      Click me
    </button>

    Using the :focus pseudo class:

    button {
      background: #ededed;
      border: 1px solid #ccc;
      padding: 10px 30px;
      border-radius: 3px;
      cursor: pointer;
    }
    
    button:focus {
      background: #e5e5e5;
      outline: none;
      -webkit-box-shadow: inset 0px 0px 5px #c1c1c1;
         -moz-box-shadow: inset 0px 0px 5px #c1c1c1;
              box-shadow: inset 0px 0px 5px #c1c1c1;
    }
    <button>
      Click me
    </button>