คุณสมบัติ background-repeat ใน CSS ใช้กำหนดว่ารูปภาพพื้นหลังจะซ้ำอย่างไร
ไวยากรณ์ (Syntax)
Selector {
background-repeat: /*value*/;
}
ตัวอย่างที่ 1: Repeat (ค่าเริ่มต้น)
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("https://www.tutorialspoint.com/images/microsoftproject.png");
background-repeat: repeat;
}
</style>
</head>
<body></body>
</html>
ผลลัพธ์

ตัวอย่างที่ 2: Space Repeat
<!DOCTYPE html>
<html>
<head>
<style>
ul {
background-image: url("https://www.tutorialspoint.com/images/questions-answers.png");
background-repeat: space repeat;
}
</style>
</head>
<body>
<h2>Tutorials</h2>
<ul>
<li>Java</li> <li>C#</li> <li>Etherum</li> <li>DBMS</li> <li>OS</li>
</ul>
</body>
</html>
ผลลัพธ์

ค่าของ background-repeat
| ค่า | คำอธิบาย |
|---|---|
repeat | ซ้ำทั้งแนวนอนและแนวตั้ง (ค่าเริ่มต้น) |
repeat-x | ซ้ำเฉพาะแนวนอน |
repeat-y | ซ้ำเฉพาะแนวตั้ง |
no-repeat | ไม่ซ้ำ แสดงเพียงครั้งเดียว |
space | ซ้ำและกระจายช่องว่างเท่าๆ กัน ไม่ตัดรูป |
round | ซ้ำและย่อ/ขยายรูปให้พอดีพื้นที่พอดี |
space repeat | ระบุแนวนอน/แนวตั้งแยกกัน (2 ค่า) |
รูปแบบ 2 ค่า (Two-value syntax)
background-repeat: repeat-x no-repeat; /* แนวนอน: repeat, แนวตั้ง: no-repeat */
background-repeat: space round; /* แนวนอน: space, แนวตั้ง: round */
background-repeat: no-repeat space; /* แนวนอน: no-repeat, แนวตั้ง: space */
เคล็ดลับ
- ใช้
no-repeatร่วมbackground-position: centerและbackground-size: coverสำหรับ Hero banner spaceและroundใช้ได้ดีกับ Pattern/Texture ที่ต้องการเติมพื้นที่