CSS selects the first li

Select the First LI with CSS

In web development, you often need to apply special styling to the first element in a list. In this case, you can use CSS selectors to select the first LI element and apply specific styles to it.

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

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<title>CSS Select First Li</title> 
<style> 
ul li:first-child { 
color: red; 
font-weight: bold; 
} 
</style> 
</head> 
<body> 
<ul> 
<li>First Li Element</li> 
<li>Second Li Element</li> 
<li>Third Li Element</li> 
</ul> 
</body> 
</html> 

Output:

CSS Select First Li


In the example above, we use the :first-child pseudo-class selector to select the first li element and set its text color to red and bold.

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

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<title>CSS Select First Li</title> 
<style> 
ul li:nth-child(1) { 
background-color: lightblue; 
padding: 5px; 
} 
</style> 
</head> 
<body> 
<ul> 
<li>First li element</li> 
<li>Second li element</li> 
<li>Third li element</li> 
</ul> 
</body> 
</html> 

Output:

CSS Select First Li

In the example above, we use the :nth-child selector to select the first li element, set its background color to light blue, and add some padding.

3. Use :first-of-type to select the first li element

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<title>CSS Select First Li</title> 
<style> 
ul li:first-of-type { 
border: 1px solid black; 
margin: 5px; 
padding: 5px; 
} 
</style> 
</head> 
<body> 
<ul> 
<li>First li element</li> 
<li>Second li element</li> 
<li>Third li element</li> 
</ul> 
</body> 
</html> 

Output:

CSS selects the first li

In the example above, we use the :first-of-type selector to select the first li element and add a border, margin, and padding to it.

4. Use :first-child and :nth-child to select the first li element

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<title>CSS Select First Li</title> 
<style> 
ul li:first-child:nth-child(1) { 
color: green; 
font-style: italic; 
} 
</style> 
</head> 
<body> 
<ul> 
<li>First li element</li> 
<li>Second li element</li> 
<li>Third li element</li> 
</ul> 
</body> 
</html> 

Output:

CSS Select First li

In the above example, we use the :first-child and :nth-child selectors together to select the first li element and set its text color to green and its font style to italic.

5. Use :first-child and :not to select the first li element

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<title>CSS Select First Li</title> 
<style> 
ul li:first-child:not(:last-child) { 
text-decoration: underline; 
} 
</style> 
</head> 
<body> 
<ul> 
<li>First li element</li> 
<li>Second li element</li> 
<li>Third li element</li> 
</ul> 
</body> 
</html> 

Output:

CSS Select First Li

In the example above, we use the :first-child and :not selectors together to select the first li element and underline it, but exclude the last li element.

6. Use :first-of-type and :nth-of-type to select the first li element

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<title>CSS Select First Li</title> 
<style> 
ul li:first-of-type:nth-of-type(1) { 
background-color: lightyellow; 
} 
</style> 
</head> 
<body> 
<ul> 
<li>First li element</li> 
<li>Second li element</li> 
<li>Third li element</li> 
</ul> 
</body> 
</html> 

Output:

CSS Select First li

In the example above, we use the :first-of-type and :nth-of-type selectors together to select the first li element and set its background color to light yellow.

7. Use :first-child and :nth-of-type to select the first li element

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<title>CSS Select First Li</title> 
<style> 
ul li:first-child:nth-of-type(1) { 
color: purple; 
} 
</style> 
</head> 
<body> 
<ul> 
<li>First li element</li> 
<li>Second li element</li> 
<li>Third li element</li> 
</ul> 
</body> 
</html> 

Output:

CSS Select First Li

In the example above, we use the :first-child and :nth-of-type selectors together to select the first li element and set its text color to purple.

8. Use :first-of-type and :not to select the first li element

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<title>CSS Select First Li</title> 
<style> 
ul li:first-of-type:not(:last-of-type) { 
font-size: 20px; 
} 
</style> 
</head> 
<body> 
<ul> 
<li>First li element</li> 
<li>Second li element</li> 
<li>Third li element</li> 
</ul> 
</body> 
</html> 

Output:

CSS Select First li

In the example above, we use the :first-of-type and :not selectors together to select the first li element and set its font size to 20 pixels, but exclude the last li element.

9. Use :first-child and :nth-last-child to select the first li element

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<title>CSS Select First Li</title> 
<style> 
ul li:first-child:nth-last-child(3) { 
text-transform: uppercase; 
} 
</style> 
</head> 
<body> 
<ul> 
<li>First li element</li> 
<li>Second li element</li> 
<li>Third li element</li> 
</ul> 
</body> 
</html> 

Output:

CSS Select First li

In the example above, we use the :first-child and :nth-last-child selectors together to select the first li element and convert its text to uppercase.

10. Use :first-of-type and :nth-last-of-type to select the first li element

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<title>CSS Select First li</title> 
<style> 
ul li:first-of-type:nth-last-of-type(3) { 
font-family: 'Arial'; 
} 
</style> 
</head> 
<body> 
<ul> 
<li>First li element</li> 
<li>Second li element</li> 
<li>The third li element</li>
</ul>
</body>
</html>

Output:

CSS Select First li

In the example above, we use the :first-of-type and :nth-last-of-type selectors together to select the first li element and set its font to Arial.

11. Use :first-child and :nth-of-type to select the first li element and add a hover effect

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<title>CSS Select First li</title> 
<style> 
ul li:first-child:nth-of-type(1):hover { 
background-color: lightcoral; 
} 
</style> 
</head> 
<body> 
<ul> 
<li>First li element</li> 
<li>Second li element</li> 
<li>The third li element</li>
</ul>
</body>
</html>

Output:

CSS Select First li

In the example above, we use the :first-child and :nth-of-type selectors together to select the first li element and add a background color effect on hover.

12. Use :first-of-type and :not to select the first li element and add animation effects

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<title>CSS Select First li</title> 
<style> 
ul li:first-of-type:not(:last-of-type) { 
transition: all 0.5s; 
} 
ul li:first-of-type:not(:last-of-type):hover { 
transform: scale(1.2); 
} 
</style> 
</head> 
<body> 
<ul> 
<li>First li element</li> 
<li>Second li element</li> 
<li>The third li element</li>
</ul>
</body>
</html>

Output:

CSS Select First li

In the example above, we use the :first-of-type and :not selectors together to select the first li element and add a scale animation on hover.

13. Use :first-child and :nth-last-child to select the first li element and add transition effects

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<title>CSS Select First li</title> 
<style> 
ul li:first-child:nth-last-child(3) { 
transition: all 0.5s; 
} 
ul li:first-child:nth-last-child(3):hover { 
color: orange; 
} 
</style> 
</head> 
<body> 
<ul> 
<li>First li element</li> 
<li>Second li element</li> 
<li>The third li element</li>
</ul>
</body>
</html>

Output:

CSS Select First li

In the example above, we use the :first-child and :nth-last-child selectors together to select the first li element and add a color transition effect on hover.

14. Use :first-of-type and :nth-last-of-type to select the first li element and add a rotation effect

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<title>CSS Select First li</title> 
<style> 
ul li:first-of-type:nth-last-of-type(3) { 
transition: all 0.5s; 
} 
ul li:first-of-type:nth-last-of-type(3):hover { 
transform: rotate(45deg); 
} 
</style> 
</head> 
<body> 
<ul> 
<li>First li element</li> 
<li>Second li element</li> 
<li>The third li element</li>
</ul>
</body>
</html>

Output:

CSS Select First li

In the example above, we use the :first-of-type and :nth-last-of-type selectors together to select the first li element and add a rotation effect on hover.

15. Use :first-child and :nth-of-type to select the first li element and add a shadow effect

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<title>CSS Select First li</title> 
<style> 
ul li:first-child:nth-of-type(1) { 
box-shadow: 5px 5px 5px rgba(0, 0, 0, 0.5); 
} 
</style> 
</head> 
<body> 
<ul> 
<li>First li element</li> 
<li>Second li element</li> 
<li>The third li element</li>
</ul>
</body>
</html>

Output:

CSS Select First li

In the example above, we use the :first-child and :nth-of-type selectors together to select the first li element and add a shadow effect to it.

16. Use :first-of-type and :not to select the first li element and add a border effect

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<title>CSS Select First li</title> 
<style> 
ul li:first-of-type:not(:last-of-type) { 
border: 2px solid blue; 
} 
</style> 
</head> 
<body> 
<ul> 
<li>First li element</li> 
<li>Second li element</li> 
<li>Third li element</li> 
</ul> 
</body> 
</html> 

Output:

CSS Select First Li

In the example above, we use the :first-of-type and :not selectors together to select the first li element and add a blue border to it, but exclude the last li element.

17. Use :first-child and :nth-last-child to select the first li element and add a background gradient effect

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<title>CSS Select First li</title> 
<style> 
ul li:first-child:nth-last-child(3) { 
background: linear-gradient(to right, lightblue, lightgreen); 
} 
</style> 
</head> 
<body> 
<ul> 
<li>First li element</li> 
<li>Second li element</li> 
<li>The third li element</li>
</ul>
</body>
</html>

Output:

CSS Select First li

In the example above, we use the :first-child and :nth-last-child selectors together to select the first li element and add a background gradient effect to it.

18. Use :first-of-type and :nth-last-of-type to select the first li element and add transparency effect

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<title>CSS Select First li</title> 
<style> 
ul li:first-of-type:nth-last-of-type(3) { 
opacity: 0.7; 
} 
ul li:first-of-type:nth-last-of-type(3):hover { 
opacity: 1; 
} 
</style> 
</head> 
<body> 
<ul> 
<li>First li element</li> 
<li>Second li element</li> 
<li>The third li element</li>
</ul>
</body>
</html>

Output:

CSS Select First li

In the example above, we use the :first-of-type and :nth-last-of-type selectors together to select the first li element and add a transparency effect to it, restoring the transparency on hover.

19. Using :first-child and :nth-of-type

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<title>CSS Select First Li</title> 
<style> 
ul li:first-child:nth-of-type(1) { 
text-decoration: underline; 
} 
</style> 
</head> 
<body> 
<ul> 
<li>First Li Element</li> 
<li>Second Li Element</li> 
<li>Third Li Element</li> 
</ul> 
</body> 
</html> 

Output:

CSS Select First Li

In the example above, we use the :first-child and :nth-of-type selectors together to select the first li element and add an underline effect to it.

20. Use :first-of-type and :not to select the first li element and add rotation and scaling effects

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<title>CSS Select First li</title> 
<style> 
ul li:first-of-type:not(:last-of-type) { 
transition: all 0.5s; 
} 
ul li:first-of-type:not(:last-of-type):hover { 
transform: rotate(45deg) scale(1.2); 
} 
</style> 
</head> 
<body> 
<ul> 
<li>First li element</li> 
<li>Second li element</li> 
<li>The third li element</li>
</ul>
</body>
</html>

Output:

CSS Select First li

In the example above, we use the :first-of-type and :not selectors together to select the first li element and add a rotation and scaling effect on hover.

Leave a Reply

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