คุณสมบัติ background-attachment
เพื่อกำหนด background-attachment ให้ใช้คุณสมบัติ background-attachment Background attachment กำหนดว่าภาพพื้นหลังจะคงที่ (fixed) หรือเลื่อนไปพร้อมหน้าเว็บ (scroll)
ตัวอย่าง: ภาพพื้นหลังคงที่ (fixed)
ลองรันโค้ดต่อไปนี้เพื่อเรียนรู้การทำงานของคุณสมบัติ background-attachment เพื่อกำหนดภาพพื้นหลังคงที่:
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url('/css/images/css.jpg');
background-repeat: no-repeat;
background-attachment: fixed;
}
</style>
</head>
<body>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
</body>
</html>
ค่าที่ใช้ได้:
- fixed - ภาพคงที่ ติดกับ viewport
- scroll - ภาพเลื่อนพร้อมเนื้อหา (ค่าเริ่มต้น)
- local - ภาพเลื่อนพร้อมเนื้อหาในองค์ประกอบที่มี scroll