CSS Pseudo Selector  CheatSheet

CSS Pseudo Selector CheatSheet

Style specified parts of an element and State of an element with Pseudo Selectors.

Hi there,

Gear up people, We are going to deep dive into CSS Pseudo Selector, It's very useful while you are designing your website using CSS.

What is CSS Pseudo-classes

CSS Pseudo class is a keyword added to a selector that is used to define a special state of an element.

It can be used to:

  • Style an element when a user mouses over it
  • Style visited and unvisited links differently
  • Style an element when it gets focus

Let's see it syntax

The syntax of pseudo-classes:

selector:pseudo-class {
  property: value;
}

Important CSS Pseudo Classes

SelectorExampleDescription
:activea:activeSelects the active link
:checkedinput:checkedSelects every checked <input> element
:disableda:disabledSelects every disabled <input> element
:emptyp:emptySelects every <p> element that has no children
:enabledinput:enabledSelects every enabled <input> element
:first-childp:first-childSelects every <p> elements that is the first child of its parent
:first-of-typep:first-of-typeSelects every <p> element that is the first <p> element of its parent
:focusinput:focusSelects the element that has focus
:hovera:hoverSelects links on mouse over
:in-rangeinput:in-rangeSelects <input> elements with a value within a specified range
:invalidinput:invalidSelects every <p> element with a lang attribute value starting with "it"
:lang(language)p:lang(it)Selects every <p> element that is the last <p> element of its parent
:last-of-typep:last-of-typeSelects every <p> elements that is the last child of its parent
:linka:linkSelects all unvisited links
:not(selector):not(p)SSelects every element that is not a <p> element
:nth-child(n):nth-child(n)Selects every <p> element that is the second child of its parent
:nth-last-child(n)p:nth-last-child(2)Selects every <p> element that is the second child of its parent, counting from the last child
:rootrootSelects the document's root element
:requiredinput:requiredSelects <input> elements with a "required" attribute specified
:visiteda:visitedSelects all visited links
:validinput:validSelects all elements with a valid value

What is CSS Pseudo-elements

A CSS pseudo-element is used to style specified parts of an element.

It can be used to:

  • Style the first letter, or line, of an element
  • Insert content before, or after, the content of an element

Let's see it syntax

The syntax of pseudo-classes:

selector::pseudo-element {
  property: value;
}

Important CSS Pseudo Elements

SelectorExampleDescription
::afterp::afterInsert content after every <p> element
::beforep::beforeInsert content before every <p> element
::first-letterp::first-letterSelects the first letter of every

element

::first-linep::first-lineSelects the first line of every

element

::selectionp::selectionSelects the portion of an element that is selected by a user

That's all for this article and with that, it's a wrap! I hope you found the article useful. Thank you for reading, If you have reached so far, please like the article, It will encourage me to write more such articles. Do share your valuable suggestions, I appreciate your honest feedback!

I create content about Programming, Design, and Technology, If this is something that interests you, please share the article with your friends and connections. You can also subscribe to my newsletter to get updates every time I write something!

Ta da! Until we meet again Happy Coding! ❤️

Did you find this article valuable?

Support Anup Kumar Maurya by becoming a sponsor. Any amount is appreciated!