Professional Documents
Culture Documents
TY Bootstrap24h H06LabelsWellsJumbotron
TY Bootstrap24h H06LabelsWellsJumbotron
There are lots of ways you can adjust how your website looks in
Bootstrap. And the labels, badges, panels, wells, and Jumbotron give you
specific tools for designing different sections of your web pages. These
are all Bootstrap components to give your site more features.
Labels
Labels are typically an inline element inside another element that adds
information about that element. Figure 6.1 shows a sample headline with
a label.
To add a label to your content, add the .label class to a <span> element
surrounding the text you want in the label. Then you must define the
variation of label you want to use. The variation choices are
.label-default
.label-primary
.label-success
.label-info
.label-warning
.label-danger
The label variations change the color of the labels. Figure 6.2 shows the
colors for the different label variations.
If you are a designer at heart, your first thought might be “but I don’t like
those colors” or just that those colors don’t fit into your design. But that’s
okay because you can easily change them to fit your design using a cus-
tom style sheet. Nearly every Bootstrap web page has a custom style sheet
associated with it; this isn’t “cheating” or breaking the framework. But
there are some things you should remember so that your custom styles
show up:
Place your style sheet last in the <head> of your document. Your styles
should be loaded after the Bootstrap styles.
Always be as specific as possible with your CSS style rules. The more spe-
cific you are, the more likely the style will be applied correctly.
Consider creating your own style classes to add to your HTML.
If you must modify the Bootstrap CSS, do it with Less rather than directly
in the CSS. I cover this in Hour 23, “Using Less and Sass with
Bootstrap.”
TRY IT YOURSELF: MODIFY LABELS WITH CUSTOM STYLES
You can modify all the labels on your site as well as the specific varia-
tions. This Try It Yourself shows you how to change the default label to
have a gradient background color and add borders and shadows to all la-
bels. Here’s how:
<p>
<span class="label label-default">Default</span>
<span class="label label-primary">Primary</span>
<span class="label label-success">Success</span>
<span class="label label-info">Info</span>
<span class="label label-warning">Warning</span>
<span class="label label-danger">Danger</span>
</p>
4. Add the CSS in Listing 6.2 to add shadows and borders to every label.
.label {
-webkit-box-shadow: 2px 2px 2px #dfdfdf;
box-shadow: 2px 2px 2px #dfdfdf;
border: medium dotted #fff;
}
5. Then add the CSS in Listing 6.3 to add a gradient to the default label
background.
LISTING 6.3 Style the Default Label
.label-default {
background-image:
-webkit-linear-gradient(270deg,rgba(153,153,153,1.00) 0%,
rgba(255,255,255,1.00) 100%);
background-image: linear-gradient(180deg,rgba(153,153,153,1.00)
0%,rgba(255,255,255,1.00) 100%);
color: #000000;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport"
content="width=device-width, initial-scale=1">
<title>Sample Styled Labels</title>
<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<style>
.label {
-webkit-box-shadow: 2px 2px 2px #DFDFDF;
box-shadow: 2px 2px 2px #DFDFDF;
border: medium dotted #fff;
}
.label-default {
background-image: -webkit-linear-gradient(270deg,
rgba(153,153,153,1.00) 0%,rgba(255,255,255,1.00) 100%);
background-image: linear-gradient(180deg,
rgba(153,153,153,1.00) 0%,rgba(255,255,255,1.00) 100%);
color: #000000;
}
</style>
<!-- HTML5 shim and Respond.js for IE8 support of HTML5
elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page
via file:// -->
<!--[if lt IE 9]>
<script
src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js">
</script>
<script
src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<h4> </h4>
<div class="container">
<div class="row">
<div class="col-xs-12">
<p>
<span class="label label-default">Default</span>
<span class="label label-primary">Primary</span>
<span class="label label-success">Success</span>
<span class="label label-info">Info</span>
<span class="label label-warning">Warning</span>
<span class="label label-danger">Danger</span>
</p>
</div>
</div>
</div>
</body>
</html>
Badges
Badges are similar to labels but are generally used to show a count of
something rather than words. To add a badge, add the code
class="badge" to the element.
For example, you might have a link to an email inbox with a script to
show how many new messages there are in a badge:
Click here to view code image
You can add badges to any part of your web page, but they are most often
used in navigation, links, and other dynamic elements.
Sometimes you want to add boxes around your content to add different
effects. Wells are a fairly simple effect, and panels are more complex.
Wells
Wells are a simple style to give the element an inset effect. Figure 6.3
shows a standard well of content.
.well-lg
.well-sm
Wells might seem similar to blockquotes because the standard look and
feel for a blockquote is indented slightly. Bootstrap handles blockquotes
differently by treating them as a form of pull quote. It styles blockquotes
with larger text and a border on the left side as well as indenting them. As
you saw in Figure 6.3, wells actually add a gray background color to the
indented content. If you add a blockquote inside a well, the blockquote is
indented more but the border color is changed to show up inside the well.
Figure 6.4 shows how different wells and wells with blockquotes look.
Listing 6.5 shows the HTML to get that figure.
Panels, at first glance, look similar to wells, but they add more features in-
cluding headers, footers, and contextual alternatives. It’s easy to add a
panel to your document. Listing 6.6 shows how.
As with labels, panels use a .panel class and a class that defines the vari-
ation. There are six variants:
.panel-default
.panel-primary
.panel-success
.panel-info
.panel-warning
.panel-danger
These variants don’t come into play unless you add a panel header.
Figure 6.5 shows the different variations of panels.
FIGURE 6.5 Panel variations.
A panel header is typically placed above the panel body content and adds
a background color to the header. If a panel header has a panel title, that
title will also have modified styles. Listing 6.7 shows how to add a panel
header with and without a title.
As you can see, you use the class .panel-heading to define the heading
and then .panel-title to define the title. You create a footer in the same
way with the .panel-footer class. Listing 6.8 shows a panel with a
footer.
You can place the footer above or below the panel body (defined by
.panel-body ) and header, but the most common position is at the bot-
tom of the panel.
The Jumbotron
The Jumbotron is a large layout block that can be used to call additional
attention to featured content on your website. Figure 6.6 shows how a
Jumbotron might look.
FIGURE 6.6 A Jumbotron.
<div class="jumbotron">
<h1>This is a Jumbotron</h1>
<p>This is content inside the Jumbotron</p>
<p><a class="btn btn-primary btn-lg" href="#" role="button">Learn more</a></
</div>
If you put it in your grid container, the Jumbotron will take up all 12 col-
umns, with rounded corners. If you put it outside the container, the
Jumbotron will fill the width of the window and the corners will not be
rounded.
One example the Get Bootstrap site shows is the Narrow Jumbotron.
Although it adds a lot of additional CSS to customize the layout, making
the entire page narrower than standard bootstrap is done with just a few
lines of CSS, as in Listing 6.10.
The key things to notice about this CSS are that it’s inside a media query—
@media (min-width: 768px) —and rather than setting a specific width, it
sets the max-width for the element. This allows the element to continue
to flex within the page in medium and large devices, but it won’t get any
larger than 730px wide. This keeps the narrow design while still being
responsive.
Summary
This hour covered a lot of components and design features. You learned
about labels and badges as ways to include additional information about
your content. You also learned about panels and wells as a way to high-
light content with background colors, borders, and some indenting.
Finally, you learned how to use the Jumbotron to create a large but light-
weight feature box on your site.
Table 6.1 covers all the CSS classes learned in this hour.
TABLE 6.1 CSS Classes for Labels, Badges, Panels, Wells, and the
Jumbotron
Workshop
The workshop contains quiz questions to help you process what you’ve
learned in this hour. Try to answer all the questions before you read the
answers.
Q&A
A. Badges are often used to display automated features such as the number
of new messages in an inbox. That automation is usually done with things
like PHP or JavaScript. A lot of scripts are included in Bootstrap, but the
actual functionality for an inbox counter or other badge feature is be-
yond the scope of Bootstrap. If you need this functionality, you should
search the Web to find a script that meets your needs.
Q. If I don’t like the default background color for wells, can I change it?
A. Yes, you change it with your style sheet. The quickest way is to add a style
to the .well property.
.well {
background-color: #white;
}
A. For the most part, no. But a few styles are added. For instance, para-
graphs inside the Jumbotron are given a 15px margin, 21px font size, and
200 font weight. If an <hr> tag is in the Jumbotron, it will be given a bor-
der top color. And, as with all other Bootstrap styles, you can add your
own style sheet to create your own designs.
Quiz
a. There is no difference.
d. Labels are primarily iconographic, while badges are more text focused.
3. True or False: You should never create your own style classes when modi-
fying Bootstrap.
a. main
b. default
c. danger
d. success
5. Why should you leave badges empty rather than using “0” or “null”?
6. True or False: Wells and blockquotes are displayed the same way in
Bootstrap.
b. .well-default
c. .well-info
Quiz Answers
6. False. Although both wells and blockquotes are typically displayed in-
dented, Bootstrap displays them slightly differently.
9. False. Although it has a heading, it does not have a title set with the
.panel-title class.
10. False. Jumbotrons work within a .container class or outside of it. But
they do look different.
Exercises
1. Find a portion of your website that could use a label or badge, and add it
to the content. Remember that badges are often more dynamic than
labels.
2. Put some content inside a well or panel. An easy way to decide which to
use is to look at whether the content needs a header (or footer). If it
doesn’t need those elements, then a well might be all you need.
3. Create a Jumbotron layout. Find something you’d like to feature and put
it in a Jumbotron to give it focus for that page.