เราสามารถเพิ่มสไตล์เฉพาะให้กับองค์ประกอบที่มีอยู่ใน HTML โดยใช้ CSS Pseudo classes ซึ่งเลือกองค์ประกอบที่มีสถานะเฉพาะ เช่น (hover, visited, disabled, ฯลฯ)
หมายเหตุ - เพื่อแยก CSS Pseudo Classes ออกจาก Pseudo Elements ใน CSS3 pseudo classes ใช้สัญลักษณ์ colon เดียว (single-colon notation)
ไวยากรณ์
ไวยากรณ์สำหรับการใช้ CSS Pseudo classes บนองค์ประกอบมีดังนี้:
Selector:pseudo-class { css-property: /*value*/; }
ต่อไปนี้คือ CSS Pseudo Classes ทั้งหมดที่มีอยู่:
| ลำดับ | Pseudo-Class & คำอธิบาย |
|---|---|
| 1 | active เลือกองค์ประกอบที่ระบุซึ่งอยู่ในสถานะ active |
| 2 | checked เลือกทุกองค์ประกอบที่ระบุซึ่งถูก checked |
| 3 | disabled เลือกทุกองค์ประกอบที่ระบุซึ่งถูก disabled |
| 4 | empty เลือกทุกองค์ประกอบที่ระบุซึ่งไม่มีลูก (children) |
| 5 | enabled เลือกทุกองค์ประกอบที่ระบุซึ่งถูก enabled |
| 6 | first-child เลือกทุกองค์ประกอบที่ระบุซึ่งเป็นลูกคนแรกของพาเรนต์ |
| 7 | first-of-type เลือกทุกองค์ประกอบที่เป็นองค์ประกอบแรกที่ระบุของพาเรนต์ |
| 8 | focus เลือกองค์ประกอบที่ระบุซึ่งมี focus |
| 9 | hover เลือกองค์ประกอบที่ระบุเมื่อเมาส์ชี้ไปอยู่เหนือ |
| 10 | in-range เลือกองค์ประกอบที่ระบุซึ่งมีค่าอยู่ในช่วงที่กำหนด |
| 11 | invalid เลือกทุกองค์ประกอบที่ระบุซึ่งมีค่าไม่ถูกต้อง |
| 12 | lang(language) เลือกทุกองค์ประกอบที่ระบุซึ่งมีค่าแอตทริบิวต์ lang เริ่มต้นด้วย "language" |
| 13 | last-child เลือกทุกองค์ประกอบที่ระบุซึ่งเป็นลูกคนสุดท้ายของพาเรนต์ |
| 14 | last-of-type เลือกทุกองค์ประกอบที่เป็นองค์ประกอบสุดท้ายที่ระบุของพาเรนต์ |
| 15 | link เลือกทุกองค์ประกอบที่ระบุซึ่งยังไม่ได้เยี่ยมชม |
| 16 | not(selector) เลือกทุกองค์ประกอบที่ไม่ใช่องค์ประกอบที่ระบุ |
| 17 | nth-child(n) เลือกทุกองค์ประกอบที่ระบุซึ่งเป็นลูกคนที่ n ของพาเรนต์ |
| 18 | nth-last-child(n) เลือกทุกองค์ประกอบที่ระบุซึ่งเป็นลูกคนที่ n ของพาเรนต์ นับจากลูกคนสุดท้าย |
| 19 | nth-last-oftype(n) เลือกทุกองค์ประกอบที่ระบุซึ่งเป็นองค์ประกอบที่ n ที่ระบุของพาเรนต์ นับจากลูกคนสุดท้าย |
| 20 | nth-of-type(n) เลือกทุกองค์ประกอบที่ระบุซึ่งเป็นองค์ประกอบที่ n ที่ระบุของพาเรนต์ |
| 21 | only-of-type เลือกทุกองค์ประกอบที่ระบุซึ่งเป็นองค์ประกอบเดียวที่ระบุของพาเรนต์ |
| 22 | only-child เลือกทุกองค์ประกอบที่ระบุซึ่งเป็นลูกคนเดียวของพาเรนต์ |
| 23 | optional เลือกองค์ประกอบที่ระบุซึ่งไม่มีแอตทริบิวต์ "required" |
| 24 | out-of-range เลือกองค์ประกอบที่ระบุซึ่งมีค่าอยู่นอกช่วงที่กำหนด |
| 25 | read-only เลือกองค์ประกอบที่ระบุซึ่งมีแอตทริบิวต์ "readonly" ระบุไว้ |
| 26 | read-write เลือกองค์ประกอบที่ระบุซึ่งไม่มีแอตทริบิวต์ "readonly" |
| 27 | required เลือกองค์ประกอบที่ระบุซึ่งมีแอตทริบิวต์ "required" ระบุไว้ |
| 28 | root เลือกองค์ประกอบราก (root element) ของเอกสาร |
| 29 | target เลือกองค์ประกอบที่ระบุซึ่ง active อยู่ (คลิกที่ URL ที่มี anchor name นั้น) |
| 30 | valid เลือกทุกองค์ประกอบที่ระบุซึ่งมีค่าถูกต้อง |
| 31 | visited เลือกทุกองค์ประกอบที่ระบุซึ่งเคยเยี่ยมชมแล้ว |
ตัวอย่าง
ลองดูตัวอย่างของ CSS Pseudo Class:
<!DOCTYPE html> <html> <head> <title>CSS Anchor Pseudo Classes</title> </head> <style> div { color: #000; padding:20px; background-image: linear-gradient(135deg, grey 0%, white 100%); text-align: center; } .anchor { color: #FF8A00; } .anchor:last-child { color: #03A9F4; } .anchor:visited { color: #FEDC11; } .anchor:hover { color: #C303C3; } .anchor:active { color: #4CAF50; } </style> <body> <div> <h1>Search Engines</h1> <a class="anchor" href="https://www.google.com" target="_blank">Go To Google</a> <a class="anchor" href="https://www.bing.com" target="_blank">Go To Bing</a> </div> </body> </html>
ผลลัพธ์
จะได้ผลลัพธ์ดังนี้:

ตัวอย่าง
ลองดูตัวอย่างเพิ่มเติมของ CSS Pseudo Class:
<!DOCTYPE html> <html> <head> <title>CSS Pseudo Classes</title> <style> form { width:70%; margin: 0 auto; text-align: center; } * { padding: 2px; margin:5px; box-sizing: border-box; } .child{ display: inline-block; height: 40px; width: 40px; color: white; border: 4px solid black; } .child:nth-of-type(1){ background-color: #FF8A00; } .child:nth-of-type(2){ background-color: #F44336; } .child:nth-of-type(3){ background-color: #C303C3; } .child:nth-of-type(4){ background-color: #4CAF50; } .child:nth-of-type(5){ background-color: #03A9F4; } .child:nth-of-type(6){ background-color: #FEDC11; } .child:hover{ background-color: #000; } </style> </head> <body> <form> <fieldset> <legend>CSS-Pseudo-Classes</legend> <div id="container"> <div class="child"></div><div class="child"></div><div class="child"></div><div class="child"></div><div class="child"></div><div class="child"></div> </div><br> </body> </html>
ผลลัพธ์
จะได้ผลลัพธ์ดังนี้:
เมื่อไม่ได้วางเมาส์บนองค์ประกอบ div:

เมื่อวางเมาส์บนองค์ประกอบ div:
