CSS selects the first child element of the current element

Selecting the First Child of the Current Element with CSS

In CSS, we often need to select the first child of an element for styling. This is very common in web design, such as styling the first element of a list or the first cell of a table. This article will detail how to use CSS selectors to select the first child of an element and provide several code examples to demonstrate this.

1. Use the :first-child pseudo-class to select the first child element.

In CSS, we can use the :first-child pseudo-class to select the first child element of the current element. This pseudo-class selects and styles the first element among all child elements of the current element. Here is a simple example code:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<title>First Child Example</title> 
<style> 
.parent div:first-child { 
color: red; 
} 
</style> 
</head> 
<body> 
<div class="parent"> 
<div>First Child</div> 
<div>Second Child</div> 
<div>Third Child</div> 
</div> 
</body> 
</html> 

Output:


CSS Selects the First Child of the Current Element

In the example above, we select the first div element under the element with the class parent and set its text color to red. Run this code and you’ll see the text of the first child element turn red.

2. Use the :nth-child pseudo-class to select the first child element

In addition to the :first-child pseudo-class, we can also use the :nth-child pseudo-class to select the first child element of the current element. This pseudo-class accepts a parameter indicating the nth child element to select. Here is an example code:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<title>Nth Child Example</title> 
<style> 
.parent div:nth-child(1) { 
font-weight: bold; 
} 
</style> 
</head> 
<body> 
<div class="parent"> 
<div>First Child</div> 
<div>Second Child</div> 
<div>Third Child</div> 
</div> 
</body> 
</html> 

Output:

CSS selects the first child of the current element

In the example above, we select the first div element under the element with the class parent and set its text to bold. Run this code and you’ll see the text of the first child element become bold.

3. Use :first-of-type to select the first child element of a specific type

Sometimes we need to select the first child element of a specific type of the current element, rather than the first element among all child elements. You can use the :first-of-type selector in this case. Here’s an example:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<title>First of Type Example</title> 
<style> 
.parent div:first-of-type { 
background-color: lightblue; 
} 
</style> 
</head> 
<body> 
<div class="parent"> 
<p>First Paragraph</p> 
<div>First Div</div> 
<div>Second Div</div> 
</div> 
</body> 
</html> 

Output:

CSS selects the first child of the current element

In the example above, we select the first div element under an element with the class parent and set its background color to light blue. Run this code and you’ll see that the background of the first div element has changed to light blue.

4. Use :nth-of-type to Select Child Elements of a Specific Type

Similar to :nth-child, we can also use the :nth-of-type selector to select child elements of a specific type within the current element. Here is a sample code:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<title>Nth of Type Example</title> 
<style> 
.parent div:nth-of-type(2) { 
color: green; 
} 
</style> 
</head> 
<body> 
<div class="parent"> 
<div>First Div</div> 
<div>Second Div</div> 
<p>First Paragraph</p> 
</div> 
</body> 
</html> 

Output:

CSS selects the first child element of the current element

In the example above, we select the second div element under the element with the class parent and set its text color to green. Run this code and you’ll see the text of the second div element turn green.

5. Select the First Letter Using :first-letter

In addition to selecting the first child of an element, we can also select and style the first letter of an element. To do this, use the :first-letter pseudo-element. Here is a sample code:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<title>First Letter Example</title> 
<style> 
.parent p:first-letter { 
font-size: 24px; 
color: blue; 
} 
</style> 
</head> 
<body> 
<div class="parent"> 
<p>First Letter Example</p> 
</div> 
</body> 
</html> 

Output:

CSS selects the first child of the current element

In the example above, we select the first letter of the first p element under an element with the class parent and set its font size to 24px and color to blue. Running this code, you’ll see that the first letter of the first p element becomes 24px in size and blue.

6. Selecting the First Line with :first-line

Similar to :first-letter, we can also select and style the first line of an element. To do this, use the :first-line pseudo-element. Here is an example code:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<title>First Line Example</title> 
<style> 
.parent p:first-line { 
font-weight: bold; 
} 
</style> 
</head> 
<body> 
<div class="parent"> 
<p>This is the first line of the paragraph. This is the second line of the paragraph.</p> 
</div> 
</body> 
</html> 

Output:

CSS selects the first child of the current element

In the example above, we select the first line of the first p element under an element with the class parent and set its text to bold. Run this code and you’ll see that the first line of the first p element is now bold.

7. Use :first-of-type to Select the First Child of a Specific Type

Sometimes we need to select the first child of a specific type, rather than the first child of all children. To do this, use the :first-of-type selector. Here is an example code:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<title>First of Type Example</title> 
<style> 
.parent div:first-of-type { 
background-color: lightblue; 
} 
</style> 
</head> 
<body> 
<div class="parent"> 
<p>First Paragraph</p> 
<div>First Div</div> 
<div>Second Div</div> 
</div> 
</body> 
</html> 

Output:

CSS selects the first child of the current element

In the example above, we select the first div element under an element with the class parent and set its background color to light blue. Run this code and you’ll see that the background of the first div element has changed to light blue.

8. Use :nth-of-type to Select Child Elements of a Specific Type

Similar to :nth-child, we can also use the :nth-of-type selector to select child elements of a specific type within the current element. Here is an example code:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<title>Nth of Type Example</title> 
<style> 
.parent div:nth-of-type(2) { 
color: green; 
} 
</style> 
</head> 
<body> 
<div class="parent"> 
<div>First Div</div> 
<div>Second Div</div> 
<p>First Paragraph</p> 
</div> 
</body> 
</html> 

Output:

CSS selects the first child of the current element

In the example above, we select the second div element under the element with the class parent and set its text color to green. Run this code and you’ll see the text of the second div element turn green.

9. Use :first-of-type to Select the First Child of a Specific Type

Sometimes we need to select the first child of a specific type, rather than the first child of all children. To do this, use the :first-of-type selector. Here is an example code:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<title>First of Type Example</title> 
<style> 
.parent div:first-of-type { 
background-color: lightblue; 
} 
</style> 
</head> 
<body> 
<div class="parent"> 
<p>First Paragraph</p> 
<div>First Div</div> 
<div>Second Div</div> 
</div> 
</body> 
</html> 

Output:

CSS selects the first child of the current element

In the example above, we select the first div element under an element with the class parent and set its background color to light blue. Run this code and you’ll see that the background of the first div element has changed to light blue.

10. Use :nth-of-type to Select Child Elements of a Specific Type

Similar to :nth-child, we can also use the :nth-of-type selector to select child elements of a specific type within the current element. Here is an example code:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<title>Nth of Type Example</title> 
<style> 
.parent div:nth-of-type(2) { 
color: green; 
} 
</style> 
</head> 
<body> 
<div class="parent"> 
<div>First Div</div> 
<div>Second Div</div> 
<p>First Paragraph</p> 
</div> 
</body> 
</html> 

Output:

CSS selects the first child of the current element

In the example above, we select the second div element under the element with the class parent and set its text color to green. Run this code and you’ll see the text of the second div element turn green.

Leave a Reply

Your email address will not be published. Required fields are marked *