Pseudo-classes คืออะไร
Pseudo-classes เป็นเครื่องมือใน CSS ที่จะช่วยปรับแต่ง style ตามสถานะต่างๆของ Element ยกตัวอย่างเช่น เปลี่ยนสีปุ่มเมื่อเมาส์เข้าไปวางอยู่บนปุ่ม หรือ กำหนดสีของลิงค์ที่เยี่ยมชมแล้วกับยังไม่ได้เยี่ยมชม เป็นต้น
ไวยากรณ์การเขียน Pseudo-classes (Syntax)
ตัวอย่าง Pseudo-classes สำหรับลิงก์
ตัวอย่าง pseudo-classes สำหรับ link
Format code using Prettier Reset Code
Focus the editor. This will trap focus until you press Escape. < style >
a :link {
color : red ;
}
a :visited {
color : pink ;
}
a :hover {
color : green ;
}
a :active {
color : blue ;
}
</ style >
< a href = " # " > Link </ a >
Label for Code Editor Resize editor. Use left/right arrows. Result
Refresh for rerender result
การใช้ Pseudo-classes ร่วมกับ Class และ ID
Pseudo-classes สามารถใช้ร่วมกับ Class และ ID ได้ โดยใช้เครื่องหมาย .
และ #
ตามลำดับ ดังตัวอย่าง
Pseudo-class :hover
:hover
คือเครื่องมือที่ใช้กำหนดสไตล์เมื่อเมาส์วางอยู่เหนือ Element
ตัวอย่างการใช้ :hover
ตัวอย่างการใช้ :hover
Format code using Prettier Reset Code
index.html style.css
Focus the editor. This will trap focus until you press Escape. < style >
a {
color : blue ;
text-decoration : none ;
}
a :hover {
color : red ;
text-decoration : underline ;
}
p :hover {
background-color : yellow ;
cursor : pointer ;
}
</ style >
< div class = " container " >
< a href = " # " > Hover me </ a >
< p > Hover me </ p >
</ div >
Label for Code Editor Resize editor. Use left/right arrows. Result
Refresh for rerender result
Pseudo-class :focus
:focus
ใช้กำหนดสไตล์เมื่อองค์ประกอบได้รับการเลือกหรือคลิก
ตัวอย่างการใช้ :focus
ตัวอย่างการใช้ :focus
Format code using Prettier Reset Code
Focus the editor. This will trap focus until you press Escape. < style >
input :focus {
outline : 20 px solid red ;
box-shadow : 0 0 5 px rgba ( 0 , 0 , 255 , 0.5 ) ;
}
button :focus {
background-color : lightblue ;
}
</ style >
< input
type = " text "
placeholder = " Type here "
/>
< button > Click me </ button >
Label for Code Editor Resize editor. Use left/right arrows. Result
Refresh for rerender result
ทำไม Pseudo-classes focus ถึงสำคัญ? ในการพัฒนาเว็บไซต์ปัจจุบัน ความสามารถในการเข้าถึง (Accessibility)
เป็นสิ่งสำคัญ การใช้ Pseudo-classes focus
ช่วยให้ผู้ใช้ที่ใช้คีย์บอร์ดหรือไม่สามารถควบคุมด้วยมือสามารถใช้งานเว็บไซต์ได้ง่ายขึ้น
โดยไม่ต้องใช้เมาส์.
Pseudo-classes :first-child
และ :last-child
:first-child
คือ Pseudo-classes ที่เลือก Element แรกภายในคอนเทนเนอร์
:last-child
คือ Pseudo-classes ที่เลือก Element สุดท้ายภายในคอนเทนเนอร์
ตัวอย่างการใช้ :first-child
และ :last-child
ตัวอย่างการใช้ :first-child กับ :last-child
Format code using Prettier Reset Code
Focus the editor. This will trap focus until you press Escape. < style >
li :first-child {
color : blue ;
font-weight : bold ;
}
li :last-child {
color : red ;
font-weight : bold ;
}
</ style >
< ul >
< li > รายการที่ 1 </ li >
< li > รายการที่ 2 </ li >
< li > รายการที่ 3 </ li >
< li > รายการที่ 4 </ li >
< li > รายการที่ 5 </ li >
< li > รายการที่ 6 </ li >
</ ul >
Label for Code Editor Resize editor. Use left/right arrows. Result
Refresh for rerender result
Pseudo-classes first-of-type กับ last-of-type คืออะไร? นอกจาก :first-child กับ :last-child เรายังมี :first-of-type กับ :last-of-type
ด้วย ทั้ง 4 อย่างนี้อาจจะดูเหมือนกันแต่ก็มีอย่างนึงที่ต่างกันอย่างสิ้นเชิง
นั้นก็คือ :first-of-type
กับ
:last-of-type
จะพิจารณาจากประเภทของ Tag HTML
ความแตกต่างระหว่าง :first-of-type กับ :first-child
Format code using Prettier Reset Code
Focus the editor. This will trap focus until you press Escape. < style >
p :first-child {
color : blue ;
font-weight : bold ;
}
</ style >
< div >
< h1 > Header 1 </ h1 >
< p > Paragraph 1 </ p >
< p > Paragraph 2 </ p >
< p > Paragraph 3 </ p >
</ div >
Label for Code Editor Resize editor. Use left/right arrows. Result
Refresh for rerender result
จากตัวอย่างจะเห็นว่า แม้ว่า p:first-child
จะเลือก Element แรกของคอนเทนเนอร์ แต่จะไม่สามารถเลือก Element แรกของประเภทของ Tag HTML ได้ ดังนั้น จึงมี p:first-of-type
มาช่วยในกรณีนี้
ความแตกต่างระหว่าง :first-of-type กับ :first-child
Format code using Prettier Reset Code
Focus the editor. This will trap focus until you press Escape. < style >
p :first-of-type {
color : blue ;
font-weight : bold ;
}
</ style >
< div >
< h1 > Header 1 </ h1 >
< p > Paragraph 1 </ p >
< p > Paragraph 2 </ p >
< p > Paragraph 3 </ p >
</ div >
Label for Code Editor Resize editor. Use left/right arrows. Result
Refresh for rerender result