English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

CSS reference manual

CSS @rules (RULES)

CSS attributes in full

Comprehensive Manual of CSS Selectors

CSS selectors are patterns used to select the styles of the elements you want.

"CSS" column shows the attribute definitions in CSS versions (CSS1, CSS2, or for CSS3)

SelectorExampleDescriptionCSS Version
.class.introSelect all elements with class="intro"1
#id#firstnameSelect all elements with id="firstname"1
**Select all elements2
elementpSelect all <p> elements1
element,elementdiv,pSelect all <div> elements and <p> elements1
element elementdiv pSelect all <p> elements within a <div> element1
element>elementdiv>pSelect all <p> elements that are children of a <div> element2
element+elementdiv+pSelect all <p> elements immediately following a <div> element2
[attribute][target]Select all elements with a target attribute2
[attribute=value][target=-blank]Select all elements using target="-elements with "blank"2
[attribute~=value][title~=flower]Select all elements with a title attribute containing the word "flower"2
[attribute|=language][lang|=en]Select all elements with a lang attribute starting with "en"2
[attribute^=value]a[src^="https"]Select every element with a src attribute value starting with "https"3
[attribute$=value]a[src$=".pdf"]Select every element with a src attribute value ending with ".pdf"3
[attribute*=value]a[src*="w3codebox]Select every element with a src attribute value containing the substring "w"3elements with "codebox"3
:linka:linkSelect all unvisited links1
:visiteda:visitedSelect all visited links1
:activea:activeSelect the active link1
:hovera:hoverSelect when the mouse is over the link1
:focusinput:focusSelect the input element with focus2
:first-letterp:first-letterSelect every first letter of each <p> element1
:first-linep:first-lineSelect every first line of each <p> element1
:first-childp:first-childSpecify the style for only when the <p> element is the first child of its parent.2
:beforep:beforeInsert content before each <p> element2
:afterp:afterInsert content after each <p> element2
:lang(language)p:lang(it)Selects all <p> elements with a starting value of 'it' for the lang attribute2
element1~element2p~ulSelects each ul element following a p element3
:first-of-typep:first-of-typeSelects each p element that is the first p element of its parent3
:last-of-typep:last-of-typeSelects each p element that is the last p element of its parent3
:only-of-typep:only-of-typeSelects each p element that is the only p element of its parent3
:only-childp:only-childSelects each p element that is the only child of its parent3
:nth-child(n)p:nth-child(2)Selects each p element that is the second child of its parent3
:nth-last-child(n)p:nth-last-child(2)Selects each p element that is the second child of its parent, counting from the last child3
:nth-of-type(n)p:nth-of-type(2)Selects each p element that is the second child of its parent3
:nth-last-of-type(n)p:nth-last-of-type(2)Selects each p element that is the second p element of its parent, counting from the last child3
:last-childp:last-childSelects each p element that is the last child of its parent3
:root:rootSelects the root element of the document3
:emptyp:emptySelects each p element that has no child elements (including text nodes)3
:target#news:targetSelects the currently active #news element (including the URL of the anchor clicked with that name)3
:enabledinput:enabledSelects each enabled input element3
:disabledinput:disabledSelects each disabled input element3
:checkedinput:checkedSelects each selected input element3
:not(selector):not(p)Selects each element that is not a p element3
::selection::selectionMatches the selected or highlighted part of the element by the user3
:out-of-range:out-of-rangeMatches input elements with values outside the specified range3
:in-range:in-rangeMatches input elements with values within the specified range3
:read-write:read-writeUsed to match elements that are both readable and writable3
:read-only:read-onlyUsed to match elements with 'readonly' (read-only) attribute set3
:optional:optionalUsed to match optional input elements3
:required :requiredUsed to match elements with 'required' attribute set3
:valid :validUsed to match elements with legal input values3
:invalid :invalidUsed to match elements with illegal input values3