css匹配指定索引的li元素

快乐打工仔 分类:实例代码

分享一段代码实例,它实现匹配指定索引li元素的功能。

代码实例如下:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.pipipi.net/" />
<title>前端教程网</title>
<style type="text/css">
* {
  margin:0px;
  padding:0px;
}
ul {
  margin:0px auto;
  width:230px;
  height:50px;
}
ul li {
  list-style-type:none;
  width:50px;
  height:50px;
  float:left;
  margin-right:10px;
  background:#ccc;
}
li:nth-child(4) {
  margin-right:0px;
  background:red;
}
</style>
</head>
<body>
<ul>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
</ul>
</body>
</html>

上面的代码可以将第四个li元素背景颜色设置为红色。

需要特别注意的是,:nth-child()参数的索引是从1开始的,而不是像其他大多数编程语言是从0开始。

回复

我来回复
  • 暂无回复内容