-
Hajipur, Bihar, 844101
A CSS pseudo-class is used to define the special state of an element. You can style elements based on user interaction, element position, input status, and more — without adding any extra classes or IDs.
Pseudo-class | Description |
---|---|
:hover |
When user hovers over an element |
:focus |
When an element (like input) is focused |
:active |
When an element is being clicked |
:first-child |
First child of a parent |
:last-child |
Last child of a parent |
:nth-child(n) |
Nth child of a parent |
:checked |
A checked radio or checkbox input |
:disabled |
A disabled input or button |
:required |
A form field marked as required |
:not(selector) |
Matches everything except the given selector |
:hover
a:hover {
color: red;
}
Changes color when a link is hovered.
:first-child
p:first-child {
font-weight: bold;
}
Styles only the first <p>
inside its parent.
:nth-child(odd)
/ :nth-child(even)
li:nth-child(even) {
background-color: #f0f0f0;
}
Useful for zebra striping.
:focus
input:focus {
outline: 2px solid blue;
}
Styles an input field when it is focused.
:checked
input[type="checkbox"]:checked + label {
color: green;
}
Applies styles to checked checkbox's associated label.
:not()
div:not(.active) {
opacity: 0.5;
}
Selects all <div>
s that don't have class active
.
Q1. Make hovered links change color to red.
Q2. Bold the first paragraph inside a container.
Q3. Apply background to even list items.
Q4. Highlight form input when focused.
Q5. Change color of checked checkboxes.
Q6. Style the last child of an <ul>
.
Q7. Make required input fields show red border.
Q8. Hide buttons that are disabled.
Q9. Apply italic style to all paragraphs except ones with class .no-style
.
Q10. Use :nth-child(3)
to underline the 3rd list item.
Q1: Which pseudo-class targets hovered elements?
Q2: What does :first-child select?
Q3: Which pseudo-class targets focused input fields?
Q4: Which one targets a selected radio button?
Q5: What does :nth-child(2) select?
Q6: Which pseudo-class is used for disabled buttons?
Q7: What does :not(.selected) do?
Q8: How to style all required inputs in red?
Q9: What is the result of li:last-child?
Q10: Which pseudo-class is not valid?