Псевдокласс CSS :first-child
Псевдокласс CSS :first-child выбирает элемент, если он является первым дочерним среди других элементов.

Селектор :first-of-type можно использовать, если нужно выбрать и применить стиль к первому абзацу. Селектор :first-child на самом деле похож на :first-of-type, но между ними есть разница: они соответствуют разным условиям. :first-child выбирает элемент только в том случае, если он является первым дочерним элементом своего родителя, тогда как :first-of-type выбирает первый элемент своего типа среди его братьев и сестёр.
Версия
Синтаксис
Пример синтаксиса CSS :first-child
:first-child {
css declarations;
}Пример использования псевдокласса :first-child с тегом <p>:
Пример кода 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>Пример использования псевдокласса :first-child с тегом <li>:
Ещё один пример кода 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>Пример использования псевдокласса :first-child с тегом <ol>:
Пример селектора :first-child с тегом 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>Пример использования псевдокласса :first-child с тегом <em>:
Пример селектора :first-child с тегом 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>Пример использования псевдокласса :first-child с тегом <ul>:
Пример селектора :first-child с тегом 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>Практика
Что представляет собой псевдокласс :first-child в CSS?