Home Web Front-end CSS Tutorial How to create a responsive card layout using CSS Flex layout

How to create a responsive card layout using CSS Flex layout

Sep 28, 2023 am 09:29 AM
Responsive layout css flex

如何使用Css Flex 弹性布局创建响应式卡片布局

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.

  1. 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 src="image1.jpg" class="card-image">
    <div class="card-content">
      <h2 class="card-title">Card 1</h2>
      <p class="card-description">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
    </div>
  </li>
  <li class="card">
    <img src="image2.jpg" class="card-image">
    <div class="card-content">
      <h2 class="card-title">Card 2</h2>
      <p class="card-description">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
    </div>
  </li>
  <li class="card">
    <img src="image3.jpg" class="card-image">
    <div class="card-content">
      <h2 class="card-title">Card 3</h2>
      <p class="card-description">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
    </div>
  </li>
</ul>
Copy after login
  1. 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;
}
Copy after login

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.

  1. 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%;
  }
}
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Tutorial on using CSS to implement responsive image automatic carousel effect Tutorial on using CSS to implement responsive image automatic carousel effect Nov 21, 2023 am 08:37 AM

With the popularity of mobile devices, web design needs to take into account factors such as device resolution and screen size of different terminals to achieve a good user experience. When implementing responsive design of a website, it is often necessary to use the image carousel effect to display the content of multiple images in a limited visual window, and at the same time, it can also enhance the visual effect of the website. This article will introduce how to use CSS to achieve a responsive image automatic carousel effect, and provide code examples and analysis. Implementation ideas The implementation of responsive image carousel can be implemented through CSS flex layout. exist

Tutorial on implementing responsive sliding menu using CSS Tutorial on implementing responsive sliding menu using CSS Nov 21, 2023 am 08:08 AM

A tutorial on using CSS to implement a responsive sliding menu requires specific code examples. In modern web design, responsive design has become an essential skill. To accommodate different devices and screen sizes, we need to add a responsive menu to the website. Today, we will use CSS to implement a responsive sliding menu and provide you with specific code examples. First, let's take a look at the implementation. We will create a navigation bar that automatically collapses when the screen width is smaller than a certain threshold and expands by clicking the menu button.

How to create a responsive tag cloud using HTML, CSS and jQuery How to create a responsive tag cloud using HTML, CSS and jQuery Oct 27, 2023 am 10:46 AM

How to use HTML, CSS and jQuery to create a responsive tag cloud. A tag cloud is a common web element used to display various keywords or tags. It usually displays the importance of keywords in different font sizes or colors. In this article, we will introduce how to use HTML, CSS and jQuery to create a responsive tag cloud, and give specific code examples. Creating the HTML Structure First, we need to create the basic structure of the tag cloud in HTML. You can use an unordered list to represent tags

Guide to solving misalignment of WordPress web pages Guide to solving misalignment of WordPress web pages Mar 05, 2024 pm 01:12 PM

Guide to solving misaligned WordPress web pages In WordPress website development, sometimes we encounter web page elements that are misaligned. This may be due to screen sizes on different devices, browser compatibility, or improper CSS style settings. To solve this misalignment, we need to carefully analyze the problem, find possible causes, and debug and repair it step by step. This article will share some common WordPress web page misalignment problems and corresponding solutions, and provide specific code examples to help develop

How to create a responsive scrolling notification bar using HTML, CSS and jQuery How to create a responsive scrolling notification bar using HTML, CSS and jQuery Oct 26, 2023 pm 12:12 PM

How to use HTML, CSS and jQuery to create a responsive scrolling notification bar. With the popularity of mobile devices and the increase in user requirements for website access experience, designing a responsive scrolling notification bar has become more and more important. Responsive design ensures that the website displays properly on different devices and that users can easily view notification content. This article will introduce how to use HTML, CSS and jQuery to create a responsive scrolling notification bar, and provide specific code examples. First we need to create the HTM

How to create a responsive carousel layout using HTML and CSS How to create a responsive carousel layout using HTML and CSS Oct 20, 2023 pm 04:24 PM

How to create a responsive carousel layout using HTML and CSS Carousels are a common element in modern web design. It can attract the user's attention, display multiple contents or images, and switch automatically. In this article, we will introduce how to create a responsive carousel layout using HTML and CSS. First, we need to create a basic HTML structure and add the required CSS styles. The following is a simple HTML structure: &lt;!DOCTYPEhtml&g

Introducing the window arrangement method in win7 Introducing the window arrangement method in win7 Dec 26, 2023 pm 04:37 PM

When we open multiple windows at the same time, win7 has the function of arranging multiple windows in different ways and then displaying them at the same time, which allows us to view the contents of each window more clearly. So how many window arrangements are there in win7? What do they look like? Let’s take a look with the editor. There are several ways to arrange Windows 7 windows: three, namely cascading windows, stacked display windows and side-by-side display windows. When we open multiple windows, we can right-click on an empty space on the taskbar. You can see three window arrangements. 1. Cascading windows: 2. Stacked display windows: 3. Display windows side by side:

Flexible application skills of position attribute in H5 Flexible application skills of position attribute in H5 Dec 27, 2023 pm 01:05 PM

How to flexibly use the position attribute in H5. In H5 development, the positioning and layout of elements are often involved. At this time, the CSS position property will come into play. The position attribute can control the positioning of elements on the page, including relative positioning, absolute positioning, fixed positioning and sticky positioning. This article will introduce in detail how to flexibly use the position attribute in H5 development.

See all articles