๐งฑ Build a Responsive Flexbox Card Layout in CSS
Modern websites often use cards to display content like blog posts, products, profiles, or services. These cards should look good on both desktop and mobile devices. That’s where Flexbox in CSS comes to the rescue.
In this guide, you’ll learn how to create a responsive card layout using Flexbox, with easy-to-understand explanations and clean code examples.
๐ง What You Will Build
You’ll create a layout like this:
-
A row of cards on large screens
-
Cards stack vertically on small screens (mobile)
-
Each card will have a title and description
-
Fully responsive using only CSS
๐ Step 1: Create the HTML Structure
Let’s start by writing the HTML. This is the skeleton of our layout.
๐ Explanation:
-
.card-container
is the outer box that holds all the cards. -
Each
.card
is an individual box with a title and paragraph.
๐จ Step 2: Style the Layout with CSS
Now let’s make it look good using CSS Flexbox.
Create a file named styles.css
and add the following styles:
๐ง Step-by-Step CSS Explanation
โ
display: flex
Turns the container into a Flexbox, which allows us to align cards horizontally.
โ
flex-wrap: wrap
If there’s not enough space in one row, the cards will move to the next line.
โ
justify-content: center
Centers the cards horizontally inside the container.
โ
gap: 20px
Adds space between the cards—cleaner than using margin manually.
โ
.card
Each card:
-
Has a fixed width (
300px
) -
Padding inside the card
-
White background with subtle shadow
-
Rounded corners
โ Hover effect
When you move your mouse over a card, it moves up slightly, making it feel interactive.
๐ฑ Step 3: Make It Responsive
We want the cards to look great on mobile too. Let’s add a media query to stack them vertically.
Add this at the end of your CSS:
๐ What It Does:
When the screen size is 768px or smaller, each card stretches to fill the container’s width. Cards appear in a vertical stack, which is easier to read on phones.
๐ผ๏ธ Bonus: Add Image to Cards
Want to make your cards more attractive? Add an image inside each card.
Update the HTML:
Add This to Your CSS:
โ Final Output Summary
-
โ Clean and responsive layout
-
โ Cards align in a row on desktop
-
โ Stack vertically on mobile
-
โ Looks professional with hover effect
-
โ No JavaScript needed
๐ Conclusion
Creating responsive layouts doesn’t have to be hard. With Flexbox, you can build flexible and beautiful card layouts with just a few lines of CSS. This technique works great for blogs, product listings, portfolios, and more.
Now you’re ready to build your own responsive card designs and take your CSS skills to the next level!