2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
CSS Style selector, used to select HTML elements in the page in order to add CSS styles.
Rendering performance from high to low Followed by:
ID Selector
#id
Select elements by their id attribute, case sensitive
<p id="p1" >第一段</p>
#p1{
color: red;
}
However, it is not recommended because:
Class Selectors
.class
Through the class attribute of the elementstyleSelect elements by class name, case sensitive
The most recommended CSS selector is the class selector, because it is semantically strong and easy to reset the style.
<span class="important" >重点词汇</span>
.important{
color: red;
font-weight: bold;
}
You can add multiple style classes to the same element, separated by spaces.
<span class="important big" >巨大的重点词汇</span>
.important {
color: red;
font-weight: bold;
}
.big {
font-size: 60px;
}
Tag selector
标签名
Select elements by their tag name, case insensitive
<a href="https://www.baidu.com/" target="_blank" >百度</a>
a {
text-decoration: none; /* 无文本装饰(消除默认的下划线) */
}
Not recommended because label selectors have poor performance and high maintenance costs
Wildcard selector
*
Selects all HTML elements in the page except pseudo elements. It is often used to clear the default style of the browser, but it is not recommended because it consumes performance.
/* 清除所有html标签默认的外边距和内边距 */
* {
margin: 0;
padding: 0;
}
Attribute Selectors
[属性]
Select elements based on their attributes and attribute values. Attributes are case-insensitive, but attribute values are case-sensitive.
Attribute Selectors | describe |
---|---|
[attribute] | Used to select elements with the specified attributes. |
[attribute=value] | Used to select elements with the specified attribute and value. |
[attribute~=value] | Used to select elements whose attribute values contain specified words, which is very suitable for scenarios containing multiple combined attribute values |
[attribute|=value] | The attribute value starts with a fragment selector. The value must be a whole word. |
[attribute^=value] | Matches every element whose attribute value begins with the specified value. |
[attribute$=value] | Matches every element whose attribute value ends with the specified value. |
[attribute*=value] | Matches every element whose attribute value contains the specified value. |
Pseudo-class selectors
:状态名
Select elements based on their different states
同一个标签,不同的状态,有不同的样式,就叫做“伪类”
Pseudo-class selectors | example | Example Description |
---|---|---|
:active | a:active | Select the active link. (Click the label with your mouse, but don't release it) |
:checked | input:checked | Select each selected<input> element. |
:disabled | input:disabled | Select each disabled<input> element. |
:empty | p:empty | Selects every element that has no children<p> element. |
:enabled | input:enabled | Select each enabled<input> element. |
:first-child | p:first-child | Selects every element that is the first child of its parent<p> element. |
:first-of-type | p:first-of-type | Select the first one that is its parent<p>Each element<p> element. |
:focus | input:focus | Select the focused<input> element. |
:hover | a:hover | Selects the link that you hover your mouse over. |
:in-range | input:in-range | Selects the<input> element. |
:invalid | input:invalid | Select all items with invalid values<input> element. |
:lang(language) | p:lang(it) | Select every lang attribute value that starts with "it"<p> element. |
:last-child | p:last-child | Selects every element that is the last child of its parent<p> element. |
:last-of-type | p:last-of-type | Select the last one that is its parent<p>Each element<p> element. |
:link | a:link | Selects all unvisited links. |
:not(selector) | :not§ | Select each non<p> Elements of elements. |
:nth-child(n) | p:nth-child(2) | Selects every element that is the second child of its parent<p> element. |
:nth-last-child(n) | p:nth-last-child(2) | Selects every element that is the second child of its parent<p> Elements, counting from the last child. |
:nth-last-of-type(n) | p:nth-last-of-type(2) | Select the second one as the parent<p>Each element<p>Elements, counting from the last child |
:nth-of-type(n) | p:nth-of-type(2) | Select the second one as its parent<p>Each element<p> element. |
:only-of-type | p:only-of-type | Select the only one whose parent<p>Each element<p> element. |
:only-child | p:only-child | Selects an element that is the only child of its parent<p> element. |
:optional | input:optional | Select the one without the "required" attribute.<input> element. |
:out-of-range | input:out-of-range | Selects values outside the specified range<input> element. |
:read-only | input:read-only | Select the one that has the "readonly" attribute specified.<input> element. |
:read-write | input:read-write | Select the one without the "readonly" attribute<input> element. |
:required | input:required | Select the one that specifies the "required" attribute<input> element. |
:root | root | Selects the root element of an element. |
:target | #news:target | Select the currently active #news element (click the URL containing the anchor name). |
:valid | input:valid | Select all with valid values<input> element. |
:visited | a:visited | Selects all visited links. |
Pseudo-class selectors | meaning |
---|---|
li:nth-child(2) | The second li |
li:nth-child(n) | All li |
li:nth-child(2n) | All even-numbered li |
li:nth-child(2n+1) | All odd-numbered li |
li:nth-child(-n+5) | First 5 li |
li:nth-last-child(-n+5) | The last 5 |
li:nth-child(7n) | Select a multiple of 7 |
n represents 0, 1, 2, 3, 4, 5, 6, 7, 8... (It is invalid when n is less than 1, because n = 0 will not be selected)
tr:nth-child(odd):
Matches rows 1, 3, and 5 of the table, which is equivalent totr:nth-child(2n+1)
。tr:nth-child(even)
: matches rows 2, 4, and 6 of the table, which is equivalent totr:nth-child(2n)
。Use :nth-child() to implement zebra stripes, align edges, highlight lists in specified intervals, and adaptive layout of dynamic lists
https://blog.csdn.net/weixin_41192489/article/details/122089484
CSS to achieve dynamic display and hiding (the magic of :checked and :target)
https://blog.csdn.net/weixin_41192489/article/details/126267866
Use :target to expand more, collapse, and switch between tabshttps://blog.csdn.net/weixin_41192489/article/details/121969768
Use :placeholder-shown to implement Material Design style interactions
https://blog.csdn.net/weixin_41192489/article/details/121976627
Use: placeholder-shown to check empty values and prompt that it cannot be empty
https://blog.csdn.net/weixin_41192489/article/details/121977510
:checked to expand and collapse
https://demo.cssworld.cn/selector/9/2-1.php
:checked implements tab switching
https://demo.cssworld.cn/selector/9/2-2.php
:checked implements custom radio buttons, check boxes, switches, label check boxes, and material radio buttons
https://blog.csdn.net/weixin_41192489/article/details/122050069
Use :valid and :invalid to implement native form validation
https://blog.csdn.net/weixin_41192489/article/details/122070084
Use :required and :optional to implement form validation prompt text
https://blog.csdn.net/weixin_41192489/article/details/122072879
:focus-within implements a drop-down list
https://blog.csdn.net/weixin_41192489/article/details/121959850
When the input box is focused, highlight the label in front (see method 5 in the link)
https://blog.csdn.net/weixin_41192489/article/details/121784196
Mouse hover to enlarge the image vs mouse click to enlarge the image
https://blog.csdn.net/weixin_41192489/article/details/121944791
:empty Hide empty elements and missing fields smart prompts
https://blog.csdn.net/weixin_41192489/article/details/122086159
:only-child implements multi-state dynamic loading animation
https://blog.csdn.net/weixin_41192489/article/details/122088416
:fullscreen Click on the image to display it in full screen
https://blog.csdn.net/weixin_41192489/article/details/122328725
Pseudo-element selectors
::
Used to select and style a portion of an element, rather than the entire element
CSS2中伪元素采用单冒号前缀语法, CSS2.1中伪元素改用双冒号前缀,所有支持双冒号的浏览器同样也支持旧的单冒号语法。考虑浏览器兼容性的话,推荐使用旧的单冒号语法,否则建议使用新的双冒号前缀
::before
and::after
Need to be used with the content attribute to set the content before and after the element.
https://blog.csdn.net/weixin_41192489/article/details/115100040
Commonly used practical examples:
::first-letter
Initials<p>很久很久以前</p>
<p>Long long ago</p>
p::first-letter {
font-size: 2em;
color: red;
}
::first-line
first row <div style="width: 120px">
<p>很久很久以前,有一个白发苍苍的老人</p>
</div>
p::first-line {
color: red;
}
::selection
Drag the mouse to select the area<p>很久很久以前,有一个白发苍苍的老人</p>
p::selection {
color: red;
background-color: yellow;
}
::placeholder
Text placeholder<input placeholder="请输入" />
/* input 不写,则会选中页面所有元素的占位符 */
input::placeholder {
color: red;
}
Relationship Selector
Select elements by their relationships
>
The first layer of tags wrapped in a tag is its children
<div>
<p>我是div的儿子</p>
</div>
div>p{
color:red;
}
空格
All tags within a tag are its descendants
<div class="parent">
<p class="red">我是子代(属于后代)</p>
<p>我是子代(属于后代,但没有 .red)</p>
<div>
<div class="red">我是孙代(也属于后代)</div>
</div>
</div>
/* 所有属于.parent元素内部的.red元素都将被染成红色。*/
.parent .red {
color: red;
}
~
Select the same parent element, in the specified elementafterAll the same-level elements of , so strictly speaking, it should be called 后面兄弟选择器
<div>
<button>按钮1(不会变红)</button>
<p>段落</p>
<button>按钮2(会变红)</button>
</div>
p ~ button {
color: red;
}
CSS No 前面兄弟选择器
, you can refer to the following link for simulation implementation
https://blog.csdn.net/weixin_41192489/article/details/121784196
+
Selects the next element immediately following an element
<div class="parent">
<p>段落</p>
<button>按钮1</button>
<button>按钮2</button>
</div>
p + button {
color: red;
}
Select elements on the page that match multiple selectors at the same time
<p class="red">很久很久以前1</p>
<p>很久很久以前2</p>
/* 选中 p 标签中class值含 red 的元素 */
p.red {
color: red;
}
,
Among multiple selectors, as long as one of them is met, it will be selected
,
Separate <p class="error">报错信息</p>
<p class="important">重要信息</p>
<p>其他信息</p>
.error,
.important {
color: red;
}