SyntaxStudy
Sign Up
CSS Beginner 1 min read

CSS Selectors

CSS Selectors

Selectors target which HTML elements a CSS rule applies to.

Basic Selectors

SelectorTargetsExample
ElementAll of that tagp { }
ClassElements with that class.card { }
IDOne element with that id#hero { }
UniversalEvery element* { }

Combinators

CombinatorMeaningExample
SpaceDescendantnav a
>Direct childul > li
+Adjacent siblingh2 + p
~General siblingh2 ~ p
Example
/* Element selector */
p { color: gray; }

/* Class selector */
.highlight { background: yellow; }

/* ID selector */
#header { font-size: 32px; }

/* Descendant: all <a> inside <nav> */
nav a { color: white; }

/* Child: direct <li> children of <ul> */
ul > li { list-style: none; }

/* Group: apply same style to multiple */
h1, h2, h3 { font-family: Georgia, serif; }