การทำซ้ำพื้นหลังใน CSS ใช้กำหนดว่าภาพพื้นหลังจะทำซ้ำบนหน้าเว็บอย่างไร ใช้คุณสมบัติ background-repeat ค่าที่ใช้ได้มีดังนี้:
background-repeat: repeat | repeat-x | repeat-y | no-repeat | initial | inherit;
ตัวอย่างที่ 1: repeat-x (ทำซ้ำแนวนอน)
<!DOCTYPE html> <html> <head> <style> body { background-image: url("https://www.tutorialspoint.com/images/Swift.png"); background-repeat: repeat-x; background-color: blue; color: white; } .demo { text-decoration: overline underline; } </style> </head> <body> <h1>Details</h1> <p class="demo">Examination Center near ABC College.</p> <p class="demo2">Exam begins at 9AM.</p> </body> </html>
ผลลัพธ์

ตัวอย่างที่ 2: repeat-y (ทำซ้ำแนวตั้ง)
<!DOCTYPE html> <html> <head> <style> body { background-image: url("https://www.tutorialspoint.com/images/Swift.png"); background-repeat: repeat-y; background-color: orange; color: white; } .demo { text-decoration: overline underline; } </style> </head> <body> <h1>Details</h1> <p class="demo">Examination Center near ABC College.</p> <p class="demo2">Exam begins at 9AM.</p> </body> </html>
ผลลัพธ์

ค่าทั้งหมดและการใช้งาน
| ค่า | ลักษณะ | เหมาะสำหรับ |
|---|---|---|
| repeat | ซ้ำทั้งแนวนอนและแนวตั้ง (ค่าเริ่มต้น) | Pattern, texture เล็กๆ |
| repeat-x | ซ้ำเฉพาะแนวนอน | Header bar, gradient แนวนอน |
| repeat-y | ซ้ำเฉพาะแนวตั้ง | Sidebar border, gradient แนวตั้ง |
| no-repeat | ไม่ซ้ำ แสดงครั้งเดียว | Hero image, logo, banner |
| space | ซ้ำและกระจายเท่าๆ กัน (ไม่ตัดภาพ) | Pattern ต้องการเต็มพื้นที่ |
| round | ซ้ำและย่อ/ขยายภาพให้พอดี (อาจบิดเบี้ยว) | Pattern ต้องการเต็มพื้นที่ไม่ตัด |
Two-value syntax (CSS Background Level 3+)
background-repeat: repeat-x repeat-y; /* แยกแนวนอน/แนวตั้ง */ background-repeat: repeat no-repeat; /* ซ้ำแนวนอน ไม่ซ้ำแนวตั้ง */ background-repeat: space round; /* space แนวนอน round แนวตั้ง */
ร่วมกับ background-position
background: url('icon.png') no-repeat right top;
background: url('pattern.png') repeat-x center top;