<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>how to remove margins for first last elements</title>
<style type="text/css">
ul {
border: 1px solid #000;
margin: 0;
padding: 0;
list-style: none;
float:left;
}
ul li {
background:#eee;
color: #F00;
margin: 50px;
}
.first {
color: #000;
margin-top: 0 !important;
margin-left: 0 !important;
}
.last {
color: #0f0;
margin-bottom: 0 !important;
margin-right: 0 !important;
}
</style>
</head>
<body>
<ul>
<li class="first">Hello, This is first element</li>
<li>WOW, so many elements</li>
<li>WOW, so many elements</li>
<li>WOW, so many elements</li>
<li class="last">Here it is, The last element</li>
</ul>
</body>
</html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>how to remove margins for first last elements</title>
<style type="text/css">
ul {
border: 1px solid #000;
margin: 0;
padding: 0;
list-style: none;
float:left;
}
ul li {
background:#eee;
color: #F00;
margin: 50px;
}
ul li:first-child {
color: #000;
margin-top: 0 !important;
margin-left: 0 !important;
}
ul li:last-child {
color: #0f0;
margin-bottom: 0 !important;
margin-right: 0 !important;
}
</style>
</head>
<body>
<ul>
<li>Hello, This is first element</li>
<li>Whidy! so many elements</li>
<li>Whidy! so many elements</li>
<li>Whidy! so many elements</li>
<li>Here it is, The last element</li>
</ul>
</body>
</html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>how to remove margins for first last elements</title>
<style type="text/css">
ul {
border: 1px solid #000;
margin: 0;
padding: 0;
list-style: none;
float:left;
}
ul li {
background:#eee;
color: #F00;
margin: 50px;
}
ul li:nth-child(2n) {
color: #000;
margin-top: 0 !important;
margin-left: 0 !important;
}
</style>
</head>
<body>
<ul>
<li>Hello, This is first element</li>
<li>Whidy! so many elements</li>
<li>Whidy! so many elements</li>
<li>Whidy! so many elements</li>
<li>Here it is, The last element</li>
</ul>
</body>
</html>
当然这个在IE8以下包括IE8的版本都是不被支持的 😯 !
最后总结一下:first-child和:last-child伪类在IE6下是完全不支持的,而IE7和IE8仅支持:first-child,IE9是完全支持的.而:nth-child只有IE9支持,其他的比如Safari 3+, Firefox 3.5+ and Chrome 1+是完全支持以上效果的.