CSS Tutorial

CSS Style

CSS Layout Style

CSS Float Style

CSS Reference

CSS :first-child Selector


What is :first-child Selector?

CSS :first-child selector matches only first child element of its parent element.

Browser Compatibility

  • Google Chrome 2+
  • Mozilla Firefox 3+
  • Internet Explorer 9+
  • Opera 9.5+
  • Safari 4+

Note: Here details of browser compatibility with version number may be this is bug and not supported. But recommended to always use latest Web browser.

Syntax

General syntax of :first-child selector,

:first-child {
	property: value;
	property: value;
	...
}

li:first-child {
	property: value;
	property: value;
	...
}

Example:

CSS :first-child selector select only first <li> element of its <ul> parent element.

<style type="text/css">
li:first-child {
	background:#ff0000;
}
</style>
</head>
<body>
    <ul>
        <li>This is first line</li>
        <li>This is second line</li>
        <li>This is third line</li>
        <li>This is forth line</li>
        <li>This is fifth line</li>
    </ul>
</body>

Run it...   »

Example Result: