CSS Magic Hall: selector and its priority

Source: Internet
Author: User

First, preface

First look at a Ali this issue of the network!

1. Find the following selectors with the same priority level

A. Img.thumb:after b.[data-job= "Frontend"]::first-letter

C. #main:: Before D. [type= "checkbox"]:checked E. ul#shop-list

Second, review selector type

HTML fragment

<body>  <div id="content">    <div>hello world</div>    <div class= "Block-end" >EOF</div>  </div></body>

Basic Selector

1. Tag Selector

div{  color:red;}

2. Class selector

. block-end{  color: #;}

3. ID Selector

#content {  color:blue;  }

4. Universal element Selector, matching any element (IE5.5 not supported)

*{  width:1000px;}

Combo Selector

1. Group Selector

/**/Div, body{  width:1200px;}

Applies the same style to DOM elements obtained by N selectors, reducing duplicate code.

2. Descendant selector (derived selector)

/** *body div{  color: #888;}

All DIV elements under the body apply the above style.

3. Child element Selector (IE5.5~ie6 not supported )

/** /> div{  color: #111;}

Only the body's div child element applies the above style.

4. Adjacent Brother Element Selector (formerly adjacent sibling element selector)(ie5.5~ie6 not supported )

/** *+ div{  color: #111;}

The above style is applied only to the sibling div immediately following the div#content, if such as <div id="content"></div><br/> <div id= "Block-end" >EOF</div> , since Div#block-end is not immediately following div#content, the above style is not applied.

5. Brother Element Selector (formerly Brother selector) (ie5.5~IE6 not supported )

/** /~ div{  Color: #222;}

The above style is applied to the div#content only sibling Div, such as <div id= "content" ></div><br/><div id= "Block-end" >eof </div> , Div#block-end will apply the above style.

Property Selector

1. Base selector [attr] (eg: Div[id] ) (ie5.5~IE6 not supported )

Matches all elements that have this attribute and conform to the underlying selector condition.

2. Base selector [Attr=value] (e.g. div[id=content] )(ie5.5~IE6 not supported )

Matches all attr attribute values equal to value and conforms to the elements of the underlying selector condition.

3. base Selector [attr~=value] (e.g. div[val~=content] )(ie5.5~IE6 not supported )

Matches elements such as <div val= "Content dir" ></div>, that is, the match attribute value has multiple space-delimited values, where one value equals the element of value.

4. Base selector [Attr|=value] (e.g. div[lang|=en] )(ie5.5~IE6 not supported )

Matches an element such as <div lang= "en-us" ></div>, which is the element that matches the value of a property to the beginning of either value or value-.

5. Base selector [Attr^=value] (e.g. Div[id^=cont] )(ie5.5~IE6 not supported )

Matches elements such as <div id= "content" ></div> or <div id= "Contdummy" ></div>, which is the element that matches the value of the attribute that begins with value.

6. Base selector [Attr$=value] (e.g. div[id$=tent] )(ie5.5~IE6 not supported )

Matches elements such as <div id= "content" ></div> or <div id= "Dummytent" ></div>, which is the element that matches the attribute value ending with value.

7. Base selector [Attr*=value] (e.g. Div[id*=ten] )(ie5.5~IE6 not supported )

Matches elements such as <div id= "content" ></div> or <div id= "dummytent" ></div> that match attribute values with value.

Pseudo class Selector

We can subdivide the pseudo-class selector again to remember!

structural pseudo-class selectors

1. E:first-child: When Element E is the first child element under its parent element, the match succeeds. (ie5.5~IE6 not supported )

2. E:root: When element E is the root element of the document (that is, the htmlhtmlelement element), the match succeeds. (IE is not supported )

3. E:nth-child (n): The match succeeds when element E is the nth child element under its parent element. n starting from 1. (IE5.5~IE8 not supported )

4. E:nth-last-child (n): As with the E:nth-child (n) effect, it is just a reverse direction traversal. n starting from 1. (IE5.5~IE8 not supported )

5. E:nth-of-type (n): The match succeeds when element E is the nth of the homogeneous label under its parent element. n starting from 1. (IE5.5~IE8 not supported )

6. E:nth-last-of-type (n): The same as the E:nth-last-of-type (n) effect, except that the reverse direction is traversed. n starting from 1. (IE5.5~IE8 not supported )

7. E:last-child: When the E element is the last child element under its parent element, the match succeeds. (IE5.5~IE8 not supported )

8. E:first-of-type: Same as E:nth-of-type (1). (IE5.5~IE8 not supported )

9. E:last-of-type: Same as E:nth-last-of-type (1). (IE5.5~IE8 not supported )

10.e:only-child: When the E element is the only child element under its parent element, the match succeeds. (IE5.5~IE8 not supported )

11.e:only-of-type: The match succeeds when the E element is the only child element of the label type under its parent element. (IE5.5~IE8 not supported )

12.e:empty: When the E element does not contain any child elements, the match succeeds. Note: Text nodes are also considered child elements. (IE5.5~IE8 not supported )

Note: The legal notation for N in Nth-child (n) is as follows

①. Pure number, specify the position index, index starting from 1;

②.N, specifying that all positional indexes are matched, n will automatically start from 1, and it is unclear when it stops;

③. Pure digit n, such as 2n, specifies the element of the position index of the number of spouses , N will automatically start from 1 to grow, as to when to stop is unknown;

④. Pure Digital n +/-pure digits, such as 2n+1, specify elements that match an odd position index , and n will automatically start from 1, and it is unclear when the stop is.

Dynamic pseudo class selector (e:hover,ie5.5~7 is only supported on element A, ie8+ support is used on other elements )

1. E:link: When element E (a tag) is not clicked, the match succeeds.

2. e:visited: When the element E (a tag) hits obsolete, the match succeeds.

3. E:hover: Must be declared after E:link, e:visited to be valid. The match succeeds when the element E (ie5.5~7 is a tag, ie8+ can be a different label ) is being hovered by the mouse.

4. E:active: Must be declared after e:hover to be valid. The match succeeds when the mouse has been pressed on the element e (a label), but the left button is not released.

5. E:focus: The match succeeds when element e (element e must be the element that can receive the keyboard or user input) gets focus. (IE8 Standard mode starts to support )

Language : lang pseudo-class selector (ie5.5~8 not supported )

1. E:lang (c): When the element's lang attribute equals C, the match succeeds.

UI Element State Pseudo-class selector (ie5.5~8 not supported )

1. e:enabled: When element E (E is a FORM element) is available, the match succeeds.

2. E:disabled: Element E (E is a FORM element) is not available, the match succeeds.

3. e:checked: When the element E (E is input[type=radio] or Input[type=checkbox] is hooked, the match succeeds.

negation pseudo class selector (ie5.5~8 not supported )

1. E:not (other types of selectors): When element E does not conform to other types of selector conditions, the match succeeds.

e:target pseudo class selector ( IE is not supported at all: Target pseudo class, another small problem is opera does not support the selector when using the forward and Back buttons )

1. E:target: When you use an anchor point (fragment identifier fragment identifier), for example, http://www.smashingmagazine.com/2009/08/02/bauhaus- ninety-years-of-inspiration/#comments, this "#comments" is a fragment identifier, and you can use the: Target pseudo-class to control the object's style.

:: Selection Pseudo-class selector (ie5.5~8 not supported )

1. E::selection: The element part of the user selection

Pseudo element Selector

A pseudo-element is an element that the browser automatically adds before, after a matching match, or as part of the capture of a matching element.

1. E:before (new E::before): Used to add content before element E (IE8 Standard mode start support )

2. E:after (new E::before): Used to add content to element E (IE8 Standard mode start support )

Example:

<style type="text/css">div:before{  "beforediv " }div:after{  "afterdiv"}</style><div> Content</div>

The effect is: beforedivcontentafterdiv

3. E:first-line (new E::first-line): valid only when Element E is a block-level element. Used to add a special style to the first line of text in element E.

4. E:first-letter (new E::first-letter): To add a special style to the first letter of element E

5. E:input-placeholder: Used to add a special style to the placeholder of element E. Because it is not a specification, you need to add a browser vendor prefix, specifically as follows

/* For-old version of FF */ input:-moz-input-placeholder{}input:-ms-input-placeholder{}/*forModern Browsers*/Input::-webkit-input-placeholder{}input::-moz-input-  placeholder{}

And only the following properties are available: color, font-size, Font-style, Font-weight, letter-spacing, line-height, padding, text-align, Text-decoration.

Iii. priority scoring rules for selectors

The priority is to determine the effect of the same style rule for different selectors on the same element, with high precedence overriding the low-priority style rule. And the priority is influenced by the style source and selector particularity, let's learn the following together.

1. Source

Inline styles > in-page styles > External reference styles > Browser default styles

2. particularity (by four latitude combination)

Selector type Latitude (a,b,c,d)
inline style 1,0,0,0
ID Selector 0,1,0,0
Class or Property Selector 0,0,1,0
Label or pseudo class selector 0,0,0,1

                  Note: When multiple selector types are available, the same bit overlay will give you a total latitude.

3. The process of calculating the priority level

(a). Calculate the total latitude based on the selector type first

(b) If the latitude is the same, the source is compared.

(c) If the previous two are the same, then the higher priority is declared later.

4. Increase priority through!important (ie5.5~6 not supported )

You can increase the style rule to the highest priority by adding the!important keyword after the style rule.

. test{  !  important;  Color:blue;}

Ie5.5~6, the text of the matching element is blue, while the other browser is the color, and red is displayed under ie7+.

Iv. Browser resolution selector sequence--key selector (keyword query)

is to parse the selector from right to left, which makes the efficiency even higher.

Five or one to do the title Wood bar!

A. Img.thumb:after, total latitude is 0,0,1,1

b.[data-job= "Frontend"]::first-letter, the total latitude is 0,0,1,0

C. #main:: Before, total latitude is 0,1,0,0

D. [type= "checkbox"]:checked, total latitude is 0,0,1,1

E. ul#shop-list, total latitude is 0,1,0,1

Because the topic assumes that the styles are from the same source, the same style precedence is a and d.

Vi. references

CSS selector http://www.cnblogs.com/liontone/archive/2010/12/29/1920437.html

Http://www.w3school.com.cn/cssref/css_selectors.asp

Http://blog.163.com/[email protected]/blog/static/1804698120134491240171/

http://blog.csdn.net/stationxp/article/details/38736261

Vii. Summary

Selectors are a bridge linking labels and style rules, and familiarity with them and browser support and appropriate pits will facilitate our development.

Respect the original, reprint please indicate from: http://www.cnblogs.com/fsjohnhuang/p/3940732.html ^_^ Fat Boy John

CSS Magic Hall: selector and its priority

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.