This tutorial aims to guide you on how to nest rows and columns in Bootstrap for creating complex layouts. Also, we will learn how to use alignment classes to position the content in your HTML document.
After going through this tutorial, you will be able to:
- Nest rows and columns in Bootstrap
- Align elements using Bootstrap classes
You should have a basic understanding of HTML and CSS. It will be helpful if you have some experience with Bootstrap, but it's not mandatory.
Bootstrap, a popular HTML, CSS, and JS framework, allows us to create responsive and mobile-first projects on the web. It provides a grid system that helps us to layout and align content. Let's dive into it.
Nesting is the process of placing a set of columns within an existing column in the Bootstrap grid system. This is useful when you want to create complex layouts.
Here's how you do it:
1. Create a .row
.
2. Inside this .row
, add a .col
.
3. Inside this .col
, you can add another .row
with its own set of .col
’s.
Keep in mind, the sum of the columns in each .row
should be equal to or less than 12 in Bootstrap.
Bootstrap provides various classes to align elements, such as .text-center
, .text-right
, .text-left
, for text alignment. It also provides classes like .mx-auto
to center block-level elements.
<!-- Nesting in Bootstrap -->
<div class="row">
<div class="col">
<div class="row">
<div class="col">Nested Column 1</div>
<div class="col">Nested Column 2</div>
</div>
</div>
<div class="col">Column 2</div>
</div>
In this example, we have a row with two columns. Inside the first column, we created another row with two columns.
<!-- Alignment in Bootstrap -->
<div class="text-center">Center aligned text</div>
<div class="mx-auto" style="width: 200px;">Center aligned block</div>
In this example, the first div contains text that is centered using the class .text-center
. The second div is a block-level element that is centered using the class .mx-auto
.
In this tutorial, we learned about nesting and alignment in Bootstrap. We saw how to nest rows and columns to create complex layouts and use alignment classes to position your content.
For further learning, you can explore more about Bootstrap's grid system and other utility classes.
<div class="row">Row 1</div>
<div class="row">
<div class="col">
<div class="row">Nested Row 1</div>
<div class="row">Nested Row 2</div>
</div>
</div>
<div class="row">Row 3</div>
<p class="text-right">This is a right-aligned paragraph.</p>
<div class="mx-auto" style="width: 200px;">This is a center-aligned div.</div>
Continue practicing to better understand the concepts. Happy coding!