219

In version two I could use

badge badge-important

I see that the .badge element no longer has contextual (-success,-primary,etc..) classes.

How do i achieve the same thing in version 3?

Eg. I want warning badges and important badges in my UI

Bobby Jack
  • 14,812
  • 10
  • 59
  • 94
Peter
  • 7,551
  • 8
  • 55
  • 92
  • 1
    They should have provided `.danger`, `.success` etc. class style definitions out of the box for general use cases like this. – Fr0zenFyr Nov 14 '15 at 21:51

9 Answers9

261

Just add this one-line class in your CSS, and use the bootstrap label component.

.label-as-badge {
    border-radius: 1em;
}

Compare this label and badge side by side:

<span class="label label-default label-as-badge">hello</span>
<span class="badge">world</span>

enter image description here

They appear the same. But in the CSS, label uses em so it scales nicely, and it still has all the "-color" classes. So the label will scale to bigger font sizes better, and can be colored with label-success, label-warning, etc. Here are two examples:

<span class="label label-success label-as-badge">Yay! Rah!</span>

enter image description here

Or where things are bigger:

<div style="font-size: 36px"><!-- pretend an enclosing class has big font size -->
    <span class="label label-success label-as-badge">Yay! Rah!</span>
</div>

enter image description here


11/16/2015: Looking at how we'll do this in Bootstrap 4

Looks like .badge classes are completely gone. But there's a built-in .label-pill class (here) that looks like what we want.

.label-pill {
  padding-right: .6em;
  padding-left: .6em;
  border-radius: 10rem;
}

In use it looks like this:

<span class="label label-pill label-default">Default</span>
<span class="label label-pill label-primary">Primary</span>
<span class="label label-pill label-success">Success</span>
<span class="label label-pill label-info">Info</span>
<span class="label label-pill label-warning">Warning</span>
<span class="label label-pill label-danger">Danger</span>

enter image description here


11/04/2014: Here's an update on why cross-pollinating alert classes with .badge is not so great. I think this picture sums it up:

enter image description here

Those alert classes were not designed to go with badges. It renders them with a "hint" of the intended colors, but in the end consistency is thrown out the window and readability is questionable. Those alert-hacked badges are not visually cohesive.

The .label-as-badge solution is only extending the bootstrap design. We are keeping intact all the decision making made by the bootstrap designers, namely the consideration they gave for readability and cohesion across all the possible colors, as well as the color choices themselves. The .label-as-badge class only adds rounded corners, and nothing else. There are no color definitions introduced. Thus, a single line of CSS.

Yep, it is easier to just hack away and drop in those .alert-xxxxx classes -- you don't have to add any lines of CSS. Or you could care more about the little things and add one line.

broc.seib
  • 19,040
  • 7
  • 56
  • 58
  • 3
    this is cool as long as you dont want to use badges in group-lists, then they dont float... i can see this braking over time as bootstrap is changed. nice hack though. – nodrog Nov 12 '14 at 20:56
  • 2
    Yes, good point @nodrog. The `list-group` with `badges` is a nice out-of-the-box feature in bootstrap. For those watching, here's what that looks like: http://getbootstrap.com/components/#list-group-badges – broc.seib Nov 13 '14 at 01:39
  • 1
    I am not convined by the "cross-pollination" argument. Reusage and recombination of CSS classes/attributes is happening all the time. The hidden argument behind your term might be, that you prefer semantic css class and subclass names. It's basically "flexibility/reusability and semantical correctness". A semantical class name should support the role of the element. "label-as-badge" indicates an element transformation. -- Sidenote: The answer is basically my comment from 22 Sept with a solution to the "not so round edges on labels" problem by adding a line of css. – Jens A. Koch Nov 20 '14 at 15:19
  • My issue is specifically with "cross-pollinating alert classes with `.badge`". When you interbreed those two species, you get the visual outcome you see above. I am not speaking against the application of multiple classes on an element. Clearly CSS classes can be designed to do exactly "cross pollination" in a beautiful, orthogonal way. In that spirit, Bootstrap 3 could have provided classes for the standard colors for `.badge` and it would have fit in beautifully. – broc.seib Nov 20 '14 at 17:03
  • 3
    If you want to get the special list-group floating just add this! `.list-group-item > .label-as-badge { float: right; } .list-group-item > .label-as-badge + .label-as-badge { margin-right: 5px; }` – Gausie Mar 04 '15 at 18:58
  • 2
    Excellent answer. Might also want to throw in there the padding to match .badge as follows: `.label-as-badge { border-radius: 1em; padding: 3px 7px; }` – Matt Borja Jun 18 '15 at 18:56
  • The solution provided is semantically wrong. You must not use one element as another. Never rely on "it is almost similar" - it is open to modifications. You'd better create your own one (or two or three), inherited from the base ONE, that provided by the included library. – Nikolay Zakharov Sep 09 '15 at 21:16
  • Maybe given your update, it would be best to change `.label-as-badge` to `.label-pill`, as a sort of polyfill until users are ready to migrate to the Bootstrap 4 way of doing business? No sense maintaining your own class when you can fall back on out-of-the-box behavior after migrating... – Coderer Jul 25 '16 at 09:06
255

In short: Replace badge-important with either alert-danger or progress-bar-danger.

It looks like this: Bootply Demo.


You might combine the CSS class badge with alert-* or progess-bar-* to color them:

With class="badges alert-*"

  <span class="badge alert-info">badge</span> Info
  <span class="badge alert-success">badge</span> Success 
  <span class="badge alert-danger">badge</span> Danger   
  <span class="badge alert-warning">badge</span> Warning

Alerts Docu: http://getbootstrap.com/components/#alerts

With class="badges progress-bar-*" (as suggested by @clami219)

  <span class="badge progress-bar-info">badge</span> Info
  <span class="badge progress-bar-success">badge</span> Success
  <span class="badge progress-bar-danger">badge</span> Danger
  <span class="badge progress-bar-warning">badge</span> Warning

Progress-Bar Docu: http://getbootstrap.com/components/#progress-alternatives

Jpsy
  • 17,245
  • 6
  • 99
  • 99
Jens A. Koch
  • 34,456
  • 12
  • 100
  • 117
  • 4
    I didn't like the way the alert was decorating the badges, so I opted to use labels instead. http://getbootstrap.com/components/#labels – Mike Purcell Apr 17 '14 at 00:25
  • 6
    This is a bit of a hack-ish answer. Sure it works, and it's simple. But you're supposed to use those contextual alert classes with alerts. You may be signing yourself up for broken styles in the future. @JasonHuang's answer is just as simple, and provides you with greater control. – mikesigs May 21 '14 at 04:23
  • You have a point @mikesigs but I guess we could simply make a css class for all the web colours and add that to our class attribute too. There are 3 excellent answers here and all deserve consideration. I think also that bootstrap 3 changed the css so dramatically from v2 that there really isn't any notion of future-proofing your styles unless you roll your own anyway. – Peter May 25 '14 at 08:53
  • 2
    What prompted me to make the above reply was that Visual Studio Web Essentials shows a warning for the code above. Namely, "When using 'alert-danger', you must also specify the class 'alert'." – mikesigs May 26 '14 at 20:26
  • @MaxFavilli it's either a button or a badge. never both. if you just want to colorize your button using an alert-color use: `Button with Alert Color` . buttons have own colors via `btn-danger`, `btn-info` etc. – Jens A. Koch Sep 22 '14 at 14:00
  • What I wanted is – Max Favilli Sep 22 '14 at 14:30
  • Inline Badges. I believe they are white or grey. The colors do not have an important flag on them. So they don't override. You could propably switch to a label on the button inside - not so "round" but should work: http://jsfiddle.net/4ojavw7e/ – Jens A. Koch Sep 22 '14 at 17:20
  • Aaaah!! No!! When you cross-pollinate classes like this, you throw out design considerations that the bootstrap folks made. See the illustration in my answer below. I propose a very simple one-line CSS solution that keeps the bootstrap design intact. – broc.seib Nov 04 '14 at 18:32
  • 3
    People land on this page, because the bootstrap folks made a lot of design considerations, which throw out design considerations they did before :) - I'm just recombined existing css classes, in order to get close to the former "badge-important". Here we go with the stuff they give use out of the box: that's it. I don’t think there is one true way to craft CSS. This page offers lots of ways to solve the issue. But please stop shouting "Aaah! No! This is not the way! you are doing something wrong". – Jens A. Koch Nov 20 '14 at 15:18
  • 1
    @Jens-AndréKoch you're right about my "Aaah!! No!!". It _does_ sound like I'm telling people "you're doing it wrong", and I have no place to say that. Sorry. – broc.seib Nov 20 '14 at 17:06
  • Wanted to add a comment with another similar alternative but character limit forced me to post a [new answer](http://stackoverflow.com/a/33713484/1369473). I'd love to read your comments. – Fr0zenFyr Nov 14 '15 at 21:45
45

Bootstrap 3 removed those color options for badges. However, we can add those styles manually. Here's my solution, and here is the JS Bin:

.badge {
  padding: 1px 9px 2px;
  font-size: 12.025px;
  font-weight: bold;
  white-space: nowrap;
  color: #ffffff;
  background-color: #999999;
  -webkit-border-radius: 9px;
  -moz-border-radius: 9px;
  border-radius: 9px;
}
.badge:hover {
  color: #ffffff;
  text-decoration: none;
  cursor: pointer;
}
.badge-error {
  background-color: #b94a48;
}
.badge-error:hover {
  background-color: #953b39;
}
.badge-warning {
  background-color: #f89406;
}
.badge-warning:hover {
  background-color: #c67605;
}
.badge-success {
  background-color: #468847;
}
.badge-success:hover {
  background-color: #356635;
}
.badge-info {
  background-color: #3a87ad;
}
.badge-info:hover {
  background-color: #2d6987;
}
.badge-inverse {
  background-color: #333333;
}
.badge-inverse:hover {
  background-color: #1a1a1a;
}
Nathan Tuggy
  • 2,239
  • 27
  • 28
  • 36
Adams.H
  • 1,430
  • 2
  • 19
  • 28
25

The context classes for badge are indeed removed from Bootstrap 3, so you'd have to add some custom CSS to create the same effect like...

.badge-important{background-color:#b94a48;}

Bootply

Zim
  • 296,800
  • 77
  • 616
  • 554
  • 20
    Why did they get rid of them? – Peter Sep 11 '13 at 04:44
  • 4
    +! I'm also wondering why they got rid of them. You can use label but it doesn't line up properly on a pill container. At least it's not centered vertically. – cbmeeks Oct 13 '13 at 03:39
  • 2
    lame. the up migration from 2 to 3 is a complete pain. – Kevin Feb 20 '14 at 15:28
  • The answer by Jens-André Koch is a Bootstrap 3.0 only solution. I think this is the right way? – Josh Petitt Mar 08 '14 at 03:13
  • 2
    @Peter: See https://github.com/twbs/bootstrap/pull/6342. TLDR: badges are intended to be used as 'unread' counters, and shouldn't be used to convey other statuses. – Bobby Jack Mar 31 '14 at 13:16
  • yes fair enough but its nice to say 3 unimportant unread items and 2 unread important items – Peter Mar 31 '14 at 21:33
20

Another possible way, in order to make the colors a bit more intense, is this one:

<span class="badge progress-bar-info">10</span>
<span class="badge progress-bar-success">20</span>
<span class="badge progress-bar-warning">30</span>
<span class="badge progress-bar-danger">40</span>

See Bootply

clami219
  • 2,710
  • 1
  • 25
  • 39
18

If using a SASS version (eg: thomas-mcdonald's one), then you may want to be slightly more dynamic (honor existing variables) and create all badge contexts using the same technique as used for labels:

// Colors
// Contextual variations of badges
// Bootstrap 3.0 removed contexts for badges, we re-introduce them, based on what is done for labels
.badge-default {
  @include label-variant($label-default-bg);
}

.badge-primary {
  @include label-variant($label-primary-bg);
}

.badge-success {
  @include label-variant($label-success-bg);
}

.badge-info {
  @include label-variant($label-info-bg);
}

.badge-warning {
  @include label-variant($label-warning-bg);
}

.badge-danger {
  @include label-variant($label-danger-bg);
}

The LESS equivalent should be straightforward.

PowerKiKi
  • 3,792
  • 4
  • 35
  • 45
5

Like the answer above but here is using bootstrap 3 names and colours:

/*css to add back colours for badges and make use of the colours*/
.badge-default {
  background-color: #999999;
}

.badge-primary {
  background-color: #428bca;
}

.badge-success {
  background-color: #5cb85c;
}

.badge-info {
  background-color: #5bc0de;
}

.badge-warning {
  background-color: #f0ad4e;
}

.badge-danger {
  background-color: #d9534f;
}
jumptiger13
  • 51
  • 1
  • 3
2

When using the LESS version you can import mixins.less and create your own classes for colored badges:

.badge-warning {
    .label-variant(@label-warning-bg);
}

Same for the other colors; just replace warning with danger, success, etc.

ThiefMaster
  • 285,213
  • 77
  • 557
  • 610
2

Well, this is a terribly late answer but I think I'll still put my two cents in... I could have posted this as a comment because this answer doesn't essentially add any new solution but it does add value to the post as yet another alternative. But in a comment I wouldn't be able to give all the details because of character limit.

NOTE: This needs an edit to bootstrap CSS file - move style definitions for .badge above .label-default. Couldn't find any practical side effects due to the change in my limited testing.

While broc.seib's solution is probably the best way to achieve the requirement of OP with minimal addition to CSS, it is possible to achieve the same effect without any extra CSS at all just like Jens A. Koch's solution or by using .label-xxx contextual classes because they are easy to remember compared to progress-bar-xxx classes. I don't think that .alert-xxx classes give the same effect.

All you have to do is just use .badge and .label-xxx classes together (but in this order). Don't forget to make the changes mentioned in NOTE above.

<a href="#">Inbox <span class="badge label-warning">42</span></a> looks like this:

Badge with warning bg

IMPORTANT: This solution may break your styles if you decide to upgrade and forget to make the changes in your new local CSS file. My solution for this challenge was to copy all .label-xxx styles in my custom CSS file and load it after all other CSS files. This approach also helps when I use a CDN for loading BS3.

**P.S: ** Both the top rated answers have their pros and cons. It's just the way you prefer to do your CSS because there is no "only correct way" to do it.

Community
  • 1
  • 1
Fr0zenFyr
  • 1,781
  • 2
  • 22
  • 42