Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
css selectors css as a basis and focus in, we often use, but do we really fully grasp it?
1. Compatibility selector
- In the following table selector from: first-of-type selector start, css3 selector in IE8 and earlier are not compatible browser
- : Checked selector only Opera browser support
- :: selection selector, IE9 +, Opera, Google Chrome and Safari are supported :: selection selector. Firefox supports an alternative :: - moz-selection.
2. It is worth noting selector
The following selector I think we are likely to be ignored, but it has some value in a particular scene
- : first-letter, effect: https://www.w3school.com.cn/tiy/t.asp?f=css_sel_firstletter
- : first-line, effect: https://www.w3school.com.cn/tiy/t.asp?f=css_sel_firstline
- : target, effect: https://www.w3school.com.cn/tiy/t.asp?f=css_sel_target
- :: selection, effect: https://www.w3school.com.cn/tiy/t.asp?f=css_sel_selection
3. Performance selector
Here the attribute selectors
- Matching attribute selectors do text, efficiency is not so high.
- When using attribute selectors, try to give it to set the scope of the force, if it only took a [href] is equivalent to an element with a href looking at all the elements, the efficiency will be very low. If a [href] will be much better, if .link [href] better. This combination we explain in the next section.
- Attribute selectors very flexible, if we can use CSS instead of JS address some of the needs, the problem can not be too tangled performance by JS achieve still have to spend resources.
Table selector (from the following table w3school )
Selector | example | Examples of description | CSS version |
---|---|---|---|
.class | .intro | Select class = "intro" all the elements. | 1 |
#id | #firstname | Select all elements = "firstname" the id. | 1 |
* | * | Select all the elements. | 2 |
element | p | Select all <p> element. | 1 |
element,element | div,p | Select all the <div> element and all <p> elements. | 1 |
element element | div p | Select <div> inside all of the elements <p> element. | 1 |
element>element | div>p | All <p> element is selected parent element <div> element. | 2 |
element+element | div+p | Select immediately after the <div> element in all <p> elements. | 2 |
[attribute] | [target] | Select all elements with a target attribute. | 2 |
[attribute=value] | [target=_blank] | Select target = "_ blank" in all elements. | 2 |
[attribute~=value] | [title~=flower] | Select the title attribute contains the word "flower" of all the elements. | 2 |
[attribute|=value] | [Lang | = one] | Select all the elements in order to "en" lang beginning of the property value. | 2 |
:link | a:link | Select all the links have not been accessed. | 1 |
:visited | a:visited | Select all the links have been visited. | 1 |
:active | a:active | Select the active link. | 1 |
:hover | a:hover | Select which link the mouse pointer is on. | 1 |
:focus | input:focus | Select the input element has focus. | 2 |
:first-letter | p:first-letter | Select the first letter of each <p> element. | 1 |
:first-line | p:first-line | Select the first line of each <p> element. | 1 |
:first-child | p:first-child | Select each <p> element belonging to the first child of the parent element of the element. | 2 |
:before | p:before | Insert content before the content of each <p> element. | 2 |
:after | p:after | Insert after the content of each <p> element. | 2 |
:lang(language) | p:lang(it) | Select each <p> element with a lang attribute value "it" in the beginning. | 2 |
element1~element2 | p~ul | 选择前面有 <p> 元素的每个 <ul> 元素。 | 3 |
[attribute^=value] | a[src^="https"] | 选择其 src 属性值以 "https" 开头的每个 <a> 元素。 | 3 |
[attribute$=value] | a[src$=".pdf"] | 选择其 src 属性以 ".pdf" 结尾的所有 <a> 元素。 | 3 |
[attribute*=value] | a[src*="abc"] | 选择其 src 属性中包含 "abc" 子串的每个 <a> 元素。 | 3 |
:first-of-type | p:first-of-type | 选择属于其父元素的首个 <p> 元素的每个 <p> 元素。 | 3 |
:last-of-type | p:last-of-type | 选择属于其父元素的最后 <p> 元素的每个 <p> 元素。 | 3 |
:only-of-type | p:only-of-type | 选择属于其父元素唯一的 <p> 元素的每个 <p> 元素。 | 3 |
:only-child | p:only-child | 选择属于其父元素的唯一子元素的每个 <p> 元素。 | 3 |
:nth-child(n) | p:nth-child(2) | 选择属于其父元素的第二个子元素的每个 <p> 元素。 | 3 |
:nth-last-child(n) | p:nth-last-child(2) | 同上,从最后一个子元素开始计数。 | 3 |
:nth-of-type(n) | p:nth-of-type(2) | 选择属于其父元素第二个 <p> 元素的每个 <p> 元素。 | 3 |
:nth-last-of-type(n) | p:nth-last-of-type(2) | 同上,但是从最后一个子元素开始计数。 | 3 |
:last-child | p:last-child | 选择属于其父元素最后一个子元素每个 <p> 元素。 | 3 |
:root | :root | 选择文档的根元素。 | 3 |
:empty | p:empty | 选择没有子元素的每个 <p> 元素(包括文本节点)。 | 3 |
:target | #news:target | 选择当前活动的 #news 元素。 | 3 |
:enabled | input:enabled | 选择每个启用的 <input> 元素。 | 3 |
:disabled | input:disabled | 选择每个禁用的 <input> 元素 | 3 |
:checked | input:checked | 选择每个被选中的 <input> 元素。 | 3 |
:not(selector) | :not(p) | 选择非 <p> 元素的每个元素。 | 3 |
::selection | ::selection | 选择被用户选取的元素部分。 | 3 |