TechTip: A Primer to Bootstrap 4, Part 1

Development Tools / Utilities
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

Let’s scratch the surface of the new version of Bootstrap.

You sit down in your office and fold your hands behind your neck. You are very pleased with yourself. You have just finished writing an RPG CGI program that will display sales data, Key Performance Indicators (KPIs) to show the production figures for last week, and the order intake split on country areas. Everything now runs fine on the intranet, and users can see it all when they connect their laptops to the VPN. Life is good, and you sing The Beatles’ “Don't Let Me Down” to yourself: Nobody ever loved me like she does. Oh, she does, yeah, she does.

Suddenly, you hear a knock on the door, and young Steven appears in the doorway: “Jan, when I look at your new websites on my mobile, they look a bit, umm, clumsy.” You suppress the urge to throw something at him and reply, “Well, Steven, who wants to see the data on a small mobile screen when you can see it on a widescreen laptop?” His reply cuts like a knife: “Everybody! What planet are you from?” He quickly walks away, leaving you with another Beatles song in your head: “Leave My Kitten Alone”: Well, Mr. Dog, I'm gonna hit you on the top of your head.

If this story sounds like something you have heard before (except for The Beatles’ songs), then Bootstrap might be something for you. Bootstrap lets you create responsive websites. I’ve already written a few articles (1, 2, 3) you should read so you get a basic understanding of the tool and how it fits into a mobile world.

One of many things that has changed in Bootstrap 4 is the use of Popper.js, which is used with modals, tools, tips, and likewise. Just include it and do not think more about it right now.

In the following examples, I use a Content Delivery Network (CDN) to copy the required data into my pages, but you can of course install locally if you prefer that.

Mobile-First

Bootstrap is “mobile-first,” and you might as well start thinking that way every time you create something. Ask yourself, “How does my program look on a mobile screen?” If it will look good there, it will also work fine on larger screens.

The way Bootstrap handles responsiveness is by using breakpoints. In version 4, an .xl breakpoint has been added, so we now have the following to use:

  • .xs (the default) is for extra-small devices, screen width less than 576px.
  • .sm is for small devices, screen width equal to or greater than 576px.
  • .md is for medium devices, screen width equal to or greater than 768px.
  • .lg is for large devices, screen width equal to or greater than 992px.
  • .xl is for extra-large devices, screen width equal to or greater than 1200px.

The above classes can be combined to create very dynamic and flexible layouts.

Let’s look at some of the other new features in version 4. These are my selections, so if you don’t like the flavor, head over to the Bootstrap website and pick some other goodies.

Borders

One thing that version 3 was missing was the ability to add borders to HTML objects. It was possible to use the .table-bordered class on a table, but that was all. Version 4 offers a whole new set of classes that can add borders to almost anything in a very rich and flexible way.

You have the ability to both add and subtract borders, and you can add borders with colors and radius and combine the lot all by using a few classes. However, one thing you can’t do is control the width of the borders. If you want to do that, you will have to create a class yourself.

If you want to add a border to a <div> tag, simply add the class .border and you’re done. If you for some reason do not want a border on the bottom of the <div>, you can remove it by adding the classes .border .border-bottom-0. So the .border adds four borders and the .border-bottom-0 subtracts the bottom border. Very easy and neat if you ask me.

Below is a code example where I have used all the different classes in various ways to show you the border layout. Make a copy of the code and then try combining the different classes to get a feeling for this very strong feature.

Please notice this part of the code:

.boot-box-border-width {

      border-width:2px !important;

}

The “boot-box-border-width” class I created is how you can control the width of the borders.

<!doctype html>

<html lang="en">

<head>

 

        <title>MCpressonline.com -> Bootstrap 4 - Borders</title>

        <!-- Required meta tags -->

        <meta charset="utf-8">

        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

       

        <!-- Latest compiled and minified CSS -->

        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">

        <!-- jQuery library -->

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

        <!-- Popper JS -->

        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>

        <!-- Latest compiled JavaScript -->

        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>        

    

<style>

.boot-box {

        width: 250px;

        height: 100px;

        margin-bottom: 15px;

        background-color: #fff;

}

.boot-box-border-width {

        border-width:2px !important;

}

</style>      

</head>

<body>

<div class="container-fluid">

<hr>

<h4>MCpressonline.com -> Bootstrap 4 - Borders</h4>

<hr>

<div class="row">

   <div class="col">

               <strong>Border - Additive</strong>

        </div>

       

   <div class="col">

               <strong>Border - Subtractive</strong>

        </div

       

   <div class="col">

               <strong>Border - Colors</strong>

        </div>        

       

   <div class="col">

               <strong>Border - Radius</strong>

        </div>                

</div>

<br>

<div class="row">

   <div class="col">

               <div class="boot-box border">class: border</div><br>

               <div class="boot-box border-top">class: border-top</div><br>

               <div class="boot-box border-right">class: border-right</div><br>

               <div class="boot-box border-left">class: border-left</div><br>

               <div class="boot-box border-bottom">class: border-bottom</div><br>

               <div class="boot-box border-bottom border-right">class: border-bottom, border-right</div><br>

        </div>

       

       

   <div class="col">

               <div class="boot-box" style="background-color:#fff"></div><br>

                <div class="boot-box border border-top-0">class: border border-top-0</div><br>

               <div class="boot-box border border-right-0">class: border-right-0</div><br>

               <div class="boot-box border border-left-0">class: border border-left-0</div><br>

               <div class="boot-box border border-bottom-0">class: border border-bottom-0</div><br>

               <div class="boot-box border border-bottom-0 border-right-0">class: border border-bottom-0 border-right-0</div><br>     

   </div>

       

   <div class="col">

               <div class="boot-box boot-box-border-width border border-primary">class: border border-primary</div><br>

               <div class="boot-box boot-box-border-width border border-secondary">class: border border-secondary</div><br>

               <div class="boot-box boot-box-border-width border border-success">class: border border-success</div><br>

               <div class="boot-box boot-box-border-width border border-danger">class: border border-danger</div><br>

               <div class="boot-box boot-box-border-width border border-warning">class: border border-warning</div><br>

               <div class="boot-box boot-box-border-width border border-info">class: border border-info</div><br>

               <div class="boot-box boot-box-border-width border border-light">class: border border-light</div><br>

               <div class="boot-box boot-box-border-width border border-dark">class: border border-dark</div><br>

               <div class="boot-box boot-box-border-width border border-white">class: border border-white</div><br>

   </div>    

       

   <div class="col">

               <div class="boot-box boot-box-border-width border border-primary rounded">class: rounded</div><br>

               <div class="boot-box boot-box-border-width border border-primary rounded-top">class: rounded-top</div><br>

               <div class="boot-box boot-box-border-width border border-primary rounded-left">class: rounded-left</div><br>

               <div class="boot-box boot-box-border-width border border-primary rounded-right">class: rounded-right</div><br>

               <div class="boot-box boot-box-border-width border border-primary rounded-bottom">class: rounded-bottom</div><br>

               <div style="height:250px" class="boot-box boot-box-border-width border border-primary rounded-circle">class: rounded-circle</div><br>

   </div>    

       

</div>

</div>

</body>

</html>

To see the example in real life, click here.

Spacing

Another set of classes provided with Bootstrap 4 offers the ability to add spacing to an element. To get the desired effect, you combine a set of letters and a number from 1 to 5.

The syntax is like this: {property}{sides}-{size} for xs and {property}{sides}-{breakpoint}-{size} for sm, md, lg, and xl.

Where property is one of:

  • m—Sets margin
  • p—Sets padding

Where sides are one of:

  • t—Sets margin-top or padding-top
  • b—Sets margin-bottom or padding-bottom
  • l—Sets margin-left or padding-left
  • r—Sets margin-right or padding-right
  • x—Sets both padding-left and padding-right or margin-left and margin-right
  • y—Sets both padding-top and padding-bottom or margin-top and margin-bottom
  • blank—Sets a margin or padding on all four sides of the element

Where size is one of:

  • 0—Sets margin or padding to 0
  • 1—Sets margin or padding to .25rem (4px if font size is 16px)
  • 2—Sets margin or padding to .5rem (8px if font size is 16px)
  • 3—Sets margin or padding to 1rem (16px if font size is 16px)
  • 4—Sets margin or padding to 1.5rem (24px if font size is 16px)
  • 5—Sets margin or padding to 3rem (48px if font size is 16px)
  • auto—Sets margin to auto

So, let’s say you want to add some padding to the top of a div box and you want to have 1.5rem but only for large displays. That’s not too hard. Your class will look like this:

.pt-lg-4

Here’s an example:

<div class="boot-box border pt-lg-4">

<pre>

You better leave my kitten all alone,

You better leave my kitten all alone.

But I told you big fat bulldog,

You better leave her alone.

You better leave my kitten all alone,

You better leave my kitten all alone.

This dog is gonna get you

If you don't leave her alone.

</pre>

</div>

To see the example in real life, click here. Try resizing the browser window to see the padding being added to the top of the div. Also try changing the class to something different to see how spacing works.

Sizing

You can also control width and height very easily in version 4.

Width

Width is controlled by the following classes:

  • w-25
  • w-50
  • w-75
  • w-100

These will set the width to the current figure in a percentage relative to its parent.

Here’s an example:

<div class="boot-box border">

      <div class="w-25 pl-1" style="background-color: #FFFF99;">25%</div>

      <div class="w-50 pl-2" style="background-color: #FFFF99;">50%</div>

      <div class="w-75 pl-sm-3" style="background-color: #FFFF99;">75%</div>

      <div class="w-100 pl-lg-4" style="background-color: #FFFF99;">100%</div>

</div>

To see the example in real life, click here. As you can see, I added some spacing to the example too. Try resizing the window to see it function.

Height

Height is controlled by the following classes:

  • .h-25
  • .h-50
  • .h-75
  • .h-100

Here’s an example:

<div class="boot-box1 border">

<div class="h-25 d-inline-block boot-height">25%</div>

<div class="h-50 d-inline-block boot-height">50%</div>

<div class="h-75 d-inline-block boot-height">75%</div>

<div class="h-100 d-inline-block boot-height">100%</div>

</div>

To see the example in real life, click here.

Note the d-inline-block class used in this example. This is a wrapped-up CSS “display” property that controls the display behavior of an element. The display property has a lot of parameters and an explanation is beyond the scope of this TechTip, but this link will show and explain.

The First Look Is Now Over

I hope this first scratch on the surface gives you an idea of what’s inside Bootstrap 4. In my next TechTip, I will dig more into the refresh of the Bootstrap grid system, which is useful when you must adapt to Steven’s and everybody else’s need to show you data on their cell phones.

Till then, be responsive, think mobile, and keep humming those old Beatles songs.

 

 

 

BLOG COMMENTS POWERED BY DISQUS