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

คุณสมบัติ HTML DOM Style background


คุณสมบัติ background ใช้สำหรับตั้งค่าหรือรับภาพพื้นหลังที่เกี่ยวข้องกับองค์ประกอบ เป็นคุณสมบัติแบบ shorthand สามารถจัดการได้ถึง 8 คุณสมบัติ

ไวยากรณ์ (Syntax)

object.style.background = "color image repeat attachment position size origin clip|initial|inherit"

ค่าที่ใช้ได้ (Values)

ลำดับ ค่าและคำอธิบาย
1 color
ตั้งค่าสีพื้นหลังขององค์ประกอบ
2 image
ตั้งค่าภาพพื้นหลังขององค์ประกอบ
3 repeat
ตั้งค่าวิธีการทำซ้ำของภาพพื้นหลัง
4 attachment
ตั้งค่าภาพพื้นหลังคงที่หรือเลื่อนตามหน้า
5 position
ตั้งค่าตำแหน่งเริ่มต้นของภาพพื้นหลัง
6 size
ตั้งค่าขนาดของภาพพื้นหลัง
7 origin
ระบุพื้นที่การจัดตำแหน่งของพื้นหลัง
8 clip
ตั้งค่าว่าภาพพื้นหลังจะถูกวาดไปจนถึงที่ไหน

ตัวอย่าง (Example)

<!DOCTYPE html> <html> <head> <style> 
  #PIC{ 
    width: 250px; 
    height: 150px; 
    border:solid 3px black; 
    font-weight:100; 
    text-align:center; 
    color:white; 
    font-size:40px; 
    background: url("https://www.tutorialspoint.com/python/images/python-mini.jpg") no-repeat fixed 2% 8%; 
  } 
</style> 
<script> 
  function changeBackground(){ 
    document.getElementById("PIC").style.backgroundImage="url(https://www.tutorialspoint.com/csharp/images/csharp-mini-logo.jpg)"; 
    document.getElementById("Sample").innerHTML="The background URL is now changed and new background image can be seen."; 
  } 
</script> </head> 
<body> 
<h2>Learning is fun</h2> 
<div id="PIC">Tutorial</div> 
<p>Click the below button to change the above background image for the div</p> 
<button onclick="changeBackground()">CHANGE BACKGROUND</button> 
<p id="Sample"></p> 
</body> </html>

ผลลัพธ์ (Output)

ผลลัพธ์ที่ได้:

คุณสมบัติ HTML DOM Style background

เมื่อคลิก CHANGE BACKGROUND:

คุณสมบัติ HTML DOM Style background