← Back to all snippets
CSS

Fluid Responsive Card Layout with Flexbox

Create dynamic, responsive card layouts using CSS Flexbox. Cards automatically wrap and adjust their size based on available space, ensuring a clean and adaptable design without media queries.

.card-container {
    display: flex;
    flex-wrap: wrap;
    gap: 20px; /* Provides spacing between items */
    justify-content: center; /* Centers cards if there's extra space */
    padding: 20px;
    max-width: 1200px;
    margin: 0 auto;
}

.card {
    flex: 1 1 300px; /* flex-grow, flex-shrink, flex-basis */
    min-width: 280px; /* Ensures cards don't get too small */
    box-shadow: 0 4px 8px rgba(0,0,0,0.1);
    border-radius: 8px;
    background-color: white;
    padding: 20px;
    text-align: center;
    box-sizing: border-box;
}
How it works: This snippet demonstrates a fluid, responsive grid of cards using Flexbox. The `.card-container` uses `display: flex` and `flex-wrap: wrap` to allow cards to flow onto new lines. Each `.card` uses the `flex: 1 1 300px` shorthand: it can grow (`1`), shrink (`1`), and has a preferred `flex-basis` (width) of `300px`. This setup enables cards to adjust their size and wrap automatically, creating a responsive layout based on available space without needing explicit media queries. The `gap` property efficiently adds space between cards.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs