如何为第一个或最后一个元素甚至是某类规律元素添加样式?

有时候,我们在写DIV+CSS的时候,在某一类相同的元素内想给其中的第一个元素或者最后一个元素,甚至是其中某些特定的有规则的排列的元素添加特别的CSS样式,我们该如何下手?下面我将给大家将就一些在CSS中十分实用的方法.

首先,你可以手动使用下面这段代码来应用修改某个元素的效果(当然这个办法很笨,是个人都知道):

http://jsfiddle.net/kingterrors/szCM9/embedded/result,html,css/

PS:由于近日(2014年6月)jsfiddle无法正常访问,可能受内网影响,现将之前所有jsfiddle预览去除,不过你仍然可将以上地址拷贝到浏览器预览,或选择尝试以下代码:

<!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>

当然你也可以利用:first-child伪类和:last-child伪类(当然这种效果IE6完全不支持,IE7,IE8部分支持,其中IE7和IE8效果相同,不支持:last-child),比如:

http://jsfiddle.net/kingterrors/44GzJ/embedded/result,html,css/

PS:由于近日(2014年6月)jsfiddle无法正常访问,可能受内网影响,现将之前所有jsfiddle预览去除,不过你仍然可将以上地址拷贝到浏览器预览,或选择尝试以下代码:

<!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>
IE6,IE7,IE8都不能完好支持first-child和last-child伪类

其中还有种很另类的方式,给任意元素的规则性的添加伪类.比如你有5个li元素,你想让每两个li有区别,比如应用在有些特殊列表,你给每两行加上不同的背景色,我这里随便做了个效果给大家参考:

http://jsfiddle.net/kingterrors/76jxP/embedded/result,html,css/

PS:由于近日(2014年6月)jsfiddle无法正常访问,可能受内网影响,现将之前所有jsfiddle预览去除,不过你仍然可将以上地址拷贝到浏览器预览,或选择尝试以下代码:

<!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+是完全支持以上效果的.

另外,你也可以用jq来控制某个任意元素的样式,之前我也提到过,有兴趣可以看看列表中最后一个元素样式清除修改方法 🙄

如果有兴趣可以参考下原文,Remove Margins for First/Last Elements

[好歌推荐]Keren Ann – The harder ships of the world

歌名: The Harder Ships Of The World

歌手: Keren Ann

专辑: Keren Ann

歌词:

Tell me my friend
Do you still break the hearts?
Do you still put the crowds under your spell?
I’ve lived in a trial
Some invisible storm
Now I’m back in the land, and I miss you as hell

I see roads of light
Close to me, close to me
I’m riding on… 继续阅读“[好歌推荐]Keren Ann – The harder ships of the world”

[好歌推荐]London Philharmonic Orchestra – Angry Birds: Main Theme

歌名: Angry Birds: Main Theme

演奏: London Philharmonic Orchestra

专辑: The Greatest Video Game Music

歌词: 

这曲来自伦敦爱乐乐团的大家熟悉的歌曲<<愤怒的小鸟>>交响曲版本,节奏感极强,声场极好,给人愉悦舒畅的心情.推荐喜爱游戏的玩家听一下,当然这张专辑的其他曲目也十分值得欣赏,个人另外推荐Super Mario Bros: Themes以及Grand Theft Auto IV: Soviet Connection.还有更多大家自己欣赏咯!

London Philharmonic Orchestra – Angry Birds: Main Theme专辑封面

希捷新3T硬盘老主板也能装系统

无奈我的几个硬盘都已经满了除了双系统的还剩5G空间,我这2200G的硬盘剩下不到20G了…憋屈了很久,想买硬盘,可是硬盘迟迟不降价.今天突然看到中关村一篇文章,叫做硬盘价格轰然倒塌,于是忍不住偷笑,这小编肯定又要挨骂了.进去看看果然不出所料,不过这次很惊喜我一直想买的3T降到1000以内了,不过,之前了解到老主板只能支持2T硬盘做系统盘,于是乎对于3T硬盘估计只能做存储盘了.

不过紧接着看到一篇文章,感到惊喜,这篇同样来自中关村的文章3TB硬盘首破千元 性能价格PK谁最出众,不仅了解到了硬盘性能简单比较和评测,同样也学到了3TB硬盘如何安装到旧主板上.这里我就直接转载过来备用收藏了吼吼… 继续阅读“希捷新3T硬盘老主板也能装系统”

[好歌推荐]Mercedes Sosa – Gracias A La Vida

歌名: Gracias A La Vida

词曲: Mercedes Sosa

演唱: Mercedes Sosa

歌词:

Gracias a la vida que me ha dado tanto
Me dio dos luceros que cuando los abro
Perfecto distingo lo negro del blanco
Y en el ancho cielo su fondo estrellado
Y en las multitudes el hombre que yo amo

Gracias a la vida que me ha dado tanto
Me ha dado el sonido y el abecedario
Con el las palabras que pienso y declaro
Madre amigo hermano y luz alumbrando
La ruta del alma del que estoy amando

Gracias a la vida que me ha dado tanto
Me ha dado la marcha de mis pies cansados
Con ellos anduve ciudades y charcos
Playas y desiertos montanas y llanos
Y la casa tuya tu calle y tu patio

Gracias a la vida que me ha dado tanto
Me dio el corazon que agita su marco
Cuando miro el fruto del cerebro humano
Cuando miro el bueno tan lejos del malo
Cuando miro el fondo de tus ojos claros

Gracias a la vida que me ha dado tanto
Me ha dado la risa y me ha dado el llanto
Asi yo distingo dicha de quebranto
Los dos materiales que forman mi canto
Y el canto de ustedes que es el mismo canto
Y el canto de todos que es mi propio canto