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

ใช้ Inline CSS Style สำหรับองค์ประกอบใน HTML


ใช้แอตทริบิวต์ style เพื่อกำหนด inline CSS โดยตรงบนองค์ประกอบ

ไวยากรณ์

<element style="property: value; property2: value2;">

คั่นคุณสมบัติ CSS หลายตัวด้วย semicolon (;)

ตัวอย่าง

<!DOCTYPE html> <html>    <body>       <h1 style="color:white;background-color:black">Tutorialspoint</h1>       <h2>Simply Easy Learning</h2>       <p>We provide free learning content.</p>    </body> </html>

ข้อควรระวัง: Inline styles มี specificity สูงสุด ยากกว่าจะ override ด้วย external CSS ควรใช้ external stylesheet สำหรับโปรเจกต์จริง ใช้ inline เฉพาะกรณีจำเป็น เช่น dynamic styles จาก JavaScript หรือ email templates