15.5. Bootstrap

CSS allows us to make our webpages attractive. For larger sites or more complex pages, the CSS code can get long and quite detailed. Fortunately, we don’t have to reinvent the wheel every time we create a page.

If we save some of our favorite style rules in a single .css file, we can reuse that code over multiple pages. Similarly, many developers are willing to share their CSS rules with others. With their permission, we can import their work to decorate our own HTML.

15.5.1. What is Bootstrap?

Bootstrap provides a library of ready-made CSS that we can use to quickly make our web pages look great.

Example

The content on the left was created with plain HTML code. The right side shows the same code with a few Bootstrap styles added.

Plain HTML on the left, more attractive styled content (buttons, table, lists) on the right.

Bootstrap classes make our page look slick!

Instead of defining our own style rules with selectors and declaration blocks, Bootstrap provides attractive options that we can drop into our code.

15.5.3. Using Bootstrap Styles

Most Bootstrap style rules are defined with class selectors. To add a particular rule to an element, we must place the class attribute inside the starting tag. The general syntax for this is:

<tag_name class="Bootstrap style name(s)">

The developers gave each class a specific name, and we can assign multiple class styles at the same time. For example, the code <section class="text-center border border-dark"> tells the browser: Create a section element, center the text inside that space, then add a border and make it dark.

15.5.3.1. Bootstrap Documentation

The Bootstrap developers created TONS of options, and learning the class names for every one would be difficult. Fortunately, their website includes instructions, pictures, and copy buttons to make using the content easier.

A good place to start is by expanding the Components option in the left menu.

Menu options from the Bootstrap documentation, with the Components option highlighted.

Take a moment to explore any components that sound interesting.

Note

Many examples on the Bootstrap website show <div> tags. Never fear! All of the class names apply equally well to the sematic tags we learned about in the last chapter.

The next step is to play!

Bootstrap provides 9 predefined button styles.

Information found in the Buttons sub-menu.

15.5.4. Try It!

The editor below contains some plain HTML. It also includes the link to the Bootstrap style rules. Explore the Bootstrap website and add class attributes to the HTML tags. Make the page look more interesting!

Tip

Click the Open in repl.it and the Open in a new tab buttons to expand the views.

Possible ideas:

  1. Change the appearance or arrangement of the buttons.
  2. Change the alignment of items within a section.
  3. Update the look of the table.
  4. Explore different list group options.
  5. Add spinners!

15.5.5. Check Your Understanding

Question

Which of the following Bootstrap class attributes produces the following table?

Table with dark cells and white text. Rows highlight when the mouse hovers over them.
  1. class="table table-dark"
  2. class="table table-hover"
  3. class="table table-hover table-dark"
  4. class="table-hover table-dark"