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>

Grid 實現

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

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

左右交替

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

.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;
}

響應式圖文並排

在小螢幕上堆疊顯示:

.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%;
  }
}

完整範例

產品特色區塊

.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;
}

關於我們區塊

.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;
}

服務項目

.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;
}

圖片位置變化

圖片在上方

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

圖片在背景

.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);
}

圖片浮動(文字環繞)

.article img {
  float: left;
  width: 300px;
  margin: 0 24px 16px 0;
  border-radius: 8px;
}