CSS 中使用 :not() 否定偽類可選中不含特定類名的元素,如 button:not(.primary) 表示選中非 .primary 類的按鈕;多個排除需鏈式書寫,如 button:not(.primary):not(.secondary);JS 中也可通過 querySelectorAll 實現(xiàn)相同效果;還可結(jié)合屬性、ID 使用,如 input:not([disabled]) 或 div:not(#header);不可嵌套使用且性能影響極小。
有時候你想在 CSS 或 JavaScript 中選中一個沒有某個類名的元素,比如給所有不帶 .active
的按鈕加個樣式。這時候就得用到 CSS 選擇器里的“否定偽類”——not()
。

下面幾種常見寫法和使用場景,能幫你準確命中目標元素。

基礎(chǔ)用法::not(.class)
最直接的方式就是用 :not()
并在里面寫上你不想選中的類名:
button:not(.primary) { background-color: gray; }
上面這段代碼的意思是:“選中所有不是 .primary
類的 <button>
元素,并設(shè)置背景色為灰色。”
也就是說,那些帶有 class="primary"
的按鈕不會被這個樣式影響。

注意:
:not()
只支持單個選擇器參數(shù),不能在里面寫多個類,比如:not(.a, .b)
是不合法的。
多個排除?可以鏈著寫
如果你想排除多個類,可以連續(xù)使用多個 :not()
:
button:not(.primary):not(.secondary) { background-color: darkgray; }
這樣就只會選中既不是 .primary
也不是 .secondary
的按鈕。
或者,在 JS 中操作時也可以配合 querySelectorAll
使用類似的語法:
document.querySelectorAll("button:not(.primary):not(.secondary)");
實際應(yīng)用:只對未激活項做處理
比如你在做一個導航欄,想給所有“非當前頁”的鏈接加一個特定樣式:
.nav-link:not(.active) { color: #666; }
這樣就能避免改動當前高亮的那個鏈接,而其他都統(tǒng)一灰一點。
這種技巧常用于菜單、標簽切換、按鈕組等 UI 組件中,用來區(qū)分狀態(tài)。
小細節(jié)提醒
:not()
里面可以不只是類名,還可以是屬性選擇器、ID 等:input:not([disabled]) { /* 所有沒有 disabled 屬性的 input */ } div:not(#header) { /* 所有不是 id 為 header 的 div */ }
不要嵌套使用
:not()
,CSS 不支持這樣的寫法。在性能方面,
:not()
幾乎沒有額外負擔,但別濫用在復雜結(jié)構(gòu)里就行。
基本上就這些。掌握好 :not()
這個工具,選中“沒有某類”的元素就變得很簡單了。
? ??? CSS ?????? ?? ?????? ??? ???? ??? ??????? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

themaindifferencesbetweendisplay : ???, ??, andinline-blockinhtml/cssarelayoutbehavior, spaceusage ? stylingcontrol.1.inlineElementsFlowWithText, do n'tStartonnewlines, ingorewidth/height, andonlyapplyhorizontalpadding/margins —IdealforIneTeTexting

??? ?? ???? ???? ??? ??, ?? ??? ??? ? ? ????? ??? ??? ???? ???? ? ? ?? ? ? ????. 1. CSS ?? : ?? ??? ???? ???? ?? ??; 2. ????? ?? ?? ?? ???? ?? ?? ??? ??? ? ????. 3. ?? ??? ??? ??? ??? ?? ?? ???? ????????. 4. ??? ????? ??? ???? ?? ? ??? ??? ?? ??? ?? ?? ??? ????? ???? ?? ????.

CSS? ?? ?? ??? ??? ??? ???? ???? SVG? ???? ?? ???, ?? ????, ??? ?? ?? ??? ?? ???? ??????. ??? ??? ????. 1. ?, ??, ??? ?? ?? ??? ?? ??? ?????. 2. ?? ?? ? ??? ???? ??? ? ????. 3. ?????? ?? ?? ?? ?? ????? ???? ?? ??? ?? ? ? ????. 4. ???? ???? ??? ??? ??? ????? ?? ? ??? ????. ???? ???? ?? ?? ?? : ? (50pxatcenter) ? ??? ?? ?? : ??? (50%0%, 100 0%, 0%)? ?? ????. ????

CSS? ???? ?? ? ???? ???? ??? ?? ??? ?? ?? ?? ? ? ????. 1. Max width : 100% ? ?? : Auto? ???? ???? ??? ????? ???? ??? ??? ? ??????. 2. HTML? SRCSET ? ?? ??? ???? ?? ??? ??? ??? ??? ???????????. 3. ?? ?? ? ?? ??? ???? ??? ??? ? ?? ?????? ??????. ? ??? ?? ???? ?? ???? ???? ???? ????????.

?? ????? CSS ?? ??? ????? ?? ?? ??? ??, ?? ?? ?? ??, Flexbox ? ??? ???? ?? ?? ? ?? CSS ??? ????? ??? ???? ???? ?? ????? ??? ?????. 1. ?? ??? ??? ???? ????. ???? CSSRESET ?? NALLER.CSS? ???? ?? ???? ???? ????. 2. IE? ?? ??? ?? ?? ?? ??? ????. ?? ?? : ?? ? ???? ??? ??? ???? ?? ????. 3. Flexbox ? Grid? Edge Case ?? Old ???? ??? ?????. ? ?? ??? ? AutopRefixer ??; 4. ?? CSS ?? ??? ???? ????. Caniuse? ???? ?? ?????????.

CSS ??? ??? ?? ?? ?? ? ?? ? ?? ??? ?? ????. 1.px? ?? ??? ????? ?? ??? ??? ?? ??? ?????. 2.EM? ?? ????, ?? ?? ??? ??? ?? ?? ???? ??, REM? ?? ??? ???? ? ????? ??? ????? ?????. 3.VW/VH? ?? ? ???? ??? ??? ??? ??????? ?? ???? ?????? ???????. 4. ??? ?? ?? ? ??, ?? ?? ?? ? ??? ???? ???? ???????. ???? ??? ???? ??? ? ?? ??? ???? ? ????.

????? CSS? ???? ??? ?? ???? ???? ????, ?? 0 (??? ??)?? 1 (??? ???)???. 1. ??? ?? ??? ??? ???? ???? ??? ???? ??? ??? ??????. 2. ??? ???? ??????? ?? ??? ??? ???; 3. ??? ??? ?? ?? ?? ???? ??? ???. ??? ?? ???? ??? ??? RGBA? ?? ?? ?? ??? ??? ????. ???? ???? ?????? ?? ? ? ??? ?? ???? ??? ??? ? ? ????. ?? ?? ?? ??? ?? ???? ?? ????. ????? ????? ???? ??? ?? ? ?? ??? ???? ? ??? ????? ??? ??????.

Accent-Color? CSS?? ???, ??? ?? ? ????? ?? ?? ??? ????? ??? ??? ???? ? ???? ?????. 1. ???? ??? ?? ??? ????? ???? ?? ?? ?? ???? ??? ??? ?? ??? ?? ?????. 2. ???? ??? type = "checkbox", type = "radio"? type = "range"? ?? ??? ?????. 3. Accent-Color? ???? ??? ??? ???? ?? DOM ??? ??? ?? ???? ??? ? ????. 4. ????? ??? ?????? ???? ??? ????? ?? ?????????. 5. Accent-col? ??????
