Bootstrap / Bootstrap Layout and Grid System
Nesting and Aligning Elements in Bootstrap
This tutorial focuses on nesting and alignment in Bootstrap. You will learn how to nest rows and columns for complex layouts and use alignment classes to position your content.
Section overview
5 resourcesCovers the layout techniques and grid system for creating responsive designs using Bootstrap.
1. Introduction
Goal of the Tutorial
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.
Learning Outcome
After going through this tutorial, you will be able to:
- Nest rows and columns in Bootstrap
- Align elements using Bootstrap classes
Prerequisites
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.
2. Step-by-Step Guide
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 in Bootstrap
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.
Alignment 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.
3. Code Examples
Example 1: Nesting
<!-- 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.
Example 2: Alignment
<!-- 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.
4. Summary
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.
5. Practice Exercises
- Create a layout with 3 rows, where the second row contains 2 nested rows.
- Align a paragraph to the right and a div block to the center of the page.
Solutions
<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!
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article