CSS 圖文並排

圖文並排是網頁設計中常見的佈局模式,用於產品展示、服務介紹、關於頁面等。

基本並排

Flexbox 實現

.image-text {
  display: flex;
  gap: 40px;
  align-items: center;
}

.image-text img {
  width: 50%;
  border-radius: 8px;
}

.image-text-content {
  flex: 1;
}
<div class="image-text">
  <img src="image.jpg" alt="圖片" />
  <div class="image-text-content">
    <h2>標題</h2>
    <p>描述文字...</p>
  </div>
</div>

實作預覽 (Flexbox)

圖片

標題

描述文字...

Grid 實現

.image-text {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 40px;
  align-items: center;
}

.image-text img {
  width: 100%;
  border-radius: 8px;
}
<div class="image-text">
  <img src="image.jpg" alt="圖片" />
  <div class="image-text-content">
    <h2>Grid 標題</h2>
    <p>Grid 描述文字...</p>
  </div>
</div>

實作預覽 (Grid)

圖片

Grid 標題

Grid 描述文字...

左右交替

常見於特色介紹,奇數項圖左文右,偶數項文左圖右:

.feature-section {
  display: flex;
  gap: 60px;
  align-items: center;
  padding: 80px 0;
}

.feature-image {
  flex: 1;
}

.feature-image img {
  width: 100%;
  border-radius: 12px;
}

.feature-content {
  flex: 1;
}

/* 偶數項反轉 */
.feature-section:nth-child(even) {
  flex-direction: row-reverse;
}

或使用 CSS class:

.feature-section.reverse {
  flex-direction: row-reverse;
}
<section class="feature-section">
  <div class="feature-image">
    <img src="image1.jpg" alt="圖片 1" />
  </div>
  <div class="feature-content">
    <h2>特色一</h2>
    <p>描述內容 1...</p>
  </div>
</section>

<section class="feature-section">
  <div class="feature-image">
    <img src="image2.jpg" alt="圖片 2" />
  </div>
  <div class="feature-content">
    <h2>特色二</h2>
    <p>描述內容 2...</p>
  </div>
</section>

實作預覽 (左右交替)

圖片 1

特色一

描述內容 1...

圖片 2

特色二

描述內容 2...

響應式圖文並排

在小螢幕上堆疊顯示:

.image-text {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.image-text img {
  width: 100%;
  border-radius: 8px;
}

@media (min-width: 768px) {
  .image-text {
    flex-direction: row;
    gap: 40px;
    align-items: center;
  }

  .image-text img {
    width: 50%;
  }
}
<div class="image-text">
  <img src="responsive.jpg" alt="響應式圖片" />
  <div class="image-text-content">
    <h2>響應式標題</h2>
    <p>在大螢幕併排,小螢幕堆疊。</p>
  </div>
</div>

實作預覽 (響應式)

圖片

響應式標題

在大螢幕併排,小螢幕堆疊。

完整範例

產品特色區塊

.feature {
  display: flex;
  flex-direction: column;
  gap: 32px;
  padding: 60px 24px;
}

@media (min-width: 768px) {
  .feature {
    flex-direction: row;
    gap: 60px;
    align-items: center;
    padding: 100px 24px;
  }

  .feature.reverse {
    flex-direction: row-reverse;
  }
}

.feature-image {
  flex: 1;
}

.feature-image img {
  width: 100%;
  border-radius: 16px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
}

.feature-content {
  flex: 1;
}

.feature-content h2 {
  font-size: 32px;
  margin: 0 0 16px;
}

.feature-content p {
  color: #666;
  line-height: 1.8;
  margin: 0 0 24px;
}

.feature-content .btn {
  display: inline-block;
  padding: 12px 28px;
  background: #007bff;
  color: white;
  text-decoration: none;
  border-radius: 8px;
}
<section class="feature">
  <div class="feature-image">
    <img src="product.jpg" alt="產品圖片" />
  </div>
  <div class="feature-content">
    <h2>強大功能</h2>
    <p>這是功能的詳細描述內容,解釋為什麼這個產品這麼出色。</p>
    <a href="#" class="btn">立即體驗</a>
  </div>
</section>

實作預覽 (產品特色)

產品圖片

強大功能

這是功能的詳細描述內容,解釋為什麼這個產品這麼出色。

立即體驗

關於我們區塊

.about-section {
  display: grid;
  grid-template-columns: 1fr;
  gap: 40px;
  max-width: 1200px;
  margin: 0 auto;
  padding: 80px 24px;
}

@media (min-width: 768px) {
  .about-section {
    grid-template-columns: 1fr 1fr;
    gap: 60px;
    align-items: center;
  }
}

.about-image {
  position: relative;
}

.about-image img {
  width: 100%;
  border-radius: 16px;
}

.about-image::after {
  content: '';
  position: absolute;
  top: 20px;
  left: 20px;
  right: -20px;
  bottom: -20px;
  border: 3px solid #007bff;
  border-radius: 16px;
  z-index: -1;
}

.about-content h2 {
  font-size: 36px;
  margin: 0 0 24px;
}

.about-content p {
  color: #666;
  line-height: 1.8;
}
<section class="about-section">
  <div class="about-image">
    <img src="about.jpg" alt="關於我們" />
  </div>
  <div class="about-content">
    <h2>關於我們</h2>
    <p>我們致力於提供最優質的服務與教學內容。</p>
  </div>
</section>

實作預覽 (關於我們)

圖片

關於我們

我們致力於提供最優質的服務與教學內容。

服務項目

.services {
  max-width: 1200px;
  margin: 0 auto;
}

.service-item {
  display: flex;
  flex-direction: column;
  gap: 32px;
  padding: 60px 24px;
  border-bottom: 1px solid #eee;
}

@media (min-width: 768px) {
  .service-item {
    flex-direction: row;
    gap: 48px;
    align-items: center;
  }

  .service-item:nth-child(even) {
    flex-direction: row-reverse;
  }
}

.service-image {
  flex: 0 0 400px;
}

.service-image img {
  width: 100%;
  border-radius: 12px;
}

.service-content {
  flex: 1;
}

.service-number {
  font-size: 64px;
  font-weight: bold;
  color: #007bff;
  opacity: 0.2;
  line-height: 1;
}

.service-content h3 {
  font-size: 24px;
  margin: 8px 0 16px;
}

.service-content p {
  color: #666;
  line-height: 1.8;
}
<section class="services">
  <div class="service-item">
    <div class="service-image">
      <img src="service1.jpg" alt="服務 1" />
    </div>
    <div class="service-content">
      <div class="service-number">01</div>
      <h3>網頁設計</h3>
      <p>提供專業的 UI/UX 設計與開發服務。</p>
    </div>
  </div>
  <div class="service-item">
    <div class="service-image">
      <img src="service2.jpg" alt="服務 2" />
    </div>
    <div class="service-content">
      <div class="service-number">02</div>
      <h3>系統開發</h3>
      <p>高效能的後端 API 與資料庫架構設計。</p>
    </div>
  </div>
</section>

實作預覽 (服務項目)

圖片 1
01

網頁設計

提供專業的 UI/UX 設計與開發服務。

圖片 2
02

系統開發

高效能的後端 API 與資料庫架構設計。

圖片位置變化

圖片在上方

.card {
  display: flex;
  flex-direction: column;
}
<div class="card">
  <img src="top.jpg" alt="上方圖片" />
  <div class="card-body">
    <h3>標題</h3>
    <p>圖片位於文字上方。</p>
  </div>
</div>

圖片在背景

.hero {
  position: relative;
  padding: 100px 24px;
  color: white;
}

.hero-background {
  position: absolute;
  inset: 0;
  z-index: -1;
}

.hero-background img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.hero-background::after {
  content: '';
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
}
<section class="hero">
  <div class="hero-background">
    <img src="hero.jpg" alt="背景圖" />
  </div>
  <div class="hero-content">
    <h1>文字在背景上</h1>
    <p>使用絕對定位將圖片置於底層。</p>
  </div>
</section>

圖片浮動(文字環繞)

.article img {
  float: left;
  width: 300px;
  margin: 0 24px 16px 0;
  border-radius: 8px;
}
<div class="article">
  <img src="float.jpg" alt="浮動圖片" />
  <p>這是一段很長的文字,會環繞在圖片周圍。文字環繞是傳統報章雜誌常見的排版方式...</p>
</div>

實作預覽 (位置變化)

上方圖片
標題
背景圖文字
浮動圖
文字環繞效果示範。這是一段模擬文字,用來展示文字如何環繞在圖片旁邊的效果。