In this write-up, we will learn about Bootstrap 4 utility classes or Helper classes. This is the third part of this article series of Bootstrap 4 utility classes. In this part, we will discuss spacing, position, and Background Color utility classes. Utility classes are useful to add style quickly without writing any CSS classes.
Bootstrap spacing classes
Bootstrap 4 provides classes to set the responsive padding and margin.
Syntax
{property}{sides} – {breakpoint} – {sides}
- Property: padding or margin
- Sides:Top,botton,left,right
- t - margin-top or padding-top
- b - margin-bottom or padding-bottom
- l - margin-left or padding-left
- r - margin-right or padding-right
- x – margin *-left and *-right or padding *-left and *-right
- Breakpoint:Xs,sm,md,lg,xl
- xs <=576px
- sm>=576px
- md>=768px
- lg>=992px
- xl>=1200px
- Size:size may be 0,1,2 etc.
- 0 - margin or padding to 0
- 1 - margin or padding to .25rem
- 2 - sets margin or padding to .5rem
- 3 - sets margin or padding to 1rem
- auto - sets margin to auto
Open Visual Studio and create a new project. Rename it as Bootstrap4 and add the reference of the Bootstrap 4 files into the page's head section.
Now, add 3 Divs and their acting classes.
- <div class="pt-3 bg-success">top padding</div>
- <div class="p-3 bg-success">all size padding</div>
- <div class="m-3 bg-success">all side margin</div>
- <!DOCTYPE html>
- <html>
-
- <head>
- <meta charset="utf-8" />
- <title>Bootstrap 4</title>
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
- <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
- </head>
-
- <body>
- <div class="container">
- <div class="pt-3 bg-success">top padding</div>
- <div class="p-3 bg-success">all size padding</div>
- <div class="m-3 bg-success">all side margin</div>
- </div>
- </body>
-
- </html>
Run the project and check the result.
Position Classes
These classes are useful to set the element position.
- fixed-top: This class is used to fix an element on the top of the page. This class is useful in creating a fixed header.
- fixed-bottom: This class is used to fix an element on the bottom of the page. This class is useful in creating a fixed footer.
- sticky-top
Now, let us create a navigation bar, add fixed top class, create a footer, and add a fixed bottom.
Header
- <nav class="fixed-top navbar navbar-expand bg-dark ">
- <ul class="navbar-nav">
- <li class="nav-item">
- <a class="nav-link" href="#Home">Home</a>
- </li>
- <li class="nav-item">
- <a class="nav-link" href="#Contact">Contact</a>
- </li>
- <li class="nav-item">
- <a class="nav-link" href="#About">About</a>
- </li>
- <li class="nav-item">
- <a class="nav-link" href="#Career">Career</a>
- </li>
- </ul>
- </nav>
Footer
- <nav class="fixed-bottom navbar navbar-expand bg-dark ">
- <ul class="navbar-nav">
- <li class="nav-item">
- <a class="nav-link" href="#Copyrights">Copyrights</a>
- </li>
- <li class="nav-item">
- <a class="nav-link" href="#2018">2018</a>
- </li>
- </ul>
- </nav>
Complete Code
- <!DOCTYPE html>
- <html>
-
- <head>
- <title></title>
- <meta charset="utf-8" />
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
- <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
- </head>
-
- <body style="height:2000px">
- <nav class="fixed-top navbar navbar-expand bg-dark ">
- <ul class="navbar-nav">
- <li class="nav-item">
- <a class="nav-link" href="#Home">Home</a>
- </li>
- <li class="nav-item">
- <a class="nav-link" href="#Contact">Contact</a>
- </li>
- <li class="nav-item">
- <a class="nav-link" href="#About">About</a>
- </li>
- <li class="nav-item">
- <a class="nav-link" href="#Career">Career</a>
- </li>
- </ul>
- </nav>
- <div class="container-fluid" style="margin-top:100px;padding-left:40px;"> In this article, we will learn how to Update the data from MongoDB Using Robo 3T. We will learn from the basics because I have written this article focusing on the beginners </div>
- <nav class="fixed-bottom navbar navbar-expand bg-dark ">
- <ul class="navbar-nav">
- <li class="nav-item">
- <a class="nav-link" href="#Copyrights">Copyrights</a>
- </li>
- <li class="nav-item">
- <a class="nav-link" href="#2018">2018</a>
- </li>
- </ul>
- </nav>
- </body>
-
- </html>
Now, let us run the project and check the result.
Background Colors
These classes are useful to set the background colors
- .bg-primary
- .bg-secondary
- .bg-success
- .bg-danger
- .bg-info
- .bg-warning
- .bg-dark
Let us add some divs and add these classes.
- <div class="bg-primary ">primary</div>
- <div class="bg-secondary ">secondary</div>
- <div class="bg-success ">success</div>
- <div class="bg-danger ">danger</div>
- <div class="bg-info ">info</div>
- <div class="bg-warning ">warning</div>
Complete Code
- <!DOCTYPE html>
- <html>
-
- <head>
- <title></title>
- <meta charset="utf-8" />
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
- <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
- </head>
-
- <body>
- <div class="container">
- <div class="bg-primary ">primary</div>
- <div class="bg-secondary ">secondary</div>
- <div class="bg-success ">success</div>
- <div class="bg-danger ">danger</div>
- <div class="bg-info ">info</div>
- <div class="bg-warning ">warning</div>
- </div>
- </body>
-
- </html>
Run the project and check the result.
Summary
In this blog, we learned about spacing classes, position classes, and background classes in Bootstrap.