Ways to adjust the color of a button when it is in an active state by utilizing inline CSS

Can the background color and border color of a button be changed using inline css when it is clicked?

<button class="btn btn-xs btn-primary btn-inverse dropdown-toggle" type="button" data-toggle="dropdown" style="margin-top: 12px; background-color: rgba(0, 0, 0, 0); border-color:rgba(0, 0, 0, 0); margin-right:20px">

Is there a way to achieve this effect for when the button is in an active state?

Answer №1

When it comes to CSS, the :active selector is considered a CSS pseudo-class and not a DOM property or attribute. This means that there is no direct inline equivalent for it.

However, if you're looking for an alternative solution where the click event can be used, you could implement something like this...

<script>
  function toggleBg(element, color) {
    if(!color) {
      color = element.dataset.normalColor;
    } else {
      element.dataset.normalColor = element.style.backgroundColor;
    }

    element.style.backgroundColor = color;
  }
</script>

<button onmousedown="toggleBg(this,'red')" onmouseup="toggleBg(this)">RED ON PRESS</button>

It's worth noting that using inline styling or JavaScript isn't generally recommended practice. If possible, consider utilizing CSS instead:

<style>
  button:active { background: red; }
</style>

<button>RED WHEN ACTIVE</button>

Answer №2

Modifying the background color and border color of a button while it is in the active state (i.e. clicked) using inline CSS is not possible. Instead, you can utilize the pseudo-class :active within your CSS class to achieve this effect.

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

JavaScript encounters an incomplete string literal error while saving dynamic content

Although I know this question has likely been asked before, I am struggling with the error mentioned above. My goal is to create a cortina effect menu where the child div's content changes based on a variable from a drop-down menu. To test it out, I ...

Apply CSS to a dynamically generated class

My system allows users to create multiple unordered lists (<ul>) with custom class names pulled from a database. Users can add or delete these lists and assign a color to each one. While I can save the color information in the database, I am unsure h ...

Improving Material UI Responsiveness: A Comprehensive Guide

Could someone please help me resolve the overflow issue in my UI? You can view the overflow here. The second image explains the reason behind it, which you can see here. I am currently working on fixing my UI using React and Material UI. When checking the ...

Unable to provide feedback on the input elements

ISSUE: Unable to provide input in the input fields // Enable dragging for the DIV element: dragElement(document.getElementById("mydiv")); function dragElement(elmnt) { var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0; if (document.getElementB ...

Utilize CSS for defining columns within a layout

How can CSS be utilized to organize elements into columns without altering the html tag order? <ul> <li class="column-2">1</li> <li class="column-1">2</li> <li class="column-1">3</li> <li class="column- ...

Errors are being encountered with React.js Bootstrap, specifically when attempting to complete basic tasks

Today is my first time using bootstrap with React and I am facing an issue that I cannot seem to figure out. I am attempting to change the placeholder text on an input field when it is in focus, but I keep getting an error message. let ColorInput=docume ...

The PHP file on my local WAMP server seems to be having trouble retrieving inputs from the HTML's GET or POST methods

<!DOCTYPE HTML> <html> <body> <form action="testrun.php" method="GET"> Name: <input type="text" name="name"><br> E-mail: <input type="text" name="email"><br> <input type="submit"> </form> </bo ...

Is it feasible to integrate HTML (UIWebView) with Cocos2d-x?

As I dive into experimenting with cocos2d-x, I've successfully built and run the Javascript samples for Android within a browser. Now, as someone with a background in HTML, I'm eager to create my own game using HTML tags and CSS instead of relyi ...

How can I display a modal exclusively on a specific screen size?

Having a situation where two components are involved. The first component has an ng-click function that triggers a modal containing the second component. Currently, everything is working smoothly. However, the issue arises when the modal should only open a ...

PHP code not inserting the expected value into MySQL database

I'm having an issue with saving images to my server using PHP and MySQL. When I try to save the image, only the folder location appears in the database table, not the actual image name. Any suggestions on how to fix this? Here is the code I am curre ...

Is there a way to apply styling to an SVG element using code before it is displayed using React?

Rendering my SVG in React is working fine render () { return ( <object type="image/svg+xml" data="./src/img/test.svg" ref="svg"> </object> ) } I am looking to allow the user to customize the fill color by selecting ...

Utilizing bootstrap to showcase images stored in a database

Below is the code I used for the image layout: <div class="container"> <div class="row"> <div class="col-md-4 col-xs-6"> <img src="images/1.jpg" class="img-responsive img-thumbnail"> </div> ...

What is the best way to make an HTML number input display more than two decimal places?

If a user inputs 1.00, will it be automatically formatted to 1.0000? Is this feature included in the HTML 5 numeric input? ...

What is the best way to achieve this layout using HTML?

[Beginner in Web Design] I am currently experimenting with creating a layout similar to the one shown here: https://i.sstatic.net/j70FD.png Here is what I have attempted so far: .inner, .outer { position: relative; } .dash { border: 0; borde ...

Shifting and positioning the card to the center in a React application

I am currently constructing a React page to display prices. To achieve this, I have created a Card element where all the data will be placed and reused. Here is how it appears at the moment: https://i.stack.imgur.com/iOroS.png Please disregard the red b ...

Generate a unique class for each img element randomly

Is there a way to assign each image a unique random class instead of giving all the images the same random class? Any help would be appreciated. $(document.body).ready(function () { bgImageTotal = 5; randomNumber = Math.round(Math.random() * (b ...

Overflow scrollbars do not appear on flex divs

I am struggling with implementing a simple flex container that contains standard divs. The issue I am facing is that the overflow does not display a vertical scroller as desired. In order to achieve this, I have included the following CSS code snippet: #c ...

Unable to eliminate the right margin, which continues to expand as the width of the div decreases

I'm facing a peculiar issue with my HTML/CSS while working on a profile page for a game with a focus on mobile responsiveness. The right-margin seems to persist even though I've tried various solutions. In portrait mode, everything appears fine b ...

Learn how to divide a container div with multiple sub-divs into two columns using CSS

Are you up for a good challenge? In my MVC project, I have a dynamic list of divs that I want to split into two columns. The number of divs in the list is unknown. How would you go about achieving this task? EDIT: Just to clarify, when I say split, I&a ...

Using XSLT with JavaScript

I am currently working with a set of XML files that are linked to XSLT files for rendering as HTML on a web browser. Some of these XML files contain links that would typically trigger an AJAX call to fetch HTML and insert it into a specific DIV element on ...