In version two I could use
badge badge-important
I see that the .badge element no longer has contextual (-success,-primary,etc..) c
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>
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>
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>
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>
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:
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.
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
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
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;
}
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.
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;}