Say Goodbye to Floats: Mastering CSS Grid and Flexbox for Modern Layouts


For years, web developers have relied on float-based layouts to arrange content on a webpage. While floats serve their purpose, they often lead to complex code, tricky alignment issues, and extra effort in clearing them.
Thankfully, modern CSS layout techniques—CSS Grid and Flexbox—offer a better way to design flexible, responsive, and easy-to-maintain layouts.
In this guide, I’ll walk you through both layout systems, breaking them down in simple terms. By the end, you’ll know when to use Grid and when to use Flexbox, with real-world examples and a complete list of properties to help you apply them effectively.
What is CSS Grid?
CSS Grid is a two-dimensional layout system that allows you to create layouts using rows and columns. Think of it as a table, but it is way more flexible!
With Grid, you can precisely place elements on your page without relying on hacks like float: left;
or position: absolute;
.
Key Features of CSS Grid:
Works in two dimensions (rows and columns)
Offers precise placement of elements
Enables gap spacing between elements
Easily reorders elements without changing the HTML structure
How to Use CSS Grid (Example)
Let’s create a simple three-column layout with CSS Grid:
Step 1: Create the HTML Structure
<div class="grid-container">
<div class="grid-item">1</div>
<div class="grid-item">2</div>
<div class="grid-item">3</div>
</div>
Step 2: Apply CSS Grid
.grid-container {
display: grid; /* Enables Grid */
grid-template-columns: 1fr 1fr 1fr; /* Three equal columns */
gap: 10px; /* Adds spacing between grid items */
}
.grid-item {
background: lightblue;
padding: 20px;
text-align: center;
}
Important CSS Grid Properties:
Properties | Description |
display: grid | Turns the container into a Grid layout |
grid-template-columns | Defines the number of columns |
grid-template-rows | Defines the number of rows |
grid-column-gap / grid-row-gap | Defines the spacing between columns/rows (If the spacing is to be equal for both columns and rows just use gap instead) |
grid-template-areas | Defines named areas in the grid layout |
grid-auto-rows / grid-auto-columns | Defines the size of automatically created rows/columns |
grid-column / grid-row | Specifies how many columns/rows an element should span |
align-items | Align grid items vertically |
justify-items | Align grid items horizontally |
place-items | Combines align-items and justify-items |
Above are the most commonly used CSS Grid properties.
What is Flexbox?
CSS Flexbox is a one-dimensional layout system, meaning it works either in a row or a column. Unlike Grid, which controls both axes, Flexbox is designed for distributing space efficiently along a single axis.
Flexbox is perfect for:
Aligning items horizontally (row) or vertically (column)
Centering elements effortlessly
Handling dynamic resizing of items
How to Use Flexbox (Example)
Let’s create a horizontal navigation bar using Flexbox:
Step 1: Create the HTML Structure
<div class="nav">
<div class="nav-item">Home</div>
<div class="nav-item">About</div>
<div class="nav-item">Contact</div>
</div>
Step 2: Apply CSS Grid
.nav {
display: flex; /* Enables Flexbox */
justify-content: space-between; /* Distributes items evenly */
background: black;
padding: 10px;
}
.nav-item {
color: white;
padding: 10px;
}
Important CSS Flexbox Properties:
Property | Description |
display: flex; | Turns the container into a Flexbox layout |
flex-direction | Defines the direction of items (row , column , row-reverse , column-reverse ) |
justify-content | Controls horizontal alignment (flex-start , center , flex-end , space-between , space-around , space-evenly ) |
align-items | Controls vertical alignment (flex-start , center , flex-end , stretch , baseline ) |
align-content | Defines space distribution for multiple rows (flex-start , center , space-between , space-around , stretch ) |
flex-wrap | Determines whether items should wrap (nowrap , wrap , wrap-reverse ) |
flex-grow | Defines how much an item should grow compared to others |
flex-shrink | Defines how much an item should shrink compared to others |
flex-basis | Defines the initial size of an item before flexing |
order | Changes the order of items in the layout |
Above are the most commonly used CSS Flexbox properties.
When to Use Grid vs. Flexbox
Feature | CSS Grid | Flexbox |
Works in two dimensions | ✅ Yes | ❌ No |
Works in one dimension (row/column) | ❌ No | ✅ Yes |
Ideal for full page layouts | ✅ Yes | ❌ No |
Best for aligning items inside a container | ❌ No | ✅ Yes |
Real-World Examples:
Use Grid for complex page layouts, such as dashboards, cards, or section-based layouts.
Use Flexbox for smaller UI elements, such as navigation bars, buttons, or centering elements.
Gone are the days of frustrating float-based layouts. CSS Grid and Flexbox are powerful tools that help you build responsive and maintainable web pages with minimal effort.
By understanding when to use Grid vs. Flexbox, you’ll create layouts that are not only visually appealing but also easy to manage and responsive.
So, next time you're structuring a webpage, ask yourself:
👉 Do I need a two-dimensional layout? Use Grid.
👉 Do I need a one-dimensional flexible arrangement? Use Flexbox.
With these tools in your CSS toolkit, you'll never need to struggle with floats again! Happy coding!
To go deep on each topic, kindly check out these CSS Grid, and CSS Flexbox
Subscribe to my newsletter
Read articles from Geraldine Okeke directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Geraldine Okeke
Geraldine Okeke
I am a software engineer with experience in creating inclusive user interfaces. My specialization lies in the development of high-performance web applications across various sectors, including e-commerce, educational technology, and healthcare. As a leader within the community, I actively mentor and educate aspiring developers in front-end development. I am passionate about creating accessible solutions and am eager to apply my expertise in front-end technologies while exploring and working with new advancements in the field.