Why does a button continue to change its background color despite setting its styles for :focus and :active states?

Here is the Less code snippet I am using:

.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 apply the

<button class='btn'>button</button>
, it functions as expected. However, when I use
<button class='btn disabled'>button</button>
, the background-color still changes upon clicking.

This strange behavior only occurs while using mobile emulation.

The browser user agent for desktop 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"
.

While simulating on mobile, the user agent is

"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"
.

Here is the compiled CSS generated from the Less code:

.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
}

Answer №1

application:

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

Answer №2

To achieve the desired effect of a disabled button with custom styling, you can use the following CSS code:

.btn[disabled] { background-color: #9ba6ae; }

By applying this style rule, the button will appear unresponsive to user interaction and will be visually styled as needed. For more detailed information on customizing disabled form elements, you can visit this website.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I position a Bootstrap dropdown textbox or menu directly beneath its button?

Currently, I am working on implementing a simple dropdown textbox for a button within a navbar. However, I am encountering an issue where the text does not drop below the button as expected. Initially, it was displaying to the left, obstructing the brand l ...

the mystical powers of z-index

I am working on a typical menu structure (ul>li>a) and my goal is to add background effects like shadow and gradient into an <li> element (ul>li>.shadow-bg-white). The challenge I'm facing is that when hovering over the items from ri ...

What is the best way to center-align an image using CSS?

Take a look at this sample: http://fiddle.jshell.net/ Here is the HTMl code snippet: <div class="wrap"> <div class="wrap1"> <div class="image1">This is some text</div> <div class="image2">This is som ...

What could be causing this JavaScript/CSS animation to only function on the initial instance and not the subsequent ones?

I've been attempting to apply this transition effect to multiple circles, but only the first one triggers it no matter where they are positioned on the page. javascript let circles = document.querySelectorAll('.circle') circles.forEach(cir ...

What is the best approach for implementing a CSS reset across multiple websites with different content?

As I work on a page that will be displayed across numerous websites beyond my control, shown within a jQuery UI dialog window using $.load(), the content is vulnerable to the CSS directives set by each individual site. To prevent conflicts, I have made sur ...

Seeking a way to display a random div at a 1% rate without generating additional div elements

Searching for an answer to display only one div with a rate of 1/100. Currently, I am utilizing the following JavaScript: var random = Math.floor(Math.random() * $('.item').length); $('.item').hide().eq(random).show(); This method wor ...

Positioning background images in Bootstrap CSS with fixed position

Currently, I am developing a website using bootstrap, HTML, and CSS. The structure of the HTML code is as follows: wrapper for fixed footer at bottom { content fixed bg content } The overall wrapper has relative positioning. The fixed background is ...

Instructions for creating a select box with checkboxes:

I am currently working on incorporating a Selectbox that includes checkboxes within the selectbox itself, similar to the image shown below. However, I am encountering an issue where clicking on the select box does not open the checkboxes from another div. ...

How can I ensure that my Overlay doesn't obstruct my dropdown content?

I'm trying to create a dropdown in my fixed navbar. When I click the open button, I want the overlay to cover everything except the dropdown content. Even after setting the z-index of my dropdown content to 10000, it still didn't work. $("#dr ...

Bold font border

I am looking to enhance the appearance of text with a thick outline. After coming across a trick, I found the following code snippet: text-shadow: -1px -1px 0 #00f, 1px -1px 0 #00f, -1px 1px 0 #00f, 1px 1px 0 #00f; However, I noticed that ...

Can the card overlay slide appear solely on top of the image?

I am trying to create a gallery section of cards using Bootstrap 4 where I want the user to hover over each card and have an overlay slide or fade in, covering only the image. Currently, the overlay slides in over the entire card instead. I have been stru ...

Issue with jQuery: CSS variable causing container misalignment

Could someone please explain why this code is not functioning correctly? var leftPos = (10 * 20) + "px"; function setPosition() { $("#Position5").css('left','(' + leftPos + ')'); } window.onload = function() { setPositi ...

Modifying Link Hover Colors with CSS and PHP

One of the challenges I am facing is managing a file that contains an array of navigation links. This setup allows me to easily add new links to the navigation menu without manually updating multiple files. However, I am struggling with assigning different ...

Steps for removing the footer from WordPress with the "powered by WordPress" message:

I am curious about how to delete the footer that says "powered by wordpress". I created this page and at the bottom of it, there is a mention of "powered by wordpress". Website ...

Is there a more efficient approach to styling with CSS?

Here is the CSS I am currently using: .navbar-inverse .nav > li > youtube-dialog > a { color: #999999; text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.25); padding: 10px 15px 10px; display: block; text-decoration: none; /*underline*/ } ...

Achieving consistent hover effects in both Firefox and Chrome with CSS

Currently, I am faced with a dilemma regarding some CSS hover effects and table formatting. Despite successfully implementing most aspects of the design, there is an issue with how different browsers interpret padding within elements during color changes o ...

CSS: Creating a block that stretches the entire width of its parent block

I've been facing the same issue multiple times. Whenever I use the float property to display one block, the next block ends up overlapping with the first one. For example: Consider the following block of code: .row_item{ width: 30%; display: block; ...

The contents are not visible when using <h> and <p> tags

As I am embarking on creating a whimsical art profile just for the pleasure of it, I've already got a banner and a navbar set up. However, when I tried to enter some text: Welcome to my art profile. This online gallery showcases all of ...

Adjusting the Aspect Ratio of an Embedded YouTube Video

<!DOCTYPE HTML> <head> <style> body { overflow: hidden; margin:0; } </style> </head> <body> <iframe id="video" src="https://www.youtube.com/embed/U4c9bBeUe4M?modestbranding=1&sh ...

Switching effortlessly between Fixed and Relative positioning

As I work on creating a unique scrolling experience, I aim to have elements stop at specific points and animate before returning to normal scroll behavior once the user reaches the final point of the page. Essentially, when div X reaches the middle of the ...