Professional Documents
Culture Documents
When you’re creating interactive websites, you need a tool that indicates
to the customer that clicks will cause an action. Buttons are an easily rec-
ognized interface, and Bootstrap makes it simple to add them to your web
pages.
This hour you learn how to create basic buttons with Bootstrap and ad-
just how they look on the page. You also learn how to create groups of
buttons and even dropdown menus with your buttons.
Basic Buttons
Buttons in Bootstrap look exactly like you expect. Figure 11.1 shows what
a basic button looks like.
Button Tags
To add a button to your page, you first add the class .btn and then the
.btn-default class to create a default button. You can create buttons on
three HTML tags:
<button>
<a>
<input>
As shown in Figure 11.2, these tags create buttons that look the same. So
you can use the HTML tags that work best on your site.
FIGURE 11.2 Four Bootstrap buttons using different HTML tags.
With Bootstrap, you can create buttons using any of the four
listed tags, but there are some best practices you should fol-
low if you can. You can use the button classes only on
<button> elements in the navigation (see Hour 12,
“Creating Navigation Systems with Bootstrap”). If you cre-
ate a link as a button, you should indicate the role with the
role="button" attribute. Also, for the widest possible future
support, you should always use the <button> element as
your first choice for buttons.
Like with other Bootstrap elements, you can change the color and size of
your buttons with different secondary classes. Seven classes are
available:
Listing 11.2 shows you how to add these button types to your HTML, and
Figure 11.3 shows how they would look.
.btn-lg
.btn-sm
.btn-xs
You can combine the size classes with the contextual classes to create a
large warning button or an extra small primary one. You also can create
buttons that span the full width of the parent element with the .btn-
block class. Figure 11.4 shows how these size classes affect the way the
buttons look. You can see the HTML for it in Listing 11.3.
<div class="container">
<button class="btn btn-default btn-lg">Large Button</button>
<button class="btn btn-primary">Medium Button</button>
<button class="btn btn-success btn-sm">Small Button</button>
<button class="btn btn-warning btn-xs">Extra Small
Button</button>
<button class="btn btn-danger btn-block">Full Width
Button</button>
<div class="row">
<div class="col-md-6">
<button class="btn btn-info btn-block">Half Width
Button</button>
</div>
</div>
</div>
The easiest way to change the size of a block button is to put it inside a
column element on your grid. In Listing 11.3, I placed the button in an el-
ement six columns wide so it would span half the width of the screen.
Button States
Buttons have states, just like links. They can be active so they appear
pressed or disabled so they can’t be clicked on. Bootstrap adds some addi-
tional styles to these states so that the buttons appear more interactive.
Disabled buttons are not clickable, and they are given styles to fade them
into the background of the page. You can make any button disabled with
the disabled attribute. For example:
You use the classes .active and .disabled to force these styles on but-
tons that might otherwise display differently. Be aware that buttons cre-
ated with an anchor ( <a> ) tag might behave slightly differently when set
to .active or .disabled . Be sure to test in your devices if you are using
that tag for your buttons.
NOTE: WHY DISABLE BUTTONS?
Button Groups
You can group buttons together on a line as a group. This connects the
buttons together and makes them a more cohesive whole. You can create
horizontal, vertical, and toolbar button groups.
To create a button group, you surround your buttons with a container ele-
ment such as <div> with the class .btn-group . Then be sure to include
the appropriate role attribute: toolbar for toolbars and group for but-
ton groups. This will ensure that screen readers know that the buttons
are grouped together. Finally, to be fully accessible, you should include a
label for the screen readers to read, such as with the aria-label attri-
bute or by including text in the buttons and hiding it with the .sr-only
class. Listing 11.4 shows the HTML for a simple button group.
Horizontal button groups are the default layout for button groups.
Just like with standalone buttons, you can resize your button groups with
three classes:
.btn-group-lg
.btn-group-sm
.btn-group-xs
These make the button groups larger and smaller, as in Figure 11.7.
If you’re using <a> tags for your buttons, just add the class to the button
group container, as in Listing 11.5. Figure 11.8 shows what it will look
like.
LISTING 11.5 Justified Button Group
It’s a little trickier with <button> tags. Most browsers don’t properly ap-
ply the Bootstrap CSS for justification correctly. So, to get it to work, you
should surround each button with another .btn-group element, as in
Listing 11.6.
If you want your button group to be stacked vertically, use the class
.btn-group-vertical . By adding this class to the HTML in Listing 11.4,
you will get a button group like in Figure 11.9.
Button Toolbars
You make toolbars by combining multiple button groups. This gives you
the ability to style more complex elements. Listing 11.7 shows how you
might write the HTML for a button toolbar with three button groups.
Figure 11.10 shows what it would look like in the default Bootstrap styles
with a couple of fancier buttons.
<div class="btn-toolbar">
<div class="btn-group" role="group" aria-label="Button Group">
<button type="button" class="btn btn-primary">1</button>
<button type="button" class="btn btn-default">2</button>
<button type="button" class="btn btn-default">3</button>
</div>
<div class="btn-group" role="group" aria-label="Button Group">
<button type="button" class="btn btn-default">4</button>
<button type="button" class="btn btn-default">5</button>
<button type="button" class="btn btn-default">6</button>
<button type="button" class="btn btn-default">7</button>
</div>
<div class="btn-group" role="group" aria-label="Button Group">
<button type="button" class="btn btn-default">8</button>
<button type="button" class="btn btn-default">9</button>
<button type="button" class="btn btn-danger">10</button>
</div>
</div>
As you can see from Figure 11.10, you can always adjust the buttons to
use the different button types.
Button JavaScript
This hour covered how to use buttons and button groups in Bootstrap.
Bootstrap provides classes to convert your <button> , <a> , and <input>
tags into buttons that respond to customer interaction. You also learned
how to combine multiple buttons into groups and toolbars.
You learned about the classes that create buttons and button groups and
change the size and color of those buttons and groups. You also learned
the classes to convert the button groups to vertical button groups and
toolbars. Table 11.1 shows the classes covered in this hour.
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. Bootstrap is a framework for providing the look and feel of a website and
a little interactivity. When you click on a button on a Bootstrap site, you
need to have some type of script (either JavaScript, PHP, or something
else) that does something. Hour 18 covers the basic actions that Bootstrap
provides, but if you want it to do more, you should read a book like Sams
Teach Yourself jQuery and JavaScript in 24 Hours by Brad Dayley or Sams
Teach Yourself PHP, MySQL, and Apache All in One by Julie Meloni.
Q. I prefer to use the <a> tag for my buttons; does this really matter?
A. You can use the <a> , <button> , or <input> tags for buttons. Firefox
versions below 30 have a rendering bug that might make your <input>
buttons a slightly different height from other buttons. And other
browsers might have similar differences. It’s better to pick one HTML ele-
ment and use that consistently for your buttons. Bootstrap recommends
<button> , but you can use any of them.
Quiz
b. <button>
c. <input>
2. Which of the following is the best tag to use for creating buttons?
a. <a>
b. <button>
c. <input>
a. .btn
b. .btn-default
c. .btn-link
d. .button
b. It creates a link that looks like a button but acts like a link.
b. .btn-default
c. .btn-primary
d. .btn-success
a. .btn-lg
b. .btn-md
c. .btn-small
8. When should you use the state classes .active and .disabled ?
b. .btngroup
c. .btn-group
d. .group
Quiz Answers
8. b. You should use the state classes when you want the state to be explic-
itly displayed.
9. c. .btn-group
10. d. Use the role attribute to define it as a toolbar or a group .
Exercises