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

กำหนดสีพื้นหลังขององค์ประกอบด้วย CSS


คุณสมบัติ background-color

เพื่อกำหนดสีพื้นหลังขององค์ประกอบ ให้ใช้คุณสมบัติ background-color

ตัวอย่าง

ลองรันโค้ดต่อไปนี้เพื่อเรียนรู้การทำงานของคุณสมบัติ background-color:

<html>
  <head>
  </head>
  <body>
    <p style="background-color:blue;">
      This text has a blue background color.</p>
  </body>
</html>

รูปแบบสีที่รองรับ:

  • ชื่อสี: red, blue, green, transparent
  • HEX: #ff0000, #00ff00, #0000ff
  • RGB: rgb(255, 0, 0), rgba(255, 0, 0, 0.5)
  • HSL: hsl(0, 100%, 50%), hsla(0, 100%, 50%, 0.5)

หมายเหตุ: โครงสร้าง HTML ในตัวอย่างมีปัญหา (แท็ก body อยู่ใน head) ควรแก้ไขให้ถูกต้อง