visit
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.
:active
It is applied to an element while the user is pressing the mouse button or finger on it.
<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;
}
:hover
It is applied to an element when the user hovers over it with the mouse cursor.
<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;
}
:focus
It is applied to an element when it has the focus, that is when the user is interacting with it using the keyboard.
<input type="email" id="email" required placeholder="Email" />
input:focus {
background-color: #111;
}
:visited
It is applied to a visited link.
<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;
}
:first-child
and :last-child
It applies to the first and last child element, respectively, of a specific element.
<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;
}
:not(selector)
It applies to all items that do not match the specified selector.
<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;
}
:enabled
and :disabled
It applies to the input type element that is enabled and to an element that is disabled respectively.
<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;
}
:checked
It applies to a selected form element.
<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;
}
:valid
and :invalid
It applies to the input type element when the value entered is valid or invalid respectively.
<form>
<input type="email" id="email" required placeholder="Email" />
</form>
input:valid {
border: 2px solid green;
}
input:invalid {
border: 2px solid red;
}
:before
and :after
to insert content before and after an element, respectively.
<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: "👀";
}
::backdrop
It applies styles to a background behind a specific element to give it more importance or emphasis.
<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);
}
::first-letter
It applies styles to the first letter of a paragraph.
<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;
}
::placeholder
It applies styles to temporary text in an input element.
<form>
<input type="email" id="email" required placeholder="Email" />
</form>
input#email::placeholder {
color: #fc5c7d;
}
::marker
It applies styles to the markers of a list item.
<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;
}
Read more:
Want to connect with the Author?
Love connecting with friends all around the world on .Also Published .