css pseudo-selector learning pseudo-class selector analysis
Aug 03, 2022 am 11:26 AMIn the previous article "css pseudo-selector learning: Pseudo-element selector analysis", we learned about pseudo-element selectors, and today we learn more about pseudo-class selectors. I hope it will be useful to everyone. Helps!
Pseudo-class selector
Pseudo-class selector is a type of selector that allows passing code that is not included in HTML Element state information to locate the usage of HTML elements. Pseudo-class selector The specific usage is to add keywords to the existing selector to represent the status information of the positioned HTML element. [Recommended learning: css video tutorial]
Through pseudo-classes, developers can set the dynamic state of elements, such as hover (hover), click (active), and other elements that cannot be passed in the document. The elements selected by the selector (these elements have no ID or class attributes), such as the first child element (first-child) or the last child element (last-child).
For example:hover
The pseudo-class selector can be used to change the color of the button when the user hovers the mouse over the button. As shown in the following sample code:
/*?所有用戶指針懸停的按鈕?*/?? ?button:hover?{?? ??color:?blue;?? ?}
The name of the pseudo-class is not case-sensitive, but needs to start with a colon :
. In addition, pseudo-classes need to be used in conjunction with selectors in CSS. The syntax format is as follows:
選擇器:偽類?{?? ??屬性?:?屬性值;?? ?}
The specific syntax format of pseudo-class selector is :pseudo-class
, Don’t forget :
here.
CSS provides a variety of pseudo-classes, as shown in the following table:
Selector | Example | Example description | |||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
:active | a :active | match clicked link | |||||||||||||||||||||||||||||||||||||||||||||||||||||
:checked | input:checked | match the selected | |||||||||||||||||||||||||||||||||||||||||||||||||||||
:disabled | input:disabled | matches every disabled <input> element | |||||||||||||||||||||||||||||||||||||||||||||||||||||
:empty | p:empty | Matches any element that has no child elements |
|||||||||||||||||||||||||||||||||||||||||||||||||||||
:enabled | input:enabled | Matches every enabled <input> element | |||||||||||||||||||||||||||||||||||||||||||||||||||||
:first-child | p:first-child | Matches the first child element in the parent element, must be the first child element in the parent element |
|||||||||||||||||||||||||||||||||||||||||||||||||||||
p:first-of-type | Matches the first | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
input:focus | Matches the focused <input> element | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
a:hover | Match the element on which the mouse is hovering | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
input:in-range | Match elements with the specified value range <input> element | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
input:invalid | Matches all <input> elements | ## that have an invalid value | |||||||||||||||||||||||||||||||||||||||||||||||||||||
p:lang(it) | Match every element whose lang attribute value starts with "it" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
p:last-child | Matches the last child element in the parent element, must be The last child element in the parent element |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
p:last-of-type | matches the The last element |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
a:link | matches all unvisited links | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
:not(p) | Match every element that is not a element |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
p:nth-child(2) | Matches the second child element in the parent element | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
p:nth-last-child(2) | Matches the penultimate child element of the parent element | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
p:nth-last-of-type(2) | Matches the penultimate element of the parent element Child element | ##:nth-of-type(n) | |||||||||||||||||||||||||||||||||||||||||||||||||||||
match The second child element of the parent element | :only-of-type | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
matches the parent The only | :only-child | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
matches the only child element in the parent element | :optional | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
Matches <input> elements without the "required" attribute | :out-of-range | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
Matches <input> elements with values ??outside the specified range | :read-only | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
Matches <input> elements that specify the "readonly" attribute | :read-write | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
Matches <input> elements without the "readonly" attribute | :required | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
Matches <input> elements that specify the "required" attribute | :root | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
matches the root element of the element. In HTML, the root element is always HTML | :target | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
Matches the currently active #news element (clicked on a URL containing this anchor name) | :valid | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
Matches all An <input> element with a valid value | :visited | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
matches all visited links |
Function | |
---|---|
selector:first-child
| is used to locate a group of sibling elements The first element of |
selector:last-child
| is used to locate the last element in a group of sibling elements|
selector:nth-child(n)
| Used to locate the nth element in a group of sibling elements|
selector:nth-last-child(n)
| Used to locate the nth element in a group of sibling elements in reverse order|
selector:first-of-type
| Used to locate the first element in a group of sibling elements of the same type|
selector:last-of-type
| Used to locate the last element in a group of sibling elements of the same type|
selector: nth-of-type(n)
| Used to locate the nth element in a group of sibling elements of the same type|
selector: nth-last-of-type(n)
| Used to locate the nth element in reverse order among a group of sibling elements of the same type|
selector:only-child
| Used to locate an element without any sibling elements|
selector:only-of-type
| Used to locate an element that does not have any sibling elements of the same type |
selector:empty
| Used to locate an element An element that has no child elements and does not have any text content|
selector:root
| is used to locate the root of the HTML page Element () |
元素中的第一個元素。<li> 關(guān)鍵字: 格式為 如下示例代碼展示了 nbsp;html> ????<meta> ????<meta> ????<meta> ????<title>nth-child偽類</title> ????<style> table { border-collapse: collapse; border-spacing: 0; width: 100%; } th, td { border-top: 1px solid lightcoral; text-align: center; } /* 最后一行單元格在底部增加一個邊框效果 */ tr:last-child td { border-bottom: 1px solid lightcoral; } /* 實現(xiàn)隔行換色 */ tr:nth-child(even) { background-color: aquamarine; } </style> ????
代碼運行結(jié)果如下圖所示:
:empty
如下示例代碼展示了 nbsp;html> ????<meta> ????<meta> ????<meta> ????<title>empty偽類</title> ????<style> body { /* 開啟flex布局 */ display: flex; } .box { background: pink; height: 80px; width: 80px; margin: 0 20px; } .box:empty { background: lime; } </style> ????<div></div> ????<div>這個元素的背景是粉色的</div> ????<div> ????????<!-- 這是一個注釋 --> ????</div> 代碼運行結(jié)果如下圖所示: :root CSS中的 如下代碼展示的 :root?{ ????height:?100vh; ????width:?100vw; ????background-color:?dodgerblue; } 代碼運行結(jié)果如下圖所示: 3、UI元素狀態(tài)偽類選擇器 使用UI偽類選擇器可以根據(jù)元素的狀態(tài)匹配元素,下方列表將簡單總結(jié)這類選擇器:
4、輸入偽類選擇器 關(guān)于表單輸入的偽類,主要介紹三種常用的,具體如下:
:enabled和:disabled
如下代碼展示了 nbsp;html> ??<meta> ??<meta> ??<meta> ??<title>:enabled和:disabled的用法</title> ??<style> input:enabled { outline: none; } input:disabled { /* 禁用狀態(tài)背景為灰色 */ background-color: gray; } </style> ??<input> ??<input> 代碼運行結(jié)果如下所示: 由上圖我們看到禁用狀態(tài)的 :read-only和:read-write
nbsp;html> ??<meta> ??<meta> ??<meta> ??<title>:read-only和:read-write</title> ??<style> input:read-write { outline: none; } /* 只讀狀態(tài) */ input:read-only { color: red; outline: none; } </style> ??<input> ??<input> 代碼運行結(jié)果如下所示: 我們可以看到,只讀的 :checked
nbsp;html> ??<meta> ??<meta> ??<meta> ??<title>checked偽類</title> ??<style> input:checked { /* 為選中的增加陰影 */ box-shadow: 2px 2px 2px 2px lightcoral; } </style> ??<input> ??<input> 關(guān)于 示例代碼如下: nbsp;html> ??<meta> ??<meta> ??<meta> ??<title>開關(guān)</title> ??<style> [type="checkbox"] { width: 44px; height: 26px; position: absolute; opacity: 0; pointer-events: none; } /* 開關(guān)樣式 */ .cs-switch { display: inline-block; width: 44px; height: 26px; border: 2px solid; border-radius: 26px; background-color: currentColor; box-sizing: border-box; color: silver; transition: all .2s; cursor: pointer; } .cs-switch::before { content: ""; display: block; width: 22px; height: 22px; border-radius: 50%; background-color: #fff; transition: margin-left .2s; } :checked+.cs-switch { color: blueviolet; } /* 選中移動 */ :checked+.cs-switch::before { margin-left: 18px; } /* 禁用狀態(tài) */ :disabled+.cs-switch { opacity: .4; cursor: not-allowed; } </style> ??<!-- 普通狀態(tài) --> ??<input> ??<label></label> ??<!-- 選中狀態(tài) --> ??<input> ??<label></label> ??<!-- 禁用狀態(tài) --> ??<input> ??<label></label> ??<!-- 選中禁用狀態(tài) --> ??<input> ??<label></label> 運行效果如下所示: 5、邏輯組合偽類
優(yōu)先級為0,優(yōu)先級由括號中的表達式?jīng)Q定; 可以不斷的級聯(lián); 不可出現(xiàn)多個表達式,也不支持選擇符; :not()的巨大的用處在于告別重置的問題; 重置web中的樣式,就好比我們在項目中經(jīng)常使用到的:添加 .cs_li?{ display:?none; } .cs_li.active?{ display:?block; } 而我們可以使用 .cs_li:not(.active)?{ display:?none; } 在列表中的設(shè)置 .cs_li:not(:nth-of-type(5n)){ margin-right:?10px;?//?除5的倍數(shù)項都設(shè)置右邊的外邊距 }
平時我們開發(fā)中經(jīng)常會用到類似下面的語法: .cs_li_a?>?img, .cs_li_b?>?img, .cs_li_c?>?img?{ display:?none; } 使用 :is(.cs_li_a,?.cs_li_b,?.cs_li_c)?>?img?{ display:?none; } 還有一種嵌套之間的關(guān)系,相互嵌套,交叉組合得出結(jié)論;如下方所示 ol?ol?li, ol?ul?li,? ul?ul?li, ul?ol?li?{ margin-left:?20px; } 使用 :is(ol,?ul)?:is(ol,?ul)?li{ margin-left:?20px; }
使用的方法與:is()完全相同,但優(yōu)先級永遠是0;底下的括號中的優(yōu)先級完全被忽略,倆句是同一個優(yōu)先級,并且級別等同于 :where(.article,?section)?.conten?{} :where(#article,?#section)?.conten?{ (學(xué)習(xí)視頻分享:web前端入門) |
The above is the detailed content of css pseudo-selector learning pseudo-class selector analysis. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

CSS blocks page rendering because browsers view inline and external CSS as key resources by default, especially with imported stylesheets, header large amounts of inline CSS, and unoptimized media query styles. 1. Extract critical CSS and embed it into HTML; 2. Delay loading non-critical CSS through JavaScript; 3. Use media attributes to optimize loading such as print styles; 4. Compress and merge CSS to reduce requests. It is recommended to use tools to extract key CSS, combine rel="preload" asynchronous loading, and use media delayed loading reasonably to avoid excessive splitting and complex script control.

Choosing the correct display value in CSS is crucial because it controls the behavior of elements in the layout. 1.inline: Make elements flow like text, without occupying a single line, and cannot directly set width and height, suitable for elements in text, such as; 2.block: Make elements exclusively occupy one line and occupy all width, can set width and height and inner and outer margins, suitable for structured elements, such as; 3.inline-block: has both block characteristics and inline layout, can set size but still display in the same line, suitable for horizontal layouts that require consistent spacing; 4.flex: Modern layout mode, suitable for containers, easy to achieve alignment and distribution through justify-content, align-items and other attributes, yes

Autoprefixer is a tool that automatically adds vendor prefixes to CSS attributes based on the target browser scope. 1. It solves the problem of manually maintaining prefixes with errors; 2. Work through the PostCSS plug-in form, parse CSS, analyze attributes that need to be prefixed, and generate code according to configuration; 3. The usage steps include installing plug-ins, setting browserslist, and enabling them in the build process; 4. Notes include not manually adding prefixes, keeping configuration updates, prefixes not all attributes, and it is recommended to use them with the preprocessor.

AnimatingSVGwithCSSispossibleusingkeyframesforbasicanimationsandtransitionsforinteractiveeffects.1.Use@keyframestodefineanimationstagesforpropertieslikescale,opacity,andcolor.2.ApplytheanimationtoSVGelementssuchas,,orviaCSSclasses.3.Forhoverorstate-b

Theconic-gradient()functioninCSScreatescirculargradientsthatrotatecolorstopsaroundacentralpoint.1.Itisidealforpiecharts,progressindicators,colorwheels,anddecorativebackgrounds.2.Itworksbydefiningcolorstopsatspecificangles,optionallystartingfromadefin

The scope of CSS custom properties depends on the context of their declaration, global variables are usually defined in :root, while local variables are defined within a specific selector for componentization and isolation of styles. For example, variables defined in the .card class are only available for elements that match the class and their children. Best practices include: 1. Use: root to define global variables such as topic color; 2. Define local variables inside the component to implement encapsulation; 3. Avoid repeatedly declaring the same variable; 4. Pay attention to the coverage problems that may be caused by selector specificity. Additionally, CSS variables are case sensitive and should be defined before use to avoid errors. If the variable is undefined or the reference fails, the fallback value or default value initial will be used. Debug can be done through the browser developer

Mobile-firstCSSdesignrequiressettingtheviewportmetatag,usingrelativeunits,stylingfromsmallscreensup,optimizingtypographyandtouchtargets.First,addtocontrolscaling.Second,use%,em,orreminsteadofpixelsforflexiblelayouts.Third,writebasestylesformobile,the

CSS's :target pseudo-class is used to style the target element based on the URL fragment identifier. It works by the browser scrolling to the element when the URL contains a # symbol and element id and allows a specific style to be applied to the element via :target. Common use cases include highlighting the page section after navigation, creating tabs or slideshows without JavaScript, and improving accessibility to long pages. Usage tips include ensuring that id is unique, combining transition or animation effects, switching content visibility with display attributes, and considering compatibility with older browsers.
