Changing the active color of a button in HTML/CSS: A simple guide

Looking for some help with this code snippet:

<button type="button" class="btn btn-success" onclick="validateForm()">Book appointment</button><br><br>
<p id="submitted" style="background:black"><p>

https://i.sstatic.net/WeznQ.png https://i.sstatic.net/7RdMJ.png

The button is shown in black, and when active, it turns green. How can I customize the active color of this button? I've attempted different solutions like:

.btn.btn-success:active{
background: #933A16;
}

Or

btn.btn-success:active{
    color #933A16;
    }

Or

.btn:active{
  color: #933A16;
}

Unfortunately, none of these methods have changed the active color, which remains green. The CSS file doesn't contain any other instances of green. Any insights on how to alter this color?

Answer №1

Consider using background-color or background instead of mixing background and color. You can also try adding the !important statement before the semicolon to override bootstrap's settings.

If you want to change the active color of every button, check out Theming Bootstrap: https://getbootstrap.com/docs/4.0/getting-started/theming/#color

To modify only this single button, consider adding a custom class:

<button type="button" class="btn btn-success custom-class" onclick="validateForm()">Book appointment</button><br><br>

.custom-class:active {background-color: green !important;}

Answer №2

Following Burela's suggestion, appending '!important' will supersede Bootstrap's default settings. To customize the background color, specifically set the 'background-color' property with !important at the end within a new CSS class. Additionally, remember that 'color' is meant for font styling, not button elements.

Answer №3

  1. Specify the styles for when a button is enabled and disabled.
  2. Add an id to your button element.
<button id="btnId" ...>
  1. Within your JavaScript function, include the line below to change the button's style when clicked.
document.getElementById("btnId").className = "button disabled";

See the example code snippet below:

<html>

<head>
  <style>
    .button {
      background-color: #4CAF50;
      /* Green */
      border: none;
      color: white;
      padding: 15px 32px;
      text-align: center;
      text-decoration: none;
      display: inline-block;
      font-size: 16px;
      margin: 4px 2px;
      cursor: pointer;
    }
    
    .disabled {
      opacity: 0.6;
      cursor: not-allowed;
      background-color: black;
    }
  </style>
</head>

<body>

  <button class="button">Normal Button</button>
  <br>
  <button class="button disabled">Disabled Button</button>
  <br>
  <button id="btnId" onclick="validateForm()" class="button">Click once to disable</button>


  <script>
    function validateForm() {
      <!-- Perform form validation -->
      document.getElementById("btnId").innerHTML = "YOU CLICKED ME! I'm disabled now";
      document.getElementById("btnId").className = "button disabled";
    }
  </script>

</body>

</html>

Answer №4

If you want to modify the button color when it is selected, you can achieve this by using the “focus” option.

Here is the CSS code for styling:

<style>
/* Define default button color */
.btn-primary{
    font-family: TimeBurner;
    font-size: 25px;
    background-color: #999;
    border-color: #222222;
    margin: 3em;
    padding: 1em;
}
/* Change button color on hover */
.btn-primary:hover{
    background-color: #ba321d;
    border-color: #000000;
}
/* Modify button color when clicked on */
.btn-primary.active, 
.btn-primary:active
{
    background-color: #339933;
    border-color: #000000;
}
/* Adjust button color when tabbed to (active) */
.btn-primary:focus{
    background-color: #339933;
    border-color: #000000;
}
</style>

Implementing it on the HTML page:

<button class="btn-primary">Button1</button>
<button class="btn-primary">Button2</button>
<button class="btn-primary">Button3</button>
<button class="btn-primary">Button5</button>

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

What steps do I need to take to ensure that the slideshow is visible upon opening

I am having trouble displaying a slideshow by default on my website. I am aiming for something like this: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_slideshow (but with only 2 photos instead of 3) However, what I currently have is: image ...

Extracting information from various ports with Jquery

Here's the JavaScript code I'm currently using to retrieve data from two different localhost ports (1000 and 2000): var group1; $.get( "http://localhost:2000/api/group1", function( data ) { group1 = data; }); var group2; $.get( ...

Discovering the differences between input values in HTML when using scripts

I have a code snippet here for an HTML project. The code includes an input field for username and password. I am looking to compare the user's input with a specific value using JavaScript. My question is, what code should be included in the button cli ...

What is the method to retrieve the class name of an element when the div is created as '<div id 'one' class="element one"></div>'?

I have three elements named one, two, and three, declared as seen below: <div id =container> <div id 'one' class="element one"></div> <div id 'two' class="element two"></div> < ...

The Chrome browser on a PC is displaying the mobile version rather than the desktop version

I am currently working on a website project using bootstrap, and I ran into an unexpected issue where the mobile version of the site is being displayed in Google Chrome on my laptop instead of the desktop version. This happens even when Inspect Element is ...

The date entered in an HTML input is being interpreted as "incorrect" by the system

Upon inputting 3/15/2019 as the date and then checking the time with console.log(fromDate), the output appears as Thu Mar 14 2019 19:00:00 GMT-0500 (Central Daylight Time). Why is it not showing Fri Mar 15 2019 00:00:00? Could this be related to the tim ...

Incorporating Bootstrap 4 JavaScript into your project

I've been trying to incorporate Bootstrap's JavaScript into the main entry point of my projects, but I seem to be facing some challenges. Even though I have all the necessary dependencies (jquery and popper.js) and it appears that the JavaScript ...

Stripping initial tags from an xml string using PHP

My URL is: http://lx2.loc.gov:210/lcdb?operation=searchRetrieve&recordSchema=marcxml&version=1.1&maximumRecords=10&query=bath.isbn%3D9781594634123%0A++++ This URL returns an xml string, and I am looking to remove the opening tags of this ...

Creating an HTML list based on a hierarchical MySQL table structure

I have retrieved a hierarchical table showing different elements and their parent-child relationships as follows: id| name | parent_id | header 1 | Assets | 0 | Y 2 | Fixed Assets | 1 | Y 3 | Asset One | 2 | N 4 | ...

Toggle jQuery slide effect showing all elements and not hiding other list items

I'm utilizing this to display a sub list: function showSublist(element) { $(element).find('ul.joinus_subtext').slideToggle(); } and below is the HTML markup: <ul class="joinus"> <li onclick="showSublis ...

Ways to shift placeholder text slightly to the right within a select dropdown?

Struggling with this issue for hours, I can't seem to figure out how to: Adjust the position of the placeholder text (Search) by 10 pixels to the right in my select component Reduce the height of the field (maybe by 5px) without success. Could someo ...

The Perfect Scrollbar feature is not functioning properly in conjunction with the accordion menu

I am having some trouble with implementing the Perfect Scrollbar alongside an accordion slider menu. The scrollbar is not working as expected, and you can view the fiddle here. On initial page load, the scrollbar only appears for the first sub-menu aft ...

Using a zoom background image in an HTML document without any accompanying text

I am trying to create a zoom effect on a background image without affecting the text overlaying it. Here is the CSS code: .page1-box { width: 1200px; height:800px; overflow:hidden; } .page1 { width: 100%; height: 100%; background: ...

What is the best method for clearing an input field when it first receives focus?

I currently have two input fields: <input type="text" value="username"> <input type="password" value="password"> My goal is to remove the default value only the first time a specific input field is focused. I tried using the following JavaScr ...

Can the -webkit-tap-highlight be animated on iOS Web applications?

Is there a way to animate the application of -webkit-tap-highlight-color on links in iOS? Right now, it is applied immediately and I would like to add a CSS transition to it. Any suggestions? ...

Issues with Bootstrap sidebar and footer functionality

I need to implement a consistent footer on multiple web pages created with jQuery and bootstrap 3. Following the example provided by Bootstrap, I have written the script below to add the footer: // Default $( document ).ready(function() { $(' ...

Positioning elements on the web page using CSS tiles

Greetings! I am currently working on a website that utilizes tiles similar to this: https://i.sstatic.net/OzKQv.png Everything seems to be in place, except for one tile that is not aligning with the rest. I am struggling to figure out how to position it c ...

Is it acceptable to use JavaScript to code positioning if the standard HTML flow is not behaving as expected?

After contemplating for a long time, I have finally mustered the courage to ask this question. The behavior of HTML elements in normal flow has always bewildered me. Controlling them can be quite challenging, especially when dealing with server-side coding ...

Steps for creating a hyperlink that exclusively opens in Chrome and includes a disable flag

I am looking to create a hyperlink that will launch chrome with the flag --disable-hang-monitor, followed by opening the website https:\\randomwebsite.com. Here is the location for Google Chrome: c:\program Files(x86)\Google\Chrom ...

"Enhance Your Website with Boostrap Inputs and a Stylish

After changing my navbar to a full-width navbar, the width of my input div and ul with icons also changed. I have tried many solutions but am unable to fix the problem. I suspect that using a container to center the content in the navbar may be causing the ...