With Bootstrap you can still create web page layouts based on fixed number of pixels, however the container width vary depending on the viewport width and the layout is responsive too.
The process of creating the fixed yet responsive layout basically starts with the .container class. After that you can create rows with the .row class to wrap the horizontal groups of columns. Rows must be placed within a .container for proper alignment and padding.
Further columns can be created inside a row using the predefined grid classes such as .col, col-{xs|sm|md|lg|xl|xxl}-*, where * represent grid number and should be from 1 to 12. Please check out the tutorial on Bootstrap grid system to learn more about grid classes..
Note: Actual content like text, images, videos, tables, etc. should be placed within columns, and only columns may be the immediate children of rows.
The following example will create a fixed width responsive layout that is 720px pixels wide on medium devices like tablets (viewport ≥ 768px), whereas 960px wide on large devices like small laptops (viewport ≥ 992px), 1140px wide on extra large devices like desktops (viewport ≥ 1200px), and 1320px wide on extra-extra large devices like large desktops (viewport ≥ 1400px).
However, on small devices such as mobile phones (576px ≤ viewport < 768px) the layout will be 540px wide. But, on extra-small devices (viewport < 576px) the layout will cover 100% width. Also, columns will be stacked vertically and navbar will be collapsed in both cases.
<div class="p-5 my-4 bg-light rounded-3">
<h1>Learn to Create Websites</h1>
<p class="lead">In today's world internet is the most popular way of connecting with the people. At <a href="https://www.tutorialrepublic.com" class="text-success" target="_blank">tutorialrepublic.com</a> you will learn the essential web development technologies along with real life practice examples, so that you can create your own website to connect with the people around the world.</p>
<p><a href="https://www.tutorialrepublic.com" target="_blank" class="btn btn-success btn-lg">Get started today</a></p>
</div>
<div class="row">
<div class="col-md-4">
<h2>HTML</h2>
<p>HTML is the standard markup language for describing the structure of the web pages. Our HTML tutorials will help you to understand the basics of latest HTML5 language, so that you can create your own web pages or website.</p>
<p><a href="https://www.tutorialrepublic.com/html-tutorial/" target="_blank" class="btn btn-success">Learn More &raquo;</a></p>
</div>
<div class="col-md-4">
<h2>CSS</h2>
<p>CSS is used for describing the presentation of web pages. CSS can save a lot of time and effort. Our CSS tutorials will help you to learn the essentials of latest CSS3, so that you can control the style and layout of your website.</p>
<p><a href="https://www.tutorialrepublic.com/css-tutorial/" target="_blank" class="btn btn-success">Learn More &raquo;</a></p>
</div>
<div class="col-md-4">
<h2>Bootstrap</h2>
<p>Bootstrap is a powerful front-end framework for faster and easier web development. Our Bootstrap tutorials will help you to learn all the features of latest Bootstrap 4 framework so that you can easily create responsive websites.</p>
<p><a href="https://www.tutorialrepublic.com/twitter-bootstrap-tutorial/" target="_blank" class="btn btn-success">Learn More &raquo;</a></p>
</div>
</div>
<hr>
<footer>
<div class="row">
<div class="col-md-6">
<p>Copyright &copy; 2021 Tutorial Republic</p>
</div>
<div class="col-md-6 text-md-end">
<a href="#" class="text-dark">Terms of Use</a>
<span class="text-muted mx-2">|</span>
<a href="#" class="text-dark">Privacy Policy</a>
</div>
</div>
</footer>
</div>
</body>
</html>
— The output of the above example will look something like this:
We’ve used the margin utility classes such as .mb-3, .ml-auto, mx-2 etc. to adjust spacing between the elements. Whereas the classes .text-dark, .text-muted, .text-md-right are text utility classes to adjust color and alignment of text. You’ll learn about them in later chapters.
Bootstrap grid system provides an easy and powerful way to create responsive layouts of all shapes and sizes. It is built with flexbox with mobile-first approach. Also, it is fully responsive and uses twelve column system (12 columns available per row) and six default responsive tiers.
You can use the Bootstrap’s predefined grid classes for quickly making the layouts for different types of devices like mobile phones, tablets, laptops, desktops, and so on. For example, you can use the .col-* classes to create grid columns for extra small devices like mobile phones in portrait mode, and the .col-sm-* classes for mobile phones in landscape mode.
Similarly, you can use the .col-md-* classes to create grid columns for medium screen devices like tablets, the .col-lg-* classes for devices like small laptops, the .col-xl-* classes for laptops and desktops, and the .col-xxl-* classes for large desktop screens.
The following table summarizes the key features of the Bootstrap’s grid system.
FeaturesBootstrap Grid System
X-Small (xs)<576px
Small (sm)≥576px
Medium (md)≥768px
Large (lg)≥992px
X-Large (xl)≥1200px
XX-Large (xxl)≥1400px
Container max-width
None (auto)
540px
720px
960px
1140px
1320px
Class prefix
.col-
.col-sm-
.col-md-
.col-lg-
.col-xl-
.col-xxl-
Number of columns
12
Gutter width
1.5rem (.75rem on left and right)
Custom gutters
Yes
Nestable
Yes
Column ordering
Yes
Above table demonstrates one important thing, applying any .col-sm-* class to an element will not only have an effect on small devices, but also on medium, large and extra large devices (viewport width ≥768px), if a .col-md-*, .col-lg-*, .col-xl-*, or .col-xxl-* class is not present.
Similarly, the .col-md-* class will not only have an effect on medium devices, but also on large and extra large devices if a .col-lg-*, .col-xl-*, or .col-xxl-* class is not present.
Now the question arises how to create rows and columns using this 12 column responsive grid system. The answer is pretty simple, at first create a container that acts as a wrapper for your rows and columns using any container classes such as .container, after that create rows inside the container using the .row class, and to create columns inside any row you can use the .col-*, .col-sm-*, .col-md-*, .col-lg-*, .col-xl-* and .col-xxl-* classes.
The columns are actual content area where we will place our contents. In the following sections we will put all these things into real action and see how it actually works:
Creating Two Column Layouts
The following example will show you how to create two column layouts for medium, large and extra large devices like tables, laptops and desktops etc. However, on mobile phones (screen width less than 768px), the columns will automatically become horizontal (2 rows, 1 column).
Example
<div class="container">
<!--Row with two equal columns-->
<div class="row">
<div class="col-md-6">Column left</div>
<div class="col-md-6">Column right</div>
</div>
<!--Row with two columns divided in 1:2 ratio-->
<div class="row">
<div class="col-md-4">Column left</div>
<div class="col-md-8">Column right</div>
</div>
<!--Row with two columns divided in 1:3 ratio-->
<div class="row">
<div class="col-md-3">Column left</div>
<div class="col-md-9">Column right</div>
</div>
</div>
Note: In a grid layout, content must be placed inside the columns (.col and .col-*) and only columns may be the immediate children of rows (.row). Also, rows should be placed inside a container (either fixed or fluid) for proper padding and alignment.
Tip: Grid column widths are set in percentages, so they’re always fluid and sized relative to their parent element. In addition, each column has horizontal padding (called a gutter) for controlling the space between individual columns.
Since the Bootstrap grid system is based on 12 columns, therefore to keep the columns in a one line (i.e. side by side), the sum of the grid column numbers within a single row should not be greater than 12. If you go through the above example code carefully you will find the numbers of grid columns (i.e. col-md-*) add up to twelve (6+6, 4+8 and 3+9) for every row.
Creating Three Column Layouts
Similarly, you can create other layouts based on the above principle. For instance, the following example will typically create three column layouts for laptops and desktops screens. It also works in tablets in landscape mode if screen resolution is more than or equal to 992 pixels (e.g. Apple iPad). However, in portrait mode the grid columns will be horizontal as usual.
Example
<div class="container">
<!--Row with three equal columns-->
<div class="row">
<div class="col-lg-4">Column left</div>
<div class="col-lg-4">Column middle</div>
<div class="col-lg-4">Column right</div>
</div>
<!--Row with three columns divided in 1:4:1 ratio-->
<div class="row">
<div class="col-lg-2">Column left</div>
<div class="col-lg-8">Column middle</div>
<div class="col-lg-2">Column right</div>
</div>
<!--Row with three columns divided unevenly-->
<div class="row">
<div class="col-lg-3">Column left</div>
<div class="col-lg-7">Column middle</div>
<div class="col-lg-2">Column right</div>
</div>
</div>
Note: If more than 12 grid columns are placed within a single row, then each group of extra columns, as a whole, will wrap onto a new line. See column wrapping behavior.
Bootstrap Auto-layout Columns
You can also create equal width columns for all devices (x-small, small, medium, large, x-large, and xx-large) through simply using the class .col, without specifying any column number.
Let’s try out the following example to understand how it exactly works:
Example
<div class="container">
<!--Row with two equal columns-->
<div class="row">
<div class="col">Column one</div>
<div class="col">Column two</div>
</div>
<!--Row with three equal columns-->
<div class="row">
<div class="col">Column one</div>
<div class="col">Column two</div>
<div class="col">Column three</div>
</div>
</div>
Additionally, you can also set the width of one column and let the sibling columns automatically resize around it equally. You may use the predefined grid classes or inline widths.
If you try the following example you’ll find columns in a row with class .col has equal width.
Example
<div class="container">
<!--Row with two equal columns-->
<div class="row">
<div class="col">Column one</div>
<div class="col">Column two</div>
</div>
<!--Row with three columns divided in 1:2:1 ratio-->
<div class="row">
<div class="col">Column one</div>
<div class="col-sm-6">Column two</div>
<div class="col">Column three</div>
</div>
</div>
Column Wrapping Behavior
Now we are going to create more flexible layouts that changes the column orientation based on the viewport size. The following example will create a three column layout on large devices like laptops and desktops, as well as on tablets (e.g. Apple iPad) in landscape mode, but on medium devices like tablets in portrait mode (768px ≤ screen width < 992px), it will change into a two column layout where the third column moves at the bottom of the first two columns.
As you can see in the example above the sum of the medium grid column numbers (i.e. col-md-*) is 3 + 9 + 12 = 24 > 12, therefore the third <div> element with the class .col-md-12 that is adding the extra columns beyond the maximum 12 columns in a .row, gets wrapped onto a new line as one contiguous unit on the medium screen size devices.
Similarly, you can create even more adaptable layouts for your websites using the Bootstrap’s grid column wrapping feature. Here’re some ready to use Bootstrap grid examples.
Creating Multi-Column Layouts with Bootstrap
With the new Bootstrap mobile first flexbox grid system you can easily control how your website layout will render on different types of devices that have different screen or viewport sizes like mobile phones, tablets, desktops, etc. Let’s consider the following illustration.
In the above illustration there are total 12 content boxes in all devices, but its placement varies according to the device screen size, like in mobile device the layout is rendered as one column grid layout which has 1 column and 12 rows placed above one another, whereas in tablet it is rendered as two column grid layout which has 2 columns and 6 rows.
Further, in large screen size devices like laptops and desktops it is rendered as three column grid layout which has 3 columns and 4 rows and finally in extra large screen devices like large desktops it is rendered as four column grid layout which has 4 columns and 3 rows.
Now the question is how we can create such responsive layouts using this Bootstrap flexbox grid system. Let’s start with the primary target device. Suppose our primary target device is laptop or normal desktop. Since our laptop layout has 3 columns and 4 rows i.e. 3×4 grid layout, so the HTML code for making such grid structure would look something like this.
Tip: The .container-lg class makes the container 100% wide if the width of the viewport is less than 992px, thus utilizing the full available width on smaller screens.
If you see the output of the above example in a large device such as a laptop or desktop which has screen or viewport width greater than or equal to 1200px but less than 1400px, you will find the layout has 4 rows where each row has 3 equal columns resulting in 3×4 grid layout.
Now it’s time to customize our layout for other devices. Let’s first start by customizing it for medium devices like tablets (768px ≤ viewport width < 1200px). Since on tablet our layout rendered as 2×6 grids (i.e. 2 columns and 6 rows). So, go ahead and add the class .col-md-6 on every column.
Tip: For convenience choose your primary target device and create layout for that device first after that add classes to make it responsive for other devices.
Similarly, you can customize the layout for extra extra large devices such as a large desktop screen by adding the class .col-xxl-3 on each column, as every row in that layout contains 4 columns (i.e. 4×3 grids layout). Here’s the final code after combining the whole process.
Tip: According to the above illustration there is no need to customize the layout for mobile phones; since columns on extra small devices will automatically become horizontal and rendered as 1×12 column grid layout in absence of .col-* or .col-sm-* classes.
Nesting of Grid Columns
The Bootstrap grid columns are also nestable, that means you can put rows and columns inside an existing column. However, the formula for placing the columns will be the same, i.e. the sum of column numbers should be equal to 12 or less within a single row.
Example
<div class="container">
<div class="row">
<div class="col-sm-8">Column left</div>
<div class="col-sm-4">
<!--Column right with nested rows and columns-->
<div class="row">
<div class="col-12"></div>
</div>
<div class="row">
<div class="col-6"></div>
<div class="col-6"></div>
</div>
</div>
</div>
</div>
Creating Variable Width Columns
You can use the col-{breakpoint}-auto classes to size columns based on the natural width of their content. Try out the following example to see how it works:
You can use the flexbox alignment utilities to vertically and horizontally align grid columns inside a container. Try out the following examples to understand how it works:
Vertical Alignment of Grid Columns
You can use the classes .align-items-start, .align-items-center, and .align-items-end to align the grid columns vertically at the top, middle and bottom of a container, respectively.
Note: You can skip the number in .col-* grid class and just use the .col class to create equal size columns for all devices (extra small, small, medium, large, and extra large).
Horizontal Alignment of Grid Columns
You can use the classes .justify-content-start, .justify-content-center, and .justify-content-end to align the grid columns horizontally at the left, center and right of a container, respectively. Let’s check out the following example to see how it works:
Alternatively, you can use the class .justify-content-around to distribute grid columns evenly with half-size spaces on either end, whereas you can use the class .justify-content-between to distribute the grid columns evenly where the first column placed at the start and the last column placed at the end. Try out the following example to see how it actually works:
Please check out the tutoiral on css3 flexbox to learn more about flex items alignment.
Reordering of Grid Columns
You can even change the visual order of your grid columns without changing their order in actual markup. Use the class .order-last to order the column in last, whereas use the class .order-first to order the column at first place. Let’s checkout an example:
Example
<div class="container">
<div class="row">
<div class="col order-last">First, but ordered at last</div>
<div class="col">Second, but unordered</div>
<div class="col order-first">Last, but ordered at first</div>
</div>
</div>
You can also use the .order-* classes to order the grid columns depending on the order numbers. Grid column with higher order number comes after the grid column with lower order number or grid column with no order classes. It includes support for 1 through 12 across all five grid tiers.
Example
<div class="container">
<div class="row">
<div class="col order-4">First, but ordered at last</div>
<div class="col">Second, but ordered at first</div>
<div class="col order-1">Last, but ordered at second</div>
</div>
</div>
Offsetting the Grid Columns
You can also move grid columns to the right for alignment purpose using the column offset classes like .offset-sm-*, .offset-md-*, .offset-lg-*, and so on.
These classes offset the columns by simply increasing its left margin by specified number of columns. For example, the class .offset-md-4 on column .col-md-8 moves it to the right over four columns from its original position. Try out the following example to see how it works:
You can also offset columns using the margin utility classes. These classes are useful in the situations where the width of the offset is not fixed. Here’s an example:
Example
<div class="container">
<div class="row">
<div class="col-md-4"></div>
<div class="col-md-4 ms-auto"><!--Offset this column to right--></div>
</div>
<div class="row">
<div class="col-auto me-auto"></div>
<div class="col-auto"><!--Move this column away from previous column--></div>
</div>
</div>
Note: You can use the class .col-auto to create columns that only take up as much space as needed, i.e. the column sizes itself based on the contents.
Creating Compact Columns
You can remove the default gutters between columns to create compact layouts by adding the class .g-0 on .row. This class removes the negative margins from row and the horizontal padding from all immediate children columns. Here’s an example:
You can also create equal-width columns that span multiple rows by inserting a <div> with .w-100 class where you want the columns to break to a new line. Additionally, you can make these breaks responsive by combining the .w-100 class with responsive display utility classes.
Example
<div class="container">
<!-- Break columns on all devices -->
<div class="row">
<div class="col">.col</div>
<div class="col">.col</div>
<div class="w-100"></div>
<div class="col">.col</div>
<div class="col">.col</div>
</div>
<!-- Break columns on all devices except extra large devices -->
<div class="row">
<div class="col">.col</div>
<div class="col">.col</div>
<div class="w-100 d-xl-none"></div>
<div class="col">.col</div>
<div class="col">.col</div>
</div>
</div>
We hope you’ve understood the basics of new Bootstrap 5 grid system. In next few chapters you’ll learn how to create basic web page layouts using this flexbox grid system.
Containers are the most basic layout element in Bootstrap and are required when using the grid system. Containers are basically used to wrap content with some padding. They are also used to align the content horizontally center on the page in case of fixed width layout.
Bootstrap provides three different types containers:
.container, which has a max-width at each responsive breakpoint.
.container-fluid, which has 100% width at all breakpoints.
.container-{breakpoint}, which has 100% width until the specified breakpoint.
The table below illustrates how each container’s max-width changes across each breakpoint.
For example, when using the .container class the actual width of the container will be 100% if the viewport width is <576px, 540px if the viewport width is ≥576px but <768px, 720px if the viewport width is ≥768px but <992px, 960px if the viewport width is ≥992px but <1200px, 1140px if the viewport width is ≥1200px but <1400px, and 1320px if the viewport width is ≥1400px.
Similarly, when you use the .container-lg class the actual width of the container will be 100% until the viewport width is <992px, 960px if the viewport width is ≥992px but <1200px, 1140px if the viewport width ≥1200px but <1400px, and 1320px if the viewport width is ≥1400px.
ClassesBootstrap Grid System
X-Small<576px
Small≥576px
Medium≥768px
Large≥992px
X-Large≥1200px
XX-Large≥1400px
.container
100%
540px
720px
960px
1140px
1320px
.container-sm
100%
540px
720px
960px
1140px
1320px
.container-md
100%
100%
720px
960px
1140px
1320px
.container-lg
100%
100%
100%
960px
1140px
1320px
.container-xl
100%
100%
100%
100%
1140px
1320px
.container-xxl
100%
100%
100%
100%
100%
1320px
.container-fluid
100%
100%
100%
100%
100%
100%
Creating Responsive Fixed-width Containers
You can simply use the .container class to create a responsive, fixed-width container. The width of the container will change at different breakpoints or screen sizes, as shown above.
Example
<div class="container">
<h1>This is a heading</h1>
<p>This is a paragraph of text.</p>
</div>
Creating Fluid Containers
You can use the .container-fluid class to create a full width container. The width of the fluid container will always be 100% irrespective of the devices or screen sizes.
Example
<div class="container-fluid">
<h1>This is a heading</h1>
<p>This is a paragraph of text.</p>
</div>
Specify Responsive Breakpoints for Containers
Since Bootstrap v4.4, you can also create containers that is 100% wide until the specified breakpoint is reached, after which max-width for each of the higher breakpoints will be applied.
For example, .container-xl will be 100% wide until the xl breakpoint is reached (i.e., viewport width ≥ 1200px), after which max-width for xl breakpoint is applied, which is 1140px.
Example
<div class="container-sm">100% wide until screen size less than 576px</div>
<div class="container-md">100% wide until screen size less than 768px</div>
<div class="container-lg">100% wide until screen size less than 992px</div>
<div class="container-xl">100% wide until screen size less than 1200px</div>
Adding Background and Borders to Containers
By default, container doesn’t have any background-color or border. But if you need you can apply your own styles, or simply use the Bootstrap background-color and border utility classes to add background-color or border on them, as shown in the following example.
Example
<!-- Container with dark background and white text color -->
<div class="container bg-dark text-white">
<h1>This is a heading</h1>
<p>This is a paragraph of text.</p>
</div>
<!-- Container with light background -->
<div class="container bg-light">
<h1>This is a heading</h1>
<p>This is a paragraph of text.</p>
</div>
<!-- Container with border -->
<div class="container border">
<h1>This is a heading</h1>
<p>This is a paragraph of text.</p>
</div>
Applying Paddings and Margins to Containers
By default, containers have padding of 12px on the left and right sides, and no padding on the top and bottom sides. To apply extra padding and margins you can use the spacing utility classes.
Example
<!-- Container with border, extra paddings and margins -->
<div class="container border py-3 my-3">
<h1>This is a heading</h1>
<p>This is a paragraph of text.</p>
In this tutorial you will learn how easy it is to create a web page using Bootstrap. But before begin, be sure to have a code editor and some working knowledge of HTML and CSS.
If you’re just starting out in the world of web development, start learning from here »
Well, let’s get started creating our first Bootstrap powered web page.
Creating Your First Web Page with Bootstrap
Now we’re going to create a basic Bootstrap template by including the Bootstrap CSS and JS files via CDN. Bootstrap requires a third-party library Popper.js for some of its components like popovers and tooltips. You can either include it separately or simply include Bootstrap JS bundled with Popper.
We recommend adding Bootstrap in your project via CDN (Content Delivery Network) because CDN offers performance benefit by reducing the loading time, since they are hosting the files on multiple servers spread across the globe so that when a user requests the file it will be served from the server nearest to them. We’re also using the CDN links in our examples:
Let’s walk through the following steps. At the end of this tutorial, you’ll have made a simple Bootstrap powered web page that displays “Hello world” message in your web browser.
Step 1: Creating a Basic HTML file
Open up your favorite code editor and create a new HTML file. Start with an empty window and type the following code and save it as “basic.html” on your desktop.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Basic HTML File</title>
</head>
<body>
<h1>Hello, world!</h1>
</body>
</html>
Tip: Always include the viewport <meta> tag inside the <head> section of your document to enable touch zooming and ensure proper rendering on mobile devices.
Step 2: Making this HTML File a Bootstrap Template
In order to make this plain HTML file a Bootstrap template, just include the Bootstrap CSS and JS files using their CDN links. Also, you should include JavaScript files at the bottom of the page, right before the closing </body> tag to improve the performance of your web pages.
And we’re all set! After adding the Bootstrap’s CSS and JS files to our HTML page, we can begin to develop any responsive site or application with the Bootstrap framework.
The attributes integrity and crossorigin have been added to CDN links to implement Subresource Integrity (SRI). It is a security feature that enables you to mitigate the risk of attacks originating from compromised CDNs, by ensuring that the files your website fetches (from a CDN or anywhere) have been delivered without unexpected or malicious modifications. It works by allowing you to provide a cryptographic hash that a fetched file must match.
Note: The biggest change in Bootstrap 5 is it doesn’t require jQuery anymore. However, you can still use jQuery to quickly implement Bootstrap component’s methods and options. If Bootstrap detects jQuery in the window object it’ll add all of its components in jQuery’s plugin system. You will learn about them in advanced section of this tutorial series.
Tip: If the visitor to your website has already downloaded the Bootstrap’s CSS and JS files from the same CDN while visiting the other sites, it will be loaded from the browser’s cache instead of re-downloading, which leads to faster loading time.
Step 3: Saving and Viewing the File
Now save the file on your desktop as “bootstrap-template.html”.
To open this file in a web browser, navigate to it, then right click on it, and select your favorite web browser. Alternatively, you can open your browser and drag this file to it.
Note: It is important that the extension .html is specified, some text editors, such as Notepad on Windows, will automatically save it as .txt otherwise.
Downloading the Bootstrap Files
Alternatively, you can also download the Bootstrap’s CSS and JS files from their official website and include in your project. There are two versions available for download, compiled Bootstrap and Bootstrap source files. You can download Bootstrap 5 files from here.
Compiled download contains compiled and minified version of CSS and JavaScript files for faster and easier web development. The compiled version also includes optional JavaScript dependencies such as Popper bundled with Bootstrap JS (bootstrap.bundle.*). Whereas, the source download contains original source files for all CSS and JavaScript, along with a local copy of the docs.
Download and unzip the compiled Bootstrap. Now if you look inside the folders you’ll find it contains the compiled CSS and JS files (bootstrap.*), as well as the compiled and minified CSS and JS (bootstrap.min.*). Use the bootstrap.min.css and bootstrap.bundle.min.js files.
Using the minified version of CSS and JS files will improve the performance of your website and saves the precious bandwidth because of lesser HTTP request and download size.
Tip: There’s no need to include the required Popper.js (in order to make the Bootstrap’s tooltip and popover components work) separately, as it is already included in the “bootstrap.bundle.js” and minified “bootstrap.bundle.min.js” files.
Bootstrap is a powerful front-end framework for faster and easier web development. It includes HTML and CSS based design templates for creating common user interface components like forms, buttons, navigations, dropdowns, alerts, modals, tabs, accordions, carousels, tooltips, and so on.
Bootstrap gives you ability to create flexible and responsive web layouts with much less efforts.
Bootstrap was originally created by a designer and a developer at Twitter in mid-2010. Before being an open-sourced framework, Bootstrap was known as Twitter Blueprint.
You can save a lot of time and effort with Bootstrap. So bookmark this website and continue on.
Tip: Our Bootstrap tutorials will help you to learn the basic as well as advanced features of the Bootstrap step-by-step through easy-to-understand explanation of every topic. If you’re a beginner, start with the basics and gradually move forward by learning a little bit every day.
What You Can Do with Bootstrap
There are lot more things you can do with Bootstrap.
You can easily create responsive websites.
You can quickly create multi-column layout with pre-defined classes.
You can quickly create different types of form layouts.
You can quickly create different variation of navigation bar.
You can easily create components like accordions, modals, etc. without writing any JS code.
You can easily create dynamic tabs to manage large amount of content.
You can easily create tooltips and popovers to show hint text.
You can easily create carousel or image slider to showcase your content.
You can quickly create different types of alert boxes.
The list does not end here, there are many other interesting things that you can do with Bootstrap. You will learn about all of them in detail in upcoming chapters.
Advantages of Using Bootstrap
If you have had some experience with any front-end framework, you might be wondering what makes Bootstrap so special. Here are some advantages why one should opt for Bootstrap framework:
Save lots of time — You can save lots of time and efforts using the Bootstrap predefined design templates and classes and concentrate on other development work.
Responsive features — Using Bootstrap you can easily create responsive websites that appear more appropriately on different devices and screen resolutions without any change in markup.
Consistent design — All Bootstrap components share the same design templates and styles through a central library, so the design and layout of your web pages will be consistent.
Easy to use — Bootstrap is very easy to use. Anybody with the basic working knowledge of HTML, CSS and JavaScript can start development with Bootstrap.
Compatible with browsers — Bootstrap is created with modern web browsers in mind and it is compatible with all modern browsers such as Chrome, Firefox, Safari, Internet Explorer, etc.
Open Source — And the best part is, it is completely free to download and use.
Note: Bootstrap is responsive by default with a mobile first approach. Bootstrap 5 is the latest and most stable version of the Bootstrap. Bootstrap 5 is supported in all major modern browsers such Google Chrome, Firefox, Safari, Internet Explorer 10 and above, etc.
What This Tutorial Covers
This Bootstrap tutorial series covers all the features of the Bootstrap framework, starting with the basics, such as grid system, typography styling mechanism, form styling methods, as well as, techniques of styling common user interface elements like tables, lists, images, etc.
Further you will learn how to utilize the ready to use Bootstrap components such as list groups, input groups, button groups, cards, navbar, breadcrumbs, pagination, labels and badges, progress bars, etc. as well as how to customize them to create their different variations.
Finally, you will explore some advanced features of the Bootstrap like creating modal window, dynamic tabs, tooltips, alerts, accordion menu, carousel, scrollspy, etc. as well as how to customize them or extend their existing functionality using the available options and methods.
In the previous chapter you’ve seen how to do simple animations like animating a property from one value to another via CSS3 transitions feature. However, the CSS3 transitions provide little control on how the animation progresses over time.
The CSS3 animations take it a step further with keyframe-based animations that allow you to specify the changes in CSS properties over time as a set of keyframes, like flash animations. Creating CSS animations is a two step process, as shown in the example below:
The first step of building a CSS animation is to defining individual keyframes and naming an animation with a keyframes declaration.
The second step is referencing the keyframes by name using the animation-name property as well as adding animation-duration and other optional animation properties to control the animation’s behavior.
However, it is not necessary to define the keyframes rules before referencing or applying it. The following example will show you how to animate a <div> box horizontally from one position to another using the CSS3 animation feature.
}
/* Chrome, Safari, Opera */
@-webkit-keyframes moveit {
from {left: 0;}
to {left: 50%;}
}
/* Standard syntax */
@keyframes moveit {
from {left: 0;}
to {left: 50%;}
}
You must specify at least two properties animation-name and the animation-duration (greater than 0), to make the animation occur. However, all the other animation properties are optional, as their default values don’t prevent an animation from happening.
Note: Not all CSS properties are animatable. In general, any CSS property that accepts values that are numbers, lengths, percentages, or colors is animatable.
Defining Keyframes
Keyframes are used to specify the values for the animating properties at various stages of the animation. Keyframes are specified using a specialized CSS at-rule — @keyframes. The keyframe selector for a keyframe style rule starts with a percentage (%) or the keywords from (same as 0%) or to (same as 100%). The selector is used to specify where a keyframe is constructed along the duration of the animation.
Percentages represent a percentage of the animation duration, 0% represents the starting point of the animation, 100% represents the end point, 50% represents the midpoint and so on. That means, a 50% keyframe in a 2s animation would be 1s into an animation.
There are many properties to consider when creating the animations. However, it is also possible to specify all the animations properties in one single property to shorten the code.
The animation property is a shorthand property for setting all the individual animation properties at once in the listed order. For example:
Example
.box {
width: 50px;
height: 50px;
background: red;
position: relative;
/* Chrome, Safari, Opera */
-webkit-animation: repeatit 2s linear 0s infinite alternate;
/* Standard syntax */
animation: repeatit 2s linear 0s infinite alternate;
}
/* Chrome, Safari, Opera */
@-webkit-keyframes repeatit {
from {left: 0;}
to {left: 50%;}
}
/* Standard syntax */
@keyframes repeatit {
from {left: 0;}
to {left: 50%;}
}
Note: If any value is missing or not specified, the default value for that property will be used instead. That means, if the value for animation-duration property is missing, no transition will occur, because its default value is 0.
CSS3 Animation Properties.
The following table provides a brief overview of all the animation-related properties.
Property
Description
animation
A shorthand property for setting all the animation properties in single declaration.
animation-name
Specifies the name of @keyframes defined animations that should be applied to the selected element.
animation-duration
Specifies how many seconds or milliseconds that an animation takes to complete one cycle of the animation.
animation-timing-function
Specifies how the animation will progress over the duration of each cycle i.e. the easing functions.
animation-delay
Specifies when the animation will start.
animation-iteration-count
Specifies the number of times an animation cycle should be played before stopping.
animation-direction
Specifies whether or not the animation should play in reverse on alternate cycles.
animation-fill-mode
Specifies how a CSS animation should apply styles to its target before and after it is executing.
animation-play-state
Specifies whether the animation is running or paused.
@keyframes
Specifies the values for the animating properties at various points during the animation.
Normally when the value of a CSS property changes, the rendered result is instantly updated. A common example is changing the background color of a button on mouse hover. In a normal scenario the background color of the button is changes immediately from the old property value to the new property value when you place the cursor over the button.
CSS3 introduces a new transition feature that allows you to animate a property from the old value to the new value smoothly over time. The following example will show you how to animate the background-color of an HTML button on mouse hover.
Example
button {
background: #fd7c2a;
/* For Safari 3.0+ */
-webkit-transition-property: background;
-webkit-transition-duration: 2s;
/* Standard syntax */
transition-property: background;
transition-duration: 2s;
}
button:hover {
background: #3cc16e;
}
To make the transition occur, you must specify at least two things — the name of the CSS property to which you want to apply the transition effect using the transition-property CSS property, and the duration of the transition effect (greater than 0) using the transition-duration CSS property. However, all the other transition properties are optional, as their default values don’t prevent a transition from happening.
Note: Not all CSS properties are animatable. In general, any CSS property that accepts values that are numbers, lengths, percentages, or colors is animatable.
Performing Multiple Transitions
Each of the transition properties can take more than one value, separated by commas, which provides an easy way to define multiple transitions at once with different settings.
There are many properties to consider when applying the transitions. However, it is also possible to specify all the transition properties in one single property to shorten the code.
The transition property is a shorthand property for setting all the individual transition properties (i.e., transition-property, transition-duration, transition-timing-function, and transition-delay) at once in the listed order.
It’s important to stick to this order for the values, when using this property.
Note: If any value is missing or not specified, the default value for that property will be used instead. That means, if the value for transition-duration property is missing, no transition will occur, because its default value is 0.
CSS3 Transition Properties
The following table provides a brief overview of all the transition properties:
With CSS3 3D transform feature you can perform basic transform manipulations such as move, rotate, scale and skew on elements in a three-dimensional space.
A transformed element doesn’t affect the surrounding elements, but can overlap them, just like the absolutely positioned elements. However, the transformed element still takes space in the layout at its default (un-transformed) location.
Using CSS Transform and Transform Functions
The CSS3 transform property uses the transform functions to manipulate the coordinate system used by an element in order to apply the transformation effect.
The following section describes the 3D transform functions:
The translate3d() Function
Moves the element from its current position to a new position along the X, Y and Z-axis. This can be written as translate(tx, ty, tz). Percentage values are not allowed for third translation-value parameter (i.e. tz).
-webkit-transform: translate3d(25px, 25px, 50px); /* Chrome, Safari, Opera */
transform: translate3d(25px, 25px, 50px); /* Standard syntax */
}
The function translate3d(25px, 25px, 50px) moves the image 25 pixels along the positive X and Y-axis, and the 50 pixels along the positive Z-axis.
The 3D transform however uses the three-dimensional coordinate system, but movement along the Z-direction is not always noticeable because the elements exist on a two-dimensional plane (a flat surface) and have no depth.
The perspective and perspective-origin CSS properties can be used to add a feeling of depth to a scene by making the elements higher on the Z-axis i.e. closer to the viewer appear larger, and those further away to the viewer appear smaller.
Note: If you apply 3D transformation to an element without setting the perspective the resulting effect will not appear as three-dimensional.
The rotate3d() Function
The rotate3d() function rotates the element in 3D space by the specified angle around the [x,y,z] direction vector. This can be written as rotate(vx, vy, vz, angle).
-webkit-transform: rotate3d(0, 1, 0, 60deg); /* Chrome, Safari, Opera */
transform: rotate3d(0, 1, 0, 60deg); /* Standard syntax */
}
The function rotate3d(0, 1, 0, 60deg) rotates the image along the Y-axis by the angle 60 degrees. You can use negative values to rotate the element in opposite direction.
The scale3d() Function
The scale3d() function changes the size of an element. It can be written as scale(sx, sy, sz). The effect of this function is not evident unless you use it in combination with other transform functions such as rotate and the perspective, as shown in the example below.
The function scale3d(1, 1, 2) scales the elements along the Z-axis and the function rotate3d(1, 0, 0, 60deg) rotates the image along the X-axis by the angle 60 degrees.
The matrix3d() Function
The matrix3d() function can perform all of the 3D transformations such as translate, rotate, and scale at once. It takes 16 parameters in the form of a 4×4 transformation matrix.
Here is an example of performing the 3D transformation using the matrix3d() function.
However, when performing more than one transformation at once, it is more convenient to use the individual transformation function and list them in order, like this:
The following table provides a brief overview of all the 3D transformation functions.
Function
Description
translate3d(tx,ty,tz)
Moves the element by the given amount along the X, Y & Z-axis.
translateX(tx)
Moves the element by the given amount along the X-axis.
translateY(ty)
Moves the element by the given amount along the Y-axis.
translateZ(tz)
Moves the element by the given amount along the Z-axis.
rotate3d(x,y,z, a)
Rotates the element in 3D space by the angle specified in the last parameter, around the [x,y,z] direction vector.
rotateX(a)
Rotates the element by the given angle around the X-axis.
rotateY(a)
Rotates the element by the given angle around the Y-axis.
rotateZ(a)
Rotates the element by the given angle around the Z-axis.
scale3d(sx,sy,sz)
Scales the element by the given amount along the X, Y and Z-axis. The function scale3d(1,1,1) has no effect.
scaleX(sx)
Scales the element along the X-axis.
scaleY(sy)
Scales the element along the Y-axis.
scaleZ(sz)
Scales the element along the Z-axis.
matrix3d(n,n,n,n,n,n, n,n,n,n,n,n,n,n,n,n)
Specifies a 3D transformation in the form of a 4×4 transformation matrix of 16 values.
perspective(length)
Defines a perspective view for a 3D transformed element. In general, as the value of this function increases, the element will appear further away from the viewer.
With CSS3 2D transform feature you can perform basic transform manipulations such as move, rotate, scale and skew on elements in a two-dimensional space.
A transformed element doesn’t affect the surrounding elements, but can overlap them, just like the absolutely positioned elements. However, the transformed element still takes space in the layout at its default (un-transformed) location.
Using CSS Transform and Transform Functions
The CSS3 transform property uses the transform functions to manipulate the coordinate system used by an element in order to apply the transformation effect.
The following section describes these transform functions:
The translate() Function
Moves the element from its current position to a new position along the X and Y axes. This can be written as translate(tx, ty). If ty isn’t specified, its value is assumed to be zero.
The function translate(200px, 50px) moves the image 200 pixels horizontally along the positive x-axis, and 50 pixels vertically along the positive y-axis.
The rotate() Function
The rotate() function rotates the element around its origin (as specified by the transform-origin property) by the specified angle. This can be written as rotate(a).
The function rotate(30deg) rotates the image in clockwise direction about its origin by the angle 30 degrees. You can use negative values to rotate the element counter-clockwise.
The scale() Function
The scale() function increases or decreases the size of the element. It can be written as scale(sx, sy). If sy isn’t specified, it is assumed to be equal to sx.
The function scale(1.5) proportionally scale the width and height of the image 1.5 times to its original size. The function scale(1) or scale(1, 1) has no effect on the element.
The skew() Function
The skew() function skews the element along the X and Y axes by the specified angles. It can be written as skew(ax, ay). If ay isn’t specified, its value is assumed to be zero.
The function skew(-40deg, 15deg) skews the element -40 degree horizontally along the x-axis, and 15 degree vertically along the y-axis.
The matrix() Function
The matrix() function can perform all of the 2D transformations such as translate, rotate, scale, and skew at once. It takes six parameters in the form of a matrix which can be written as matrix(a, b, c, d, e, f). The following section will show you how each of the 2D transformation functions can be represented using the matrix().
translate(tx, ty) = matrix(1, 0, 0, 1, tx, ty); — where tx and ty are the horizontal and vertical translation values.
rotate(a) = matrix(cos(a), sin(a), -sin(a), cos(a), 0, 0); — where a is the value in deg. You can swap the sin(a) and -sin(a) values to reverse the rotation. The maximum rotation you could perform is 360 degrees.
scale(sx, sy) = matrix(sx, 0, 0, sy, 0 ,0); — where sx and sy are the horizontal and vertical scaling values.
skew(ax, ay) = matrix(1, tan(ay), tan(ay), 1, 0 ,0); — where ax and ay are the horizontal and vertical values in deg.
Here is an example of performing the 2D transformation using the matrix() function.
However, when performing more than one transformation at once, it is more convenient to use the individual transformation function and list them in order, like this:
The CSS3 gives you ability to add drop shadow effects to the elements like you do in Photoshop without using any images. Prior to CSS3, sliced images are used for creating the shadows around the elements that was quite annoying.
The following section will describe you how to apply the shadow on text and elements.
CSS3 box-shadow Property
The box-shadow property can be used to add shadow to the element’s boxes. You can even apply more than one shadow effects using a comma-separated list of shadows. The basic syntax of creating a box shadow can be given with:
box-shadow: offset-x offset-y blur-radius color;
The components of the box-shadow property have the following meaning:
offset-x — Sets the horizontal offset of the shadow.
offset-y — Sets the vertical offset of the shadow.
blur-radius — Sets the blur radius. The larger the value, the bigger the blur and more the shadow’s edge will be blurred. Negative values are not allowed.
color — Sets the color of the shadow. If the color value is omitted or not specified, it takes the value of the color property.
See the CSS3 box-shadow property to learn more about the other possible values.
Note: When adding the box-shadow, if the value for the blur-radius component is not specified, or set to zero (0), the edges of the shadow will be sharp.
Similarly, you can add the multiple box shadow using a comma-separated list:
You can use the text-shadow property to apply the shadow effects on text. You can also apply multiple shadows to text using the same notation as box-shadow.
Example
h1 {
text-shadow: 5px 5px 10px #666;
}
h2 {
text-shadow: 5px 5px 10px red, 10px 10px 20px yellow;