Search code examples
cssbuttonlesspseudo-class

why a button still change it background color even I set its :focus :active style?


My Less code is below:

.btn(@width: 80px, @height: 36px, @radius, @font-size: 14px, @font-color, @color) {

    ...

    background-color: @color;
    &:focus, &:active {
        background-color: darken(@color, 10%);
        outline: none;
    }
}
.btn {
    .btn(5.8rem, 2.6rem, 0.5rem, 1.2rem, #fff, @c-blue);
}
.btn.disabled {
    background-color: @c-gray;
    &:focus, &:active {
        background-color: @c-gray;
    }
}

When I use <button class='btn'>button</button>, it will behave as expected. But if I use <button class='btn disabled'>button</button>, when I click the button, it still change background-color.

It's strange! And it only occurs when I use mobile emulation.

Browser is "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/37.0.2062.120 Chrome/37.0.2062.120 Safari/537.36".

And when simulating, "Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X; en-us) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53".

The compiled CSS:

.btn {
    display: inline-block;
    width: 5.8rem;
    height: 2.6rem;
    line-height: 2.6rem;
    font-size: 1.2rem;
    color: #fff;
    text-align: center;
    background-color: #4099de;
    border-radius: .5rem;
    border: none;
    cursor: pointer
}
.btn:focus, .btn:active {
    background-color: #2380c8;
    outline: none
}
.btn.disabled {
    background-color: #9ba6ae
}
.btn.disabled:focus, .btn.disabled:active {
    background-color: #9ba6ae
}

Solution

  • use:

    .btn {
        -webkit-tap-highlight-color: rgba(0,0,0,0);    
    }