Friday, September 3, 2010

CSS :first-child, :last-child pseudo elements

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<title>CSS first-child last-child pseudo elements</title>
<style type="text/css">
.pDiv p:first-child {
color: #00f;
}
.pDiv p:last-child {
color: #f00;
}
</style>
</head>

<body>
<h1>First child and last child of any element. Best use for navigation/td borders/float
div margins, paddings/UL LI first and last</h1>

<div class="pDiv">
<p>This is some text.</p>
<p>This is some text.</p>
<p>This is some text.</p>
<p>This is some text.</p>
<p><b>Note:</b> For :first-child to work in IE, a DOCTYPE must be declared.</p>
</div>

<h4>:last-child doesn't work in IE 7 and 8</h4>

</body>

</html>