Selector ใช้เลือก element ที่ต้องการตกแต่ง ส่วน specificity ใช้ตัดสินว่า style ใดมีน้ำหนักมากกว่าเมื่อมีกฎ CSS หลายชุดชนกัน การเข้าใจสองเรื่องนี้ช่วยลดปัญหา CSS ทับกันแบบคาดเดายาก
เลือก element ตามชื่อ tag มี specificity ต่ำ
h1 {
color: navy;
}
p {
line-height: 1.7;
}
เหมาะกับการกำหนด style พื้นฐานของ tag ทั่วทั้งหน้า
เลือก element ด้วย class มี specificity ปานกลาง ใช้ซ้ำได้
.card {
border: 1px solid #ddd;
padding: 16px;
}
.btn {
padding: 8px 12px;
}
Class selector เป็นรูปแบบที่นิยมสำหรับ component และ utility classes
เลือก element ด้วย id มี specificity สูง
#header {
background: #111827;
}
ควรใช้เท่าที่จำเป็น เพราะแก้ override ยากกว่า class
เลือก element จาก attribute
input[type="text"] {
border: 1px solid #aaa;
}
a[href^="https"] {
color: green;
}
ตัวอย่าง ^= หมายถึงขึ้นต้นด้วย เช่น URL ที่ขึ้นต้นด้วย https
Pseudo-class เลือก state หรือเงื่อนไขพิเศษของ element
a:hover {
text-decoration: underline;
}
input:focus {
outline: 2px solid #3b82f6;
}
li:nth-child(odd) {
background: #f5f5f5;
}
button:not(.primary) {
opacity: 0.8;
}
ที่พบบ่อย ได้แก่ :hover, :focus, :nth-child(), :first-child, :not()
Pseudo-element ใช้สร้างหรือเลือกส่วนเสมือนของ element
.badge::before {
content: "★ ";
}
.badge::after {
content: " ใหม่";
}
input::placeholder {
color: #888;
}
::selection {
background: #fde68a;
}
Combinator ใช้อธิบายความสัมพันธ์ระหว่าง selector
article p {
color: #333;
}
nav > a {
margin-right: 12px;
}
h2 + p {
margin-top: 0;
}
h2 ~ p {
color: #555;
}
| Combinator | ความหมาย |
|---|---|
| ช่องว่าง | descendant |
> |
child โดยตรง |
+ |
adjacent sibling ถัดไปทันที |
~ |
sibling ที่ตามมาทั้งหมด |
โดยทั่วไปน้ำหนัก specificity เรียงจากสูงไปต่ำ:
ถ้า specificity เท่ากัน rule ที่เขียนทีหลังจะชนะ
p {
color: black;
}
.lead {
color: blue;
}
#intro {
color: red;
}
ถ้า element เป็น <p id="intro" class="lead"> สีจะเป็นแดง เพราะ ID selector มี specificity สูงกว่า
แนวทางที่ดี:
!important ถ้าไม่จำเป็นสร้างไฟล์ CSS ที่มี type, class, id, attribute, pseudo-class และ pseudo-element อย่างละ 1 ตัวอย่าง แล้วทดลองดูว่า rule ใดชนะเมื่อ style ชนกัน
:hover และ ::before ต่างกันอย่างไร> หมายถึงอะไร