CSS styling is an essential part of creating an attractive and user-friendly website. Some of the most powerful tools we have at our disposal are pseudo-classes and pseudo-elements.

This time, we are going to explore the importance of using these tools in CSS to improve the interaction and accessibility of our websites, as well as to give them a more sophisticated and professional look.

Pseudo-elements

These are elements that can be added to an HTML element to give it additional style or functionality.

They are called “pseudo” because they are not actual HTML elements but are “simulated” by special syntax in CSS.

Here, you can learn all about pseudo-elements.

Pseudo-classes

These are keywords added to selectors that specify a special state of the selected element. These special states can include user actions such as hovering over an element or having an active element.

Here, you can learn all about pseudo-classes.

Why use pseudo-elements and pseudo-classes?

In some cases, you might be tempted to create new HTML elements to add additional styles or functionality to your page. However, there are several reasons why you should consider using pseudo elements instead of creating new elements.

Here are some reasons why you should consider using pseudo-elements and pseudo-classes in your projects:

The most used Pseudo-classes in CSS

<ul>
  <li><a href="#">Home</a></li>
  <li><a href="#">About Us</a></li>
  <li><a href="#">Contact Us</a></li>
</ul>
nav a:active {
  background-color: #6a82fb;
}

<ul>
  <li><a href="#">Home</a></li>
  <li><a href="#">About Us</a></li>
  <li><a href="#">Contact Us</a></li>
</ul>
nav a:hover {
   color: #333;
   background-color: #fc5c7d;
 }

<input type="email" id="email" required placeholder="Email" />
input:focus {
   background-color: #111;
 }

<footer>
   <ul>
    <li><a href="/" class="link">Link 1</a></li>
    <li><a href="#" class="link">Link 2</a></li>
    <li><a href="/" class="link">Link 3</a></li>
   </ul>
  </footer>
footer .link:visited {
  color: #444;
}

<ol>
  <li>Lorem ipsum dolor sit amet, consectetur adipiscing.</li>
  <li>Lorem ipsum dolor sit amet, consectetur adipiscing.</li>
  <li>Lorem ipsum dolor sit amet, consectetur adipiscing.</li>
</ol>
ol li:first-child {
  font-weight: bold;
}

ol li:last-child {
  text-decoration: underline;
}

<footer>
  <ul>
    <li><a href="/" class="link">Link 1</a></li>
    <li><a href="#" class="link">Link 2</a></li>
    <li><a href="/" class="link">Link 3</a></li>
  </ul>
</footer>
footer ul li:not(:nth-child(3)) {
  font-weight: normal;
 }

<form>
  <button type="submit" disabled>Subscribe</button>h
</form>

<form>
  <button type="submit" enabled>Subscribe</button>h
</form>
form button:enabled {
  background-color: #6a82fb;
  color: white;
  padding: 10px 20px;
  border: none;
}

form button:disabled {
  background-color: #ccc;
  color: #666;
  padding: 10px 20px;
  border: none;
}

<form>
  <div id="radio-group">
  <input type="radio" class="radio" id="radio1" />
  <label for="radio1">I agree</label>
  <input type="radio" class="radio" id="radio2" />
  <label for="radio2">I desagree</label>
  </div>
</form>
.radio:checked {
  accent-color: #6a82fb;
 }

<form>
  <input type="email" id="email" required placeholder="Email" />
</form>
input:valid {
  border: 2px solid green;
}

input:invalid {
  border: 2px solid red;
}

The most used Pseudo-elements in CSS

<section id="about-us">
   <h2>About Us</h2>
<section>

<section id="services">
   <h2>Services</h2>
</section>
#about-us h2::before {
  content: "😀";
}

#services h2::after {
  content: "👀";
}

<dialog>
  <h2>I'm an open dialog window</h2>
  <p>Check out my backdrop :)</p>
  <button class="btn" onclick="closeDialog()">Close</button>
</dialog>
dialog::backdrop {
  background-color: rgba(106, 130, 251, 0.4);
}

<section id="about-us">
  <h2>About Us</h2>
  <p>
    We are a company dedicated to providing high quality products and services to our customers. We have been in
    business for over 10 years and have a reputation for excellent customer service and satisfaction.
  </p>
</section>
#about-us p::first-letter {
  font-size: 150%;
  color: #6a82fb;
}


<form>
  <input type="email" id="email" required placeholder="Email" />
</form>
input#email::placeholder {
  color: #fc5c7d;
}

<section id="about-us">
    <li>First element</li>
    <li>Second element</li>
    <li>Third element</li>
   </ul>
  </section>
#about-us ul li::marker {
  color: #6a82fb;
  font-size: 1.5em;
}

Putting it all together

https://codesandbox.io/s/peaceful-pine-ju3gjw?from-embed=&embedable=true

Conclusion

In summary, pseudoclasses are an essential tool for web developers, as they allow us to create interactions and dynamic visual effects on web pages, improving the user experience and making the page more attractive and easy to use.

It is important to keep in mind that some pseudoclasses are more compatible with specific browsers, so it is always advisable to check compatibility before using them in a project.


Read more:

Want to connect with the Author?

Love connecting with friends all around the world on Twitter.


Also Published here.