More pseudo-classes
In the previous chapter you learned the pseudo classes to style something on hover. In this chapter you will learn some more. We start with refreshing the syntax:
:nth-child()
With this pseudo class you can style the nth child of a parent element, or any child element whose numeric position in the series of children matches the pattern an+b. The formula an+b is placed inside the parentheses, e.g.: nth-child(1n+0) would match every child element.
The values a and b must both be integers, and the index of an element's first child is 1. A common use case is to match every other row in a table.
In a list with list elements, the following code would affect every odd numbered list element:
HTML
CSS
tr:nth-child(2n+1)
Represents the odd rows of an HTML table.
r:nth-child(odd)
Represents the odd rows of an HTML table.
tr:nth-child(2n)
Represents the even rows of an HTML table.
tr:nth-child(even)
Represents the even rows of an HTML table.
span:nth-child(0n+1)
Represents a span element which is the first child of its parent; this is the same as the :first-child selector.
span:nth-child(1)
Equivalent to the above.
span:nth-child(-n+3)
Matches if the element is one of the first three children of its parent and also a span.
:last-child
The :last-child pseudo class represents any element that is the last child element of its parent.
:required
The :required
pseudo class represents any <input>
element that has the required
attribute set on it. This allows forms to easily indicate which fields must have valid data before the form can be submitted.
:focus
The :focus pseudo class is applied when an element has received focus, either from the user selecting it with the use of a keyboard or by activating with the mouse (e.g. a form input).
:checked
The :checked pseudo class selector represents any radio (<input type="radio">
), checkbox (<input type="checkbox">
) or option (<option>
in a <select>
) element that is checked or toggled to an on state. The user can change this state by clicking on the element, or selecting a different value, in which case the :checked pseudo class no longer applies to this element, but will to the relevant one.
:disabled and :enabled
The :enabled pseudo class represents any enabled element. An element is enabled if it can be activated (e.g. selected, clicked on or accept text input) or accept focus. The element also has an disabled state, in which it can't be activated or accept focus.
The :disabled pseudo class represents any disabled element. An element is disabled if it can't be activated (e.g. selected, clicked on or accept text input) or accept focus. The element also has an enabled state, in which it can be activated or accept focus.
:valid and :invalid
The :valid pseudo class represents any <input>
or <form>
element whose content validates correctly according to the input's type setting. This allows to easily make valid fields adopt an appearance that helps the user confirm that their data is formatted properly.
The :invalid pseudo class represents any <input>
or <form>
element whose content fails to validate according to the input's type setting. This allows you to easily have invalid fields adopt an appearance that helps the user identify and correct errors.
HTML
CSS
:lang()
The :lang pseudo class matches elements based on the language the element is determined to be in. In HTML, the language is determined by a combination of the lang attribute, the <meta>
element, and possibly by information from the protocol (such as HTTP headers).
HTML
CSS
Last updated
Was this helpful?