How to select the first element in CSS
How to Select the First Element with CSS
In CSS, we often need to select specific elements on a page to apply styles. Sometimes, we need to select the first element to apply a specific style, which requires using specific selectors. This article will detail how to select the first element with CSS and provide some sample code to help readers better understand.
1. Using the :first-child Selector
The :first-child
selector is used to select the first child element under a parent element. Here’s a simple example:
<!DOCTYPE html>
html>
<head>
<style>
p:first-child {
color: red;
}
</style>
</head>
<body>
<p>First paragraph</p>
<p>Second paragraph</p>
<p>Third paragraph</p>
</body>
</html>
Output:
In the example above, we use the :first-child
selector to select the first <p>
element and set its text color to red. After running the code, the first paragraph text color will turn red.
2. Use the :first-of-type selector
:first-of-type
selector is used to select the first child element of a specified type under a parent element. Here’s a sample code:
<!DOCTYPE html>
<html>
<head>
<style>
p:first-of-type {
font-weight: bold;
}
</style>
</head>
<body>
<p>First paragraph</p>
<div>
<p>Second paragraph</p>
<p>Third paragraph</p>
</div>
</body>
</html>
Output:
:first-of-type selector to select the first
<p>
element and set its text to bold. After running the code, the first paragraph text will be bold.
3. Using the :nth-child selector
:nth-child
selector is used to select child elements at a specified position under a parent element. Here is a sample code:
<!DOCTYPE html>
<html>
<head>
<style>
p:nth-child(2) {
color: blue;
}
</style>
</head>
<body>
<p>First paragraph</p>
<p>Second paragraph</p>
<p>Third paragraph</p>
</body>
</html>
Output:
In the above example, we use the :nth-child(2)
selector to select the second <p>
element and set its text color to blue. After running the code, the second paragraph text will turn blue.
4. Using the :nth-of-type Selector
:nth-of-type
selector is used to select child elements of a specified type at a specified position under a parent element. Here is a sample code:
<!DOCTYPE html>
<html>
<head>
<style>
p:nth-of-type(odd) {
background-color: lightgray;
}
</style>
</head>
<body>
<p>First paragraph</p>
<p>Second paragraph</p>
<p>Third paragraph</p>
</body>
</html>
Output:
In the example above, we use the
:nth-of-type(odd)
selector to select the <p>
elements in odd positions and set their background color to light gray. After running the code, the background color of the first and third paragraphs will change to light gray.
5. Using the :first-letter Selector
The :first-letter
selector is used to select the first letter of an element. Here’s a sample code:
<!DOCTYPE html>
<html>
<head>
<style>
p:first-letter {
font-size: 24px;
color: red;
}
</style>
</head>
<body>
<p>Geek-docs.com is an online technical documentation website. </p>
</body>
</html>
Output:
In the example above, we use the :first-letter
selector to select the first letter of a paragraph and set its font size to 24 pixels and color to red. After running the code, the first letter of the paragraph will be 24 pixels in size and red.
6. Using the :first-line Selector
The :first-line
selector is used to select the first line of an element. Here’s a sample code:
<!DOCTYPE html>
<html>
<head>
<style>
p:first-line {
font-weight: bold;
}
</style>
</head>
<body>
<p>Geek-docs.com is an online technical documentation website. Geek-docs.com provides a variety of technical documentation and tutorials. </p>
</body>
</html>
Output:
In the example above, we use the :first-line
selector to select the first line of a paragraph and set its text to bold. After running the code, the first line of the paragraph will be bold.
7. Using the :first-of-type and :nth-of-type Selectors Together
We can also combine the :first-of-type
and :nth-of-type
selectors to select child elements of a specific type at a specific position. Here is a sample code:
<!DOCTYPE html>
<html>
<head>
<style>
p:first-of-type:nth-of-type(2) {
color: green;
}
</style>
</head>
<body>
<p>First paragraph</p>
<p>Second paragraph</p>
<p>Third paragraph</p>
</body>
</html>
Output:
In the above example, we use the :first-of-type:nth-of-type(2)
selector to select the first <p>
element and set its text color to green. After running the code, the first paragraph text will turn green.
8. Use the :not selector to exclude the first element
:not
selector is used to exclude specified elements. Here’s a sample code:
<!DOCTYPE html>
<html>
<head>
<style>
p:not(:first-of-type) {
font-style: italic;
}
</style>
</head>
<body>
<p>First paragraph</p>
<p>Second paragraph</p>
<p>Third paragraph</p>
</body>
</html>
Output:
In the example above, we use the :not(:first-of-type)
selector to exclude the first <p>
element and italicize its text. After running the code, the second and third paragraphs will be italicized.
9. Combining the :first-child and :nth-child selectors
We can also combine the :first-child
and :nth-child
selectors to select child elements at a specific position. Here is a sample code:
<!DOCTYPE html>
<html>
<head>
<style>
p:first-child:nth-child(2) {
text-decoration: underline;
}
</style>
</head>
<body>
<p>First paragraph</p>
<p>Second paragraph</p>
<p>Third paragraph</p>
</body>
</html>
Output:
In the above example, we use the :first-child:nth-child(2)
selector to select the first <p>
element and set its text to underline. After running the code, the first paragraph text will be underlined.
10. Using the :first-of-type and :last-of-type selectors together
We can also combine the :first-of-type
and :last-of-type
selectors to select the first and last child elements of a specified type. Here’s a sample code:
<!DOCTYPE html>
<html>
<head>
<style>
p:first-of-type:last-of-type {
font-size: 20px;
}
</style>
</head>
<body>
<p>First paragraph</p>
<p>Second paragraph</p>
<p>Third paragraph</p>
</body>
</html>
Output:
<!DOCTYPE html>
<html>
<head>
<style>
p:first-of-type:nth-last-of-type(2) {
color: purple;
}
</style>
</head>
<body>
<p>First paragraph</p>
<p>Second paragraph</p>
<p>Third paragraph</p>
</body>
</html>
Output:
:first-of-type:nth-last-of-type(2) selector to select the first and second-to-last
<p>
elements and set their text color to purple. After running the code, the first and second-to-last paragraph text will turn purple.
12. Using the :first-of-type and :nth-child selectors together
We can also combine the :first-of-type
and :nth-child
selectors to select the first and Nth child elements. Here is a sample code:
<!DOCTYPE html>
<html>
<head>
<style>
p:first-of-type:nth-child(3) {
text-transform: uppercase;
}
</style>
</head>
<body>
<p>First paragraph</p>
<p>Second paragraph</p>
<p>Third paragraph</p>
</body>
</html>
Output:
:first-of-type:nth-child(3) selector to select the first and third
<p>
elements and convert their text to uppercase. After running the code, the first and third paragraph text will be uppercase.
13. Using the :first-of-type and :nth-last-child selectors together
We can also combine the :first-of-type
and :nth-last-child
selectors to select the first and last Nth child elements. Here is a sample code:
<!DOCTYPE html>
<html>
<head>
<style>
p:first-of-type:nth-last-child(2) {
font-family: 'Arial';
}
</style>
</head>
<body>
<p>First paragraph</p>
<p>Second paragraph</p>
<p>Third paragraph</p>
</body>
</html>
Output:
:first-of-type:nth-last-child(2) selector to select the first and second-to-last
<p>
elements and set their fonts to Arial. After running the code, the first and second-to-last paragraph text will be in Arial font.
14. Using the :first-of-type and :nth-of-type selectors together
We can also combine the :first-of-type
and :nth-of-type
selectors to select the first and Nth child elements of a specified type. Here is a sample code:
<!DOCTYPE html>
<html>
<head>
<style>
p:first-of-type:nth-of-type(2) {
text-decoration: underline;
}
</style>
</head>
<body>
<p>First paragraph</p>
<p>Second paragraph</p>
<p>Third paragraph</p>
</body>
</html>
Output:
:first-of-type:nth-of-type(2) selector to select the first and second
<p>
elements and set their text to underline. After running the code, the first and second paragraph text will be underlined.
15. Using the :first-of-type and :nth-last-of-type selectors together
We can also combine the :first-of-type
and :nth-last-of-type
selectors to select the first and Nth-to-last child elements of a specified type. Here is a sample code:
<!DOCTYPE html>
<html>
<head>
<style>
p:first-of-type:nth-last-of-type(2) {
font-style: italic;
}
</style>
</head>
<body>
<p>First paragraph</p>
<p>Second paragraph</p>
<p>Third paragraph</p>
</body>
</html>
Output:
:first-of-type:nth-last-of-type(2) selector to select the first and second-to-last
<p>
elements and set their text to italic. After running the code, the first and second-to-last paragraph text will be italicized.
16. Using the :first-of-type and :not selectors together
We can also combine the :first-of-type
and :not
selectors to select the first but exclude specific elements. Here’s a sample code:
<!DOCTYPE html>
<html>
<head>
<style>
p:first-of-type:not(:last-of-type) {
font-weight: bold;
}
</style>
</head>
<body>
<p>First paragraph</p>
<p>Second paragraph</p>
<p>Third paragraph</p>
</body>
</html>
Output:
<!DOCTYPE html>
<html>
<head>
<style>
p:first-of-type:nth-child(2) {
color: orange;
}
</style>
</head>
<body>
<p>First paragraph</p>
<p>Second paragraph</p>
<p>Third paragraph</p>
</body>
</html>
Output:
:first-of-type:nth-child(2) selector to select the first and second
<p>
elements and set their text color to orange. After running the code, the first and second paragraph text will turn orange.
18. Using the :first-of-type and :nth-last-child selectors together
We can also combine the :first-of-type
and :nth-last-child
selectors to select the first and last Nth child elements. Here is a sample code:
<!DOCTYPE html>
<html>
<head>
<style>
p:first-of-type:nth-last-child(2) {
background-color: lightblue;
}
</style>
</head>
<body>
<p>First paragraph</p>
<p>Second paragraph</p>
<p>Third paragraph</p>
</body>
</html>
Output:
In the above example, we use the :first-of-type:nth-last-child(2)
selector to select the first and second-to-last <p>
elements and set their background color to light blue. After running the code, the background color of the first and second-to-last paragraphs will become light blue.
19. Using the :first-of-type and :nth-of-type Selectors in Combination
We can also combine the :first-of-type
and :nth-of-type
selectors to select the first and Nth child elements of a specified type. Here is a sample code:
<!DOCTYPE html>
<html>
<head>
<style>
p:first-of-type:nth-of-type(2) {
text-shadow: 2px 2px 2px black;
}
</style>
</head>
<body>
<p>First paragraph</p>
<p>Second paragraph</p>
<p>Third paragraph</p>
</body>
</html>
Output:
In the above example, we use the :first-of-type:nth-of-type(2)
selector to select the first and second <p>
elements and add a black text shadow to them. After running the code, the first and second paragraph text will have a black text shadow effect.
20. Use the :first-of-type and :nth-last-of-type selectors together
We can also combine the :first-of-type
and :nth-last-of-type
selectors to select the first and last Nth child element of a specified type. Here is a sample code:
<!DOCTYPE html>
<html>
<head>
<style>
p:first-of-type:nth-last-of-type(2) {
letter-spacing: 2px;
}
</style>
</head>
<body>
<p>First paragraph</p>
<p>Second paragraph</p>
<p>Third paragraph</p>
</body>
</html>
Output:
In the above example, we use the :first-of-type:nth-last-of-type(2)
selector to select the first and second-to-last <p>
elements and set their text spacing to 2 pixels. After running the code, the first and second-to-last paragraph text will have a 2-pixel text spacing.