# CSS Pseudo cheat sheet

## Simple selectors

| **Selector** | **Syntax** | **Example** |
| --- | --- | --- |
| Element | element | **div {** |
| 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 &lt;p&gt; |
| Two elements | element, element | **p, div { }** | All &lt;p&gt; and &lt;div&gt; elements |
| Two elements | element element | **p div { }** | All &lt;div&gt; elements inside &lt;p&gt; |

## Descendant selectors/combinators

| **Selector** | **Syntax** | **Example** | **Description** |
| --- | --- | --- | --- |
| Descendant | element element | **div p { }** | All &lt;p&gt; descendants of &lt;div&gt; |
| Child | element&gt;element | **div &gt; p { }** | All &lt;p&gt; direct descendants of &lt;div&gt; |
| Adjacent Sibling | element+element | **div + p { }** | &lt;p&gt; element directly after &lt;div&gt; |
| General Sibling | element~element | **div ~ p { }** | All &lt;p&gt; element iterations after &lt;div&gt; |

## Attribute selectors

| **Selector** | **Syntax** | **Example** |
| --- | --- | --- |
| \[attribute\] | **\[href\] {** | Selects all elements with a href attribute |
| \[attribute=value\] | **\[lang="fr"\] {** | Selects all elements with lang attribute that have a value of "fr" |
| \[attribute~=value\] | **\[input~=hello\] {** | Elements with input attribute containing the whitespace-separated substring "hello" |
| \[attribute | \=value\] | \*\*\[lang |
| \[attribute^=value\] | **a\[href^="https"\] {** | Every &lt;a&gt; element with href attribute value begins with "https" |
| \[attribute$=value\] | **a\[href$=".docx"\] {** | Every &lt;a&gt; element with href attribute value ends with ".docx" |
| \[attribute\*=value\] | **a\[href\*="meta"\] {** | Every &lt;a&gt; 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 &lt;input&gt; elements |
| : default | **input: default { }** | All default &lt;input&gt; elements |
| :disabled | **input: disabled { }** | All disabled &lt;input&gt; elements |
| : empty | **div: empty { }** | All the &lt;div&gt; elements with no children |
| : enabled | **input: enabled { }** | All the enabled &lt;input&gt; elements |
| :first-child | **p:first-child { }** | All the &lt;p&gt; elements who are the first child of a parent element |
| :first-of-type | **p:first-of-type { }** | All the &lt;p&gt; elements who are the first &lt;p&gt; 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 &lt;p&gt; elements who are the last child of a parent element |
| :last-of-type | **p:last-of-type { }** | All the &lt;p&gt; elements who are the last &lt;p&gt; element of a parent element |
| : link | **a: link { }** | All unvisited links |
| :not(*selector*) | **:not(div) { }** | All the elements that are not a &lt;div&gt; element |
| :nth-child(*n*) | **div:nth-child(3) { }** | All the &lt;p&gt; elements that are the third child of a parent element |
| :nth-last-child(*n*) | **div:nth-last-child(3) { }** | All the &lt;div&gt; elements which are the third child of a parent element, counting from the last child element |
| :nth-last-of-type(*n*) | **p:nth-last-of-type(2) { }** | The second sibling from the last child of a parent element. |
| :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 &lt;p&gt; elements which are only &lt;p&gt; elements inside its parent |
| :only-child | **p:only-child { }** | All the &lt;p&gt; elements which are the 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 the 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 { }** | Select all visited links |

## Pseudo-element selectors

| **Syntax** | **Example** | **Description** |
| --- | --- | --- |
| ::after | **p::after { }** | Inserts content after content of &lt;p&gt; element |
| ::before | **p::before { }** | Inserts content before content of &lt;p&gt; element |
| ::first-letter | **p::first-letter { }** | Select the first letter of every &lt;p&gt; element |
| ::first-line | **p::first-line { }** | Select the first line of every &lt;p&gt; element |
| ::placeholder | **input::placeholder { }** | Select input elements with the "placeholder" attribute specified |
| ::marker | **::marker { }** | Selects markers in a list |
