:first-child - HTML CSS CSS Selector

HTML CSS examples for CSS Selector:first-child

Description

The :first-child selector selects elements if it is the first child of its parent.

Example

Select and style the first <li> element in lists:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
li:first-child {<!--   w  w  w .  j  a v a  2  s  . c o m-->
    background: red;
}
</style>
</head>
<body>

<ul>
  <li>Coffee</li>
  <li>Tea</li>
  <li>Coca Cola</li>
</ul>

<ol>
  <li>Coffee</li>
  <li>Tea</li>
  <li>Coca Cola</li>
</ol>

</body>
</html>

Related Tutorials