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

การกำหนดสีข้อความด้วย CSS color property

ใช้คุณสมบัติ color ใน CSS เพื่อกำหนดสีข้อความ สามารถระบุสีได้หลายรูปแบบ: ชื่อสี, ค่า Hex, RGB, HSL, HSLA, RGBA

ตัวอย่างโค้ด

<!DOCTYPE html> <html> <head> <style> span {    background-color: orange;    color: white; } p.demo {    display: none; } span.demo1 {    display: inline; } </style> </head> <body> <h1>Match Details</h1> <div> Match will begin at <p class="demo">9AM</p> 10AM on 20th December. </div> <div> Match will end at <span class="demo1">5PM</span> on 20th December. </div> </body> </html>

ตัวอย่างเพิ่มเติม: รูปแบบสีต่างๆ

<!DOCTYPE html> <html> <head> <style> p.demo1 {    word-spacing: 1cm;    color: #F8E61C; } p.demo2 {    word-spacing: 40px; } </style> </head> </body> <body> <h1>Demo Heading</h1> <h2>Heading2</h2> <p class="demo1">This is demo text.</p> <h2>Heading2</h2> <p class="demo2">This is demo text.</p> </body> </html>

คำอธิบาย

ตัวอย่างแรกแสดงการใช้ color: white บนพื้นหลังส้ม ตัวอย่างที่สองใช้ค่า Hex #F8E61C (สีเหลืองทอง)

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

รูปแบบตัวอย่างคำอธิบาย
ชื่อสีred, blue140+ ชื่อสีมาตรฐาน
Hex#FF0000, #F006 หรือ 3 หลัก
RGBrgb(255, 0, 0)0-255 ต่อช่อง
RGBArgba(255, 0, 0, 0.5)พร้อมความทึบแสง
HSLhsl(0, 100%, 50%)Hue, Saturation, Lightness
HSLAhsla(0, 100%, 50%, 0.5)HSL พร้อม Alpha

เคล็ดลับ:

ใช้ currentColor เพื่ออ้างอิงค่า color ปัจจุบัน สำหรับ border, box-shadow, ฯลฯ