Forum Moderators: not2easy

Message Too Old, No Replies

CSS3 guide: 3.6. last-child pseudo class

         

swa66

10:38 pm on Jun 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is a post in a series, please see the Table of Contents [webmasterworld.com] for the other posts in the series.

3.6. last-child pseudo selector

The last-child [w3.org] pseudo selector is the opposite of the CSS2.1 first-child pseudo selector and acts exactly the same as the new nth-last-child(1) pseudo selector.

Syntax: E:first-child (CSS2.1)

    selects the first child in a parent

Syntax: E:last-child

    selects the last child in a parent

Example:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>list-child psuedo selector test</title>
<style type="text/css">
* {
margin:0;
padding:0;
}
.menu {
text-align: center;
}
.menu li {
display: inline-block;
padding: 5px;
}
.menu li:first-child a, .menu li:last-child a {
text-decoration: none;
color: white;
}
.menu li:first-child {
background-color: green;
}
.menu li:last-child {
background-color: red;
}
</style>
</head>
<body>
<ul class="menu">
<li><a href="#">first</a></li>
<li><a href="#">second</a></li>
<li><a href="#">third</a></li>
<li><a href="#">last</a></li>
</ul>
</body>
</html>

Support:

  • Supported in most standard compliant browsers
  • Not supported by IE (including IE8)

IE6 doesn't support first-child either

Graceful fallback:
As always hard to do with selectors

IE8.js adds support for last-child in IE7 and IE6.

No known solution for IE8.

First child support for IE6 can be added with IE7.js

Practical use:
Many uses would be possible, but the lack of IE8 support nor workaround is crippling to getting this in use without a degraded solution.

[edited by: swa66 at 11:08 pm (utc) on June 30, 2009]

swa66

9:35 pm on Jun 30, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



reserved for future use