In CSS, targeting only the first child of an element is useful for applying styles exclusively to the first child element within a container. The :first-child
pseudo-class is used to select and style the first child element.
Syntax
/* Selecting and styling the first <p> element within a container */
.container p:first-child {
font-weight: bold;
}
/* Selecting and styling the first child of any type within a container */
.container :first-child {
color: red;
}
Features
-
:first-child
Pseudo-class: Targets the first child element of its parent. - Specific Element Type: It can be used to select the first child of a specific type within a container.
-
Universal Selector: It can also be applied using the universal selector (
*
) to select the first child regardless of its type.
Article Tags :