0% found this document useful (0 votes)
90 views3 pages

CSS Selectors Cheat Sheet

This document provides a cheat sheet of CSS pseudo-classes and pseudo-elements including their syntax and examples. It lists simple selectors like elements, classes, and IDs. It also covers descendant selectors, attribute selectors, pseudo-classes like :hover and :active, and pseudo-elements like ::before and ::after with their uses.

Uploaded by

Shiza Masood
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views3 pages

CSS Selectors Cheat Sheet

This document provides a cheat sheet of CSS pseudo-classes and pseudo-elements including their syntax and examples. It lists simple selectors like elements, classes, and IDs. It also covers descendant selectors, attribute selectors, pseudo-classes like :hover and :active, and pseudo-elements like ::before and ::after with their uses.

Uploaded by

Shiza Masood
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

CSS Pseudo cheat sheet

Simple selectors

Selector Syntax Example

div {
Element element
}

Class .class .alpha { }

ID #id #alpha { }

Universal * *{}

Variations of simple selectors

Elements Syntax Example Description

Two classes .first-class.second-class .alpha.beta { } All elements with classes alpha and beta

Element and class element.class p.alpha { } All alpha class elements inside <p>

Two elements element, element p, div { } All <p> and <div> elements

Two elements element element p div { } All <div> elements inside <p>

Descendant selectors/combinators

Selector Syntax Example Description

Descendant element element div p { } All <p> descendants of <div>

Child element>element div > p { } All <p> direct descendants of <div>

Adjacent Sibling element+element div + p { } <p> element directly after <div>

General Sibling element~element div ~ p { } All <p> element iterations after <div>

Attribute selectors

Selector Syntax Example

[href] {
[attribute] Selects all elements with a href attribute
}

[lang="fr"] {
[attribute=value] Selects all elements with lang attribute that has a value of "fr"
}

[attribute~=value] [input~=hello] { Elements with input attribute containing the whitespace separated substring "
Selector Syntax Example

[lang|=en] {
[attribute|=value] Elements with lang attribute value equal to "en" or "en-"(en hyphen)
}

a[href^="https"] {
[attribute^=value] Every <a> element with href attribute value begins with "https"
}

a[href$=".docx"] {
[attribute$=value] Every <a> element with href attribute value ends with ".docx"
}

a[href*="meta"] {
[attribute*=value] Every <a> element with href attribute value has substring "meta"
}

Pseudo-class Example Description of selection

:active a:active { } All active links

:checked input:checked { } All the checked <input> elements

:default input:default { } All default <input> elements

:disabled input:disabled { } All disabled <input> elements

:empty div:empty { } All the <div> elements with no children

:enabled input:enabled { } All the enabled <input> elements

:first-child p:first-child { } All the <p> elements who are the first child of a parent element

:first-of-type p:first-of-type { } All the <p> element who are the first <p> element of a parent element

:focus input:focus { } Input element under focus

:fullscreen :fullscreen { } The element in full-screen mode

:hover p:hover { } Action effect on mouse hover

:invalid input:invalid { } Input elements with an invalid value

:last-child p:last-child { } All the <p> elements who are the last child of a parent element

:last-of-type p:last-of-type { } All the <p> elements who are the last <p> element of a parent element

:link a:link { } All unvisited links

:not(selector) :not(div) { } All the elements that are not a <div> element
Selector Syntax Example

:nth-child(n) div:nth-child(3) { } All the <p> elements that are the third child of a parent element

div:nth-last-child(3) All the <div> elements which are the third child of a parent element, counting f
:nth-last-child(n)
{} child element

:nth-last-of- p:nth-last-of-type(2)
The second sibling from the last child of a parent element.
type(n) {}

:nth-of-type(n) p:nth-of-type(2) { } The second sibling of a parent element.

:only-of-type p:only-of-type { } All the <p> elements which are only <p> elements inside its parent

:only-child p:only-child { } All the <p> elements which are only child of a parent element

:optional input:optional { } The input elements with no "required" attribute

:required input:required { } Selects input elements with the "required" attribute specified

:root :root { } The Root element of document

::selection ::selection { } The portion of an element that is selected by a user

:valid input:valid { } All the input elements with a valid value

:visited a:visited { } Selects all visited links

Pseudo-element selectors

Syntax Example Description

::after p::after { } Inserts content after content of <p> element

::before p::before { } Inserts content before content of <p> element

::first-letter p::first-letter { } Selects first letter of every <p> element

::first-line p::first-line { } Selects first line of every <p> element

::placeholder input::placeholder { } Selects input elements with "placeholder" attribute specified

::marker ::marker { } Selects markers in a list

You might also like