In this tutorial, we will delve into the basics of using Bootstrap rows and columns to create responsive, flexible, and well-structured layouts. The grid system in Bootstrap uses a series of containers, rows, and columns to layout and align content.
The Bootstrap Grid System
Bootstrap's grid system is built with flexbox and allows up to 12 columns across the page. It’s a powerful tool to create complex layouts with ease. The key elements are:
.container or .container-fluid: Wraps the entire grid and ensures it is centered and has proper padding.
.row: Creates a horizontal group of columns.
.col: Defines the column within a row.
Creating a Basic Grid
To start using the grid system, we need a container, a row, and columns inside the row.
Bootstrap allows you to create responsive layouts that change based on the screen size. You can use different column classes for different breakpoints.
Example:
<!DOCTYPEhtml><htmllang="en"><head> <metacharset="UTF-8"> <metaname="viewport"content="width=device-width, initial-scale=1.0"> <title>Responsive Columns - codeswithpankaj.com</title> <linkhref="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"rel="stylesheet"></head><body> <divclass="container"> <divclass="row"> <divclass="col-12 col-md-8"> <p>Column 1 (12/12 on small screens, 8/12 on medium and larger screens)</p> </div> <divclass="col-6 col-md-4"> <p>Column 2 (6/12 on small screens, 4/12 on medium and larger screens)</p> </div> </div> </div> <scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <scriptsrc="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script></body></html>
Nesting Columns
You can nest columns inside other columns to create complex layouts.
Understanding and utilizing Bootstrap's grid system is essential for creating responsive and structured web layouts. By mastering rows and columns, you can design complex and flexible layouts with ease.