When it comes to creating modern, responsive websites, mastering layout techniques is essential. Two of the most powerful tools in CSS for building flexible and structured designs are Flexbox and CSS Grid. Whether you're a complete beginner or brushing up on web design skills, this CSS tutorial will guide you step by step through both systems in simple terms.
If you’ve ever struggled with aligning items, creating responsive columns, or managing spacing on your webpage, this CSS tutorial for beginners will help you solve those problems with ease.
Before diving into code, let’s understand what Flexbox and Grid are and why they’re important:
Flexbox is a one-dimensional layout system. It excels at aligning items in a row or a column and distributing space between them. It’s perfect for menus, navbars, and small layout components.
Grid is a two-dimensional layout system. It can arrange items in both rows and columns, making it ideal for more complex page layouts like galleries, dashboards, and multi-section pages.
Let’s start with Flexbox, then move on to Grid. This CSS tutorial assumes basic knowledge of HTML and CSS selectors.
To use Flexbox, you need to apply display: flex;
to a container element. All direct children of that container become “flex items.”
<div class="flex-container">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
</div>
.flex-container {
display: flex;
justify-content: space-around;
align-items: center;
height: 100px;
background-color: #f2f2f2;
}
.box {
background-color: #4CAF50;
color: white;
padding: 20px;
text-align: center;
}
justify-content
: Aligns items horizontally (flex-start
, center
, space-between
, etc.)align-items
: Aligns items verticallyflex-direction
: Sets the direction (row
, column
, etc.)flex-wrap
: Allows items to wrap onto multiple linesChange justify-content
to space-between
or flex-end
to see how alignment changes.
Grid allows you to create full page layouts with rows and columns. Apply display: grid;
to a container, then define columns and rows.
<div class="grid-container">
<div class="item">A</div>
<div class="item">B</div>
<div class="item">C</div>
<div class="item">D</div>
</div>
.grid-container {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 10px;
background-color: #e0e0e0;
padding: 10px;
}
.item {
background-color: #2196F3;
color: white;
padding: 20px;
text-align: center;
}
grid-template-columns
: Defines column layoutgrid-template-rows
: Defines row layoutgap
: Adds spacing between itemsgrid-column
/ grid-row
: Lets items span multiple columns or rowsChange repeat(2, 1fr)
to repeat(3, 1fr)
to add more columns, or use fixed widths like 200px
.
In real-world websites, developers often use both together—Grid for the overall layout and Flexbox for smaller components inside.
Both Flexbox and Grid are built for responsive design. Combine them with media queries to adjust layout based on screen size.
@media (max-width: 600px) {
.grid-container {
grid-template-columns: 1fr;
}
}
This CSS will make the grid stack into a single column on smaller screens like mobile phones.
justify-content
and align-items
values.In this CSS tutorial for beginners, you’ve learned how to use Flexbox and Grid—the two most powerful layout tools in modern web design. With just a few lines of CSS, you can create clean, responsive, and organized layouts for all kinds of websites.
The more you experiment and build, the more comfortable you'll get with these techniques. Start with small layout challenges and gradually create full landing pages or portfolio sites. Remember: layout is the foundation of great design, and now you have the tools to master it.