10.1 Pseudo-class :focus
CSS pseudo-classes are awesome tools for styling elements in different states. They help enhance user interactions with forms and other interactive elements on a webpage. Let's dive into how you can use pseudo-classes like :focus, :hover, :disabled, and :checked for styling input elements (<input>).
The :focus pseudo-class is applied to a form element when it gains focus. This usually happens when the element is clicked or navigated to using the Tab key. Styling focusable elements helps users better navigate through a form.
Example Usage:
input:focus {
border-color: #4A90E2;
box-shadow: 0 0 5px rgba(74, 144, 226, 0.5);
outline: none;
}
<form>
<label for="name">Name:</label>
<input type="text" id="name" name="name">
</form>
Explanation
border-color: changes the border color of the element when it gains focusbox-shadow: adds a shadow effect around the element to highlight itoutline: removes the default browser outline, replacing it with a custom style
10.2 Pseudo-class :hover
The :hover pseudo-class is applied to an element when the user hovers over it with a cursor. This can be useful for providing visual feedback during interactions with the element.
Example Usage:
input:hover {
border-color: #50E3C2;
background-color: #F0F8FF;
}
<form>
<label for="email">Email:</label>
<input type="email" id="email" name="email">
</form>
Explanation
border-color: changes the border color of the element when the cursor is over itbackground-color: changes the background color of the element on hover
10.3 Pseudo-class :disabled
The :disabled pseudo-class is applied to form elements that are disabled, meaning they are inactive and cannot be used or changed by the user. This helps users understand that the element is unavailable for interaction.
Example Usage:
input:disabled {
background-color: #E0E0E0;
color: #A0A0A0;
cursor: not-allowed;
}
<form>
<label for="username">Username:</label>
<input type="text" id="username" name="username" disabled>
</form>
Explanation
background-color: changes the background color of the disabled element to show it's inactivecolor: changes the text color inside the disabled elementcursor: sets the cursor to "not-allowed" to visually indicate the element is unavailable
10.4 Pseudo-class :checked
The :checked pseudo-class is applied to checkbox and radio elements that are in a checked state. This allows you to style checked elements, making them more noticeable.
Example Usage:
input[type="checkbox"]:checked,
input[type="radio"]:checked {
background-color: #4A90E2;
border-color: #4A90E2;
}
<form>
<label>
<input type="checkbox" name="option" checked>
Option 1
</label>
<label>
<input type="checkbox" name="option">
Option 2
</label>
<label>
<input type="radio" name="choice" checked>
Choice 1
</label>
<label>
<input type="radio" name="choice">
Choice 2
</label>
</form>
Explanation
background-color: changes the background color of the checked elementborder-color: changes the border color of the checked element
GO TO FULL VERSION