Front-end web developer——[to be a better man]
文章字体大小Font Size文章字体大小:12px, 14px

CSS

Aug 26

CSS3的强大,让人惊叹,人们在惊喜之余,又不得不为其艰难的道路感到可惜:好的标准只有得到行业浏览器的良好支持才算得上“标准”。CSS3标准已提出数年,但是目前能实现她的浏览器并不多,虽然部分浏览器能实现部分规范,但这又有什么用呢?面对更多的兼容性问题,CSSer们只有望洋轻叹。虽然如此,但有前瞻性的我们,又怎能停步不前呢?今天我们就来“前瞻”一下CSS3的一个伪类选择器“:nth-child()”

语法:

:nth-child(an+b)

为什么选择她,因为我认为,这个选择器是最多学问的一个了。很可惜,据我所测,目前能较好地支持她的只有Opera9+和Safari3+。

描述:

伪类:nth-child()的参数是an+b,如果按照w3.org上的描述,写成中文,很可能会让人头晕,再加上笔者的文笔水平有限,所以我决定避开an+b的说法,把它拆分成5种写法共5部分来说明。

第一种:简单数字序号写法

:nth-child(number)

直接匹配第number个元素。参数number必须为大于0的整数。 阅读全文(Read the rest of this entry) »

Popularity: 60% [?]

Aug 12

http://flickr.com/photos/andallthatmalarkey/301121794/

关于CSS选择器优先级,目前国内已有很多人进行过解释,但感觉不如人意,特别对于初学者,更是难以理解。这里我把W3C上所描述的规范以我的理解再解释一下,希望能给大家提供到帮助。

Calculating a selector’s specificity上的原文摘选如下:
(相信很多人更喜欢看原文)

A selector’s specificity is calculated as follows:

  • count 1 if the declaration is from is a ’style’ attribute rather than a rule with a selector, 0 otherwise (= a) (In HTML, values of an element’s “style” attribute are style sheet rules. These rules have no selectors, so a=1, b=0, c=0, and d=0.)
  • count the number of ID attributes in the selector (= b)
  • count the number of other attributes and pseudo-classes in the selector (= c)
  • count the number of element names and pseudo-elements in the selector (= d)

The specificity is based only on the form of the selector. In particular, a selector of the form “[id=p33]” is counted as an attribute selector (a=0, b=0, c=1, d=0), even if the id attribute is defined as an “ID” in the source document’s DTD.

Concatenating the four numbers a-b-c-d (in a number system with a large base) gives the specificity.

Example(s):

Some examples:

 *             {}  /* a=0 b=0 c=0 d=0 -> specificity = 0,0,0,0 */
 li            {}  /* a=0 b=0 c=0 d=1 -> specificity = 0,0,0,1 */
 li:first-line {}  /* a=0 b=0 c=0 d=2 -> specificity = 0,0,0,2 */
 ul li         {}  /* a=0 b=0 c=0 d=2 -> specificity = 0,0,0,2 */
 ul ol+li      {}  /* a=0 b=0 c=0 d=3 -> specificity = 0,0,0,3 */
 h1 + *[rel=up]{}  /* a=0 b=0 c=1 d=1 -> specificity = 0,0,1,1 */
 ul ol li.red  {}  /* a=0 b=0 c=1 d=3 -> specificity = 0,0,1,3 */
 li.red.level  {}  /* a=0 b=0 c=2 d=1 -> specificity = 0,0,2,1 */
 #x34y         {}  /* a=0 b=1 c=0 d=0 -> specificity = 0,1,0,0 */
 style=""          /* a=1 b=0 c=0 d=0 -> specificity = 1,0,0,0 */

============

CSS优先级的读法

这里先更正一些错误的读法。通过百度搜索到的内容中,通常出现这样的写法:(1,0,0,0),但有部分文章对它的解释不全面,有的甚至有误。

其中最大的一个错误就是把结果加:(1,0,0,0)=1000,(0,0,2,2)=22,更有甚者:(0,1,0,1)=0+1+0+1=2!虽然这些理解在很简单的情况下看上去是正确的,但本质上却是个重大的错误。

另外有部分文章把它理解为4个级别,也相近,但不能把条理分清楚,理解起来也难。

“CSS优先级包含四个级别(文内选择符,ID选择符,Class选择符,元素选择符)以及各级别出现的次数。根据这四个级别出现的次数计算得到CSS的优先”级。

这句话总结得很好,但对初学者来说,在理解方面就有点难度了,“四个级别”,太容易混淆,其实应该是“四组级别”。

我认为,对优先级的读法,应该是以“组”来分,这个组之间相互独立,从左到右进行对比。它们成组出现,以逗号分隔。

  selector( a , b , c , d )
   compare: ↑ , ↑ , ↑ , ↑
  selector( a , b , c , d )

正如w3c.org中原文所示,分为a,b,c,d四组,全为正整娄,默认为0,对应于不同的选择器结构和组成形式。在选择器之间的优先级进行对比时,从左到右1对1对比,当比出有大者时即可停止比较。

 li.red.level  {}  /* a=0 b=0 c=2 d=1 -> specificity = 0 , 0 , 2 , 1  */
       /*compare                                       ↑ , ↑ , √      */
 h1 + *[rel=up]{}  /* a=0 b=0 c=1 d=1 -> specificity = 0 , 0 , 1 , 1  */
       /*compare                                       ↑ , ↑ , ↑ , √  */
 ul ol li.red  {}  /* a=0 b=0 c=1 d=3 -> specificity = 0 , 0 , 1 , 3  */
       /*compare                                       ↑ , ↑ , √      */
 #x34y         {}  /* a=0 b=1 c=0 d=0 -> specificity = 0 , 1 , 0 , 0  */
       /*compare                                       ↑ , √          */
 style=""          /* a=1 b=0 c=0 d=0 -> specificity = 1 , 0 , 0 , 0  */

(上表中,表示还要进行比较,表示从此处已得到了结果)

其实这里就像个奥运金牌榜,到我修改文章为止(2008-08-12),我国以13金领先,详细如下:

1 中国 13 3 4
2 美国 7 6 8
3 韩国 5 6 1
4 意大利 3 4 2

.

再有,只要正确书写,仅从优先级中大概知道选择器结构形式了,如:

1,0,0,0表示是元素内的style;

0,2,1,1表示是一个由两个ID选择器,1个类或伪类或属性选择器,以及一个元素选择器组成的选择器。

CSS优先级规则的细节:

在纠正读法后,才能开始讲详细的规则:

  • a组数值只有把CSS写进style属性时才会为1,否则为0.写进style的样式声明其实不算是个选择器,所以这里面的b,c,d组值均为0,只有真正的选择器才会有b,c,d组值。
  • b组数值决定于ID选择器#ID,有多少个ID选择器,并会进行此组数值累加;
  • c组数值决定于类、伪类和属性选择符,并会进行该组数值累加;
  • d组数值决定于元素名,即元素选择器,并会进行该组数值累加;

注意,这四组数值分别对应于不同类型的选择器,互不影响,根据读法法则进行比较。

(这里没有讨论到!important,就近原则和继承,也没有实例代码,欢迎大家共同讨论!)

参考:

[1] CSS优先级问题

[2]Calculating a selector’s specificity

Popularity: 49% [?]

Apr 27

做了几天的一个专题,分析了CSS清除浮动的要点和方法,对成因和结果进行归纳总结,以及如何避免在IE中偶尔遇到的一些不可思议的问题,其实是一个自我学习的过程,请大家给意见,谢谢!

点击链接进入专题:

CSS清除浮动专题

leaningfloatscreenshoot

Popularity: 51% [?]

Apr 01
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
* Get the class string from the object
* @param object o the DOM object 
* @return string the class string of the object
*/
//var getClass=function(o){
var getClassName=function(o){
	return o.className;
}
/*
* Set the class string from the object
* @param string cls the class string to be set
* @param object o the DOM object where
* the class string to be set to
*/
//var setClass=function(o){
var setClassName=function(cls,o){
	o.className=cls;
}
/*
* Parse the classes list to array by split(" ")
* @param string cl a string of class or a list of classes
* @return array with the element of class name
*/
var getClassArray=function(cl){
	if(cl && typeof(cl)=="string")
	return cl.split(" ");
	else return cl;
}
/*
* If the class is listed in the classes list ,return true
* @param string cl a single class name 
* @param string cls class name list
* @return true if the class is listed 
* in the classes list.else return false.
*/
var matchClass=function(cl,cls){
	if(!cls) return false;
	cls= getClassArray(cls);
	for(var c in cls) if(cl==cls[c]) return true;
	return cl==cls;
}
/*
* remove one of the classes from the object
* @param string cl a single class name 
* @param string cls class name list
*/
var removeClass=function(cl,o){
	var cls=getClassArray(getClassName(o));
	var _cls="";
	for(var i in cls) if (cl!=cls[i]) _cls+=" "+cls[i];
	setClassName(_cls,o);
}
/*
* add a class to the object
* @param string cl a single class name 
* @param object o the DOM object
* where the class string to be ADD to
*/
var addClass=function(cl,o){
	removeClass(cl,o);
	setClassName(getClassName(o)+" "+cl,o);
}

  上面几个函数,相信很多人早就写过不少,在这里我仅为总结一下经验,也希望有点价值。
  通常,在xhtml中标签改变class的值,只要

obj.className=stringValue

  这是对付简单的情况。当一个元素中已经有多个class时,一个简单的obj.className=stringValue就无能为力了。 阅读全文(Read the rest of this entry) »

Popularity: 51% [?]