Menu
CoddyTech

卡片网格布局

本课属于 Coddy HTML 学习旅程的“前端实践”单元。第 27 课,共 35 课。

卡片网格是一种常见的响应式布局模式,常用于电商网站、作品集或博客。它会将各个项目(如商品、文章或个人资料)排列成间距均匀的卡片。每张卡片通常包含一张图片、一个标题、一些文字,有时还会包含一个按钮或链接。

这种布局通常使用 CSS Grid 或 Flexbox 构建,这样每行卡片的数量会根据屏幕大小而变化(例如,手机上为 1 列,平板上为 2-3 列,桌面设备上为 4 列或更多)。

首先,为商品列表创建 HTML 结构:

<div class="product-grid">
  <div class="product-card">
    <img src="product1.jpg" alt="Product 1">
    <h3>Product Name</h3>
    <p>$19.99</p>
    <button>Add to Cart</button>
  </div>
  <!-- 这里还有更多商品卡片 -->
</div>

现在,添加 CSS 让它具有响应式布局:

.product-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1rem;
}

/* 适用于平板电脑及更大屏幕 */
@media (min-width: 600px) {
  .product-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

/* 适用于台式电脑 */
@media (min-width: 900px) {
  .product-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

.product-card {
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 1rem;
}

这样会在手机上创建单列布局,在平板电脑上创建两列布局,在桌面屏幕上创建三列布局。

challenge icon

挑战

简单

你会得到一个用于展示旅行照片的 HTML 页面。你的任务是创建一个响应式卡片网格,使其能够适应不同的屏幕尺寸。

要求:

  • 使用 CSS Grid 进行布局。
  • 在手机(小屏幕)上,每行显示 1 张卡片。
  • 在平板电脑上,每行显示 2 张卡片。
  • 在台式电脑上,每行显示 3 张或更多卡片。

为每张卡片设置以下样式:

  • 美观的背景颜色
  • 圆角

目标:创建简洁、现代的画廊布局,让它在任何屏幕尺寸上都赏心悦目。

亲手试试

<!DOCTYPE html>
<html>
<head>
  <title>Card Grid Layout</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      background: #f9f9f9;
      margin: 0;
      padding: 2rem;
    }

    h1 {
      text-align: center;
      margin-bottom: 2rem;
      color: #333;
    }

    .gallery-grid {
      gap: 1.5rem;
    }

    .card {
      box-shadow: 0 4px 10px rgba(0,0,0,0.1);
      overflow: hidden;
      transition: transform 0.3s ease;
    }

    .card:hover {
      transform: scale(1.03);
    }

    .card img {
      width: 100%;
      height: 200px;
      object-fit: cover;
    }

    .card h3 {
      margin: 0.8rem;
      color: #444;
    }

    .card p {
      margin: 0 0.8rem 1rem;
      font-size: 0.9rem;
      color: #666;
    }

    /* Tablet: 2 columns */
    @media (min-width: 600px) {
      .gallery-grid {
          
      }
    }

    /* Desktop: 3 columns */
    @media (min-width: 900px) {
      .gallery-grid {

      }
    }
  </style>
</head>
<body>
    <h1>Travel Gallery</h1>

    <div class="gallery-grid">
        <div class="card">
          <img src="https://images.unsplash.com/photo-1507525428034-b723cf961d3e" alt="Beach">
          <h3>Sunny Beach</h3>
          <p>Relaxing views of golden sand and turquoise waters.</p>
        </div>
    
        <div class="card">
          <img src="https://images.unsplash.com/photo-1469474968028-56623f02e42e" alt="Mountains">
          <h3>Mountain Escape</h3>
          <p>Fresh air and breathtaking peaks for hiking lovers.</p>
        </div>
    
        <div class="card">
          <img src="https://upload.wikimedia.org/wikipedia/commons/0/05/View_of_Empire_State_Building_from_Rockefeller_Center_New_York_City_dllu.jpg" alt="City Lights">
          <h3>City Lights</h3>
          <p>A bustling city skyline shining bright at night.</p>
        </div>
    
        <div class="card">
          <img src="https://upload.wikimedia.org/wikipedia/commons/b/be/Hutan_Tangkahan.jpg" alt="Forest">
          <h3>Green Forest</h3>
          <p>Peaceful trails and endless shades of green.</p>
        </div>
    
        <div class="card">
          <img src="https://upload.wikimedia.org/wikipedia/commons/c/c6/Caravan_in_the_desert.jpg" alt="Desert">
          <h3>Desert Adventure</h3>
          <p>Golden dunes stretching far into the horizon.</p>
        </div>
    
        <div class="card">
          <img src="https://images.unsplash.com/photo-1501785888041-af3ef285b470" alt="Lake">
          <h3>Crystal Lake</h3>
          <p>Mirror-like reflections surrounded by mountains.</p>
        </div>
    </div>
</body>
</html>
quiz icon自我测试

这节课包含一个小测验。开始学习即可作答,并记录你的进度。

前端实践中的所有课

自己练习: Web 在线编辑器