Pseudo-classe CSS :first-child
Use a pseudo-classe CSS :first-child para selecionar e aplicar estilos ao primeiro filho entre os outros elementos irmãos. Saiba mais sobre a pseudo-classe e pratique com os exemplos.
A pseudo-classe CSS :first-child seleciona o elemento se este for o primeiro filho entre os outros elementos.

O seletor :first-of-type pode ser utilizado se pretender selecionar e aplicar o estilo ao primeiro parágrafo. O seletor :first-child é, na verdade, semelhante a :first-of-type, mas há uma diferença: correspondem a condições diferentes. :first-child seleciona um elemento apenas se este for o primeiro filho do seu pai, ao passo que :first-of-type seleciona o primeiro elemento do seu tipo específico entre os seus irmãos.
Versão
Sintaxe
Exemplo de sintaxe CSS :first-child
:first-child {
css declarations;
}Exemplo da pseudo-classe :first-child com a tag <p>:
Exemplo de código CSS :first-child
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
p:first-child {
background-color: #1c87c9;
color: #fff;
}
</style>
</head>
<body>
<p>Lorem ipsum is simply dummy text...</p>
<h2>First-child selector example</h2>
<p>Lorem Ipsum is simply dummy text...</p>
</body>
</html>Exemplo da pseudo-classe :first-child com a tag <li>:
Outro exemplo de código CSS :first-child
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
li:first-child {
background: #8ebf42;
}
</style>
</head>
<body>
<h2>:first-child selector example</h2>
<ul>
<li>Paris</li>
<li>Moscow</li>
<li>Rome</li>
</ul>
<ol>
<li>Paris</li>
<li>Moscow</li>
<li>Rome</li>
</ol>
</body>
</html>Exemplo da pseudo-classe :first-child com a tag <ol>:
Exemplo do seletor :first-child com a tag HTML ol
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
ol:first-child {
background: #8ebf42;
}
</style>
</head>
<body>
<ol>
<li>London</li>
<li>Paris</li>
<li>Rome</li>
</ol>
<ol>
<li>London</li>
<li>Paris</li>
<li>Rome</li>
</ol>
</body>
</html>Exemplo da pseudo-classe :first-child com a tag <em>:
Exemplo do seletor :first-child com a tag HTML em
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
p em:first-child {
background: #82b534;
}
</style>
</head>
<body>
<h2>:first-child selector example</h2>
<article>
<p>Here is a <em>some</em> text. This is a <em>example</em>.</p>
<p>Here is a <em>some</em> text. This is a <em>example</em>.</p>
<p>Here is a <em>some</em> text. This is a <em>example</em>.</p>
</article>
</body>
</html>Exemplo da pseudo-classe :first-child com a tag <ul>:
Exemplo do seletor :first-child com a tag HTML ul
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
ul li {
color: blue;
}
ul li:first-child {
color: #8ebf42;
font-weight: bold;
}
</style>
</head>
<body>
<h2>:first-child selector example</h2>
<ul>
<li>List Item 1</li>
<li>List Item 2</li>
<li>
List Item 3
<ul>
<li>List Item 3.1</li>
<li>List Item 3.2</li>
<li>List Item 3.3</li>
</ul>
</li>
</ul>
</body>
</html>