Back to: CSS Tutorials for Beginners and Professionals
Pseudo Classes and Elements in CSS with Examples
In this article, I am going to discuss CSS Pseudo Classes and Elements with Examples. Please read our previous article where we discussed Display Property in CSS with Examples.
CSS Pseudo Classes
Pseudo-classes allow us to define a style for a special state of an element, like the hover or active state. Pseudo-classes are defined with a single colon and then the class name like hover or active.
Pseudo Classes List in CSS
- :active
- :any-link
- :autofill
- :blank
- :checked
- :current
- :default
- :defined
- :dir()
- :disabled
- :empty
- :enabled
- :first
- :first-child
- :first-of-type
- :fullscreen
- :future
- :focus
- :focus-visible
- :focus-within
- :has()
- :host
- :host()
- :host-context()
- :hover
- :indeterminate
- :in-range
- :invalid
- :is()
- :lang()
- :last-child
- :last-of-type
- :left
- :link
- :local-link
- :not()
- :nth-child()
- :nth-last-col()
- :nth-last-of-type()
- :nth-of-type()
- :only-child
- :only-of-type
- :optional
- :out-of-range
- :past
- :picture-in-picture
- :placeholder-shown
- :paused
- :playing
- :read-only
- :read-write
- :required
- :right
- :root
- :scope
- :state()
- :target
- :target-within
- :user-invalid
- :valid
- :visited
- :where()
Example of Hover Pseudo Class in CSS:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>CSS Course</title> <link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet"> <style> #product-overview { width: 100%; height: 500px; background-color: blue; border: black solid 5px; padding: 20px; margin: 30px; box-sizing: border-box; } .main-nav__item{ display: inline-block; } .main-nav__item a:hover{ color: red; } </style> </head> <body> <header class="main-header"> <div> <a href="index.html"> CSS Tutorials </a> </div> <nav class="main-nav"> <ul class="main-nav__items"> <li class="main-nav__item"> <a href="index.html">Courses</a> </li> <li class="main-nav__item"> <a href="index.html">Teachers</a> </li> <li class="main-nav__item"> <a href="index.html">Start Learning</a> </li> </ul> </nav> </header> <main> <section class="product-overview"> <h1> Get the freedom to learn from anywhere, anytime </h1> </section> </main> </body> </html>
Output:
CSS Pseudo Elements
CSS Pseudo-elements allow us to define a style of a specific part of an element. Pseudo-elements are defined by adding two colons and then the element name.
Pseudo Elements List in CSS
- ::after
- ::backdrop
- ::before
- ::cue
- ::cue-region
- ::first-letter
- ::first-line
- ::first-selector-button
- ::grammar-error
- ::marker
- ::part()
- ::placeholder
- ::selection
- ::slotted()
- ::spelling-error
- ::target-text
Example of First-Letter CSS Pseudo Element
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>CSS Course</title> <link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet"> <style> #product-overview { width: 100%; height: 500px; background-color: blue; border: black solid 5px; padding: 20px; margin: 30px; box-sizing: border-box; } .main-nav__item{ display: inline-block; } p::first-letter{ font: bold; color: red; font-size: 80px; } </style> </head> <body> <header class="main-header"> <div> <a href="index.html"> CSS Tutorials </a> </div> <nav class="main-nav"> <ul class="main-nav__items"> <li class="main-nav__item"> <a href="index.html">Courses</a> </li> <li class="main-nav__item"> <a href="index.html">Teachers</a> </li> <li class="main-nav__item"> <a href="index.html">Start Learning</a> </li> </ul> </nav> </header> <main> <section class="product-overview"> <h1> <p> Get the freedom to learn from anywhere, anytime</p> </h1> </section> </main> </body> </html>
Output:
In the next article, I am going to discuss Pseudo Classes and Elements in CSS with Examples. Here, in this article, I try to explain Pseudo Classes and Elements in CSS with Examples. I hope this CSS Pseudo Classes and Elements with Examples article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article.