CSS Selectors

最終更新: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 技術チーム

複数の開発者によって共同維持されているプログラミングチュートリアルプラットフォーム。各チュートリアルは専門分野の開発者が執筆・レビューしています。正確で信頼性の高いコンテンツを目指しています — 問題を見つけた場合はお知らせください。

100%