CSS selector: CSS is to specify the label to the role, the name of the label is the selector. Meaning: Select which container.
Selector syntax
CSS selectors:
1, the basic selector syntax
Selector | Types of | Functional Description |
* | Wild selector | Select the document so HTML elements |
E | Element selector | Select the specified type of HTML element |
#id | ID selector | Select Specifies the ID attribute value "id" any type of element |
.class | Class selector | Select any number of elements in the specified class attribute value of "class" of any type |
selector1,selectorN | Group selector | Each element of a selected set of matching and |
2, the level selector syntax
Selector | Types of | Functional Description |
E F | Descendant selectors (selectors included) | F Select matched elements and matched elements of F is contained within the matching element E |
E>F | Child selector | F child element selected element matches, and matching the matching elements E F element |
E+F | Adjacent Sibling Selector | F behind the selection of matched elements and matched elements located immediately F matching element E |
E~F | Universal selector | F element selected after all matching matching elements F, E and the matching element located |
3, dynamic pseudo class selector syntax
Selector | Types of | Functional Description |
E:link | Link pseudo class selector | Select the matching element E, and match element is defined as a hyperlink has not been visited. Commonly used to describe the link point |
E:visited | Link pseudo class selector | Select the matching element E, and match element is defined as a hyperlink and has been visited. Commonly used to describe the link point |
E:active | User behavior selector | Select the matching element E, and the matching element is activated. Commonly used to describe the links and buttons points |
E:hover | User behavior selector | Select the matching element E, and the user mouse over the element E. IE6 and below the browser supports only a: hover |
E:focus | User behavior selector | Select the matching element E, and obtaining focus matching elements |
4, the pseudo target class selector
Selector | Functional Description |
E:target | Select all matching elements E, and matching elements are to related URL |
5, language pseudo-class selectors, it is particularly convenient to use in a multi-language version of the site
Elements used to match the specified language.
E: lang(language)
6, UI Element Status pseudo-class selector syntax
Selector | Types of | Functional Description |
E:checked | Selected pseudo class selector | Match the selected check button or radio button form element |
E:enabled | Enabled pseudo-class selector | All matches enabled form elements |
E:disabled | Unavailable state pseudo class selector | Matches all disabled form elements |
7,结构伪类选择器使用语法
选择器 | 功能描述 |
E:first-child | 作为父元素的第一个子元素的元素E。与E:nth-child(1)等同,(IE6不支持) |
E:last-child | 作为父元素的最后一个子元素的元素E。与E:nth-last-child(1)等同 |
E F:nth-child(n) | [CSS3]选择父元素E的第n个子元素F。其中n可以是整数(1,2,3)、关键字(even,odd)、可以是公式(2n+1),而且n值起始值为1,而不是0. |
E F:nth-last-child(n) | [CSS3]选择父元素E的倒数第n个子元素F。此选择器与E:nth-child(n)选择器计算顺序刚好相反,但使用方法都是一样的,其中:nth-last-child(1)始终匹配最后一个元素,与last-child等同 |
E:nth-of-type(n) | [CSS3]选择父元素内具有指定类型的第n个E元素 |
E:nth-last-of-type(n) | [CSS3]选择父元素内具有指定类型的倒数第n个E元素 |
E:first-of-type | [CSS3]选择父元素内具有指定类型的第一个E元素,与E:nth-of-type(1)等同 |
E:last-of-tye | [CSS3]选择父元素内具有指定类型的最后一个E元素,与E:nth-last-of-type(1)等同 |
E:only-child | [CSS3]选择父元素只包含一个子元素,且该子元素匹配E元素 |
E:only-of-type | [CSS3]选择父元素只包含一个同类型子元素,且该子元素匹配E元素 |
E:root | 选择匹配元素E所在文档的根元素。在HTML文档中,根元素始终是html,此时该选择器与html类型选择器匹配的内容相同 |
E:empty | 选择没有子元素的元素,而且该元素也不包含任何文本节点 |
注:(1),“ul>li:nth-child(3)”表达的并不是一定选择列表ul元素中的第3个子元素li,仅有列表ul中第3个li元素前不存在其他的元素,命题才有意义,否则不会改变列表第3个li元素的样式。
(2),:nth-child(n) 中参数只能是n,不可以用其他字母代替。
(3),:nth-child(odd) 选择的是奇数项,而使用:nth-last-child(odd) 选择的却是偶数项
8,否定伪类选择器
选择器 | 功能描述 |
E:not(F) | 匹配所有除元素F外的E元素 |
9,属性选择器语法
选择器 | 功能描述 |
---|---|
[attribute] | 用于选取带有指定属性的元素。 |
[attribute=value] | 用于选取带有指定属性和值的元素。 |
[attribute~=value] | 用于选取属性值中包含指定词汇的元素。 |
[attribute|=value] | 用于选取带有以指定值开头的属性值的元素,该值必须是整个单词。 |
[attribute^=value] | 匹配属性值以指定值开头的每个元素。 |
[attribute$=value] | 匹配属性值以指定值结尾的每个元素。 |
[attribute*=value] | 匹配属性值中包含指定值的每个元素。
|
CSS 选择符:
1) id选择器(# myid)
2) 类选择器(.myclassname)
3) 标签选择器(div, h1, p)
4) 相邻选择器(h1 + p)
5) 子选择器(ul > li)
6) 后代选择器(li a)
7) 通配符选择器( * )
8) 属性选择器(a[rel = "external"])
9) 伪类选择器(a: hover, li:nth-child)
可继承的样式:
1) font-size
2) font-family
3) color
4) text-indent
不可继承的样式:
1) border
2) padding
3) margin
4) width
5) height
优先级算法:
1) 优先级就近原则,同权重情况下样式定义最近者为准;
2) 载入样式以最后载入的定位为准;
3) 3.!important > id > class > tag
4) important 比 内联优先级高,但内联比 id 要高
CSS3新增伪类举例:
1) p:first-of-type 选择属于其父元素的首个 <p> 元素的每个 <p> 元素。
2) p:last-of-type 选择属于其父元素的最后 <p> 元素的每个 <p> 元素。
3) p:only-of-type 选择属于其父元素唯一的 <p> 元素的每个 <p> 元素。
4) p:only-child 选择属于其父元素的唯一子元素的每个 <p> 元素。
5) p:nth-child(2) 选择属于其父元素的第二个子元素的每个 <p> 元素。
6) :enabled :disabled 控制表单控件的禁用状态。
7) :checked 单选框或复选框被选中。
CSS3有哪些新特性?
1) CSS3实现圆角(border-radius),阴影(box-shadow),
2) 对文字加特效(text-shadow、),线性渐变(gradient),旋转(transform)
3) transform:rotate(9deg) scale(0.85,0.90) translate(0px,-30px) skew(-9deg,0deg);// 旋转,缩放,定位,倾斜
4) 增加了更多的CSS选择器 多背景 rgba
5) 在CSS3中唯一引入的伪元素是 ::selection.
6) 媒体查询,多栏布局
7) border-image
伪元素选择器
selection |
[CSS4]应用于文档中被用户高亮的部分(比如使用鼠标或其他选择设备选中的部分)。(IE8及以下不支持)(火狐-moz-selection) |
first-line |
选择每个 <p> 元素的首行,并为其设置样式。 |
first-letter |
选择每个 <p> 元素的首字母,并为其设置样式。 |
before |
在每个 <p> 元素的内容之前插入新内容。(IE7及以下不支持) |
after |
在每个 <p> 元素的内容之后插入新内容。(IE7及以下不支持) |
cue |
匹配所选元素中的WebVTT提示。这可以用于在VTT轨道的媒体中使用字幕和其他线索。 |
backdrop |
参见MDN相关介绍(nDos翻译)(兼容性很差不推荐使用) |
placeholder |
(兼容性很差不推荐使用) |
marker |
::marker伪元素 选择列表的前置标记,一般包含着重符号或数字。任何设置 display: list-item属性的元素或伪元素都能使用::marker 比如 <li> 和<summary> 元素。(兼容性很差不推荐使用) |
spelling-error |
表示浏览器标记为不正确拼写的文本段。(目前没有浏览器实现) |
grammar-error |
Used in the browser identified as a syntax error text segment. (There is no browser implementations) |