How to Use Bootstrap CSS

Content, Styles and Components

The CSS is the most essential part of Bootstrap. The Bootstrap CSS can be used alone, without the Bootstrap JavaScript components. Generally speaking, Bootstrap’s CSS provides two things…

  • 1. Element Styles

    Buttons, fonts, colors, badges, typography, progress bars, alerts, etc…

  • 2. The Bootstrap Grid

    Layout helpers using “rows” and “columns”.


First, I’ll explore all of the element styles Bootstrap provides to “make Web development faster and easier”. Remember that simple Bootstrap example I showed earlier?

Let’s understand what Bootstrap really does to the Web page...


Reboot

First of all, the Bootstrap CSS includes Reboot which “normalizes” the Web page styles. Web browsers have different, somewhat ugly, styles that are applied to Web pages by default.

Since each browser’s defaults are a little different, Bootstrap uses Reboot to override the browser defaults, and provide the same “elegant, consistent baseline” to build upon, regardless of the browser.

This means that if the Web page is viewed on Safari, Chrome, Firefox or IE it will start out with the same baseline CSS styles.


Fonts (Typeface)

Text readability and scale are an important consideration in Web design. Bootstrap took this into consideration when selecting the native font stack for Bootstrap 4:

                    
        $font-family-sans-serif:
          // Safari for OS X and iOS (San Francisco)
          -apple-system,
          // Chrome < 56 for OS X (San Francisco)
          BlinkMacSystemFont,
          // Windows
          "Segoe UI",
          // Android
          "Roboto",
          // Basic web fallback
          "Helvetica Neue", Arial, sans-serif,
          // Emoji fonts
          "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol" !default;
        The Bootstrap CSS makes the text look like this…
                    
                    

Bootstrap's font stack (included in the CSS) makes the text look like this…

Bootstrap 4 Fonts

Colors

No one gets too excited about a Web page that lacks color. Color is often used to convey meaning, and highlight specific page content. Bootstrap provides 8 contextual colors that can be easily applied to HTML elements. You’ll see that colors are named to convey meaning.

Bootstrap 4 Contextual Colors

Additionally, Bootstrap CSS can be used to style and format any page content such as Tables, Forms, Images and Figures.


Components

Take a quick glance at the following examples of the Bootstrap Components. These are just some the “things” that can be easily “plugged-in” to your Web pages when using Bootstrap…

Bootstrap 4 Alerts Bootstrap 4 Badges Bootstrap 4 Breadcrumbs Bootstrap 4 Buttons Bootstrap 4 Cards Bootstrap 4 Collapse Bootstrap 4 Dropdown Bootstrap 4 Forms Bootstrap 4 Modal Bootstrap 4 NavBar

How to Use Bootstrap CSS “Styles”

Using any of these Bootstrap Components is very easy. It’s just a matter of referencing the appropriate Bootstrap CSS class in the HTML markup. For example, here’s how we use Bootstrap’s primary colored button on a Web page…

                    
        <button type="button" class="btn btn-primary">Primary</button>
                    
                    

So, the buttons’ style comes from the CSS “rules” for the .btn and .btn-primary CSS selectors. Here are the relevant rules in the Bootstrap CSS…

                    
        .btn {
          display: inline-block;
          font-weight: 400;
          text-align: center;
          white-space: nowrap;
          vertical-align: middle;
          -webkit-user-select: none;
          -moz-user-select: none;
          -ms-user-select: none;
          user-select: none;
          border: 1px solid transparent;
          padding: 0.375rem 0.75rem;
          font-size: 1rem;
          line-height: 1.5;
          border-radius: 0.25rem;
          transition: color 0.15s ease-in-out, background-color 0.15s ease- in-out, border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
        }
        .btn-primary {
          color: #fff;
          background-color: #007bff;
          border-color: #007bff;
        }
                    
                    

You might be wondering…

Why does the button have 2 classes: .btn + .btn-primary?

“Bootstrap’s components are built with a base-modifier class approach. This means the bulk of the styling is contained to a base class (e.g., .btn) while style variations are confined to modifier classes (e.g., .btn-danger).” This base-modifier approach in Bootstrap’s CSS makes customizing Bootstrap much easier which I will explain later in this article.

Here's an overview of the relationship between the HTML markup and the Bootstrap CSS:
CSS HTML and the Web page

As you can see in the picture above, the “Primary” button has a different color than the normal Bootstrap blue color. That’s because this Web page also includes a “theme.css” stylesheet after the “bootstrap.min.css”.

The CSS styles (or rules) defined in “theme.css” are overriding the default Bootstrap CSS styles, such as the definition of the .btn-primary color. I will explain Bootstrap theming and customization later in this article.


Let’s look at more examples of Bootstrap Component usage...

Badges

                
        <span class="badge badge-primary">Badge</span>
        <span class="badge badge-pill badge-primary">Rounded Badge</span>
                

Breadcrumbs

                
        <nav aria-label="breadcrumb">
          <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="#">Home</a></li>
            <li class="breadcrumb-item"><a href="#">Library</a></li>
            <li class="breadcrumb-item active">Breadcrumb</li>
          </ol>
        </nav>
                

Buttons

                
        <button type="button" class="btn btn-primary btn-lg">Large</button>
        <button type="button" class="btn btn-primary btn-sm">Small</button>
                

Cards

                
        <div class="card">
         <div class="card-body d-flex flex-column align-items-start">
          <h4 class="card-title text-primary">Card Outline</h4>
          <p class="card-text">With supporting text.</p>
          <a href="#" class="btn btn-primary mt-auto">Button</a>
         </div>
        </div>
                

Forms & Inputs

                
        <form role="form">
            <div class="form-group row">
                <label class="col-md-2 col-form-label form-control-label">
                    First
                </label>
                <div class="col-md-4">
                    <input class="form-control" type="text" value="Jane">
                </div>
                <label class="col-md-1 col-form-label form-control-label">
                    Last
                </label>
                <div class="col-md-5">
                    <input class="form-control" type="text" value="Bishop">
                </div>
            </div>
            <div class="form-group row">
                <label class="col-lg-2 col-form-label form-control-label">
                    Address
                </label>
                <div class="col-lg-10">
                    <input class="form-control" type="text">
                </div>
            </div>
            <div class="form-group row">
                <div class="col-lg-12 text-right">
                   <input type="button" class="btn btn-primary" value="Submit">
                </div>
            </div>
        </form>
                

Jumbotron

                
        <div class="jumbotron py-4">
            <h1 class="display-3">Hello, world!</h1>
            <p class="lead">
            This is simple jumbotron component...
            </p>
        </div>
                

Pagination

                
        <nav>
          <ul class="pagination">
            <li class="page-item"><a class="page-link" href="#">1</a></li>
            <li class="page-item"><a class="page-link" href="#">2</a></li>
            <li class="page-item"><a class="page-link" href="#">3</a></li>
          </ul>
        </nav>
                

Progress

                
        <div class="progress">
          <div class="progress-bar w-25">
          </div>
        </div>
                

What's the correct HTML markup & structure to use?

This is where developing with Bootstrap does require some “leg work”. You do need to know and use the correct HTML structure when utilizing any of the Bootstrap Components.

When using Components like the Button (.btn .btn-primary) shown earlier, the HTML markup is straight-forward. But, for other Components like the Navbar, Modal or Carousel, determining the correct HTML structure is more complex, and will vary according to use-case and the desired implementation.

When using these more complex Bootstrap Components, you must start with examples. The best resource is the Official Bootstrap Documentation. The docs have many examples, and a “How it Works” section for each Component.

There are also tools like Codeply (that’s the one I wrote), and Bootstrap.build that include example Bootstrap code snippets that you can easily drop in an editor and play around with. For example, here’s the Navbar picked from the Codeply’s Bootstrap 4 Components list…

Find Bootstrap 4 element stucture on Codeply

The Navbar

While most of the Bootstrap Components shown above are self-explanatory, I think the Navbar requires a full explanation. Along with the Grid, the Navbar is one of the most popular Bootstrap Components.

Content, Structure and Alignment

Only use supported content inside the Navbar. The Navbar can contain:

  • Logo/brand
  • Nav links, buttons, or dropdowns
  • Inline forms
  • Text

Don’t use Grid Rows/Columns inside the Navbar. There’s really no reason to since Utility classes can be used to align Navbar content. For example, here the ml-auto (left auto margin) class is used to align the 2nd Nav to the right:

                
        <nav class="navbar navbar-expand-lg navbar-light bg-light">
          <a class="navbar-brand" href="#">Navbar</a>
          <button class="navbar-toggler" 
             data-toggle="collapse" 
             data-target="#nb">
             <span class="navbar-toggler-icon"></span>
         </button>
         <div id="nb" class="navbar-collapse collapse">
           <ul class="navbar-nav">
             <li class="nav-item">
              <a class="nav-link" href="#">Left aligned</a>
             </li>
           </ul>
           <ul class="navbar-nav ml-auto">
             <li class="nav-item">
              <a class="nav-link" href="#">Right aligned</a>
             </li>
          </ul>
         </div>
        </nav>
                
Navbar Breakpoints

By default, the Navbar is designed for smaller screen widths on Mobile devices. Therefore the Navbar will stack vertically in a column, unless overridden with a “expand” class make the Navbar to remain horizontal above specified breakpoint widths. There are 5 breakpoints…

  • navbar-expand = always expanded (remains horizontal)
  • navbar-expand-sm = expands on sm widths > 576px
  • navbar-expand-md = expands on md widths > 768px
  • navbar-expand-lg = expands on lg widths > 992px
  • navbar-expand-xl = expands on xl widths > 1200px
Try it on Codeply
Light or Dark Background Color?

The Navbar uses the navbar-light and navbar-dark classes to determine the foreground colors of the brand, links and toggler (hamburger) icon. Make sure you use the appropriate classes to make the Navbar content display.

  • Use navbar-dark to for darker colored backgrounds
  • Use navbar-light to for lighter colored backgrounds

To give the Navbar a background color, use one of the contextual colors (e.g. bg-primary) otherwise the Navbar is transparent.

Read more about the Bootstrap 4 Navbar

Utility Classes

Of course, Bootstrap is the Swiss army knife of the Web design & development world. Bootstrap encompasses many widely used CSS properties in handy Utility classes. This eliminates the need to write the same CSS over-and-over again to apply styles to different HTML elements. You many know it as D.R.Y.

So instead of writing this…

                
        <style>#mydiv { text-alignment: center }</style>
        <div id="mydiv" class="text-center">Centered text</div>
                

You simply utilize Bootstrap’s text-center alignment class wherever it’s needed…

                
        <div id="mydiv" class="text-center">Centered text</div>
                

Remember, the most up-to-date Bootstrap resource is the Official Bootstrap Documentation. The docs have many examples, and a “How it Works” section for each Component.

Now that you understand the basics of using Bootstrap’s styling Components, next I will explain Bootstrap’s powerful, responsive Layout Component__ the Grid System.