This tutorial aims to provide a clear understanding of managing the visual order of your columns based on screen size, a key skill needed for responsive web design.
By the end of this tutorial, you will learn:
- How to use Bootstrap's grid system to control column ordering
- How to create responsive designs that adapt to different screen sizes
Prerequisites:
- Basic knowledge of HTML and CSS
- Familiarity with Bootstrap would be beneficial, but not compulsory.
Bootstrap provides a powerful grid system that allows you to control the order of your layout columns based on the viewport size. This is achieved through the order classes (order-0
, order-1
, ..., order-12
).
You can also control the order for different viewport sizes using order-{sm|md|lg|xl}-{1-12}
classes.
Let's take a look at an example:
<div class="container">
<div class="row">
<div class="col-md-6 order-md-2">Column 1</div>
<div class="col-md-6 order-md-1">Column 2</div>
</div>
</div>
In the code above, Column 1
will appear before Column 2
on medium (md
) screen sizes or larger, but Column 2
will appear first on smaller screens.
Let's see some more practical examples.
<div class="container">
<div class="row">
<div class="col order-2">Column 1</div>
<div class="col order-1">Column 2</div>
</div>
</div>
Here, regardless of the screen size, Column 2
will always appear first.
<div class="container">
<div class="row">
<div class="col-md-4 order-1 order-md-2">Column 1</div>
<div class="col-md-8 order-2 order-md-1">Column 2</div>
</div>
</div>
In this example, Column 2
will appear first on medium (md
) screen sizes or larger, but Column 1
will appear first on smaller screens.
In this tutorial, we learned about managing the visual order of layout columns based on screen size using Bootstrap's grid system. The order-
classes are used to control the column order, and we can adapt this ordering to different screen sizes using order-{sm|md|lg|xl}-{1-12}
classes.
To continue learning, you can explore other grid features in Bootstrap, like alignment and nesting.
<div class="container">
<div class="row">
<div class="col-md-4 order-1 order-md-3">Column 1</div>
<div class="col-md-4 order-2 order-md-2">Column 2</div>
<div class="col-md-4 order-3 order-md-1">Column 3</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-sm-3 order-1 order-sm-4">Column 1</div>
<div class="col-sm-3 order-2 order-sm-1">Column 2</div>
<div class="col-sm-3 order-3 order-sm-2">Column 3</div>
<div class="col-sm-3 order-4 order-sm-3">Column 4</div>
</div>
</div>
Keep practicing to get a better understanding of how column ordering works across different screen sizes. Happy coding!