英文教程:五种CSS选择器类型


CSS commands are usually grouped in the curly braces to make a set of rules. Following are the various ways available to attach these set of rules with HTML code.

Selector (in simple words) means how you name these set of rules.

1 CLASS SelectORS

Class selectors is the simplest form of selectors where you assign your own meaningful name to the set of CSS rules. To create a class selector you simply need to write name of the class followed by a period.
(A class name cannot start with a number or a symbol as it is not supported by various browsers.)
For example,

p.big { font-weight:bold; font-size:12px; }   
.center { text-align:center; }  

And this HTML :

<p class="big"> This paragraph will be rendered bold. www.phpstudy.net</p>

You can apply more than one class to a given element.
And this HTML:

<p class="center big"> This paragraph will be rendered bold. </p>  
 
In the above example .big and .center are name of CSS classes and these classes are applied to P tag in HTML.   
If class name is followed by HTML element in your CSS code like p.big in above example it means that this class will work on P tag only.
Otherwise you can apply the CSS class on any element.
It’s a good practice to add HTML element before class name in CSS if you are writing CSS rules for a particular element (It adds more clarity to CSS code. 

2 ID SelectORS 

ID selectors work like class selectors, except that they can only be used on one element per page because they work with ID of the html element. The id selector is defined as id of the HTML element followed by a # symbol.
 For example,

p#navigation { width:12em; color:#666; font-weight:bold; }  

And this HTML :

<p id="navigation"> This paragraph will be rendered bold. www.phpstudy.net</p> 
  
 As a good practice ID selectors must be used if you are writing the CSS code for a single HTML element only. ID selectors are well supported across standards-compliant browsers. 

3 TAG SelectOR 

Tag selector is another simple method of CSS rules implementation. You can use this selector to redefine the rules for a particular HTML element.
For example:

p {color:#999; font-weight:bold;} 

In the above example CSS code will be automatically applied on every p tag.  

4 DESCENDENT SelectORS 

Descendent selectors specify that styles should only be applied when the element in question is a descendent (for example, a child, or a grandchild) of another element.
For example,

h3 em { color:white; background-color:black; }

And this HTML :

<h3> This is <em>emphasized</em> www.phpstudy.net</h3>
 
In the above example em is descendent of h3 element. Above css rule will automatically be applied on all em elements inside h3 element in the HTML code. Descendent selectors are well supported across standards-compliant browsers. 

5 GROUPING SelectORS 

You can also specify the same set of rules for more than one selctor, like this: 

p, h1, h2 { text-align: left; }

Just place a comma between each one.  
You can even get more complex, and group multiple class and id selectors:

p.navigation, h1#title { font-weight : bold; } 

« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3