How to create a responsive card layout using CSS Flex layout
How to use Css Flex elastic layout to create a responsive card layout
In modern web design, responsive layout is an essential design method. Flexbox is a powerful and flexible layout model that allows us to create responsive layouts more easily. This article will introduce how to use CSS Flex elastic layout to create a simple responsive card layout and provide specific code examples.
- Create HTML structure
First, we need to create an HTML structure that contains cards. We'll use the ul and li elements to create a card container and add some styling.
<ul class="card-container"> <li class="card"> <img class="card-image lazy" src="/static/imghwm/default1.png" data-src="image1.jpg" alt="How to create a responsive card layout using CSS Flex layout" > <div class="card-content"> <h2 id="Card">Card 1</h2> <p class="card-description">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p> </div> </li> <li class="card"> <img class="card-image lazy" src="/static/imghwm/default1.png" data-src="image2.jpg" alt="How to create a responsive card layout using CSS Flex layout" > <div class="card-content"> <h2 id="Card">Card 2</h2> <p class="card-description">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p> </div> </li> <li class="card"> <img class="card-image lazy" src="/static/imghwm/default1.png" data-src="image3.jpg" alt="How to create a responsive card layout using CSS Flex layout" > <div class="card-content"> <h2 id="Card">Card 3</h2> <p class="card-description">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p> </div> </li> </ul>
- Add CSS styles
Next, we need to add some CSS styles to create a flexible layout. We will use display: flex;
to set the card container as a flex container and use some flex properties to control the layout of the card.
.card-container { display: flex; flex-wrap: wrap; justify-content: center; } .card { margin: 10px; width: 300px; background-color: #f1f1f1; border-radius: 10px; box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2); } .card-image { width: 100%; height: auto; border-radius: 10px 10px 0 0; } .card-content { padding: 10px; } .card-title { font-size: 20px; margin-top: 0; } .card-description { font-size: 14px; }
In the above code, we use justify-content: center;
to align the card horizontally and center, flex-wrap: wrap;
to make the card automatically Wrap lines to fit different screen sizes.
- Responsive Layout
In order for the cards to line up well on screens of different widths, we can use media queries and elastic properties to implement responsive layout.
@media only screen and (max-width: 600px) { .card { width: calc(50% - 20px); } } @media only screen and (max-width: 400px) { .card { width: 100%; } }
In the above code, we use media queries to detect the width of the screen. When the screen width is less than 600px, the width of the card will be 50% of the screen width, and since we set a 10px margin for the card, use calc(50% - 20px)
to allow the cards to be arranged normally. When the screen width is less than 400px, the width of the card will be 100% and the cards will be stacked on a single line.
Through the above steps, we can use Css Flex elastic layout to create a simple responsive card layout. Not only can cards be arranged adaptively on different devices, but the card container and card styles can be easily adjusted.
Summary
This article introduces how to use Css Flex elastic layout to create a responsive card layout. With a simple HTML structure and some CSS styles, we can easily create a responsive layout. The power of flexible layout is that it can adapt to different screen sizes and device types, allowing our web pages to display well on various devices. I hope this article can help you understand and apply flexible layout.
The above is the detailed content of How to create a responsive card layout using CSS Flex layout. For more information, please follow other related articles on the PHP Chinese website!

In this post, Blackle Mori shows you a few of the hacks found while trying to push the limits of Cohost’s HTML support. Use these if you dare, lest you too get labelled a CSS criminal.

Custom cursors with CSS are great, but we can take things to the next level with JavaScript. Using JavaScript, we can transition between cursor states, place dynamic text within the cursor, apply complex animations, and apply filters.

Interactive CSS animations with elements ricocheting off each other seem more plausible in 2025. While it’s unnecessary to implement Pong in CSS, the increasing flexibility and power of CSS reinforce Lee's suspicion that one day it will be a

Tips and tricks on utilizing the CSS backdrop-filter property to style user interfaces. You’ll learn how to layer backdrop filters among multiple elements, and integrate them with other CSS graphical effects to create elaborate designs.

Well, it turns out that SVG's built-in animation features were never deprecated as planned. Sure, CSS and JavaScript are more than capable of carrying the load, but it's good to know that SMIL is not dead in the water as previously

Yay, let's jump for text-wrap: pretty landing in Safari Technology Preview! But beware that it's different from how it works in Chromium browsers.

This CSS-Tricks update highlights significant progress in the Almanac, recent podcast appearances, a new CSS counters guide, and the addition of several new authors contributing valuable content.

Most of the time, people showcase Tailwind's @apply feature with one of Tailwind's single-property utilities (which changes a single CSS declaration). When showcased this way, @apply doesn't sound promising at all. So obvio


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version
Chinese version, very easy to use

Notepad++7.3.1
Easy-to-use and free code editor
