Skip to content

CSS Card 组件

简单 约 20 分钟

你将构建什么

HTML page 使用 3 card 组件边 通过边. 每个 card 有镜像, title, 描述, and button, 样式使用 padding, 边框, and shadow.

学习目标

  • 创建 reusable card 布局使用 consistent spacing
  • Apply CSS box model (padding, 边框, margin, shadow)
  • Use flexbox 到排列 cards in 行
  • 添加 hover 副作用 for interactivity

前置要求

  • HTML 结构
  • 基础 CSS (selectors, 属性, flexbox)

步骤

  1. 1

    编写 HTML for 3 cards

    创建 cards.HTML 使用 div.cards 容器持有 3 div.card 元素, 每个使用镜像, h3 title, p 描述, and 链接 button.

    代码
    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Cards</title>
        <link rel="stylesheet" href="cards.css">
      </head>
      <body>
        <div class="cards">
          <div class="card">
            <img src="https://placehold.co/300x150" alt="Card 1">
            <h3>Card Title</h3>
            <p>A short description of this card.</p>
            <a href="#" class="btn">Learn more</a>
          </div>
          <div class="card">
            <img src="https://placehold.co/300x150" alt="Card 2">
            <h3>Another Card</h3>
            <p>Another description here.</p>
            <a href="#" class="btn">Learn more</a>
          </div>
          <div class="card">
            <img src="https://placehold.co/300x150" alt="Card 3">
            <h3>Third Card</h3>
            <p>Third description text.</p>
            <a href="#" class="btn">Learn more</a>
          </div>
        </div>
      </body>
    </html>

    预期结果

    Three cards stacked vertically 使用默认样式.

  2. 2

    布局 cards in 行

    创建 cards.CSS. Use flexbox 到排列 cards horizontally 使用 gap.

    代码
    .cards {
      display: flex;
      gap: 1.5rem;
      padding: 2rem;
      flex-wrap: wrap;
    }

    预期结果

    Cards appear 边通过边 使用 spacing 之间它们.

  3. 3

    样式 card box

    Apply box model: 边框, 边框-radius, box-shadow, and overflow 隐藏到 clip 镜像角落.

    代码
    .card {
      background: #fff;
      border: 1px solid #e2e8f0;
      border-radius: 0.75rem;
      box-shadow: 0 4px 6px -1px rgba(0,0,0,0.1);
      overflow: hidden;
      width: 300px;
      transition: transform 0.2s, box-shadow 0.2s;
    }
    .card:hover {
      transform: translateY(-4px);
      box-shadow: 0 10px 15px -3px rgba(0,0,0,0.1);
    }

    预期结果

    Cards 有 rounded 角落, shadow, and lift on hover.

  4. 4

    样式 card 内容 and button

    添加 padding 内部 card for 内容, 制作镜像完整-宽度, and 样式 button.

    代码
    .card img { width: 100%; display: block; }
    .card h3 { margin: 0.75rem 1rem 0.5rem; font-size: 1.25rem; }
    .card p { margin: 0 1rem 1rem; color: #64748b; font-size: 0.875rem; }
    .card .btn {
      display: inline-block;
      margin: 0 1rem 1rem;
      padding: 0.5rem 1rem;
      background: #2563eb;
      color: #fff;
      text-decoration: none;
      border-radius: 0.375rem;
      font-size: 0.875rem;
    }

    预期结果

    内容是 padded 内部 card, 镜像 fills 宽度, button 是样式.

  5. 5

    添加响应式样式

    使用媒体查询为移动端调整布局:在小屏幕上将多列改为单列、缩小字号、调整间距。移动优先意味着小屏幕体验同样重要。

    代码
    @media (max-width: 640px) {
      .container {
        flex-direction: column;
        padding: 1rem;
      }
      h1 { font-size: 1.5rem; }
      .card { width: 100%; margin-bottom: 1rem; }
    }

    预期结果

    在 640px 以下视口,布局变为单列,字号和间距缩小,内容不溢出。

自己动手试试

你学到了什么

你构建 reusable card 组件使用 CSS box model — 边框, shadows, padding, and hover transitions. 这个 card 模式是 构建块 的大多数 modern web UIs.

相关资源

相关课程

相关练习

相关速查表

相关术语

  • CSS

    Cascading 样式 Sheets.

相关术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

  • CSS

    Cascading 样式 Sheets.

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回主题