亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

目錄
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() 可將多個選擇器分組,減少重複代碼並保持原有特異性,適用於需要維護可讀性和結(jié)構(gòu)清晰的場景;2. :where() 語法與功能類似,但其特異性始終為零,非常適合設置可被輕鬆覆蓋的默認樣式,常用於設計系統(tǒng)或組件庫;3. 兩者均支持複雜選擇器和嵌套結(jié)構(gòu),提升代碼可維護性;4. 瀏覽器支持良好,現(xiàn)代瀏覽器已廣泛兼容,舊環(huán)境可漸進增強使用。正確使用這兩個偽類能讓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()偽級的詳細內(nèi)容。更多資訊請關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本網(wǎng)站聲明
本文內(nèi)容由網(wǎng)友自願投稿,版權(quán)歸原作者所有。本站不承擔相應的法律責任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請聯(lián)絡admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅(qū)動的應用程序,用於創(chuàng)建逼真的裸體照片

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整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

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

如何用PHP搭建社交分享功能 PHP分享接口集成實戰(zhàn) 如何用PHP搭建社交分享功能 PHP分享接口集成實戰(zhàn) Jul 25, 2025 pm 08:51 PM

在PHP中搭建社交分享功能的核心方法是通過動態(tài)生成符合各平臺要求的分享鏈接。 1.首先獲取當前頁面或指定的URL及文章信息;2.使用urlencode對參數(shù)進行編碼;3.根據(jù)各平臺協(xié)議拼接生成分享鏈接;4.在前端展示鏈接供用戶點擊分享;5.動態(tài)生成頁面OG標籤優(yōu)化分享內(nèi)容展示;6.務必對用戶輸入進行轉(zhuǎn)義以防止XSS攻擊。該方法無需複雜認證,維護成本低,適用於大多數(shù)內(nèi)容分享需求。

PHP打造博客評論系統(tǒng)變現(xiàn) PHP評論審核與防刷策略 PHP打造博客評論系統(tǒng)變現(xiàn) PHP評論審核與防刷策略 Jul 25, 2025 pm 08:27 PM

1.評論系統(tǒng)商業(yè)價值最大化需結(jié)合原生廣告精準投放、用戶付費增值服務(如上傳圖片、評論置頂)、基於評論質(zhì)量的影響力激勵機制及合規(guī)匿名數(shù)據(jù)洞察變現(xiàn);2.審核策略應採用前置審核 動態(tài)關(guān)鍵詞過濾 用戶舉報機制組合,輔以評論質(zhì)量評分實現(xiàn)內(nèi)容分級曝光;3.防刷需構(gòu)建多層防禦:reCAPTCHAv3無感驗證、Honeypot蜜罐字段識別機器人、IP與時間戳頻率限制阻止灌水、內(nèi)容模式識別標記可疑評論,持續(xù)迭代應對攻擊。

如何用Mac搭建PHP Nginx環(huán)境 MacOS配置Nginx與PHP服務組合 如何用Mac搭建PHP Nginx環(huán)境 MacOS配置Nginx與PHP服務組合 Jul 25, 2025 pm 08:24 PM

Homebrew在Mac環(huán)境搭建中的核心作用是簡化軟件安裝與管理。 1.Homebrew自動處理依賴關(guān)係,將復雜的編譯安裝流程封裝為簡單命令;2.提供統(tǒng)一的軟件包生態(tài),確保軟件安裝位置與配置標準化;3.集成服務管理功能,通過brewservices可便捷啟動、停止服務;4.便於軟件升級與維護,提升系統(tǒng)安全性與功能性。

什麼是常見的CSS瀏覽器不一致? 什麼是常見的CSS瀏覽器不一致? Jul 26, 2025 am 07:04 AM

不同瀏覽器對CSS解析存在差異,導致顯示效果不一致,主要包括默認樣式差異、盒模型計算方式、Flexbox和Grid佈局支持程度及某些CSS屬性行為不一致。 1.默認樣式處理不一致,解決方法是使用CSSReset或Normalize.css統(tǒng)一初始樣式;2.舊版IE的盒模型計算方式不同,建議統(tǒng)一使用box-sizing:border-box;3.Flexbox和Grid在邊緣情況或舊版本中表現(xiàn)有差異,應多測試並使用Autoprefixer;4.某些CSS屬性行為不一致,需查閱CanIuse並提供降級

如何將SCSS編譯到CSS? 如何將SCSS編譯到CSS? Jul 27, 2025 am 01:58 AM

installdartsassvianpmafterinstallingnode.jsusingnpminstall-gsass.2.compilescsstocssssusingthecommandSassInput.scsssoutput.css.3。 useass - watchinput.scssoutput.csstoauto-compileonsave.4.watchentirefolderswithsass-watchscss:css.5.usepartialswith_prefixfo

什麼是口音色的物業(yè)? 什麼是口音色的物業(yè)? Jul 26, 2025 am 09:25 AM

accent-color是CSS中用於自定義復選框、單選按鈕和滑塊等表單元素高亮顏色的屬性;1.它直接改變表單控件選中狀態(tài)的默認顏色,如將復選框的藍色勾選標記改為紅色;2.支持的元素包括type="checkbox"、type="radio"和type="range"的輸入框;3.使用accent-color可避免複雜的自定義樣式和額外DOM結(jié)構(gòu),保持原生可訪問性;4.現(xiàn)代瀏覽器普遍支持,舊瀏覽器需降級處理;5.設置accent-col

描述'垂直align”屬性及其典型用例 描述'垂直align”屬性及其典型用例 Jul 26, 2025 am 07:35 AM

1.ItAdjustSelementsLikeImagesRikeImagesOrformInputswithIntExtLineSustLineSlineSlineSlineSlikeLikeLikeBaseline,中間,Super,Super,Super和Sub.2.intablebecells,ItControlScontentalStalteNtalmscontentalMedwithThtop,Middle,Middle,Midder,Midder,經(jīng)常

如何更改CSS中的文本顏色? 如何更改CSS中的文本顏色? Jul 27, 2025 am 04:25 AM

要改變CSS中文本顏色,需使用color屬性;1.使用color屬性可設置文本前景色,支持顏色名稱(如red)、十六進制碼(如#ff0000)、RGB值(如rgb(255,0,0))、HSL值(如hsl(0,100%,50%))以及帶透明度的RGBA或HSLA(如rgba(255,0,0,0.5));2.可將顏色應用於包含文本的任何元素,如h1至h6標題、段落p、鏈接a(需注意a:link、a:visited、a:hover、a:active不同狀態(tài)的顏色設置)、按鈕、div、span等;3.最

See all articles