Responsive Flexbox Card Layout
This example demonstrates how to create a responsive card layout using CSS Flexbox. The cards automatically adjust their widths, wrap onto additional rows, and maintain consistent spacing as the available screen space changes.
01
Web Design
Learn how to create attractive, organized webpages using modern HTML and CSS techniques.
- Page structure
- Color and typography
- Responsive styling
Learn More
02
CSS Layouts
Explore flexible layout techniques for arranging webpage content across different screen sizes.
Flexbox is particularly useful for navigation bars, content cards, toolbars, and other one-dimensional layouts.
- Flexible sizing
- Content alignment
- Automatic wrapping
Learn More
03
Responsive Design
Build webpages that adapt to desktops, tablets, and mobile devices.
- Flexible components
- Mobile-friendly layouts
- Accessible navigation
Learn More
04
CSS Effects
Enhance webpage components with subtle visual effects that improve their appearance and usability.
- Hover effects
- Transitions
- Visual feedback
Learn More
Responsive Design: Resize the browser window to see how the cards automatically wrap onto additional rows. Cards in the same row stretch to equal heights, and their buttons remain aligned at the bottom.
How the Flexbox Layout Works
The display: flex declaration creates a Flexbox container that arranges its child elements along the main axis.
The flex-wrap: wrap declaration allows cards to move onto additional rows when there is not enough horizontal space.
The gap: 20px declaration creates consistent spacing between cards.
Each card uses the following declaration:
flex: 1 1 280px;
The first value, 1, allows the card to grow and occupy available space.
The second value, 1, allows the card to shrink when necessary.
The third value, 280px, establishes the card's preferred starting width before available space is distributed.
Creating Equal-Height Cards
The Flexbox container uses align-items: stretch to make cards in the same row stretch to the height of the tallest card.
Each card is also a Flexbox container with flex-direction: column, arranging its content vertically.
The buttons use margin-top: auto to occupy the remaining vertical space above them. This pushes each button to the bottom of its card, even when the cards contain different amounts of text.
This technique is useful for product cards, service descriptions, pricing options, and other repeating components.
Responsive Design
Flexbox automatically adjusts the number of cards displayed in each row based on the available width.
On wider screens, several cards can appear next to each other. As the viewport becomes narrower, cards wrap onto additional rows.
A media query at 480 pixels or less changes the preferred card width to 100%, reduces the spacing, and adjusts the card padding.
This allows the cards to display in a single column on smaller screens without horizontal scrolling.
Customizing the Card Layout
You can change the preferred card width by modifying the flex-basis value in the flex shorthand declaration.
For example, increasing the value to 350px creates wider cards and generally results in fewer cards per row.
Reducing the value to 220px allows more cards to fit within the available space.
You can also customize the background colors, borders, padding, typography, and button styles.
To add another card, duplicate an existing article element with the flex-card class and replace its content.
Web Design
The Web Design card demonstrates how a reusable component can combine headings, descriptions, lists, and a call-to-action link.
CSS Layouts
The CSS Layouts card contains additional text to demonstrate how cards with different content lengths maintain equal heights within the same row.
Responsive Design Details
The Responsive Design card demonstrates how Flexbox can automatically rearrange components as the available screen space changes.
CSS Effects
The CSS Effects card includes a styled button with a hover transition and visible keyboard focus indicator.
The prefers-reduced-motion media query disables the button transition for users who request reduced motion.