This tutorial aims to guide you on building responsive layouts using Bootstrap. By the end of this guide, you'll have a solid understanding of how to create flexible and adaptive layouts that work well on different screen sizes.
Bootstrap uses a grid system that consists of rows and columns, which you can use to layout and align your content. The grid is divided into 12 columns, and you can decide how many columns a particular content should occupy.
A responsive layout adapts to the screen size of the device. Bootstrap makes this easy by using a combination of CSS media queries and flexible grid columns.
In Bootstrap, you can specify how many columns a certain element should span across different screen sizes using class prefixes like .col-
, .col-sm-
, .col-md-
, .col-lg-
, .col-xl-
. These classes correspond to different screen sizes, and the number after the dash specifies the number of columns the element should span.
<div class="container">
<div class="row">
<div class="col">
Column 1
</div>
<div class="col">
Column 2
</div>
</div>
</div>
This example creates a simple two-column layout. The .container
class centers the grid on the page, .row
creates a new row, and .col
creates a column that automatically adjusts its size to fit evenly within the row.
<div class="container">
<div class="row">
<div class="col-12 col-md-6 col-lg-4">
Column 1
</div>
<div class="col-12 col-md-6 col-lg-4">
Column 2
</div>
<div class="col-12 col-md-6 col-lg-4">
Column 3
</div>
</div>
</div>
In this example, on small screens (col-12
), each column takes up the whole row. On medium screens (col-md-6
), each column takes up half the row. On large screens (col-lg-4
), each column takes up one-third of the row.
For further learning, you can explore other Bootstrap components and utilities such as navigation bars, forms, cards, etc.
<div class="container">
<div class="row">
<div class="col-12 col-md-6 col-lg-3">
Column 1
</div>
<div class="col-12 col-md-6 col-lg-3">
Column 2
</div>
<div class="col-12 col-md-6 col-lg-3">
Column 3
</div>
<div class="col-12 col-md-6 col-lg-3">
Column 4
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-12 col-md-6">
Column 1
</div>
<div class="col-12 col-md-6">
Column 2
</div>
</div>
</div>
In these solutions, we're simply applying the concepts we've learned in the tutorial. Practice with different configurations to get a stronger grasp of the Bootstrap grid system.