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

CSS Pseudo Elements


เราสามารถจัดรูปแบบส่วนเฉพาะขององค์ประกอบ เช่น first-letter, first-line หรือแม้แต่การแทรกก่อน/หลังมัน สำหรับวัตถุประสงค์เหล่านี้ ใช้ CSS Pseudo Elements

หมายเหตุ - เพื่อแยก CSS Pseudo Classes จาก Pseudo Elements ใน CSS3 pseudo elements ใช้การเขียนแบบ double-colon

ไวยากรณ์

ไวยากรณ์สำหรับการใช้ CSS Pseudo elements บนองค์ประกอบ:

Selector::pseudo-element {    css-property: /*value*/; }

Pseudo Elements ที่มีอยู่ทั้งหมดใน CSS:

ลำดับPseudo Element และคำอธิบาย
1after
แทรกสิ่ง gì đóหลังเนื้อหาของแต่ละองค์ประกอบที่กล่าวถึง
2before
แทรกสิ่ง gì đóก่อนเนื้อหาของแต่ละองค์ประกอบที่กล่าวถึง
3first-letter
เลือกตัวอักษรแรกของแต่ละองค์ประกอบที่กล่าวถึง
4first-line
เลือกบรรทัดแรกของแต่ละองค์ประกอบที่กล่าวถึง
5placeholder
เลือกข้อความ placeholder ในองค์ประกอบฟอร์ม
6selection
เลือกส่วนขององค์ประกอบที่ผู้ใช้เลือก

ลองดูตัวอย่างของ CSS Pseudo Elements:

ตัวอย่าง

<!DOCTYPE html> <html> <head> <style> p::first-letter {    background-color: black; } p::first-line {    background-color: lightgreen;    color: white; } span {    font-size: 2em;    color: #DC3545; } </style> </head> <body> <h2>เครือข่ายคอมพิวเตอร์</h2> <p><span>A</span> system of interconnected computers and computerized peripherals such as printers is called computer network. </p> </body> </html>

ผลลัพธ์

ลองดูอีกตัวอย่างของ CSS Pseudo Elements:

CSS Pseudo Elements

ตัวอย่างเพิ่มเติม

<!DOCTYPE html> <html> <head> <style> div:nth-of-type(1) p:nth-child(2)::after {    content: " LEGEND!";    background: orange;    padding: 5px; } div:nth-of-type(2) p:nth-child(2)::before {    content: "Book:";    background-color: lightblue;    font-weight: bold;    padding: 5px; } </style> </head> <body> <div> <p>Cricketer</p> <p>Sachin Tendulkar:</p> </div> <hr> <div> <p><q>Chase your Dreams</q></p> <p><q>Playing It My Way</q></p> </div> </body> </html>

ผลลัพธ์

CSS Pseudo Elements