← Pages with Bootstrap

Style by class names

Link Bootstrap's stylesheet, then style buttons, text and messages by adding its class names instead of writing CSS.

Thousands of school, club and company websites use Bootstrap, so their buttons and forms look tidy on phones and computers without much CSS of their own.

You built every part of the club page with your own CSS. Bootstrap is a big stylesheet that someone else wrote, full of ready-made rules. You link it with one line, then style a tag by adding its class names:

<link rel="stylesheet" href="bootstrap.min.css">
<button class="btn btn-primary">Join</button>

bootstrap.min.css sits beside the page here. On your own site, you download it from getbootstrap.com and put it beside your page.

PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.

Predict

page.html
<link rel="stylesheet" href="bootstrap.min.css">

<button class="btn btn-primary">Join</button>
<button>Join</button>

The first line links Bootstrap. Neither button has a <style> rule of its own.

There is no CSS rule for btn anywhere in this code. How will the two buttons look?

RunRun it and compare the result with your prediction.

Run

Compare with your prediction. Then delete the <link> line and watch everything go plain, though every class name is still there. Put it back.

page.html
<link rel="stylesheet" href="bootstrap.min.css">

<div class="container mt-3">
  <h1>Chess club</h1>
  <p class="lead">Chess every Wednesday at lunch in the library.</p>
  <div class="alert alert-warning">Sign-ups close on Friday.</div>
  <button class="btn btn-primary">Join</button>
  <button class="btn btn-outline-secondary">Read more</button>
</div>
Try it yourselfYour edits are saved in this browser.

InvestigateTrace how each line of code connects to what you see.

Investigate

page.html
<link rel="stylesheet" href="bootstrap.min.css">

<div class="container mt-3">
  <h1>Chess club</h1>
  <p class="lead">Chess every Wednesday at lunch in the library.</p>
  <div class="alert alert-warning">Sign-ups close on Friday.</div>
  <button class="btn btn-primary">Join</button>
  <button class="btn btn-outline-secondary">Read more</button>
</div>
The page this code makes
What this code makes

Shape, colour and spacing.

What is a class name on its own?

One button is plain.

Match each class name with what it does.

container
mt-3
lead
alert alert-warning
btn
btn-primary

You delete the <link> line. What happens to class="btn btn-primary"?

Join should be a blue Bootstrap button, but it looks like a plain browser button. There is no error. Click the line that is wrong.

ModifyChange the code on purpose and check the result.

Modify

Look the names up in the Reference drawer: Bootstrap's class names are fixed, so nobody remembers them all.

Choose different classes.

Need a hint?

Bootstrap names its colours: primary, secondary, success, danger, warning, info.

Another hint

Swap btn-primary for btn-success, and add btn-lg for a large button. Keep btn.

Show the expected result
What the finished page looks like
What it should look like. Your words can be different.

Same base class, new colour.

Need a hint?

There is already a yellow alert. A red one has the same base class with a different colour class.

Another hint

A div with class="alert alert-danger" under the heading.

Show the expected result
What the finished page looks like
What it should look like. Your words can be different.
Try it yourselfYour edits are saved in this browser.

Make Join a large green button.

    This ticks when your page has all of the above.

    Add a red alert under the heading: The tournament is full.

      This ticks when your page has all of the above.

      MakeBuild something new using what you have learned. Expect a few tries; that is normal.

      A notice for your own event

      A concert, a bake sale, a match: use a container, a lead paragraph, an alert and two buttons in different colours. No <style> block: Bootstrap's classes only.

      Try it yourselfYour edits are saved in this browser.

      Style the notice with Bootstrap: a container, a lead paragraph, an alert and two buttons.

        This ticks when your page has all of the above.

        Info

        Where this leads

        Next: the club page's navbar, cards and grid, rebuilt with Bootstrap.

        Your club page had about 40 lines of CSS for the menu, cards and form. With Bootstrap, where does that CSS live?

        For teachers

        Students link Bootstrap's stylesheet, predict that a class name with no rule of their own still styles a button, match classes to what they do, find a button missing its base class, and style a notice for an event of their own. About 50 minutes.

        Open the teacher guide: answers, solutions, slides and a worksheet.

        Students will be able to

        Before class

        Read the unit primer first. Bootstrap is a stylesheet: one big CSS file of ready-made rules (about 230 KB). Linking it with <link rel="stylesheet" href="bootstrap.min.css"> gives the page all its rules; adding a class name such as btn switches one on. Nothing new happens in the browser: students already know classes and CSS rules from HTML & CSS; the rules are just someone else's.

        Most looks come in two parts, a base class for the shape (btn, alert) and a second for the colour or size (btn-primary, btn-lg). The planted bug leaves out the base class. Here the stylesheet sits beside the page (it is served from this site); on a real site, download it from getbootstrap.com. This course uses Bootstrap's CSS only, not its JavaScript, so nothing needs a script.

        Running the lesson

        1. Predict: ask what a class name does when nobody has written a rule for it.
        2. Run: have everyone delete the link line and watch the page go plain. The class names are still there.
        3. Investigate: for the bug, ask which class gives the shape and which gives the colour.
        4. Modify: point students to the Reference drawer; looking names up is the skill, not remembering them.
        5. Make: no style block allowed. Ask each student to read out the class for each part.

        The hard part (on purpose)

        Students want to write CSS to fix a look. Ask "which Bootstrap class does that?" and send them to the Reference drawer before they type a rule.

        When students get stuck

        Stretch and support

        Stretch: Use btn-sm and btn-lg, outline buttons (btn-outline-danger), and the text colour classes (text-success). Try a badge: span class="badge text-bg-secondary".

        Support: Give the class names on the board for the Make task and ask students only to place them.

        Where it leads

        Next: the navbar, cards and grid, where Bootstrap parts are whole shapes of nested tags. Class idea: put the hand-built club page and this page side by side and count the lines of CSS.

        Sources

        What each step is for

        Now you can
        Next lesson2. Navbar, cards and a gridRebuild the club page's menu, cards and form with Bootstrap: each part is a fixed shape of nested tags with class names, and the grid's row and col line the cards up.