CSS Selectors

A CSS selector is a way for you to select HTML elements, and style them.

CSS Selectors

CSS selectors are used to "find" (or select) HTML elements you want to style.

We can divide CSS selectors into five categories:


The CSS element Selector

The element selector selects HTML elements based on the element name

Example

Here, all <p> elements on the page will be center-aligned, with a red text color:

                        
                            p {
                                text-align: center;
                                color: red;
                              }
                        
                    

The CSS id Selector

The id selector uses the id attribute of an HTML element to select a specific element.

The id of an element is unique within a page, so the id selector is used to select one unique element.

To select an element with a specific id, write a hash (#) character, followed by the id of the element.

Example

The CSS rule below will be applied to the HTML element with id="para1":

                        
                            #para1 {
                                text-align: center;
                                color: red;
                              }
                        
                    

Note: An id name cannot start with a number!


The CSS class Selector

The class selector selects HTML elements with a specific class attribute.

To select elements with a specific class, write a period (.) character, followed by the class name.

Example

In this example all HTML elements with class="center" will be red and center-aligned:

                        
                            .center {
                                text-align: center;
                                color: red;
                              }
                        
                    

You can also specify that only specific HTML elements should be affected by a class.

Example

In this example only <p> elements with class="center" will be red and center-aligned:

                        
                            p.center {
                                text-align: center;
                                color: red;
                              }      
                        
                    

HTML elements can also refer to more than one class.

Example

In this example only <p> element will be styled according to class="center" and to class="large":

                        
                            <p class="center large">This paragraph refers to two classes.<p/>
                        
                    

Note:A class name cannot start with a number!


The CSS Universal Selector

The universal selector (*) selects all HTML elements on the page.

Example

The CSS rule below will affect every HTML element on the page:

                        
                            * {
                                text-align: center;
                                color: blue;
                              }
                        
                    

The CSS Grouping Selector

The grouping selector selects all the HTML elements with the same style definitions.

Look at the following CSS code (the h1, h2, and p elements have the same style definitions):

                    
                        h1 {
                            text-align: center;
                            color: red;
                          }
                          
                          h2 {
                            text-align: center;
                            color: red;
                          }
                          
                          p {
                            text-align: center;
                            color: red;
                          }
                    
                

It will be better to group the selectors, to minimize the code.

To group selectors, separate each selector with a comma.

Example

In this example we have grouped the selectors from the code above:

                    
                        h1, h2, p {
                            text-align: center;
                            color: red;
                          }
                    
                

CSS Specifity

Specificity is the algorithm browsers use to determine the CSS declaration that is the most relevante to an which in turn, determines the property value to apply to the element. The specificity algorithm calculates the weight of a CSS selector to determine which rule from competing CSS declarations gets applied to an element.

How is specifity calculated?

Specificity is an algorithm that calculates the weight that is applied to a given CSS declaration. The weight is determined by the number of selectors of each weight category in the selector matching the element (or pseudo-element). If there are two or more declarations providing different property values for the same element, the declaration value in the style block having the matching selector with the greatest algorithmic weight gets applied.

The specificity algorithm is basically a three-column value of three categories or weights: id, class, and type - corresponding to the three types of selectors. The value represents the count of selector components in each weight category and is written as ID - CLASS - TYPE. The three columns are created by counting the number of selector components for each selector weight category in the selectors that match the element.

Selector weight categories

The selector weight categories are listed below in the order of specificity:

Selector Weights
Selector name Description Selector weight
ID Includes only ID selectors, such as #example It's weight value is 1-0-0
Class Includes class selectors, such as .myClass, and pseudo-classes, such as :hover It's weight value is 0-1-0
Type Includes type selectors, such as p, h1, and td, and pseudo-elements It's weight value is 0-0-1