در CSS، where() یک pseudo-class مدرن است که برای گروهبندی selectorها استفاده میشود، اما یک تفاوت مهم دارد:
specificity آن همیشه
0است.
یعنی میتوانی selectorهای پیچیده بنویسی بدون اینکه specificity بالا برود.
Syntax
:where(selector1, selector2, ...)
مثال ساده
:where(h1, h2, h3) {
color: blue;
}
معادل:
h1,
h2,
h3 {
color: blue;
}
اما بدون افزایش specificity.
تفاوت :where() و :is()
:is()
specificity را از قویترین selector داخلش میگیرد.
:is(.card, #hero) {
color: red;
}
specificity بالا میرود چون #hero وجود دارد.
:where()
همیشه specificity = 0
:where(.card, #hero) {
color: red;
}
حتی با وجود #hero باز هم specificity صفر است.
کاربرد مهم در Design System و UI Kit
برای نوشتن styleهای پایه که بعداً راحت override شوند.
مثلاً:
:where(.btn) {
padding: 12px;
border-radius: 8px;
}
بعداً:
.custom .btn {
padding: 20px;
}
بدون نیاز به !important.
مثال حرفهای
بدون :where()
.card ul li a {
color: gray;
}
override سخت میشود.
با :where()
:where(.card ul li a) {
color: gray;
}
الان هر selector سادهای میتواند override کند:
a {
color: black;
}
ترکیب با nesting
.card {
& :where(a, button) {
transition: 0.3s;
}
}
Browser Support
تقریباً همه مرورگرهای مدرن پشتیبانی میکنند:
- Chrome
- Firefox
- Safari
- Edge
بهترین کاربردها
- Design systems
- UI kits
- Utility-first architecture
- Odoo website styling
- کاهش جنگ specificity
- ساخت CSS قابل override
نکته مهم
این:
:where(.a .b .c)
از نظر specificity مثل این است:
* {}
یعنی صفر.
ولی همچنان فقط روی .a .b .c اعمال میشود.

دیدگاهتان را بنویسید