CSS 卡片佈局 (Card Layout)

卡片佈局是現代網頁設計中最常見的模式,用於展示產品、文章、團隊成員等內容。

Grid 實現(推薦)

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 24px;
}

auto-fitminmax() 的組合讓卡片自動響應式排列。

Flexbox 實現

.card-list {
  display: flex;
  flex-wrap: wrap;
  gap: 24px;
}

.card {
  flex: 1 1 280px;    /* 最小寬度 280px */
  max-width: 400px;   /* 最大寬度限制 */
}

卡片樣式

基本卡片

.card {
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  overflow: hidden;
}

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

.card-body {
  padding: 20px;
}

.card-title {
  margin: 0 0 12px;
  font-size: 18px;
}

.card-text {
  color: #666;
  line-height: 1.6;
}
<div class="card">
  <img class="card-image" src="image.jpg" alt="圖片">
  <div class="card-body">
    <h3 class="card-title">標題</h3>
    <p class="card-text">描述文字...</p>
  </div>
</div>

卡片 Hover 效果

.card {
  transition: transform 0.3s, box-shadow 0.3s;
}

.card:hover {
  transform: translateY(-4px);
  box-shadow: 0 12px 24px rgba(0,0,0,0.15);
}

帶邊框的卡片

.card-outlined {
  background: white;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
}

.card-outlined:hover {
  border-color: #007bff;
}

完整範例

產品卡片

.product-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 24px;
}

.product-card {
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.08);
  overflow: hidden;
  transition: box-shadow 0.3s;
}

.product-card:hover {
  box-shadow: 0 8px 24px rgba(0,0,0,0.12);
}

.product-image {
  width: 100%;
  aspect-ratio: 1;
  object-fit: cover;
}

.product-info {
  padding: 16px;
}

.product-name {
  font-size: 16px;
  margin: 0 0 8px;
}

.product-price {
  font-size: 18px;
  font-weight: bold;
  color: #e53935;
}

文章卡片

.article-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 32px;
}

.article-card {
  display: flex;
  flex-direction: column;
  background: white;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

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

.article-card-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 20px;
}

.article-card-title {
  font-size: 20px;
  margin: 0 0 12px;
}

.article-card-excerpt {
  flex: 1;
  color: #666;
  margin-bottom: 16px;
}

.article-card-meta {
  font-size: 14px;
  color: #999;
}

團隊成員卡片

.team-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 24px;
}

.team-card {
  text-align: center;
  padding: 32px 24px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.08);
}

.team-avatar {
  width: 120px;
  height: 120px;
  border-radius: 50%;
  object-fit: cover;
  margin-bottom: 16px;
}

.team-name {
  font-size: 18px;
  margin: 0 0 4px;
}

.team-role {
  color: #666;
  margin: 0;
}

水平卡片

.horizontal-card {
  display: flex;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  overflow: hidden;
}

.horizontal-card img {
  width: 200px;
  object-fit: cover;
}

.horizontal-card-body {
  flex: 1;
  padding: 20px;
}

@media (max-width: 600px) {
  .horizontal-card {
    flex-direction: column;
  }
  
  .horizontal-card img {
    width: 100%;
    height: 200px;
  }
}

等高卡片

使用 Grid 或 Flexbox 可以自動讓同一行的卡片等高:

/* Grid 自動等高 */
.card-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 24px;
}

/* Flexbox 需要設定 align-items: stretch(預設值) */
.card-list {
  display: flex;
  align-items: stretch;
}

卡片內容填滿

讓卡片內的按鈕或連結固定在底部:

.card {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.card-body {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.card-text {
  flex: 1;
}

.card-button {
  margin-top: auto;
}