Imagine you’re organizing a display of different fruits and want to highlight the first apple, the first orange, and the first banana in each row. In web design, the :first-of-type
selector in CSS works similarly. It allows you to style the first occurrence of a specific element type within its parent, making it stand out and giving you more control over your design.
What is the :first-of-type Selector?
The :first-of-type
selector is a CSS pseudo-class that targets the first element of its type within its parent. This can be particularly useful for styling the first instance of a specific element, such as the first paragraph, the first heading, or the first image, within a section or container.
Here’s how it looks:
element:first-of-type {
property: value;
}
Why Use the :first-of-type Selector?
- Precise Targeting: The
:first-of-type
selector allows you to apply styles specifically to the first occurrence of an element type within its parent, providing precise control over your designs.
- Dynamic Styling: It enables you to create dynamic and flexible styles that respond to the structure of your content, enhancing the visual appeal.
- Simplified Code: Using the
:first-of-type
selector can reduce the need for additional classes or IDs, keeping your HTML and CSS clean and maintainable.
Practical Examples
Let’s explore some practical examples to see how the :first-of-type
selector can be used effectively.
Example 1: Styling the First Paragraph of Each Section
You can style the first paragraph in each section to make it stand out.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>First Paragraph</title>
<style>
p:first-of-type {
font-weight: bold;
color: blue;
}
</style>
</head>
<body>
<section>
<p>This is the first paragraph in the first section.</p>
<p>This is another paragraph in the first section.</p>
</section>
<section>
<p>This is the first paragraph in the second section.</p>
<p>This is another paragraph in the second section.</p>
</section>
</body>
</html>
In this example, the p:first-of-type
selector applies bold text and blue color to the first paragraph within each section, making it stand out.
Example 2: Highlighting the First List Item of Each Type
You can style the first list item of each type in a mixed list to give it special attention.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>First List Item</title>
<style>
ul li:first-of-type {
background-color: yellow;
}
ol li:first-of-type {
background-color: lightgreen;
}
</style>
</head>
<body>
<ul>
<li>First item in unordered list</li>
<li>Second item in unordered list</li>
</ul>
<ol>
<li>First item in ordered list</li>
<li>Second item in ordered list</li>
</ol>
</body>
</html>
Here, the ul li:first-of-type
selector applies a yellow background to the first item in each unordered list, while the ol li:first-of-type
selector applies a light green background to the first item in each ordered list, highlighting their positions.
Example 3: Styling the First Image in Each Container
You can style the first image in each container to give it a special border.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>First Image</title>
<style>
.gallery img:first-of-type {
border: 5px solid red;
}
</style>
</head>
<body>
<div class="gallery">
<img src="image1.jpg" alt="First Image in Gallery">
<img src="image2.jpg" alt="Second Image in Gallery">
</div>
<div class="gallery">
<img src="image3.jpg" alt="First Image in Another Gallery">
<img src="image4.jpg" alt="Second Image in Another Gallery">
</div>
</body>
</html>
In this example, the .gallery img:first-of-type
selector applies a red border to the first image within each gallery container, making it visually distinct.
Example 4: Styling the First Heading in Each Article
You can style the first heading in each article to give it a unique style.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>First Heading</title>
<style>
article h2:first-of-type {
text-decoration: underline;
color: purple;
}
</style>
</head>
<body>
<article>
<h2>First heading in the first article</h2>
<p>Content of the first article.</p>
<h2>Another heading in the first article</h2>
</article>
<article>
<h2>First heading in the second article</h2>
<p>Content of the second article.</p>
<h2>Another heading in the second article</h2>
</article>
</body>
</html>
Here, the article h2:first-of-type
selector applies an underline and purple color to the first h2
heading within each article, highlighting its position.
Example 5: Combining :first-of-type with Other Selectors
You can combine :first-of-type
with other selectors for more complex styles.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Combined Selectors</title>
<style>
.container p:first-of-type {
font-style: italic;
color: orange;
}
</style>
</head>
<body>
<div class="container">
<p>This is the first paragraph in the first container.</p>
<p>This is another paragraph in the first container.</p>
</div>
<div class="container">
<p>This is the first paragraph in the second container.</p>
<p>This is another paragraph in the second container.</p>
</div>
</body>
</html>
In this example, the .container p:first-of-type
selector styles the first paragraph in each container with italic text and orange color, making it stand out.
Conclusion
The :first-of-type
selector is a versatile and powerful tool in CSS, allowing you to apply styles specifically to the first occurrence of an element type within its parent. Whether you’re highlighting the first paragraph, the first list item, the first image, or the first heading, the :first-of-type
selector helps you create dynamic, organized, and visually appealing designs. So, the next time you want to style the first instance of an element type, remember the :first-of-type
selector — your key to precise and effective styling.