Loading Please Wait...

Logo Lynxsia IT Solutions

CSS Pseudo Selectors

CSS Pseudo Selectors

Pseudo selectors allow us to select HTML elements based on the state and entity information of the element that is not included in the HTML DOM tree

  • Pseudo Class Selectors
  • Pseudo Element Selectors
Pseudo Class Selectors

A pseudo class is a keyword that defines a state of the selected element. For example, hover class represent the state when user hover mouse over the elements like buttons, links, etc.

It is defined by the colon (:) followed by pseudo-class name. Function pseudo class selectors are defined by the colon (:) followed by pseudo-class name and then parenthesis ().

Syntax = :pseudo-class or :pseudo-class()

Let's see with some example
					 
        
          /* All link elements. */
          a{
            color: green;
          }

          /* All link elements when user point the mouse over it. */
          a:hover{
            color: brown;
          }
        
      
Standard Pseudo Classes
:active

The :active pseudo class represent the state when element is activated such as links, buttons. The "activation" start by clicking primary mouse button and completed when user release the mouse button

:any-link

The :any-link pseudo class represent the elements that has an href attribute regardless of it has been visited or not.

:autofill

The :autofill pseudo class represent the input elements that is filled by the suggestions.

:blank

The :blank pseudo class represent the input elements that is empty. Check for browser compatibility while using it.

:checked

The :checked pseudo class represent the radio, checkbox, and sometime option elements that is selected.

:current()

The :current() pseudo class represent the elements or ancestor that is currently being displayed. Check for browser compatibility while using it. For example, :current(div, p)

:default

The :default pseudo class represent the form elements in a group (radio, checkbox, option) with default value (sometime first one).

:defined

The :defined pseudo class represent the standard element (built-in) or custom element that is successfully defined with CustomElementRegistry.define() method.

:dir()

The :dir() pseudo class represent the elements with text direction utility. Check for browser compatibility while using it. For example, :dir(rtl)

:disabled

The :disabled pseudo class represent the element that is disabled such as input text, selection, clicking, etc.

:empty

The :empty pseudo class represent the element with no children or elements inside it.

:first

The :first pseudo class represent the first page of the print document and generally used along with @page at rule.

:first-child

The :first-child pseudo class represent the first element among a group of elements such as first list element inside lists, first paragraph inside div.

:first-of-type

The :first-of-type pseudo class represent the first element of its type among a group of elements.

:fullscreen

The :fullscreen pseudo class represent the elements that are in fullscreen mode such as videos.

:future()

The :future() pseudo class represent the elements that appears after :current() element. Check for browser compatibility while using it. For example, :future(div, p)

:focus

The :focus pseudo class represent the elements that are current focused such as writing on text input, selecting radio options.

:focus-visible

The :focus-visible pseudo class represent the elements that are current focused and User Agent also determines that the element is focused.

:focus-within

The :focus-within pseudo class represent the elements and its descendant that are focused.

:has()

The :has() pseudo class represent the parent or sibling elements based on the relative selectors provided as arguments. Check for browser compatibility while using it. For example div:has(h3.active) = selects all div that has h3 headings with active class.

:host

The :host pseudo class represent the shadow host elements of shadow DOM.

:host()

The :host() pseudo class represent the shadow host elements of shadow DOM based on the relative selectors provided as arguments.

:host-context()

The :host-context() pseudo class represent the specific element in shadow host elements of shadow DOM based on the relative selectors provided as arguments.

:hover

The :hover pseudo class represent the elements when user point the mouse over it.

:indeterminate

The :indeterminate pseudo class represent the elements that has indeterminate state such as checkbox.

:in-range

The :in-range pseudo class represent the input elements whose value is within a specified range given by min and max attributes.

:invalid

The :invalid pseudo class represent the input elements whose value failed to validate.

:is()

The :is() pseudo class represent the elements based on the selector list given as arguments. For example, :is(#navigation li.active) = selects all li having active class inside element having id navigation.

:lang()

The :lang() pseudo class represent the elements based on the language. For example, :lang("en-US")

:last-child

The :last-child pseudo class represent the last element among a group of elements such as last list element inside lists, last paragraph inside div.

:last-of-type

The :last-of-type pseudo class represent the last element of its type among a group of elements.

:left

The :left pseudo class represent the left handed page of the print document and generally used along with @page at rule.

:link

The :link pseudo class represent the link element that are unvisited by the user.

:local-link

The :local-link pseudo class represent the link element whose absolute URL are same as HTML document root URL. Check for browser compatibility while using it.

:modal

The :modal pseudo class represent the element that excludes all the interaction outside of it.

:not()

The :not() pseudo class represent the element that do not match the criteria given as arguments. For example, :not(#navigation li.main-nav) = selects all list items not having main-nav class inside element having id navigation

:nth-child()

The :nth-child() pseudo class represent the element based on the their position. For example, :nth-child(even), :nth-child(n+5)

:nth-col()

The :nth-col() pseudo class represent the table cell element based on the their position. Check for browser compatibility while using it. For example, :nth-col(even), :nth-col(n+5)

:nth-last-child()

The :nth-last-child() pseudo class represent the element based on the their position starting from the end.

:nth-last-col()

The :nth-last-col() pseudo class represent the table cell element based on the their position starting from the end. Check for browser compatibility while using it.

:nth-of-type()

The :nth-of-type() pseudo class represent the element of its type (same tag) based on the their position.

:nth-last-of-type()

The :nth-last-of-type() pseudo class represent the element of its type (same tag) based on the their position starting from the end. Check for browser compatibility while using it.

:only-child()

The :only-child() pseudo class represent the element without any sibling.

:only-of-type()

The :only-of-type() pseudo class represent the element of same type without any sibling.

:optional

The :optional pseudo class represent the input element not having required attribute.

:out-of-range

The :out-of-range pseudo class represent the input element whose value is outside thee range specified by min and mx attributes.

:past()

The :past() pseudo class represent the elements that appears before :current() element. Check for browser compatibility while using it. For example, :past(div, p)

:picture-in-picture

The :picture-in-picture pseudo class represent the element that is in picture-in-picture mode like video.

:placeholder-shown

The :placeholder-shown pseudo class represent the input elements whose placeholder is currently displaying.

:paused

The :paused pseudo class represent the elements that is paused like audio and video.

:playing

The :playing pseudo class represent the elements that is playing like audio and video.

:read-only

The :read-only pseudo class represent the input elements having readonly attribute or can only be read but not write.

:read-write

The :read-write pseudo class represent the input elements can be read and write.

:required

The :required pseudo class represent the input elements having required attribute.

:right

The :right pseudo class represent the right handed page of the print document and generally used along with @page at rule.

:root

The :root pseudo class represent the root element that is html element.

:scope

The :scope pseudo class represent the element that are a reference point for selectors to match against.

:state

Not properly documented yet.

:target

The :target pseudo class represent the element whose id is equal to the URL fragment.

:target-within

The :target-within pseudo class represent the element or container element whose id is equal to the URL fragment. Check for browser compatibility while using it.

:user-invalid

The :user-invalid pseudo class represent the input element whose value is not valid after user interaction. Check for browser compatibility while using it.

:valid

The :valid pseudo class represent the input element whose value s valid.

:visited

The :visited pseudo class represent the link element which is visited by user.

:where()

The :where() pseudo class represent the elements based on the selector list given as arguments. For example, :is(#navigation li.active) = selects all li having active class inside element having id navigation.

Pseudo Element Selectors

A pseudo element is a keyword that defines an entity of the selected element. For example, placeholder class represent the entity of input element where it show text when field is empty.

It is defined by the double colon (::) followed by pseudo-element name.

Syntax = ::pseudo-element

Let's see with some example
					 
        
          /* Select all input element placeholder text */
          input::placeholder{
            color: green;
          }
        
      
Standard Pseudo Elements
::after

The ::after pseudo element creates a last child of selected element to add some content.

::backdrop

The ::backdrop pseudo element creates box of viewport size when element is in fullscreen mode or a dialog is open.

::before

The ::before pseudo element creates a first child of selected element to add some content.

::cue

The ::cue pseudo element represent the cue element such as video caption.

::cue-region

The ::cue-region pseudo element represent the cue element such as video caption. Check for browser compatibility while using it.

::file-selector-button

The ::file-selector-button pseudo element represent the button for file input element.

::first-letter

The ::first-letter pseudo element represent the first letter of first line of selected element.

::first-line

The ::first-line pseudo element represent the first line of selected element.

::grammar-error

The ::grammar-error pseudo element represent text which failed grammar check by User Agent. Check for browser compatibility while using it.

::highlight()

The ::highlight() pseudo element represent custom highlight content. Check for browser compatibility while using it.

::marker

The ::marker pseudo element represent the marker of list such as bullet, number, etc.

::part()

The ::part() pseudo element represent the shadow element having part attribute.

::placeholder

The ::placeholder pseudo element represent the placeholder text of input element.

::selection

The ::selection pseudo element represent the highlighted content such as clicking, dragging, and selecting.

::slotted()

The ::slotted() pseudo element represent the elements that has been placed in a slot of HTML shadow DOM tree.

::spelling-error

The ::spelling-error pseudo element represent text which failed spelling check by User Agent. Check for browser compatibility while using it.

::target-text

The ::target-text pseudo element represent text that has been scroll to. Check for browser compatibility while using it.

How you feel about this blog:

Share this blog on:

Report Us

If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com

Contact Us
Ads
Logo
Lynxsia IT Solutions

We are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..

Kotwali Road, Chhiptehri, Banda, 210001, UP, India

Copyright © 2022, Lynxsia IT Solutions, All rights reserved