กวดวิชาCSS

CSS HOME บทนำ CSS ไวยากรณ์ CSS ตัวเลือก CSS CSS วิธีการ ความคิดเห็น CSS CSS สี พื้นหลัง CSS CSS Borders CSS Margins CSS Padding CSS ความสูง/ความกว้าง โมเดลกล่อง CSS CSS Outline CSS Text แบบอักษร CSS ไอคอน CSS ลิงค์ CSS รายการ CSS ตาราง CSS จอแสดงผล CSS CSS ความกว้างสูงสุด ตำแหน่ง CSS CSS Z-ดัชนี CSS Overflow CSS Float CSS Inline-block CSS Align CSS Combinators CSS Pseudo-class CSS Pseudo-element CSS ความทึบ แถบนำทาง CSS CSS Dropdowns CSS Image Gallery CSS Image Sprites CSS Attr Selectors แบบฟอร์ม CSS CSS Counters เค้าโครงเว็บไซต์ CSS หน่วย CSS ความจำเพาะของ CSS CSS !สำคัญ ฟังก์ชันคณิตศาสตร์ CSS

CSS ขั้นสูง

CSS มุมโค้งมน รูปภาพ CSS Border พื้นหลัง CSS CSS สี คำหลักสี CSS CSS Gradients CSS Shadows เอฟเฟกต์ข้อความ CSS CSS Web Fonts การแปลง CSS 2D การแปลง CSS 3D การเปลี่ยน CSS ภาพเคลื่อนไหว CSS CSS Tooltips CSS สไตล์รูปภาพ ภาพสะท้อน CSS CSS วัตถุพอดี CSS วัตถุตำแหน่ง CSS Masking ปุ่ม CSS CSS Pagination CSS หลายคอลัมน์ ส่วนต่อประสานผู้ใช้ CSS ตัวแปร CSS ขนาดกล่อง CSS CSS Media Queries ตัวอย่าง CSS MQ CSS Flexbox

CSS Responsive

แนะนำ RWD วิวพอร์ต RWD RWD Grid View แบบสอบถามสื่อ RWD รูปภาพ RWD วิดีโอ RWD กรอบงาน RWD เทมเพลต RWD

CSS Grid

บทนำกริด คอนเทนเนอร์กริด รายการกริด

CSS SASS

กวดวิชา SASS

ตัวอย่างCSS

เทมเพลต CSS ตัวอย่าง CSS แบบทดสอบ CSS แบบฝึกหัด CSS ใบรับรอง CSS

การอ้างอิงCSS

การอ้างอิง CSS ตัวเลือก CSS ฟังก์ชัน CSS CSS อ้างอิง Aural CSS Web Safe Fonts CSS Animable หน่วย CSS ตัวแปลง CSS PX-EM CSS สี ค่าสี CSS ค่าดีฟอลต์ CSS รองรับเบราว์เซอร์ CSS

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;
}

ทดสอบตัวเองด้วยแบบฝึกหัด

ออกกำลังกาย:

ตั้งค่าสีพื้นหลังเป็นสีแดงของบรรทัดแรกของย่อหน้า

<style>
 {
  background-color: red;
}
</style>

<body>

<p class="intro">
In my younger and more vulnerable years
my father gave me some advice that I've
been turning over in my mind ever since.
'Whenever you feel like criticizing anyone,' he told me,
'just remember that all the people in this world
haven't had the advantages that you've had.'
</p>

</body>


องค์ประกอบหลอก 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