CSS Selectors

Last updated: 2026-09-13

CSS Selectors

Selectors pick the HTML elements you want to style. CSS offers simple type selectors up to complex combinators and pseudo-classes for precise targeting.

Basic Selectors

CSS SelectorsDescription
*The universal selector matches every element in the document.
elementType (tag) selector targets all elements of a given tag name, e.g. p.
.classClass selector targets elements with a given class attribute.
#idID selector targets the single element with a given id attribute.

Combinators

CSS SelectorsDescription
A BDescendant combinator: matches B that is a descendant of A.
A > BChild combinator: matches B that is a direct child of A.
A + BAdjacent sibling: matches B immediately after A under the same parent.
A ~ BGeneral sibling: matches any B sibling following A.

Attribute Selectors

CSS SelectorsDescription
[attr]Matches elements that have the given attribute.
[attr=value]Matches elements whose attribute equals the value exactly.
[attr~=value]Matches elements whose attribute is a space-separated list containing value.
[attr^=value]Matches elements whose attribute starts with value.
[attr$=value]Matches elements whose attribute ends with value.
[attr*=value]Matches elements whose attribute contains value as substring.

Pseudo-classes

CSS SelectorsDescription
:hoverMatches an element while the user hovers over it.
:focusMatches an element that has keyboard focus.
:first-childMatches the first child element among siblings.
:nth-child(n)Matches the n-th child element among siblings.
:not(x)Negation: matches elements that do NOT match selector x.
:checkedMatches radio/checkbox/option in selected state.

Pseudo-elements

CSS SelectorsDescription
::beforeInserts generated content before an element's content.
::afterInserts generated content after an element's content.
::first-lineMatches the first formatted line of a block.
::first-letterMatches the first letter of a block.
::selectionMatches the portion of a document highlighted by the user.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏