CSS องค์ประกอบหลอก
Pseudo-Elements คืออะไร?
องค์ประกอบเทียม CSS ใช้เพื่อจัดรูปแบบส่วนที่ระบุขององค์ประกอบ
ตัวอย่างเช่น สามารถใช้เพื่อ:
- จัดรูปแบบตัวอักษรตัวแรกหรือบรรทัดขององค์ประกอบ
- แทรกเนื้อหาก่อนหรือหลังเนื้อหาขององค์ประกอบ
ไวยากรณ์
ไวยากรณ์ขององค์ประกอบหลอก:
selector::pseudo-element {
property: value;
}
The ::first-line Pseudo-element
องค์ประกอบ::first-line
หลอกใช้เพื่อเพิ่มสไตล์พิเศษให้กับบรรทัดแรกของข้อความ
ตัวอย่างต่อไปนี้จัดรูปแบบบรรทัดแรกของข้อความในองค์ประกอบ <p> ทั้งหมด:
ตัวอย่าง
p::first-line
{
color: #ff0000;
font-variant: small-caps;
}
หมายเหตุ:องค์ประกอบ::first-line
เทียมสามารถใช้ได้กับองค์ประกอบระดับบล็อกเท่านั้น
คุณสมบัติต่อไปนี้ใช้กับ::first-line
องค์ประกอบหลอก:
- คุณสมบัติแบบอักษร
- คุณสมบัติสี
- คุณสมบัติพื้นหลัง
- การเว้นวรรค
- ระยะห่างระหว่างตัวอักษร
- ข้อความตกแต่ง
- แนวตั้งจัด
- การแปลงข้อความ
- ความสูงของเส้น
- แจ่มใส
สังเกตสัญกรณ์โคลอนคู่ - ::first-line
เทียบกับ
:first-line
ทวิภาคแทนที่ สัญกรณ์โคลอนเดี่ยวสำหรับองค์ประกอบหลอกใน CSS3 นี่เป็นความพยายามจาก W3C ในการแยกแยะระหว่างคลาส หลอก
และองค์ประกอบหลอก
ไวยากรณ์โคลอนเดียวใช้สำหรับทั้งคลาสหลอกและองค์ประกอบหลอกใน CSS2 และ CSS1
สำหรับความเข้ากันได้แบบย้อนหลัง ไวยากรณ์โคลอนเดียวสามารถใช้ได้สำหรับองค์ประกอบเทียม CSS2 และ CSS1
The ::first-letter Pseudo-element
องค์ประกอบ::first-letter
หลอกใช้เพื่อเพิ่มลักษณะพิเศษให้กับตัวอักษรตัวแรกของข้อความ
ตัวอย่างต่อไปนี้จัดรูปแบบตัวอักษรตัวแรกของข้อความในองค์ประกอบ <p> ทั้งหมด:
ตัวอย่าง
p::first-letter
{
color: #ff0000;
font-size: xx-large;
}
หมายเหตุ:องค์ประกอบ::first-letter
เทียมสามารถใช้ได้กับองค์ประกอบระดับบล็อกเท่านั้น
คุณสมบัติต่อไปนี้ใช้กับ ::first-letter pseudo- องค์ประกอบ:
- คุณสมบัติแบบอักษร
- คุณสมบัติสี
- คุณสมบัติพื้นหลัง
- คุณสมบัติระยะขอบ
- คุณสมบัติการเติม
- คุณสมบัติชายแดน
- ข้อความตกแต่ง
- แนวตั้ง-จัด (เฉพาะในกรณีที่ "ลอย" คือ "ไม่มี")
- การแปลงข้อความ
- ความสูงของเส้น
- ลอย
- แจ่มใส
องค์ประกอบหลอกและคลาส HTML
องค์ประกอบหลอกสามารถรวมกับคลาส HTML:
ตัวอย่าง
p.intro::first-letter {
color: #ff0000;
font-size: 200%;
}
ตัวอย่างข้างต้นจะแสดงอักษรตัวแรกของย่อหน้าที่มี class="intro" เป็นสีแดงและมีขนาดใหญ่ขึ้น
องค์ประกอบหลอกหลายตัว
สามารถรวมองค์ประกอบหลอกได้หลายตัว
ในตัวอย่างต่อไปนี้ ตัวอักษรตัวแรกของย่อหน้าจะเป็นสีแดงในขนาดตัวอักษร xx ขนาดใหญ่ ส่วนที่เหลือของบรรทัดแรกจะเป็นสีน้ำเงิน และเป็นตัวพิมพ์เล็ก ส่วนที่เหลือของย่อหน้าจะเป็นขนาดตัวอักษรและสีเริ่มต้น:
ตัวอย่าง
p::first-letter
{
color: #ff0000;
font-size: xx-large;
}
p::first-line
{
color: #0000ff;
font-variant: small-caps;
}
CSS - The ::before Pseudo-element
องค์ประกอบ::before
เทียมสามารถใช้เพื่อแทรกเนื้อหาบางส่วนก่อนเนื้อหาขององค์ประกอบ
ตัวอย่างต่อไปนี้แทรกรูปภาพก่อนเนื้อหาของแต่ละองค์ประกอบ <h1>:
ตัวอย่าง
h1::before
{
content: url(smiley.gif);
}
CSS - ::หลัง Pseudo-element
องค์ประกอบ::after
เทียมสามารถใช้เพื่อแทรกเนื้อหาบางส่วนหลังเนื้อหาขององค์ประกอบ
ตัวอย่างต่อไปนี้แทรกรูปภาพหลังเนื้อหาของแต่ละองค์ประกอบ <h1>:
ตัวอย่าง
h1::after
{
content: url(smiley.gif);
}
CSS - The ::marker Pseudo-element
องค์ประกอบ::marker
เทียมจะเลือกเครื่องหมายของรายการ
ตัวอย่างต่อไปนี้กำหนดรูปแบบตัวทำเครื่องหมายของรายการ:
ตัวอย่าง
::marker {
color: red;
font-size: 23px;
}
CSS - The ::selection Pseudo-element
องค์ประกอบ::selection
เทียมตรงกับส่วนขององค์ประกอบที่ผู้ใช้เลือก
คุณสมบัติ CSS ต่อไปนี้ใช้ได้กับ::selection
:
color
,
background
, , cursor
และoutline
ตัวอย่างต่อไปนี้ทำให้ข้อความที่เลือกเป็นสีแดงบนพื้นหลังสีเหลือง:
ตัวอย่าง
::selection {
color: red;
background: yellow;
}
องค์ประกอบหลอก CSS ทั้งหมด
Selector | Example | Example description |
---|---|---|
::after | p::after | Insert something after the content of each <p> element |
::before | p::before | Insert something before the content of each <p> element |
::first-letter | p::first-letter | Selects the first letter of each <p> element |
::first-line | p::first-line | Selects the first line of each <p> element |
::marker | ::marker | Selects the markers of list items |
::selection | p::selection | Selects the portion of an element that is selected by a user |
CSS Pseudo Classes ทั้งหมด
Selector | Example | Example description |
---|---|---|
:active | a:active | Selects the active link |
:checked | input:checked | Selects every checked <input> element |
:disabled | input:disabled | Selects every disabled <input> element |
:empty | p:empty | Selects every <p> element that has no children |
:enabled | input:enabled | Selects every enabled <input> element |
:first-child | p:first-child | Selects every <p> elements that is the first child of its parent |
:first-of-type | p:first-of-type | Selects every <p> element that is the first <p> element of its parent |
:focus | input:focus | Selects the <input> element that has focus |
:hover | a:hover | Selects links on mouse over |
:in-range | input:in-range | Selects <input> elements with a value within a specified range |
:invalid | input:invalid | Selects all <input> elements with an invalid value |
:lang(language) | p:lang(it) | Selects every <p> element with a lang attribute value starting with "it" |
:last-child | p:last-child | Selects every <p> elements that is the last child of its parent |
:last-of-type | p:last-of-type | Selects every <p> element that is the last <p> element of its parent |
:link | a:link | Selects all unvisited links |
:not(selector) | :not(p) | Selects every element that is not a <p> element |
:nth-child(n) | p:nth-child(2) | Selects every <p> element that is the second child of its parent |
:nth-last-child(n) | p:nth-last-child(2) | Selects every <p> element that is the second child of its parent, counting from the last child |
:nth-last-of-type(n) | p:nth-last-of-type(2) | Selects every <p> element that is the second <p> element of its parent, counting from the last child |
:nth-of-type(n) | p:nth-of-type(2) | Selects every <p> element that is the second <p> element of its parent |
:only-of-type | p:only-of-type | Selects every <p> element that is the only <p> element of its parent |
:only-child | p:only-child | Selects every <p> element that is the only child of its parent |
:optional | input:optional | Selects <input> elements with no "required" attribute |
:out-of-range | input:out-of-range | Selects <input> elements with a value outside a specified range |
:read-only | input:read-only | Selects <input> elements with a "readonly" attribute specified |
:read-write | input:read-write | Selects <input> elements with no "readonly" attribute |
:required | input:required | Selects <input> elements with a "required" attribute specified |
:root | root | Selects the document's root element |
:target | #news:target | Selects the current active #news element (clicked on a URL containing that anchor name) |
:valid | input:valid | Selects all <input> elements with a valid value |
:visited | a:visited | Selects all visited links |