Technology Sharing

CSS [Detailed Explanation] Style selectors (including ID, class, tag, wildcard, attribute, pseudo-class, pseudo-element, Content attribute, child, descendant, sibling, adjacent sibling, intersection, union, etc.)

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>
  • 1
#p1{
  color: red;
}
  • 1
  • 2
  • 3

However, it is not recommended because:

  • The id selector has a higher priority, which makes it inconvenient to reset the style.
  • The id selector is mainly used by JS

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>
  • 1
.important{
  color: red;
  font-weight: bold;
}
  • 1
  • 2
  • 3
  • 4

You can add multiple style classes to the same element, separated by spaces.

<span class="important big" >巨大的重点词汇</span>
  • 1
.important {
  color: red;
  font-weight: bold;
}
.big {
  font-size: 60px;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

Tag selector 标签名

Select elements by their tag name, case insensitive

<a href="https://www.baidu.com/" target="_blank" >百度</a>
  • 1
a {
  text-decoration: none; /* 无文本装饰(消除默认的下划线) */
}
  • 1
  • 2
  • 3

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5

Attribute Selectors [属性]

Select elements based on their attributes and attribute values. Attributes are case-insensitive, but attribute values ​​are case-sensitive.

Attribute Selectorsdescribe
[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

同一个标签,不同的状态,有不同的样式,就叫做“伪类”
  • 1
Pseudo-class selectorsexampleExample Description
:activea:activeSelect the active link. (Click the label with your mouse, but don't release it)
:checkedinput:checkedSelect each selected<input> element.
:disabledinput:disabledSelect each disabled<input> element.
:emptyp:emptySelects every element that has no children<p> element.
:enabledinput:enabledSelect each enabled<input> element.
:first-childp:first-childSelects every element that is the first child of its parent<p> element.
:first-of-typep:first-of-typeSelect the first one that is its parent<p>Each element<p> element.
:focusinput:focusSelect the focused<input> element.
:hovera:hoverSelects the link that you hover your mouse over.
:in-rangeinput:in-rangeSelects the<input> element.
:invalidinput:invalidSelect all items with invalid values<input> element.
:lang(language)p:lang(it)Select every lang attribute value that starts with "it"<p> element.
:last-childp:last-childSelects every element that is the last child of its parent<p> element.
:last-of-typep:last-of-typeSelect the last one that is its parent<p>Each element<p> element.
:linka:linkSelects 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-typep:only-of-typeSelect the only one whose parent<p>Each element<p> element.
:only-childp:only-childSelects an element that is the only child of its parent<p> element.
:optionalinput:optionalSelect the one without the "required" attribute.<input> element.
:out-of-rangeinput:out-of-rangeSelects values ​​outside the specified range<input> element.
:read-onlyinput:read-onlySelect the one that has the "readonly" attribute specified.<input> element.
:read-writeinput:read-writeSelect the one without the "readonly" attribute<input> element.
:requiredinput:requiredSelect the one that specifies the "required" attribute<input> element.
:rootrootSelects the root element of an element.
:target#news:targetSelect the currently active #news element (click the URL containing the anchor name).
:validinput:validSelect all with valid values<input> element.
:visiteda:visitedSelects all visited links.

Using pseudo-class selectors in lists

Pseudo-class selectorsmeaning
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)

Using pseudo-class selectors in tables