Is there a way to modify the badge class color in Bootstrap 4 using CSS?

The badge class in Bootstrap v4.3.1 is defined as follows:

.badge {
   display: inline-block;
   padding: 0.25em 0.4em;
   font-size: 75%;
   font-weight: 700;
   line-height: 1;
   text-align: center;
   white-space: nowrap;
   vertical-align: baseline;
   border-radius: 0.25rem;
   transition: color 0.15s ease-in-out, background-color 0.15s ease-in-out, 
   border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
}

To change the color of the badge, you can use the following class:

.badge-success {
   color: #fff;
   background-color: #28a745;
}

However, creating a custom color class like this:

.badge-mine {
   color: #212529;
   background-color: #808080;
}

may not work as expected when applied to a badge element:

<span class="badge badge-mine">test</span>

Even though it seems similar to the .badge-success class, there may be reasons why it does not produce the desired result.

If any CSS or Bootstrap experts could provide insight into why the badge-mine class behaves differently from badge-success, I would appreciate it.

Thank you!

Answer №1

Your implementation should work as expected. However, if your two badges appear to be identical, the issue may lie elsewhere. The badge must have the .badge-success class in order for it to inherit its styling. CSS class priority is determined based on specificity. For instance:

.badge.badge-success {
   background-color: red;
} 

will take precedence over

.badge-success {
   background-color: green
}

If you need to override this behavior, you can use !important in your CSS properties, like so:

background-color: red!important;

Answer №2

A successful implementation:

.theme-dark {
   color: #212529;
   background-color: #808080;
}

.theme-light {
   color: #ff3300;
   background-color: #0022ff;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<span class="badge theme-dark">test</span>
<span class="badge theme-light">test</span>

Answer №3

If you want to personalize the color of a bootstrap badge, you can achieve it by using the following CSS code snippet...

.red {
  background-color: red;
  color: blue !important;
}
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d9bbb6b6adaaadabb8a999ecf7eaf7eb">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous"/>

<p class="badge red">test</p>

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

Is there a way to convert basic less code into CSS using VS2012 and Web Essentials?

I'm encountering an issue with the following less code: .aa { > { li { color: red;} } } After running this in Visual Studio 2012 with the latest web essentials, I am getting a compiler error on line 2. Is this really correct? As f ...

Struggling to implement the bootstrap dropdown feature in ASP.NET

I'm currently working on adding a dropdown feature to my ASP.NET Core Application. The code for the dropdown is directly taken from the getbootstrap.com website: <li class="nav-item"> <div class="dropdown"> ...

Two miniature tables placed next to each other within a single cell, both sharing equal height

Is there a way to make two tables within a parent table cell have the same height using HTML and CSS? If one cell has more content than the other, is it possible to adjust the height of both tables accordingly? <table cellpadding="0" border="1" cell ...

Continuous Div Carousel with Looping in jQuery

I have developed a basic carousel to cycle through a series of divs, but I am facing some issues. I want the carousel to loop continuously so that after reaching "slide 07," it goes back to "slide 01." Unlike using a carousel plugin, I prefer having an ove ...

What methods are most effective for adjusting font size across mobile devices, desktop screens, and printed materials?

As I work on creating a website, I am faced with the challenge of making sure the font size is easily readable on mobile devices, desktop computers, and also when printed. I have noticed that the default font size on my desktop looks great, but is too sm ...

Tips on updating primeng Panel Menu appearance with scss

I'm looking to customize the color of my panel menu to black. Below is the HTML code I am using. <p-panelMenu styleClass="font-bold text-xs m-0 w-11" [model]="items" [multiple]='false'></p-panelMenu> ...

The CSS JSON code I have written is not having any impact on the appearance of my div

<!DOCTYPE html> <html> <head> <link rel="stylesheet" type="text/css" href="styles/common.css"> <script src="http://code.jquery.com/jquery-latest.min.js"></script> <script type='text/javascript'> ...

Changing the shape of a background using CSS when hovering

My Bootstrap navigation has a unique setup, as shown below. I've observed that many users tend to only interact with the headings and ignore the submenus where the actual products are located. To address this issue, I want to change the design of th ...

Ways to ensure a video completely fills the frame

I am experiencing an issue where the video in the main div of my home page does not fully fill the div right away - I have to refresh the page for this to happen. As a result, there is black space on the left and right sides. Interestingly enough, this pr ...

Tricky problem with z-index - how to position a CSS3 triangle under the menu

Hey there, thank you for taking the time to look into my issue. I'm currently working on a menu design that I would like to resemble this example: The key feature here is creating triangular shapes on either side and positioning them beneath the men ...

What is the best way to showcase top-rated posts from my feed of posts?

I have a basic PHP script that allows users to post without logging in. Once they submit their posts, the posts are displayed below in a feed similar to Facebook's news feed. I'm now looking to extract the three most liked posts and display them ...

After installation, the Tailwind classes seem to be malfunctioning

Let me start by mentioning that although the title of this question is reminiscent of this other question on Stack Overflow, I have already tried the solutions provided there with no success. I initially attempted to set up Tailwind using their official C ...

The HTML element seems to be missing its height parameter

My element doesn't have a set height, but it's populated with images causing the Anchor around the images to be unclickable unless I assign a fixed height to .portfolio. Any ideas on how to resolve this? HTML Snippet : <h1 class="text-c ...

Side Menu with Fixed Position Created with Tailwind CSS

Can anyone help me create a social media sidebar that stays fixed on the bottom right side of the screen, regardless of its size? Currently, the sidebar moves when I scroll down, but I want it to remain in place. How can I achieve this fixed positioning? ...

Tips for creating a CSS transition that reverses direction

Is there a way to create a hover effect where a border appears at the bottom of an element when it is being hovered over, and then retracts back when the mouse leaves the element? I've managed to get the border to appear on hover using CSS, but I&apos ...

Alignment within bootstrap using accordion

I am new to using bootstrap. Currently, I am creating a simple FAQ page with bootstrap and facing some challenges. https://i.sstatic.net/6fUW3.png I have been trying to adjust the position and margin of elements, but I am struggling to center the title a ...

The ineffectiveness of setting width to 0

When I set @media screen and (max-width: 700px), aside {width: 0}, it doesn't disappear. Even after resizing the window, it remains as null space with width: 52px. What could be causing this issue and how can I resolve it? * { margin:0; padding ...

What is the method for moving one div above or below another div using the scroll bar?

Here's the scenario I'm facing: I have rows with buttons that, when clicked, reveal a set of options. The challenge is that depending on where the row is located on the page, the settings need to open either above or below the button. When the b ...

Unable to retrieve table header information when clicking on a table cell

My code is working perfectly, except for the fact that when I click on a cell, I cannot retrieve the table header text. Retrieving the row text works fine based on the code. If I use Object.keys(data) inside the alert function to retrieve the data, it give ...

Elementor custom CSS for Wordpress header

I've been struggling to make my header transparent and then change to a colored background when scrolling down. I tried following the instructions in a video, but I couldn't find the OFFSET EFFECT setting. WEBSITE: Video: https://www.youtube.co ...