What are the CSS selectors and what are the priorities?

Mondo Technology Updated on 2024-02-01

Keywords:: CSS selectors, CSS precedence.

There are several types of CSS selectors:

Element Selector: Select an element by tag name, for example:p {}。Class selector: passed+Class name, for example:.my-class {}。ID selector: Pass#+id name, for example:#my-id {}。Wildcard selector: passedSelect all elements, for example:。Descendant selector: via a spaceSelect a descendant element under an element, for example:.my-parent .my-child {}。Child Element Selector: PassSelect a child element of an element, for example:ul > li {}。Adjacent Sibling Selector: PassSelect an adjacent subsequent sibling element, for example:.my-class + p {}。Generic Brother Selector: PassSelect a successor sibling element, for example:.my-class ~ p {}。The priority of the CSS selectors from high to low is as follows:

important: The attribute that uses the keyword has the highest priority. Inline style: The style set with the element's style property takes precedence. ID Selector: Specifies that the style of the id takes precedence over the class selector and element selector. Class selectors and property selectors: The priority is the same. Element selector and pseudo-class selector: the priority is the same. Wildcards and combo selectors: Takes the lowest priority when there is no more specific selector. Note that when the priority is the same, the later style will override the previous style. In this case, we can do this by increasing the priority of the selector, using !important, using inline styles, etc.

Related Pages