目錄
What :is() Does (and Why It's Useful)
How :where() Is Different (and When to Use It)
Practical Examples and Real-World Use Cases
1. Simplifying Nested Hover States
2. Resetting Styles Without Fighting Specificity
3. Responsive Utility Classes
Browser Support and Compatibility
首頁 web前端 H5教程 探索新的CSS:IS()和:where()偽級

探索新的CSS:IS()和:where()偽級

Jul 30, 2025 am 05:33 AM
css 偽類

:is() 和:where() 是功能強大且語法簡潔的CSS 偽類,用於簡化選擇器編寫並解決特異性問題。 1. :is() 可將多個選擇器分組,減少重複代碼並保持原有特異性,適用於需要維護可讀性和結構清晰的場景;2. :where() 語法與功能類似,但其特異性始終為零,非常適合設置可被輕鬆覆蓋的默認樣式,常用於設計系統或組件庫;3. 兩者均支持複雜選擇器和嵌套結構,提升代碼可維護性;4. 瀏覽器支持良好,現代瀏覽器已廣泛兼容,舊環境可漸進增強使用。正確使用這兩個偽類能讓CSS 更加靈活、健壯且易於擴展。

CSS has quietly gotten a lot more powerful — and a bit more elegant — with the introduction of the :is() and :where() pseudo-classes. These aren't just syntactic sugar; they solve real pain points in writing and maintaining CSS selectors. If you've ever found yourself repeating long selector lists or struggling with specificity issues, these two pseudo-classes are here to help.

Let's break down what they do, how they're different, and why you'll want to start using them.


What :is() Does (and Why It's Useful)

:is() is a CSS pseudo-class that takes a selector list and matches any element that fits at least one of the selectors inside it. Think of it as a way to group selectors without repeating the same pattern over and over.

Before :is() , you might write something like this:

 .card:hover,
.sidebar:hover,
.nav-link:hover,
.modal-header:hover {
  opacity: 0.8;
}

That's repetitive and hard to maintain. With :is() , you can simplify it:

 :is(.card, .sidebar, .nav-link, .modal-header):hover {
  opacity: 0.8;
}

This is cleaner, easier to read, and much easier to update. Just add or remove a class from the list inside :is() .

It also works with more complex selectors:

 :is(header, main, footer) h1 {
  font-size: 2rem;
}

This applies the style to h1 elements inside header , main , or footer .

Key benefit : :is() follows normal specificity rules — the specificity is determined by the most specific selector in the list. So if one of the items in :is() is #id , the whole selector gets high specificity.


How :where() Is Different (and When to Use It)

:where() looks and works almost exactly like :is() — it also takes a list of selectors and matches any that apply. But here's the critical difference :

:where() always has zero specificity .

That means no matter what you put inside :where() , it won't add any specificity weight to the selector.

This is incredibly useful for default styles or reset/utility patterns , where you want to define base styles that are easy to override later.

For example, a UI library might set default button styles:

 :where(button, [role="button"], .btn) {
  padding: 0.5em 1em;
  border: 1px solid #ccc;
  background: #f0f0f0;
  border-radius: 4px;
}

Because :where() has zero specificity, a simple class rule later in the CSS can easily override it:

 .primary-button {
  background: blue;
  color: white;
}

Without :where() , you'd often need to use !important or match high specificity (like button.btn.primary ), which gets messy.

Use :where() when you want flexible, override-friendly styles — especially in design systems or component libraries.


Practical Examples and Real-World Use Cases

1. Simplifying Nested Hover States

You want any direct child of a container to respond to hover:

 .container :is(.card, .item, .post):hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}

Clean and scalable.

2. Resetting Styles Without Fighting Specificity

Use :where() in a reset or base layer:

 :where(ul, ol) {
  margin: 0;
  padding: 0;
  list-style: none;
}

Now you can style a specific list later without worrying about being overridden by reset rules.

3. Responsive Utility Classes

Combine with utility-first approaches:

 :where(.md\\:hover:hover) {
  /* Only applies when .md:hover is used, but won't fight other styles */
}

Note: You need to escape the colon with \\: in class names.


Browser Support and Compatibility

Both :is() and :where() are widely supported in modern browsers:

  • :is() — Supported in Chrome 88 , Firefox 78 , Safari 13.4
  • :where() — Chrome 88 , Firefox 78 , Safari 14.1

For older browsers, you may need to stick with traditional selectors or use build tools that can expand these (though no reliable autoprefixer exists yet for :is() / :where() ).

If you're targeting older environments, consider using them progressively — they fail silently, so fallbacks can be provided.


Summary: :is() vs :where()

Feature :is() :where()
Function Selector grouping Selector grouping
Specificity Based on highest inside list Always zero
Best for DRY selectors, reducing repetition Reset styles, themes, utilities
Override difficulty Can be high (depends on input) Very easy

Use :is() when you want cleaner, more maintainable CSS without changing how specificity works.

Use :where() when you want to set defaults that won't get in the way later.

Both make CSS more expressive and less fragile. And honestly, once you start using them, you'll wonder how you lived without them.

Basically — they're small features with big impact.

以上是探索新的CSS:IS()和:where()偽級的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Laravel 教程
1604
29
PHP教程
1509
276
如何使用CSS Backdrop-Filter屬性? 如何使用CSS Backdrop-Filter屬性? Aug 02, 2025 pm 12:11 PM

backdrop-filter用於對元素背後的內容應用視覺效果,1.使用backdrop-filter:blur(10px)等語法實現毛玻璃效果;2.支持blur、brightness、contrast等多種濾鏡函數並可疊加;3.常用於玻璃態卡片設計,需確保元素與背景重疊;4.現代瀏覽器支持良好,可用@supports提供降級方案;5.避免過大模糊值和頻繁重繪以優化性能,該屬性僅在元素背後有內容時生效。

CSS方面比例屬性是什麼?如何使用它? CSS方面比例屬性是什麼?如何使用它? Aug 04, 2025 pm 04:38 PM

Theaspect-ratioCSSpropertydefinesthewidth-to-heightratioofanelement,ensuringconsistentproportionsinresponsivedesigns.1.Itisapplieddirectlytoelementslikeimages,videos,orcontainersusingsyntaxsuchasaspect-ratio:16/9.2.Commonusecasesincludemaintainingres

如何使用CSS創建彈跳動畫? 如何使用CSS創建彈跳動畫? Aug 02, 2025 am 05:44 AM

Define@keyframesbouncewith0%,100%attranslateY(0)and50%attranslateY(-20px)tocreateabasicbounce.2.Applytheanimationtoanelementusinganimation:bounce0.6sease-in-outinfiniteforsmooth,continuousmotion.3.Forrealism,use@keyframesrealistic-bouncewithscale(1.1

如何使用CSS:空偽級? 如何使用CSS:空偽級? Aug 05, 2025 am 09:48 AM

:emptyPseudo-classSelectSelectsselemtswithnochildrenorcontent,包括pacesorcomments,sonlyTrulyEmpterementLikeMatchit; 1.ItcanhideEmptycontainersbousing:intume {note {note display:none;} toCleanuplayouts; 2.ItallowSaddingplacePlacePlacePlaceLanderStylingLingvia :: Forefore :: Forefor :: show offor :: show

虛擬貨幣交易app有哪些_2025正規虛擬貨幣交易app推薦前十名 虛擬貨幣交易app有哪些_2025正規虛擬貨幣交易app推薦前十名 Aug 08, 2025 pm 06:42 PM

1、Binance币安以庞大的交易量和丰富的交易对著称,提供多元交易模式与完善生态系统,并通过SAFU基金和多重安全技术保障用户资产安全且高度重视合规运营;2、OKX欧易提供广泛的数字资产交易服务和统一交易账户模式,积极布局Web3领域,并通过严格风控和用户教育提升交易安全与体验;3、gate.io芝麻开门以上币速度快和币种丰富见长,提供多样化交易工具与增值服务,采用多重安全验证机制并坚持资产储备透明化以增强用户信任;4、火币Huobi凭借深厚的行业积累提供一站式数字资产服务,拥有强大交易深度与

如何創建僅CSS的手風琴菜單? 如何創建僅CSS的手風琴菜單? Aug 03, 2025 pm 01:48 PM

使用隱藏的複選框和CSS的:checked偽類結合相鄰兄弟選擇器( )來控制內容顯示;2.HTML結構包含每個折疊項的input、label和內容div;3.通過設置max-height過渡實現平滑展開/收起動畫;4.可用偽元素添加打開/關閉狀態圖標;5.使用radio類型可實現單開模式,checkbox則允許多開。這是一種無需JavaScript、兼容現代瀏覽器的交互式折疊菜單實現方法。

如何將CSS剪輯路徑用於創意形狀? 如何將CSS剪輯路徑用於創意形狀? Aug 04, 2025 pm 02:55 PM

使用CSSclip-path可在瀏覽器中創建非矩形形狀,無需額外圖像或複雜SVG;2.常用形狀函數包括inset()、circle()、ellipse()和polygon(),其中polygon()通過定義坐標點實現自定義形狀,適合創建如對話框氣泡等創意設計;3.clip-path可通過CSS過渡或關鍵幀動畫實現動態效果,如懸停時的圓形展開,但僅支持相同類型和頂點數的形狀間動畫;4.應注意響應式與可訪問性,確保內容在不支持時仍可用,文本可讀,避免過度裁剪,並控制多邊形頂點數量以優化性能,同時需知

如何使用CSS創建文本梯度? 如何使用CSS創建文本梯度? Aug 01, 2025 am 07:39 AM

使用background-image與background-clip:text可實現CSS文字漸變效果;2.必須設置-webkit-background-clip:text和-webkit-text-fill-color:transparent以確保瀏覽器兼容性;3.可自定義線性或徑向漸變,並建議使用粗體或大號文字以提升視覺效果;4.推薦為不支持的環境設置color作為備用顏色;5.替代方案可使用-webkit-mask-image實現更複雜效果,但主要適用於高級場景;該方法簡單、兼容性好且視覺

See all articles