Changing the text color of an active button in Bootstrap

Hello everyone, I've been struggling to find a solution to my issue and I could really use some help.

The problem I'm facing is changing the text color of the active button in Bootstrap. Specifically, after clicking on a button, I want it to transition from white to a green hue, but I can't seem to change that color. Any suggestions on how I can achieve this?

I attempted the following CSS code:

.button:focus, .button.active, .button.active:focus {
    color: white !important;
}

Unfortunately, this approach did not yield the desired result.

(Also, if anyone knows how to input code without triggering "run code" but still have proper color formatting, please share with me.)

Below is the code snippet:

HTML:

<!DOCTYPE html>

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootswatch/3.3.5/darkly/bootstrap.min.css" rel="stylesheet" />
    <link href="https://cdnjs.cloudflare.com/ajax/libs/bootswatch/3.3.5/darkly/bootstrap.min.css" rel="stylesheet" />
    <link rel="stylesheet" type="text/css" href="css/style.css" />
</head>

<html lang="en">
    
    <div class="navbar navbar-default navbar-custom navbar-fixed-top">
        <div class="collapse navbar-collapse">
            <ul class="nav navbar-nav" id="dropdowns">
                <a class="navbar-brand" id="brand">Traders: </a>
                <li class="dropdown">
                    <a class="dropdown-toggle" aria-expanded="false">General <span class="caret"></span></a>
                    <ul class="dropdown-menu pull-left" role="menu">
                        <li><a href="#medical">Medical</a></li>
                        <li><a href="#utility">Utility</a></li>
                        <li><a href="#supplies">Supplies</a></li>
                        <li><a href="#banker">Banker</a></li>
                    </ul>
                </li>
                
                <li class="dropdown">
                    <a class="dropdown-toggle" aria-expanded="false">Vehicles <span class="caret"></span></a>
                    <ul class="dropdown-menu pull-left" role="menu">
                        <li><a href="#aircraft">Aircraft </a></li>
                        <li><a href="#vehicle">Vehicle </a></li>
                    </ul>
                </li>
                
                <li class="button"><a href="#blackmarket">Black Market</a></li>
                
                <li class="button"><a href="#wholesaler">Wholesaler</a></li>
                
                <li class="button"><a href="#hero">Hero</a></li>
                
                <li class="button"><a href="#bandit">Bandit</a></li>
            </ul>
        </div>
    </div>
    
    <body></body>
    
</html>

CSS:

@media {
    ul.nav li.dropdown:hover > ul.dropdown-menu {
        display: block;    
    }
    
    .navbar-custom {
        color: #262626;
        background-color: #262626;
    }
    
    .button:hover a{
        color: white !important;
    }
    
    .navbar-default {
        color: #262626;
        background-color: #262626;
        border-top: 4px solid red;
    }
    
    .navbar-default .navbar-brand:hover {
        color: white;
    }
     
    .dropdown:hover a{
        color: white !important;
    }
    
    .dropdown-menu > li > a:hover {
        background-color: #3d3d3d;
    }
    
}

body {
    background-color: black;
}

#navbar {
    width: 100%;
}

Answer №1

When styling buttons in CSS, there are various pseudo-selectors that can be utilized to achieve different effects. For instance:

:active is used to apply a background color only when the button is clicked, without persisting.

:focus will maintain the background color as long as the button is in focus.

http://jsfiddle.net/zakCa/1017/

.button:focus
{
    font-size: 13px;
    color:orange;
}

Answer №2

If you want to customize the look of a specific button, you can use CSS to override its default styling. Here is an example of how you can accomplish this:

HTML

<button class="btn-success btn">
  Change My Color
</button>

CSS

.btn-success:active,
.btn-success.active {
  color: blue !important;
}

By using this code snippet, you can change the color of a button by overriding Bootstrap's default styles.

JSFiddle

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

Tips for splitting a word in the uib-typeahead menu

I have successfully implemented a uib-typeahead dropdown in my AngularJS application where I display two attributes. The API is called based on the first name, and the response contains an object with both first name and last name. However, currently, they ...

Include a clickable jQuery icon in an HTML document for testing purposes

Is there a way to include an icon [+] at the end of this statement, so that when clicked it triggers an alert? <div id="catalog" style="width: 518px;"> <ul id="catalog111" style="left: -37px;top: -140px;width=500%;"> < ...

What could be the reason the background-color isn't changing when I apply button:hover?

body{ font-family:"Arial","Lucida Grande", "Lucida Sans Unicode", Verdana, Helvetica, sans-serif; font-size:12px; } p, h1, form, button{border:0; margin:0; padding:0;} .spacer{clear:both; height:1px;} /* ----------- My Form ----------- */ .myform{ margin:0 ...

What could be causing the uneven application of CSS padding when it is applied to a div that serves as the parent of an HTML form?

Padding has been added only to the top and bottom of a div. The padding attribute is displaying consistently for the form element, however, it is not behaving uniformly for the parent div of the form as illustrated in the image provided below. Output:- h ...

Ensuring DIV Fills without Compromising Aspect Ratio

I am currently attempting to fill a div with an image in both width and height. However, I have only been able to control the width while the height respects the aspect ratio without filling the div completely. I have tried various combinations of backgrou ...

Adjusting background position using incremental changes through JavaScript

Although I have a strong background in PHP coding, using javascript is a new venture for me so I hope this question doesn't sound naive. My objective is to create a button that, when clicked, shifts the background-position of a specified DIV object b ...

Using JSTL tags may result in returning null or empty values when making Javascript calls or populating HTML

This strange error is puzzling because it appears on some of the webpages I create, but not on others, even though the elements are exactly the same. For instance, this issue does not occur: <main:uiInputBox onDarkBG="${hasDarkBG}" name="quest ...

Quicker loading times for background images when creating PDFs using wkhtmltopdf

As I work on generating a 5-page PDF from my HTML file using wkhtmltopdf, everything seems to be running smoothly. However, I've encountered an issue when it comes to the time it takes to complete this task, especially when a background image is inclu ...

The values in my array are causing it to output NAN

I'm currently working on a program that aims to combine a variable number of one-row matrices with varying lengths. The goal is to add the elements of each matrix together, where element one of array one adds to element one of array two, and so forth. ...

Ensure text within list items is positioned properly and not obscured by any decorative elements

I'm struggling to create top-bottom borders for list items with customized decorations. The text of the element should not be hidden under the decoration even if it's too long. Any ideas on how to achieve this? div { width: 20%; } ul ...

Utilize the .mat-column-name attributes to apply custom styles to a nested component within Angular Material

Within the Child component, an Angular material table is created with columns passed as input: <table mat-table> <ng-container *ngFor="let col of columns" [matColumnDef]="col"> </table> @Input() columns: string[] T ...

Issues arise when using ng-repeat in conjunction with ng-click

I am facing some new challenges in my spa project with angularjs. This is the HTML snippet causing issues: <a ng-repeat="friend in chat.friendlist" ng-click="loadChat('{{friend.friend_username}}')" data-toggle="modal" data-target="#chat" d ...

Troubleshooting the compatibility issue between Bootstrap file upload and ASP Ajax AsyncFileUpload

Currently, I am encountering an issue with my ASP web form that involves using Bootstrap alongside the Ajax AsyncFileUpload control. The specific code for the file upload control is as follows: <div id="LoafFileUploadTemplate" class="cust ...

Is there a way to allow an HTML page rendered by node.js to communicate back to its corresponding node.js file?

I am currently in the process of developing a registry system using node.js and HTML. However, I have encountered an issue where my HTML page is rendered by node.js, but when trying to call it back to the node.js file, it appears that the JS file cannot be ...

The front page is excessively tall, lacking any rationale for its lengthy design

I have noticed that my website, located at , is unexpectedly long. I am puzzled as to what could be causing this issue and although I know how to correct it, I am curious to find out what exactly is making the page scroll so far down. It's quite stra ...

Update the division by clicking the button with a randomly generated JavaScript string element

Trying to solve a unique problem here as none of the proposed solutions on similar questions have worked for me. Apologies if I'm missing something, I'm new at this. The issue is with loading an empty div on my page using javascript and strings ...

Do additional MySql tables have an impact on the speed of searches within a MySql database?

I am in the process of considering a database redesign for my classifieds website. Currently, I have 7 different tables in the database, each corresponding to a "MAIN CATEGORY". For instance, there is a "VEHICLES" table that contains information on variou ...

Utilizing jquery for displaying and hiding notifications - Notyfy in action

Recently, I've started working with jQuery and have come across the useful Notyfy jQuery plugin. My goal is to display a notification bar with specific messages based on certain conditions such as success or error. I'm trying to achieve somethin ...

Having trouble with your website not adjusting properly when resizing the browser or viewing it on a mobile phone

My website is not adapting properly to different screen sizes, even after I have added the META tags below. Any advice would be appreciated. Thank you. HTML <!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="UTF-8> ...

Comparing HTML5 Drag and Drop to jQuery UI Drag and Drop: What's the Difference

In the ever-evolving world of web development, what is the most effective method for creating a drag and drop builder in 2017? While this question has been posed in the past (as seen here), technology changes rapidly. Is HTML5 still the go-to option, or ha ...