Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> CSS

การทำซ้ำพื้นหลังใน CSS (background-repeat)

การทำซ้ำพื้นหลังใน 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>

ผลลัพธ์

การทำซ้ำพื้นหลังใน CSS (background-repeat)

ตัวอย่างที่ 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>

ผลลัพธ์

การทำซ้ำพื้นหลังใน CSS (background-repeat)

ค่าทั้งหมดและการใช้งาน

ค่าลักษณะเหมาะสำหรับ
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;