BootStrap 5
BootStrap 5
CSS Framework:
BOOTSTARP 5
Bootstrap gives you ability to create flexible and responsive web layouts with
much less efforts.
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.
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.
</body>
</html>
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 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.
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.
<div class="container">
<h1>This is a heading</h1>
<p>This is a paragraph of text.</p>
</div>
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.
<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>
The # character in the classes .mt-#, .pt-#, etc. represents the size of the margin or
padding. The value of the size can be an integer from 0 to 5, or auto.
Tip: You can also apply negative margins using the margin utility classes. Just
append the size number with the character n, like .mt-n1, .mb-n2, and so on.
Float Classes
You can use the float utility classes to quickly float an element to the left or right
or remove float from an element altogether. Here's the list of available float
classes.
Class Description
.float-start Float an element to the left.
.float-end Float an element to the right.
.float-none Disable floating from an element.
.clearfix Clear floats to prevent parent element from collapsing.
.float-*-start Float an element to the left on based on the viewport size.
.float-*-end Float an element to the right on based on the viewport size.
.float-*-none Disable floating from an element on based on the viewport size.
See https://www.tutorialrepublic.com/twitter-bootstrap-tutorial/bootstrap-grid-
system.php for more detail.
Shadow Classes
You can use the shadow utility classes to quickly add or remove shadows to
elements.
Class Description
.shadow Add a shadow to an element.
.shadow-sm Add a small shadow to an element.
.shadow-lg Add a larger shadow to an element.
.shadow-none Remove shadow from an element.
Sizing Classes
You can use the sizing utility classes to easily make an element as wide or as tall.
Class Description
.w-25 Set the width of an element to 25% of its parent element.
.w-50 Set the width of an element to 50% of its parent element.
.w-75 Set the width of an element to 75% of its parent element.
.w-100 Set the width of an element to 100% of its parent element.
.w-auto Set the width of an element to auto, i.e. the browser will calculate and select a width for
the specified element.
.mw-100 Set the max-width of an element to 100%.
.h-25 Set the height of an element to 25% of its parent element.
.h-50 Set the height of an element to 50% of its parent element.
.h-75 Set the height of an element to 75% of its parent element.
.h-100 Set the height of an element to 100% of its parent element.
.h-auto Set the height of an element to auto, i.e. the browser will calculate and select a height
for the specified element.
.mh-100 Set the max-height of an element to 100%.
.vw-100 Set the width of an element to 100% of the width of the viewport.
.min-vw-100 Set the min-width of an element to 100% of the width of the viewport.
.vh-100 Set the height of an element to 100% of the height of the viewport.
.min-vh-100 Set the min-height of an element to 100% of the height of the viewport.
Position Classes
You can use the position utility classes to quickly configuring the position of an
element.
Class Description
.position-static Set the position of element to static.
.position-relative Set the position of element to relative.
.position-absolute Set the position of element to absolute.
.position-fixed Set the position of element to fixed.
.position-sticky Set the position of element to sticky.
.fixed-top Position an element at the top of the viewport, from edge to edge.
.fixed-bottom Position an element at the bottom of the viewport, from edge to edge.
.sticky-top Position an element at the top of the viewport, from edge to edge, but that will
only happen after you scroll past it.
Display Classes
You can use the display utility classes to quickly and responsively toggle the
display value of an element. You can also control an element's display when
printing the page.
Class Description
.d-none Hide an element. It does not take up any space in the layout.
.d-inline Force an element to generate an inline-level box.
.d-inline-block Force an element to generate a block box that will be flowed with surrounding
content i.e. in the same line as adjacent content like inline box.
.d-block Force an element to generate a block-level box.
.d-table Force an element to behave like a <table> element.
.d-table-row Force an element to behave like a <tr> element.
.d-table-cell Force an element to behave like a <td> element.
.d-flex Force an element to generate a block-level flex container box.
.d-inline-flex Force an element to generate an inline-level flex container box.
You can also use the responsive breakpoints (sm, md, lg, xl, and xxl.) with these
display utility classes to show or hide elements depending on viewport width
using the format .d-{breakpoint}-{value}, for example, .d-sm-none, .d-md-block, .d-lg-
block, .d-xl-block, .d-xxl-block, and so on.
Tip: You can create print variant of the display classes to change the display value
of elements when printing by simply inserting the string print- after .d-, for
example, .d-print-none, .d-print-inline, .d-print-block, .d-print-table,
and so on.
Miscellaneous Classes
Here's the list some more utility classes that can used for various purposes.
Class Description
.btn-close Create generic close icon for dismissing components like modals and alerts.
.overflow-auto Provide scrollbars to view overflowed content of an element if required.
.overflow-hidden Hide overflowed content of an element.
.overflow-visible Show overflowed content of an element.
.overflow-scroll Provide scrollbars to view overflowed content of an element.
.stretched-link Make an element clickable by stretching a nested link.
.visible Make an element visible, without modifying the display.
.invisible Make an element invisible, but it still take up space in the page.
.visually-hidden Hide an element to all devices except screen readers.
Bootstrap Grid System
What is Bootstrap Grid System?
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.
Features X-Small (xs) Small (sm) Medium (md) Large (lg) X-Large (xl) XX-Large (xxl)
Bootstrap Grid System <576px ≥576px ≥768px ≥992px ≥1200px ≥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:
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.
Let's try out the following example to understand how it exactly works:
<div class="container">
<!--Row with two equal columns-->
<div class="row">
<div class="col">Column one</div>
<div class="col">Column two</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.
<div class="container">
<!--Row with two equal columns-->
<div class="row">
<div class="col">Column one</div>
<div class="col">Column two</div>
</div>
////////////////////////////////////////
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Bootstrap Auto-resize Columns</title>
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.c
ss"
rel="stylesheet"
/>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js
"></script>
<style>
/* Some custom styles to beautify this example */
.row {
margin-top: 1rem;
background: #dbdfe5;
}
[class^="col"] {
padding: 10px 15px;
background: #abb1b8;
border: 1px solid rgba(0, 0, 0, 0.5);
}
</style>
</head>
<body>
<div class="container">
<!--Row with two equal columns-->
<div class="row">
<div class="col">.col</div>
<div class="col">.col</div>
</div>
///////////////////////////////////////////////////////////
<body>
<h2 class="text-center mt-3">Bootstrap Column Wrapping</h2>
<div class="text-center my-3">
Open the output in a new blank tab (Click the arrow next to "Show Output"
button) and resize the browser window to understand how the Bootstrap
column wrapping works.
</div>
<div class="container">
<div class="row">
<div class="col-md-4 col-lg-3">
<div class="demo-content">.col-md-4 .col-lg-3</div>
</div>
<div class="col-md-8 col-lg-6">
<div class="demo-content bg-alt">.col-md-8 .col-lg-6</div>
</div>
<div class="col-md-12 col-lg-3">
<div class="demo-content">.col-md-12 .col-lg-3</div>
</div>
</div>
</div>
</body>
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
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. 3x4 grid layout, so the HTML code for making such
grid structure would look something like this.
<div class="container-lg">
<div class="row">
<div class="col-xl-4"><p>Box 1</p></div>
<div class="col-xl-4"><p>Box 2</p></div>
<div class="col-xl-4"><p>Box 3</p></div>
<div class="col-xl-4"><p>Box 4</p></div>
<div class="col-xl-4"><p>Box 5</p></div>
<div class="col-xl-4"><p>Box 6</p></div>
<div class="col-xl-4"><p>Box 7</p></div>
<div class="col-xl-4"><p>Box 8</p></div>
<div class="col-xl-4"><p>Box 9</p></div>
<div class="col-xl-4"><p>Box 10</p></div>
<div class="col-xl-4"><p>Box 11</p></div>
<div class="col-xl-4"><p>Box 12</p></div>
</div>
</div>
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.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Bootstrap Grid Layouts for Large Devices</title>
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.c
ss"
rel="stylesheet"
/>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js
"></script>
<style>
/* Some custom styles to beautify this example */
p {
padding: 50px;
font-size: 32px;
font-weight: bold;
text-align: center;
background: #dbdfe5;
}
</style>
</head>
<body>
<h2 class="text-center mt-3">Bootstrap Responsive Layout</h2>
<div class="text-center my-3">
Open the output in a new blank tab (Click the arrow next to "Show Output"
button) and resize the browser window to understand how the Bootstrap
responsive grid system works.
</div>
<div class="container-lg">
<div class="row">
<div class="col-xl-4"><p>Box 1</p></div>
<div class="col-xl-4"><p>Box 2</p></div>
<div class="col-xl-4"><p>Box 3</p></div>
<div class="col-xl-4"><p>Box 4</p></div>
<div class="col-xl-4"><p>Box 5</p></div>
<div class="col-xl-4"><p>Box 6</p></div>
<div class="col-xl-4"><p>Box 7</p></div>
<div class="col-xl-4"><p>Box 8</p></div>
<div class="col-xl-4"><p>Box 9</p></div>
<div class="col-xl-4"><p>Box 10</p></div>
<div class="col-xl-4"><p>Box 11</p></div>
<div class="col-xl-4"><p>Box 12</p></div>
</div>
</div>
</body>
</html>
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 3x4 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 2x6 grids (i.e. 2 columns and 6
rows). So, go ahead and add the class .col-md-6 on every column.
<div class="container-lg">
<div class="row">
<div class="col-xl-4 col-md-6"><p>Box 1</p></div>
<div class="col-xl-4 col-md-6"><p>Box 2</p></div>
<div class="col-xl-4 col-md-6"><p>Box 3</p></div>
<div class="col-xl-4 col-md-6"><p>Box 4</p></div>
<div class="col-xl-4 col-md-6"><p>Box 5</p></div>
<div class="col-xl-4 col-md-6"><p>Box 6</p></div>
<div class="col-xl-4 col-md-6"><p>Box 7</p></div>
<div class="col-xl-4 col-md-6"><p>Box 8</p></div>
<div class="col-xl-4 col-md-6"><p>Box 9</p></div>
<div class="col-xl-4 col-md-6"><p>Box 10</p></div>
<div class="col-xl-4 col-md-6"><p>Box 11</p></div>
<div class="col-xl-4 col-md-6"><p>Box 12</p></div>
</div>
</div>
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.
//////////////////////////////
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Bootstrap Grid Layouts for Large Devices</title>
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.c
ss"
rel="stylesheet"
/>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js
"></script>
<style>
/* Some custom styles to beautify this example */
p {
padding: 50px;
font-size: 32px;
font-weight: bold;
text-align: center;
background: #dbdfe5;
}
</style>
</head>
<body>
<h2 class="text-center mt-3">Bootstrap Responsive Layout</h2>
<div class="text-center my-3">
Open the output in a new blank tab (Click the arrow next to "Show Output"
button) and resize the browser window to understand how the Bootstrap
responsive grid system works.
</div>
<div class="container-lg">
<div class="row">
<div class="col-xl-4 col-md-6"><p>Box 1</p></div>
<div class="col-xl-4 col-md-6"><p>Box 2</p></div>
<div class="col-xl-4 col-md-6"><p>Box 3</p></div>
<div class="col-xl-4 col-md-6"><p>Box 4</p></div>
<div class="col-xl-4 col-md-6"><p>Box 5</p></div>
<div class="col-xl-4 col-md-6"><p>Box 6</p></div>
<div class="col-xl-4 col-md-6"><p>Box 7</p></div>
<div class="col-xl-4 col-md-6"><p>Box 8</p></div>
<div class="col-xl-4 col-md-6"><p>Box 9</p></div>
<div class="col-xl-4 col-md-6"><p>Box 10</p></div>
<div class="col-xl-4 col-md-6"><p>Box 11</p></div>
<div class="col-xl-4 col-md-6"><p>Box 12</p></div>
</div>
</div>
</body>
</html>
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. 4x3 grids layout). Here's the final code
after combining the whole process.
<div class="container-lg">
<div class="row">
<div class="col-xl-4 col-md-6 col-xxl-3"><p>Box 1</p></div>
<div class="col-xl-4 col-md-6 col-xxl-3"><p>Box 2</p></div>
<div class="col-xl-4 col-md-6 col-xxl-3"><p>Box 3</p></div>
<div class="col-xl-4 col-md-6 col-xxl-3"><p>Box 4</p></div>
<div class="col-xl-4 col-md-6 col-xxl-3"><p>Box 5</p></div>
<div class="col-xl-4 col-md-6 col-xxl-3"><p>Box 6</p></div>
<div class="col-xl-4 col-md-6 col-xxl-3"><p>Box 7</p></div>
<div class="col-xl-4 col-md-6 col-xxl-3"><p>Box 8</p></div>
<div class="col-xl-4 col-md-6 col-xxl-3"><p>Box 9</p></div>
<div class="col-xl-4 col-md-6 col-xxl-3"><p>Box 10</p></div>
<div class="col-xl-4 col-md-6 col-xxl-3"><p>Box 11</p></div>
<div class="col-xl-4 col-md-6 col-xxl-3"><p>Box 12</p></div>
</div>
</div>
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 1x12 column grid layout in absence of .col-
* or .col-sm-* classes.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Bootstrap Grid Layouts for All Devices</title>
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.c
ss"
rel="stylesheet"
/>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js
"></script>
<style>
/* Some custom styles to beautify this example */
p {
padding: 50px;
font-size: 32px;
font-weight: bold;
text-align: center;
background: #dbdfe5;
}
</style>
</head>
<body>
<h2 class="text-center mt-3">Bootstrap Responsive Layout</h2>
<div class="text-center my-3">
Open the output in a new blank tab (Click the arrow next to "Show Output"
button) and resize the browser window to understand how the Bootstrap
responsive grid system works.
</div>
<div class="container-lg">
<div class="row">
<div class="col-xl-4 col-md-6 col-xxl-3"><p>Box 1</p></div>
<div class="col-xl-4 col-md-6 col-xxl-3"><p>Box 2</p></div>
<div class="col-xl-4 col-md-6 col-xxl-3"><p>Box 3</p></div>
<div class="col-xl-4 col-md-6 col-xxl-3"><p>Box 4</p></div>
<div class="col-xl-4 col-md-6 col-xxl-3"><p>Box 5</p></div>
<div class="col-xl-4 col-md-6 col-xxl-3"><p>Box 6</p></div>
<div class="col-xl-4 col-md-6 col-xxl-3"><p>Box 7</p></div>
<div class="col-xl-4 col-md-6 col-xxl-3"><p>Box 8</p></div>
<div class="col-xl-4 col-md-6 col-xxl-3"><p>Box 9</p></div>
<div class="col-xl-4 col-md-6 col-xxl-3"><p>Box 10</p></div>
<div class="col-xl-4 col-md-6 col-xxl-3"><p>Box 11</p></div>
<div class="col-xl-4 col-md-6 col-xxl-3"><p>Box 12</p></div>
</div>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Bootstrap Nested Rows and Columns</title>
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.c
ss"
rel="stylesheet"
/>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js
"></script>
<style>
/* Some custom styles to beautify this example */
.main-content {
min-height: 230px;
background: #dbdfe5;
}
.sidebar-content {
min-height: 100px;
margin-bottom: 30px;
background: #b4bac0;
}
</style>
</head>
<body>
<div class="container mt-3">
<div class="row">
<div class="col-sm-8">
<div class="main-content"></div>
</div>
<div class="col-sm-4">
<!--Nested rows within a column-->
<div class="row">
<div class="col-12">
<div class="sidebar-content"></div>
</div>
</div>
<div class="row">
<div class="col-6">
<div class="sidebar-content"></div>
</div>
<div class="col-6">
<div class="sidebar-content"></div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
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:
<div class="container">
<div class="row justify-content-md-center">
<div class="col-md-3">Column left</div>
<div class="col-md-auto">Variable width column</div>
<div class="col-md-3">Column right</div>
</div>
<div class="row">
<div class="col">Column left</div>
<div class="col-auto">Variable width column</div>
<div class="col">Column right</div>
</div>
</div>
/////////////////////////
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Sizing Columns Based on the Width of their Content</title>
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.c
ss"
rel="stylesheet"
/>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js
"></script>
<style>
/* Some custom styles to beautify this example */
.row {
margin-top: 1rem;
background: #dbdfe5;
}
[class^="col"] {
padding: 10px 15px;
background: #abb1b8;
border: 1px solid rgba(0, 0, 0, 0.5);
}
</style>
</head>
<body>
<div class="container">
<div class="row justify-content-md-center">
<div class="col-md-3">Column left</div>
<div class="col-md-auto">Variable width column</div>
<div class="col-md-3">Column right</div>
</div>
<div class="row">
<div class="col">Column left</div>
<div class="col-auto">Variable width column</div>
<div class="col">Column right</div>
</div>
</div>
<p class="text-center mt-3">
<strong>Note:</strong> Enter some more content inside the variable width
column to understand how it works.
</p>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Bootstrap Justify Grid Columns</title>
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.c
ss"
rel="stylesheet"
/>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js
"></script>
<style>
/* Some custom styles to beautify this example */
.row {
margin-top: 1rem;
background: #dbdfe5;
}
[class^="col-"] {
padding: 10px 15px;
background: #abb1b8;
border: 1px solid rgba(0, 0, 0, 0.5);
}
</style>
</head>
<body>
<div class="container">
<div class="row justify-content-around">
<div class="col-4">Column one</div>
<div class="col-4">Column two</div>
</div>
<div class="row justify-content-between">
<div class="col-4">Column one</div>
<div class="col-4">Column two</div>
</div>
</div>
</body>
</html>
////////////////////////////
<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>
/////////////////////////////////////
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Order Grid Columns Using Order Number in Bootstrap</title>
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.c
ss"
rel="stylesheet"
/>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js
"></script>
<style>
/* Some custom styles to beautify this example */
.row {
margin-top: 1rem;
background: #dbdfe5;
}
.col {
padding: 10px 15px;
background: #abb1b8;
border: 1px solid rgba(0, 0, 0, 0.5);
}
</style>
</head>
<body>
<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>
</body>
</html>
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.
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.
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.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap Fixed Layout Example</title>
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet">
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js
"></script>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="container-fluid">
<a href="#" class="navbar-brand">Tutorial Republic</a>
<button type="button" class="navbar-toggler" data-bs-toggle="collapse"
data-bs-target="#navbarCollapse">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarCollapse">
<div class="navbar-nav">
<a href="#" class="nav-item nav-link active">Home</a>
<a href="#" class="nav-item nav-link">Services</a>
<a href="#" class="nav-item nav-link">About</a>
<a href="#" class="nav-item nav-link">Contact</a>
</div>
<div class="navbar-nav ms-auto">
<a href="#" class="nav-item nav-link">Register</a>
<a href="#" class="nav-item nav-link">Login</a>
</div>
</div>
</div>
</nav>
<div class="container">
<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 »</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 »</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 »</a></p>
</div>
</div>
<hr>
<footer>
<div class="row">
<div class="col-md-6">
<p>Copyright © 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.
The following example will create a fluid layout that covers 100% width of the
screen.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Bootstrap Fluid Layout</title>
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.c
ss"
rel="stylesheet"
/>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js
"></script>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="container-fluid">
<a href="#" class="navbar-brand">Tutorial Republic</a>
<button
type="button"
class="navbar-toggler"
data-bs-toggle="collapse"
data-bs-target="#navbarCollapse"
>
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarCollapse">
<div class="navbar-nav">
<a href="#" class="nav-item nav-link active">Home</a>
<a href="#" class="nav-item nav-link">Services</a>
<a href="#" class="nav-item nav-link">About</a>
<a href="#" class="nav-item nav-link">Contact</a>
</div>
<div class="navbar-nav ms-auto">
<a href="#" class="nav-item nav-link">Login</a>
<a href="#" class="nav-item nav-link">Register</a>
</div>
</div>
</div>
</nav>
<div class="container-fluid">
<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 »</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 »</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 »</a
>
</p>
</div>
</div>
<hr />
<footer>
<div class="row">
<div class="col-md-6">
<p>Copyright © 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:
Bootstrap Responsive Layout
Creating Responsive Layout with Bootstrap
With the Bootstrap powerful mobile first flexbox grid system creating the
responsive and mobile friendly websites and applications has become much
easier.
Bootstrap is responsive and mobile friendly from the start. Its six tier grid
classes provides better control over the layout as well as how it will be rendered
on different types of devices like mobile phones, tablets, laptops and desktops,
large screen devices, and so on.
Bootstrap Typography
Working with Headings
You can define all HTML headings, <h1> through <h6> — In the same way you
define in simple HTML document. You can also utilize the heading
classes .h1 through .h6 on other elements, if you want to apply the style on
element's text same as headings.
<h1>h1. Bootstrap heading</h1>
<h2>h2. Bootstrap heading</h2>
<h3>h3. Bootstrap heading</h3>
<h4>h4. Bootstrap heading</h4>
<h5>h5. Bootstrap heading</h5>
<h6>h6. Bootstrap heading</h6>
— The output of the above example will look something like this:
Customizing Headings
Additionally, you can use the <small> tag with .text-muted class to display the
secondary text of any heading in a smaller and lighter variation. Here's an
example:
<h2>
Fancy display heading
<small class="text-muted">With faded secondary text</small>
</h2>
— The output of the above example will look something like this:
Display Headings
Bootstrap also provides display headings that can be used when you need a
heading to stand out. Display headings are displayed in larger font-size but
lighter font-weight.
— The output of the above example will look something like this:
Working with Paragraphs
Bootstrap's global default font-size is 1rem (typically 16px), with a line-
height of 1.5 (typically 24px), which is applied to the <body> element as well as all
the paragraphs i.e. the <p> elements. In addition to that margin-bottom of 1rem is
also applied to all the paragraphs.
You can also make a paragraph stand out by adding the class .lead on it.
<p>This is how a normal paragraph looks like in Bootstrap.</p>
<p class="lead">This is how a paragraph stands out in Bootstrap.</p>
— The HTML code in the above examples will produce the following result:
Tip: In CSS rem stands for "root em". 1rem is equal to the font size of the root
element (i.e. the <html> element), which is 16px in most browsers by default.
Text Alignment
You can easily align text to left, right, and center using the text alignment classes.
<p class="text-start">Left aligned text on all viewport sizes.</p>
<p class="text-center">Center aligned text on all viewport sizes.</p>
<p class="text-end">Right aligned text on all viewport sizes.</p>
— The output of the above example will look something like this:
You can also align text based on screen size using the responsive text alignment
classes. These classes use the same viewport width breakpoints as the grid
system.
<p class="text-sm-center">Text will be center aligned on small sized
(sm) viewports and up.</p>
<p class="text-md-center">Text will be center aligned on medium sized
(md) viewports and up.</p>
<p class="text-lg-center">Text will be center aligned on large sized
(lg) viewports and up.</p>
<p class="text-xl-center">Text will be center aligned on extra-large
sized (xl) viewports and up.</p>
Text Formatting
You are free to use text formatting tags like <strong>, <i>, <small> to make your
text bold, italic, small and so on, in the same way you do in simple HTML page.
Here's an example:
<p><b>This is bold text</b></p>
<p><code>This is computer code</code></p>
<p><em>This is emphasized text</em></p>
<p><i>This is italic text</i></p>
<p><mark>This is highlighted text</mark></p>
<p><small>This is small text</small></p>
<p><strong>This is strongly emphasized text</strong></p>
<p>This is <sub>subscript</sub> and <sup>superscript</sup></p>
<p><ins>This text is inserted to the document</ins></p>
<p><del>This text is deleted from the document</del></p>
— The output of the above example will look something like this:
Text Transformation
You can also transform the text to lowercase, uppercase or make them capitalize.
<p class="text-lowercase">The quick brown fox jumps over the lazy
dog.</p>
<p class="text-uppercase">The quick brown fox jumps over the lazy
dog.</p>
<p class="text-capitalize">The quick brown fox jumps over the lazy
dog.</p>
— The output of the above example will look something like this:
Text Coloring
Colors are the powerful method of conveying important information in website
design.
Bootstrap has handful of emphasis utility classes that can be used for this
purpose such as showing success message in green color, warning or error
message in red color, etc.
<p class="text-primary">Primary: Please read the instructions
carefully before proceeding.</p>
<p class="text-secondary">Secondary: This is featured has been removed
from the latest version.</p>
<p class="text-success">Success: Your message has been sent
successfully.</p>
<p class="text-info">Info: You must agree with the terms and
conditions to complete the sign up process.</p>
<p class="text-warning">Warning: There was a problem with your network
connection.</p>
<p class="text-danger">Danger: An error has been occurred while
submitting your data.</p>
<p class="text-muted">Muted: This paragraph of text is grayed out.</p>
— The output of the above example will look something like this:
Wrapping Long Word
You can use the class .text-break to prevent long word from breaking your layout.
Let's try out the following example to understand how it basically works:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap Word Break</title>
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet">
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js
"></script>
</head>
<body>
<div class="m-4">
<div class="container">
<h2>Without Word Break</h2>
<div class="row">
<div class="col-2">
<p class="bg-warning">veryveryveryveryveryveryverylongword</p>
</div>
</div>
<hr />
<h2>With Word Break</h2>
<div class="row">
<div class="col-2">
<p class="bg-warning text-
break">veryveryveryveryveryveryverylongword</p>
</div>
</div>
</div>
</div>
</body>
</html>
Bootstrap Tables
What is Table?
The HTML tables are used to present data in grid manner like row and columns.
Using Bootstrap you can greatly improve the appearance of table in a quick and
easy way.
— The output of the above example will look something like this:
For example, you can create a dark version of the table (i.e. table with light text
on dark backgrounds) by adding the contextual class .table-dark to
the .table base class, like this:
<table class="table table-dark">
<thead>
<tr>
<th>#</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Clark</td>
<td>Kent</td>
<td>[email protected]</td>
</tr>
<tr>
<td>2</td>
<td>Peter</td>
<td>Parker</td>
<td>[email protected]</td>
</tr>
<tr>
<td>3</td>
<td>John</td>
<td>Carter</td>
<td>[email protected]</td>
</tr>
</tbody>
</table>
Similar to the tables you can also use these contextual classes to emphasize the
rows within a table. Here's an example of a table with emphasized rows, let's take
a look:
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Bill</th>
<th>Payment Date</th>
<th>Payment Status</th>
</tr>
</thead>
<tbody>
<tr class="table-primary">
<td>1</td>
<td>Credit Card</td>
<td>04/07/2021</td>
<td>Waiting for statement</td>
</tr>
<tr class="table-secondary">
<td>2</td>
<td>Insurance</td>
<td>02/07/2021</td>
<td>Cancelled</td>
</tr>
<tr class="table-success">
<td>3</td>
<td>Water</td>
<td>01/07/2021</td>
<td>Paid</td>
</tr>
<tr class="table-info">
<td>4</td>
<td>Internet</td>
<td>05/07/2021</td>
<td>Change plan</td>
</tr>
<tr class="table-warning">
<td>5</td>
<td>Electricity</td>
<td>03/07/2021</td>
<td>Pending</td>
</tr>
<tr class="table-danger">
<td>6</td>
<td>Telephone</td>
<td>06/07/2021</td>
<td>Due</td>
</tr>
<tr class="table-light">
<td>7</td>
<td>Car Service</td>
<td>08/07/2021</td>
<td>Call in to confirm</td>
</tr>
<tr class="table-dark">
<td>8</td>
<td>Gas</td>
<td>06/07/2021</td>
<td>Payment failed</td>
</tr>
</tbody>
</table>
— The output of the above example will look something like this:
— The output of the above example will look something like this:
— The output of the above example will look something like this:
— The output of the above example will look something like this:
— The output of the above example will look something like this:
Creating Small or Compact Tables
You can also make your tables more compact and save the space through adding
the modifier class .table-sm to the .table base class. The .table-sm class makes the
table compact by cutting all cell padding in half. Let's take a look at the following
example:
<table class="table table-sm">
<thead>
<tr>
<th>#</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Clark</td>
<td>Kent</td>
<td>[email protected]</td>
</tr>
<tr>
<td>2</td>
<td>Peter</td>
<td>Parker</td>
<td>[email protected]</td>
</tr>
<tr>
<td>3</td>
<td>John</td>
<td>Carter</td>
<td>[email protected]</td>
</tr>
</tbody>
</table>
— The output of the above example will look something like this:
Setting Table Head Colors
Similar to light and dark tables, you can use the modifier classes .table-
light or .table-dark on the <thead> element to make it appear in light or dark gray.
The following example will create a table with light gray background head.
<table class="table">
<thead class="table-light">
<tr>
<th>#</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Clark</td>
<td>Kent</td>
<td>[email protected]</td>
</tr>
<tr>
<td>2</td>
<td>Peter</td>
<td>Parker</td>
<td>[email protected]</td>
</tr>
<tr>
<td>3</td>
<td>John</td>
<td>Carter</td>
<td>[email protected]</td>
</tr>
</tbody>
</table>
— The output of the above example will look something like this:
The following example will create a table with dark gray background head.
<table class="table">
<thead class="table-dark">
<tr>
<th>#</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Clark</td>
<td>Kent</td>
<td>[email protected]</td>
</tr>
<tr>
<td>2</td>
<td>Peter</td>
<td>Parker</td>
<td>[email protected]</td>
</tr>
<tr>
<td>3</td>
<td>John</td>
<td>Carter</td>
<td>[email protected]</td>
</tr>
</tbody>
</table>
— The output of the above example will look something like this:
To make any table responsive just place it inside a <div> element and apply
the .table-responsive class on it. You can also specify when the table should have
a scrollbar, based on the viewport width (i.e. breakpoints), using the
classes .table-responsive{-sm|-md|-lg|-xl}.
Let's try out the following example to understand how it basically works:
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>#</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Biography</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Clark</td>
<td>Kent</td>
<td>[email protected]</td>
<td>Lorem ipsum dolor sit amet...</td>
</tr>
<tr>
<td>2</td>
<td>Peter</td>
<td>Parker</td>
<td>[email protected]</td>
<td>Integer pulvinar leo id risus...</td>
</tr>
<tr>
<td>3</td>
<td>John</td>
<td>Carter</td>
<td>[email protected]</td>
<td>Vestibulum consectetur scelerisque...</td>
</tr>
</tbody>
</table>
</div>
Tip: Text inside the cells of <thead> are always vertical aligned to the bottom.
Whereas, text inside the cells of <tbody> inherit their alignment from <table> and
are vertically aligned to the top by default. Use the vertical align classes to re-
align text where needed.
Bootstrap Forms
Creating Forms with Bootstrap
HTML forms are an integral part of the web pages and applications, but creating
the form layouts or styling the form controls manually one by one using CSS are
often boring and tedious. Bootstrap greatly simplifies the process of styling and
alignment of form controls like labels, input fields, selectboxes, textareas, buttons,
etc. through predefined set of classes.
Here's an example in which form controls are vertically stacked with labels on the
top.
<body>
<div class="m-4">
<form action="/examples/actions/confirmation.php" method="post">
<div class="mb-3">
<label class="form-label" for="inputEmail">Email</label>
<input
type="email"
class="form-control"
id="inputEmail"
placeholder="Email"
required
/>
</div>
<div class="mb-3">
<label class="form-label" for="inputPassword">Password</label>
<input
type="password"
class="form-control"
id="inputPassword"
placeholder="Password"
required
/>
</div>
<div class="mb-3">
<div class="form-check">
<input class="form-check-input" type="checkbox" id="checkRemember" />
<label class="form-check-label" for="checkRemember"
>Remember me</label
>
</div>
</div>
<button type="submit" class="btn btn-primary">Sign in</button>
</form>
</div>
</body>
— The output of the above example will look something like this:
Note: All textual form controls, such as <input> and <textarea> requires the class
.form-control, while <select> requires the class .form-select for general
styling. These classes also makes the forms controls 100% wide. To change their
width or use them inline, you can utilize the Bootstrap's predefined grid classes.
https://www.tutorialrepublic.com/twitter-bootstrap-tutorial/bootstrap-grid-
system.php
Tip: It is recommend to use margin-bottom utility classes (e.g., mb-2, mb-3, etc.) to
add vertical spacing between the form groups. As, using single direction margin
throughout in the form prevent margin collapsing and create more consist form.
Also, be sure to apply the class .col-form-label on the <label> elements, so that
they're vertically centered with their associated form controls. Let's check out an
example:
<body>
<div class="m-4">
<form action="/examples/actions/confirmation.php" method="post">
<div class="row mb-3">
<label for="inputEmail" class="col-sm-2 col-form-label">Email</label>
<div class="col-sm-10">
<input
type="email"
class="form-control"
id="inputEmail"
placeholder="Email"
required
/>
</div>
</div>
<div class="row mb-3">
<label for="inputPassword" class="col-sm-2 col-form-label"
>Password</label
>
<div class="col-sm-10">
<input
type="password"
class="form-control"
id="inputPassword"
placeholder="Password"
required
/>
</div>
</div>
<div class="row mb-3">
<div class="col-sm-10 offset-sm-2">
<div class="form-check">
<input
class="form-check-input"
type="checkbox"
id="checkRemember"
/>
<label class="form-check-label" for="checkRemember"
>Remember me</label
>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-10 offset-sm-2">
<button type="submit" class="btn btn-primary">Sign in</button>
</div>
</div>
</form>
</div>
</body>
— The output of the above example will look something like this:
Let's take a look at following example and see how it actually works:
<body>
<div class="m-4">
<form action="/examples/actions/confirmation.php" method="post">
<div class="row align-items-center g-3">
<div class="col-auto">
<label class="visually-hidden" for="inputEmail">Email</label>
<input
type="email"
class="form-control"
id="inputEmail"
placeholder="Email"
required
/>
</div>
<div class="col-auto">
<label class="visually-hidden" for="inputPassword">Password</label>
<input
type="password"
class="form-control"
id="inputPassword"
placeholder="Password"
required
/>
</div>
<div class="col-auto">
<div class="form-check">
<input
class="form-check-input"
type="checkbox"
id="checkRemember"
/>
<label class="form-check-label" for="checkRemember"
>Remember me</label
>
</div>
</div>
<div class="col-auto">
<button type="submit" class="btn btn-primary">Sign in</button>
</div>
</div>
</form>
</div>
</body>
— The output of the above example will look something like this:
Check out the snippets section for examples of some beautifully designed
Bootstrap forms.
https://www.tutorialrepublic.com/snippets/gallery.php?tag=form
Tip: It is recommended to include a label for every form inputs otherwise screen
readers will have trouble with your forms. However, in case of inline form layouts
you can hide the labels using the .visually-hidden class, so that only screen
readers can read it.
Creating Responsive Form Layout
You can also make your forms responsive through using the grid classes with
specific breakpoints.
The following example will create a form which laid out inline on medium devices
and up (i.e., viewport width ≥768px), but will become vertically stacked on smaller
viewports.
<body>
<div class="m-4">
<form action="/examples/actions/confirmation.php" method="post">
<div class="row align-items-center g-3">
<div class="col-md-auto col-12">
<label class="form-label d-md-none" for="inputEmail">Email</label>
<input
type="email"
class="form-control"
id="inputEmail"
placeholder="Email"
required
/>
</div>
<div class="col-md-auto col-12">
<label class="form-label d-md-none" for="inputPassword"
>Password</label
>
<input
type="password"
class="form-control"
id="inputPassword"
placeholder="Password"
required
/>
</div>
<div class="col-md-auto col-12">
<div class="form-check">
<input
class="form-check-input"
type="checkbox"
id="checkRemember"
/>
<label class="form-check-label" for="checkRemember"
>Remember me</label
>
</div>
</div>
<div class="col-md-auto col-12">
<button type="submit" class="btn btn-primary">Sign in</button>
</div>
</div>
</form>
<p class="mt-4">
<strong>Note:</strong> Please change the Editor layout/orientation to see
how responsive layout actually works.
</p>
</div>
</body>
The .form-control-plaintext class removes the default styling from the form field,
but preserves the correct margin and padding. Let's take a look at an example:
<body>
<div class="m-4">
<form action="/examples/actions/confirmation.php" method="post">
<div class="row mb-3">
<label for="inputEmail" class="col-sm-2 col-form-label">Email</label>
<div class="col-sm-10">
<input
type="email"
readonly
class="form-control-plaintext"
id="inputEmail"
value="[email protected]"
/>
</div>
</div>
<div class="row mb-3">
<label for="inputPassword" class="col-sm-2 col-form-label"
>Password</label
>
<div class="col-sm-10">
<input
type="password"
class="form-control"
id="inputPassword"
placeholder="Password"
required
/>
</div>
</div>
<div class="row mb-3">
<div class="col-sm-10 offset-sm-2">
<div class="form-check">
<input
class="form-check-input"
type="checkbox"
id="checkRemember"
/>
<label class="form-check-label" for="checkRemember"
>Remember me</label
>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-10 offset-sm-2">
<button type="submit" class="btn btn-primary">Sign in</button>
</div>
</div>
</form>
</div>
</body>
— The output of the above example will look something like this:
Column Sizing of Form Controls
You can also match the size of your inputs, textareas and select boxes to the
Bootstrap grid column sizes. Simply, place your form controls
(i.e. <input>, <textarea>, and <select>) in grid columns.
Let's try out the following example to understand how it basically works:
<body>
<div class="m-4">
<div class="row g-3">
<div class="col-6">
<input type="text" class="form-control" placeholder="City" />
</div>
<div class="col-4">
<select class="form-select">
<option>State</option>
</select>
</div>
<div class="col-2">
<input type="text" class="form-control" placeholder="Zip" />
</div>
</div>
</div>
</body>
— The output of the above example will look something like this:
But, you can also place these custom checkboxes and radio buttons inline (i.e., in
the same line) by simply adding the class .form-check-inline to .form-
check element, like this:
<body>
<div class="m-4">
<h3 class="mb-4">Default Placement of Checkboxes</h3>
<div class="row">
<div class="col-12">
<div class="form-check mb-3">
<input
type="checkbox"
class="form-check-input"
name="hobbies"
id="checkMusic"
/>
<label class="form-check-label" for="checkMusic">Music</label>
</div>
<div class="form-check mb-3">
<input
type="checkbox"
class="form-check-input"
name="hobbies"
id="checkTravel"
checked
/>
<label class="form-check-label" for="checkTravel">Travel</label>
</div>
<div class="form-check">
<input
type="checkbox"
class="form-check-input"
name="hobbies"
id="checkReading"
checked
/>
<label class="form-check-label" for="checkReading">Reading</label>
</div>
</div>
</div>
<hr />
<h3 class="mb-4">Inline Placement of Checkboxes</h3>
<div class="row">
<div class="col-12">
<div class="form-check form-check-inline">
<input
type="checkbox"
class="form-check-input"
name="hobbies"
id="checkMusic"
/>
<label class="form-check-label" for="checkMusic">Music</label>
</div>
<div class="form-check form-check-inline ms-3">
<input
type="checkbox"
class="form-check-input"
name="hobbies"
id="checkTravel"
checked
/>
<label class="form-check-label" for="checkTravel">Travel</label>
</div>
<div class="form-check form-check-inline ms-3">
<input
type="checkbox"
class="form-check-input"
name="hobbies"
id="checkReading"
checked
/>
<label class="form-check-label" for="checkReading">Reading</label>
</div>
</div>
</div>
</div>
</body>
— The output of the above example will look something like this:
Similarly, you can place the radio buttons inline, as shown in the following
example:
<div class="row">
<div class="col-12">
<div class="form-check form-check-inline">
<input
type="radio"
class="form-check-input"
name="gender"
id="radioMale"
checked
/>
<label class="form-check-label" for="radioMale">Male</label>
</div>
<div class="form-check form-check-inline ms-3">
<input
type="radio"
class="form-check-input"
name="gender"
id="radioFemale"
/>
<label class="form-check-label" for="radioFemale">Female</label>
</div>
</div>
</div>
— The output of the above example will look something like this:
— The output of the above example will look something like this:
Similarly, you can also place inline help text using the <small> element. No need
to use .form-text in this case. The following example shows how to implement
this:
<div class="row align-items-center g-3">
<div class="col-auto">
<label class="col-form-label" for="inputPassword">Password</label>
</div>
<div class="col-auto">
<input type="password" class="form-control" id="inputPassword" />
</div>
<div class="col-auto">
<span class="form-text">Must be 8-20 characters long.</span>
</div>
</div>
— The output of the above example will look something like this:
Use the form control height sizing classes such as .form-control-lg and .form-
control-sm on the text inputs to create it's larger or smaller sizes. Here's an
example:
<body>
<div class="m-4">
<div class="row mb-3">
<label class="col-sm-2 col-form-label col-form-label-lg">Email</label>
<div class="col-sm-10">
<input
type="email"
class="form-control form-control-lg"
placeholder="Large input"
/>
</div>
</div>
<div class="row mb-3">
<label class="col-sm-2 col-form-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" placeholder="Default input" />
</div>
</div>
<div class="row">
<label class="col-sm-2 col-form-label">Email</label>
<div class="col-sm-10">
<input
type="email"
class="form-control form-control-sm"
placeholder="Small input"
/>
</div>
</div>
</div>
</body>
— The output of the above example will look something like this:
— The output of the above example will look something like this:
In the following sections you will see how to extend form controls by adding the
text, icons and buttons before, after, or on both sides of it to make your form
more attractive.
Further, wrap the text or icon in a <span> element as well as apply the class .input-
group-text on it and place it before or after the input. Let's take a look at the
following example:
<body>
<div class="m-4">
<div class="row g-2">
<div class="col-6">
<div class="input-group">
<span class="input-group-text">
<span class="bi-person"></span>
</span>
<input type="text" class="form-control" placeholder="Username" />
</div>
</div>
<div class="col-6">
<div class="input-group">
<input type="text" class="form-control" placeholder="Amount" />
<span class="input-group-text">.00</span>
</div>
</div>
<div class="col-6">
<div class="input-group">
<span class="input-group-text">https://www.</span>
<input type="text" class="form-control" placeholder="Domain name" />
</div>
</div>
<div class="col-6">
<div class="input-group">
<span class="input-group-text">$</span>
<input type="text" class="form-control" placeholder="US Dollar" />
<span class="input-group-text">.00</span>
</div>
</div>
</div>
</div>
</body>
— The output of the above example will look something like this:
Since Bootstrap 5 you can also prepend or append select box dropdown and
textarea form controls. Let's try out the following example and see how it
basically works:
<body>
<div class="m-4">
<div class="row g-2">
<div class="col-12">
<div class="input-group">
<span class="input-group-text">Address</span>
<textarea class="form-control"></textarea>
</div>
</div>
<div class="col-6">
<div class="input-group">
<label class="input-group-text">Country</label>
<select class="form-select">
<option selected>Choose...</option>
<option>France</option>
<option>Germany</option>
<option>Hungary</option>
</select>
</div>
</div>
<div class="col-6">
<div class="input-group">
<select class="form-select">
<option selected>Choose...</option>
<option>One</option>
<option>Two</option>
<option>Three</option>
</select>
<button type="button" class="btn btn-secondary">Submit</button>
</div>
</div>
</div>
</div>
</body>
— The output of the above example will look something like this:
Similarly, you can prepend or append addons to Bootstrap's custom file input,
like this:
<div class="input-group">
<input type="file" class="form-control">
<button type="button" class="btn btn-secondary">Upload</button>
</div>
— The output of the above example will look something like this:
//////////////////////
Example:
<body>
<div class="m-4">
<h1 class="border-bottom pb-3 mb-4">Registration Form</h1>
<form
class="needs-validation"
action="/examples/actions/confirmation.php"
method="post"
novalidate
>
<div class="row mb-3">
<label class="col-sm-3 col-form-label" for="firstName"
>First Name:</label
>
<div class="col-sm-9">
<input
type="text"
class="form-control"
id="firstName"
placeholder="First Name"
required
/>
</div>
</div>
<div class="row mb-3">
<label class="col-sm-3 col-form-label" for="lastName">Last Name:</label>
<div class="col-sm-9">
<input
type="text"
class="form-control"
id="lastName"
placeholder="Last Name"
required
/>
</div>
</div>
<div class="row mb-3">
<label class="col-sm-3 col-form-label" for="emailAddress"
>Email Address:</label
>
<div class="col-sm-9">
<input
type="email"
class="form-control"
id="emailAddress"
placeholder="Email Address"
required
/>
</div>
</div>
<div class="row mb-3">
<label class="col-sm-3 col-form-label" for="phoneNumber"
>Mobile Number:</label
>
<div class="col-sm-9">
<input
type="number"
class="form-control"
id="phoneNumber"
placeholder="Phone Number"
required
/>
</div>
</div>
<div class="row mb-3">
<label class="col-sm-3 col-form-label">Date of Birth:</label>
<div class="col-sm-3">
<select class="form-select" required>
<option value="">Date</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
</div>
<div class="col-sm-3">
<select class="form-select" required>
<option value="">Month</option>
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
</div>
<div class="col-sm-3">
<select class="form-select">
<option>Year</option>
</select>
</div>
</div>
<div class="row mb-3">
<label class="col-sm-3 col-form-label" for="postalAddress"
>Postal Address:</label
>
<div class="col-sm-9">
<textarea
rows="3"
class="form-control"
id="postalAddress"
placeholder="Postal Address"
required
></textarea>
</div>
</div>
<div class="row mb-3">
<label class="col-sm-3 col-form-label" for="ZipCode">Zip Code:</label>
<div class="col-sm-9">
<input
type="text"
class="form-control"
id="ZipCode"
placeholder="Zip Code"
required
/>
</div>
</div>
<div class="row mb-3">
<label class="col-sm-3 col-form-label" for="uploadImage"
>Upload Image:</label
>
<div class="col-sm-9">
<input type="file" class="form-control" id="uploadImage" />
</div>
</div>
<div class="row mb-3">
<label class="col-sm-3 col-form-label">Gender:</label>
<div class="col-sm-9 mt-2">
<div class="form-check form-check-inline">
<input
type="radio"
class="form-check-input"
name="gender"
id="radioMale"
/>
<label class="form-check-label" for="radioMale">Male</label>
</div>
<div class="form-check form-check-inline">
<input
type="radio"
class="form-check-input"
name="gender"
id="radioFemale"
/>
<label class="form-check-label" for="radioFemale">Female</label>
</div>
</div>
</div>
<div class="row mb-3">
<div class="col-sm-9 offset-sm-3">
<div class="form-check">
<input
class="form-check-input"
type="checkbox"
id="checkAgree"
value="agree"
/>
<label class="form-check-label" for="checkAgree"
>I agree to the <a href="#">Terms and Conditions</a></label
>
</div>
</div>
</div>
<div class="row mb-3">
<div class="col-sm-9 offset-sm-3">
<input type="submit" class="btn btn-primary" value="Submit" />
<input type="reset" class="btn btn-secondary ms-2" value="Reset" />
</div>
</div>
</form>
</div>
</body>
Bootstrap Buttons
Creating Buttons with Bootstrap
Buttons are the integral part of a website and application. They are used for
various purposes like, submit or reset an HTML form, performing interactive
actions such as showing or hiding something on a web page on click of the
button, redirecting user to another page, and so on. Bootstrap provides a quick
and easy way to create and customize the buttons.
The following example will show you how to create different styles of buttons in
Bootstrap:
<button type="button" class="btn btn-primary">Primary</button>
<button type="button" class="btn btn-secondary">Secondary</button>
<button type="button" class="btn btn-success">Success</button>
<button type="button" class="btn btn-danger">Danger</button>
<button type="button" class="btn btn-warning">Warning</button>
<button type="button" class="btn btn-info">Info</button>
<button type="button" class="btn btn-dark">Dark</button>
<button type="button" class="btn btn-light">Light</button>
<button type="button" class="btn btn-link">Link</button>
— The output of the above example will look something like this:
— The output of the above example will look something like this:
Changing the Sizes of Buttons
Bootstrap gives you option further to scaling a button up or down.
To make buttons larger add an extra class .btn-lg to the buttons, like this:
<button type="button" class="btn btn-primary btn-lg">Large
button</button>
<button type="button" class="btn btn-secondary btn-lg">Large
button</button>
— The output of the above example will look something like this:
Similarly, to make buttons smaller add an extra class .btn-sm to the buttons, like
this:
<button type="button" class="btn btn-primary btn-sm">Small
button</button>
<button type="button" class="btn btn-secondary btn-sm">Small
button</button>
— The output of the above example will look something like this:
You can also create full-width or block buttons (buttons that covers the full width
of the parent elements) through using the Bootstrap's display and gap utility
classes. These utilities offers much greater control over spacing, alignment, and
responsive behaviors.
<div class="d-grid gap-2">
<button type="button" class="btn btn-primary">Block
button</button>
<button type="button" class="btn btn-secondary">Block
button</button>
</div>
— The output of the above example will look something like this:
— The output of the above example will look something like this:
— The output of the above example will look something like this:
Note: The .disabled class only make links visually appear like disabled, however
the link will remain clickable unless you remove the href attribute from it.
To create a button group just wrap a series of buttons with .btn class in
a <div> element and apply the class .btn-group on it. You can additionally apply the
class .active on an individual button to indicate the active state. Let's take a look
at the following example:
<div class="btn-group">
<button type="button" class="btn btn-primary">Home</button>
<button type="button" class="btn btn-primary
active">About</button>
<button type="button" class="btn btn-primary">Services</button>
</div>
— The output of the above example will look something like this:
Similarly, you can also create button groups using outline buttons, like this:
<div class="btn-group">
<button type="button" class="btn btn-outline-
primary">Home</button>
<button type="button" class="btn btn-outline-primary
active">About</button>
<button type="button" class="btn btn-outline-
primary">Services</button>
</div>
— The output of the above example will look something like this:
Mixed Styles Button Groups
You can also mix and match different button styles to create button groups like
this:
<div class="btn-group">
<button type="button" class="btn btn-success">
<i class="bi-eye"></i> View
</button>
<button type="button" class="btn btn-warning">
<i class="bi-pencil"></i> Edit
</button>
<button type="button" class="btn btn-danger">
<i class="bi-trash"></i> Delete
</button>
</div>
— The output of the above example will look something like this:
— The output of the above example will look something like this:
— The output of the above example will look something like this:
— The output of the above example will look something like this:
<div class="btn-group-vertical">
<a href="#" class="btn btn-primary">Home</a>
<a href="#" class="btn btn-primary">About</a>
<div class="btn-group">
<a
href="#"
class="btn btn-primary dropdown-toggle"
data-bs-toggle="dropdown"
>Services</a
>
<div class="dropdown-menu">
<a class="dropdown-item" href="#">Web Design</a>
<a class="dropdown-item" href="#">Web Development</a>
</div>
</div>
</div>
— The output of the above example will look something like this:
Creating Justified Button Groups
You can also stretch your button groups to fill all the available width by applying
the flex utility class .d-flex to the .btn-group element. Every button has equal
width in this case.
<div class="btn-group d-flex">
<button type="button" class="btn btn-primary">Home</button>
<button type="button" class="btn btn-primary">About</button>
<button type="button" class="btn btn-primary">Services</button>
</div>
— The output of the above example will look something like this:
Bootstrap Cards
Using the Bootstrap Cards
Bootstrap card is a flexible and extensible content container. It includes options
for headers and footers, a wide variety of content, contextual background colors,
and powerful display options. Card replaces panel, well, and thumbnail
components in old Bootstrap 3 version.
In the following sections, you will see what you can do with the card component.
— The output of the above example will look something like this:
Note: Card doesn't have fixed width, they'll occupy the full width of its parent
element. So, if you need a fixed width card you need to apply the width property
on card yourself. Also, card have no margin by default,
Content Types for Card Component
The card component support a wide variety of content, including images, text, list
groups, links, navs, and more. Here are the examples of what's supported by the
card.
— The output of the above example will look something like this:
— The output of the above example will look something like this:
Card with Header and Footer
You can also add header and footer within your cards using the .card-
header and .card-footer class, respectively. Let's take a look at the following
example:
<div class="card text-center">
<div class="card-header">Featured</div>
<div class="card-body">
<h5 class="card-title">NASA Launched Solar Probe</h5>
<p class="card-text">NASA launched Parker space probe in 2018
with the mission of making observations of the outer corona of the
Sun. It is the first-ever mission to "touch" the Sun.</p>
<a href="#" class="btn btn-primary">Know more</a>
</div>
<div class="card-footer text-muted">3 years ago</div>
</div>
— The output of the above example will look something like this:
Placing List Groups within Card
You can also place list groups inside the card along with other content types, as
shown here.
<div class="card" style="width: 300px;">
<div class="card-header">Featured</div>
<ul class="list-group list-group-flush">
<li class="list-group-item">An item</li>
<li class="list-group-item">A second item</li>
<li class="list-group-item">A third item</li>
</ul>
<div class="card-body">
<a href="#" class="card-link">Add More</a>
<a href="#" class="card-link">Share</a>
</div>
</div>
— The output of the above example will look something like this:
— The output of the above example will look something like this:
To add tabs navigation to a card simply place the tabs markup inside the card
header, and the tabs content inside the card body. You are also required to use
an additional class .card-header-tabs on the .nav element along with the class .nav-
tabs for proper alignment.
Let's try out the following example which creates an elegant tabbed navigation.
<body>
<div class="m-4">
<div class="card text-center">
<div class="card-header">
<ul class="nav nav-tabs card-header-tabs" id="myTab">
<li class="nav-item">
<a href="#home" class="nav-link active" data-bs-toggle="tab"
>Home</a
>
</li>
<li class="nav-item">
<a href="#profile" class="nav-link" data-bs-toggle="tab">Profile</a>
</li>
<li class="nav-item">
<a href="#messages" class="nav-link" data-bs-toggle="tab"
>Messages</a
>
</li>
</ul>
</div>
<div class="card-body">
<div class="tab-content">
<div class="tab-pane fade show active" id="home">
<h5 class="card-title">Home tab content</h5>
<p class="card-text">
Here is some example text to make up the tab's content. Replace it
with your own text anytime.
</p>
<a href="#" class="btn btn-primary">Go somewhere</a>
</div>
<div class="tab-pane fade" id="profile">
<h5 class="card-title">Profile tab content</h5>
<p class="card-text">
Here is some example text to make up the tab's content. Replace it
with your own text anytime.
</p>
<a href="#" class="btn btn-primary">Go somewhere</a>
</div>
<div class="tab-pane fade" id="messages">
<h5 class="card-title">Messages tab content</h5>
<p class="card-text">
Here is some example text to make up the tab's content. Replace it
with your own text anytime.
</p>
<a href="#" class="btn btn-primary">Go somewhere</a>
</div>
</div>
</div>
</div>
</div>
</body>
— The output of the above example will look something like this:
Similarly, you can add pills nav to the card by using an additional class .card-
header-pills along with the class .nav-pills on the .nav element, as shown
below:
<body>
<div class="m-4">
<div class="card text-center">
<div class="card-header">
<ul class="nav nav-pills card-header-pills">
<li class="nav-item">
<a href="#home" class="nav-link active" data-bs-toggle="tab"
>Home</a
>
</li>
<li class="nav-item">
<a href="#profile" class="nav-link" data-bs-toggle="tab">Profile</a>
</li>
<li class="nav-item">
<a href="#messages" class="nav-link" data-bs-toggle="tab"
>Messages</a
>
</li>
</ul>
</div>
<div class="card-body">
<div class="tab-content">
<div class="tab-pane fade show active" id="home">
<h5 class="card-title">Home tab content</h5>
<p class="card-text">
Here is some example text to make up the tab's content. Replace it
with your own text anytime.
</p>
<a href="#" class="btn btn-primary">Go somewhere</a>
</div>
<div class="tab-pane fade" id="profile">
<h5 class="card-title">Profile tab content</h5>
<p class="card-text">
Here is some example text to make up the tab's content. Replace it
with your own text anytime.
</p>
<a href="#" class="btn btn-primary">Go somewhere</a>
</div>
<div class="tab-pane fade" id="messages">
<h5 class="card-title">Messages tab content</h5>
<p class="card-text">
Here is some example text to make up the tab's content. Replace it
with your own text anytime.
</p>
<a href="#" class="btn btn-primary">Go somewhere</a>
</div>
</div>
</div>
</div>
</div>
</body>
— The output of the above example will look something like this:
Customizing the Card Styles
There are several options available for customizing the card's backgrounds,
borders, and color.
— The output of the above example will look something like this:
— The output of the above example will look something like this:
— The output of the above example will look something like this:
Creating Horizontal Cards
You can also create horizontal cards where image and text content are placed
side-by-side using a combination of grid and utility classes, as shown in the
following example:
<body>
<div class="m-4">
<div class="card" style="max-width: 500px">
<div class="row g-0">
<div class="col-sm-5" style="background: #868e96">
<img
src="/examples/images/sample.svg"
class="card-img-top h-100"
alt="..."
/>
</div>
<div class="col-sm-7">
<div class="card-body">
<h5 class="card-title">Alice Liddel</h5>
<p class="card-text">
Alice is a freelance web designer and developer based in London.
She is specialized in HTML5, CSS3, JavaScript, Bootstrap, etc.
</p>
<a href="#" class="btn btn-primary stretched-link">View Profile</a>
</div>
</div>
</div>
</div>
</div>
</body>
— The output of the above example will look something like this:
— The output of the above example will look something like this:
Let's try out the following example to understand how it basically works:
<body>
<div class="m-4">
<div class="row row-cols-1 row-cols-md-3 g-3">
<div class="col">
<!-- Card with default left text alignment -->
<div class="card">
<div class="card-body">
<h5 class="card-title">Card title</h5>
<p class="card-text">
Some dummy text to make up the card's content. You can replace it
anytime.
</p>
<a href="#" class="btn btn-primary">Know more</a>
</div>
</div>
</div>
<div class="col">
<!-- Card with center text alignment -->
<div class="card text-center">
<div class="card-body">
<h5 class="card-title">Card title</h5>
<p class="card-text">
Some dummy text to make up the card's content. You can replace it
anytime.
</p>
<a href="#" class="btn btn-primary">Know more</a>
</div>
</div>
</div>
<div class="col">
<!-- Card with right text alignment -->
<div class="card text-end">
<div class="card-body">
<h5 class="card-title">Card title</h5>
<p class="card-text">
Some dummy text to make up the card's content. You can replace it
anytime.
</p>
<a href="#" class="btn btn-primary">Know more</a>
</div>
</div>
</div>
</div>
</div>
</body>
Bootstrap Navbar
Creating Navbar with Bootstrap
You can use the Bootstrap navbar component to create responsive navigation
header for your website or application. These responsive navbar will be collapsed
on devices having small viewports like mobile phones but expand when user click
the toggle button. However, it will be horizontal as normal on the medium and
large devices such as laptop or desktop.
You can also create different variations of the navbar such as navbars with
dropdown menus and search boxes as well as fixed positioned navbar with much
less effort. The following example will show you how to create a simple static
navbar with navigation links.
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<a href="#" class="navbar-brand">Brand</a>
<button type="button" class="navbar-toggler" data-bs-
toggle="collapse" data-bs-target="#navbarCollapse">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarCollapse">
<div class="navbar-nav">
<a href="#" class="nav-item nav-link active">Home</a>
<a href="#" class="nav-item nav-link">Profile</a>
<a href="#" class="nav-item nav-link">Messages</a>
<a href="#" class="nav-item nav-link disabled"
tabindex="-1">Reports</a>
</div>
<div class="navbar-nav ms-auto">
<a href="#" class="nav-item nav-link">Login</a>
</div>
</div>
</div>
</nav>
— The output of the above example will look something like this:
— The output of the above example will look something like this:
Tip: Use the utility classes such as .ms-auto, .me-auto, .justify-content-between, etc.
to align the nav links, forms, buttons or text inside the navbar.
— The output of the above example will look something like this:
— The output of the above example will look something like this:
— The output of the above example will look something like this:
Ex: sticky
<body>
<div class="p-5 bg-warning">
<h1>Demo Header</h1>
</div>
<nav class="navbar sticky-top navbar-expand-lg navbar-dark bg-dark">
<div class="container-fluid">
<a href="#" class="navbar-brand">Brand</a>
<button
type="button"
class="navbar-toggler"
data-bs-toggle="collapse"
data-bs-target="#navbarCollapse"
>
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarCollapse">
<div class="navbar-nav">
<a href="#" class="nav-item nav-link active">Home</a>
<a href="#" class="nav-item nav-link">Profile</a>
<a href="#" class="nav-item nav-link">Messages</a>
<a href="#" class="nav-item nav-link disabled" tabindex="-1"
>Reports</a
>
</div>
<div class="navbar-nav ms-auto">
<a href="#" class="nav-item nav-link">Login</a>
</div>
</div>
</div>
</nav>
/////
<body>
<div class="m-4">
<nav class="navbar fixed-bottom navbar-expand-lg navbar-dark bg-dark">
<div class="container-fluid">
<a href="#" class="navbar-brand">Brand</a>
<button
type="button"
class="navbar-toggler"
data-bs-toggle="collapse"
data-bs-target="#navbarCollapse"
>
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarCollapse">
<div class="navbar-nav">
<a href="#" class="nav-item nav-link active">Home</a>
<a href="#" class="nav-item nav-link">Profile</a>
<a href="#" class="nav-item nav-link">Messages</a>
<a href="#" class="nav-item nav-link disabled" tabindex="-1"
>Reports</a
>
</div>
<div class="navbar-nav ms-auto">
<a href="#" class="nav-item nav-link">Login</a>
</div>
</div>
</div>
</nav>
//////
<body>
<div class="m-4">
<nav class="navbar fixed-top navbar-expand-lg navbar-dark bg-dark">
<div class="container-fluid">
<a href="#" class="navbar-brand">Brand</a>
<button
type="button"
class="navbar-toggler"
data-bs-toggle="collapse"
data-bs-target="#navbarCollapse"
>
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarCollapse">
<div class="navbar-nav">
<a href="#" class="nav-item nav-link active">Home</a>
<a href="#" class="nav-item nav-link">Profile</a>
<a href="#" class="nav-item nav-link">Messages</a>
<a href="#" class="nav-item nav-link disabled" tabindex="-1"
>Reports</a
>
</div>
<div class="navbar-nav ms-auto">
<a href="#" class="nav-item nav-link">Login</a>
</div>
</div>
</div>
</nav>