Selectors in css

CSS Selectors are used to target an HTML Element or group of elements based on selector we are using. CSS Selectors are divided as Simple Selectors, Combinators Selectors and Pseudo Selectors. Selectors are backbone of css. The latest selectors in css are called css3 selectors introduced with HTML5.

css selectors
CSS Selector

After selector, a declaration block is used within curly brackets {}. Within {}, css code for targeted element is written with key:value pair. Sometimes values are there, so white space is the separator. After Property:value, semi-colon ; is used to add next property.


Type of CSS selectors

There are many type of css selectors like Simple Selectors, Combinators Selectors and Pseudo Selectors. Here is a list of popular css selectors used in css2. css3 selectors are defined in css3 tutorial.

Css selectors types


CSS Selectors List

CSS Selectors List
SelectorExampleUse
Tag Selectorp{}Used to call all p tags
ID Selector#para{}Used to call that unique element with id para.
Class Selector.para{}Used to call group of different elements with class para
Tag with Class Selectorp.para{}Used to call only p elements with class para
Grouping Selectorh1, h3, h5{}Used to group <h1>, <h3> <h5> elements
Descendant Selectordiv p{}Used to call all p elements of div, i.e, children, grand children and so on.
Child Selectordiv > p{}Used to call only child p elements of div, not grand and great grand.
Attribute Selectorinput[type="radio"]Used to call an element on the basics of attribute and value.
Universal Selector*{}Used to call all elements in web document.

Tag Selectors

Tag selector or Element type Selector is the first Major selector in css. Tag Selectors are LEVEL 1 selectors in css. Any html element can be accessed in css using their tag name. Tags can be used more than once, so all elements will be called.

Tag Selector in css

This heading will be red.

This text will be blue color and center align.

Another text will be blue color and center align.


<style>
	h3{ color:red}
	p{ color:blue; text-align:center}
</style>

    <h3>This heading will be red.</h3>
    <p>This text will be blue and center align.</p>
    <p>This text will be blue and center align.</p>

ID Selectors


ID Selector is used to call an HTML Element by its unique id name. Id is always unique in a single web page. We can not give same ID name to any other HTML Element in same webpage.

ID is basically an attribute used in opening or start tag of html element. Inside double quotation, the value of ID is given. ID value is single, means no white space separation

In css, id selector is called using HASH ( #), followed by id name. See example

Id Selector Example

This heading will be green.

This text will be red and background yellow.

i am a paragraph without id


<style>
    #head1{ color:green}
    #para{ color:red; background:yellow}
    p{ color:blue}
</style>

    <h3 id="head1">This heading will be green.</h3>
    <p id="para>This text will be red and background yellow.</p>
    <p>i am a paragraph without id</p>

Class Selectors


Class Selector in css is used to call all html elements with same class name.
Class represents a group of different or same html elements. We can give same class name to two or more different HTML Elements.

Class is basically an attribute used in Opening or Start Tag. Inside double quotation, the value of class is given.

In css, class is called using DOT ( .), followed by class name. See example

Class Selector Example

This heading will be red.

This heading will be red .

p tag with class para.

P tag with class para and text-italic.


<style>
    .head{ color:red}
    .para{ color:white; background:blue}
    .text-italic{ font-style:italic}
</style>

    <h5 class="head">This heading will be red.</h5>
    <h3 class="head">This heading will be red.</h3>
    <p class="para">P tag with class para.</p>
    <p class="para text-italic">P tag with class para and text-italic.</p>

Tag with Class

CSS Class Selector can also be used with element or tag. This can increase class specificity or importance. See example

h4 Tag with lead

P Tag with lead


<style>
    .lead{ color:red }      /* all lead class*/
    p.lead{ color:blue }    /* all p with lead class*/
</style>
   
    <h4 class="lead">h4 Tag with class lead</h4>    
    <p class="lead">P Tag with class lead</p>    

Grouping Selector


Grouping is used to call a group of HTML Elements by tagname, classname or id.
Unlike Class selector, we don't need to create attribute first.
We can group multiple tags, IDs and Classes. We have to use COMMA (,) to separate Selectors in grouping.

Grouping Selector in css

This heading will be blue and center aligned.

This heading will be blue and center aligned.

This para will be blue and center aligned.


<style>
    h5, h3, p{ 
	   color:blue; 
	   text-align:center;
    }
</style>
	<h5>This heading will be red.</h5>
	<h3>This heading will be red too.</h3>
	<p class="para>This para will be red too.</p>

Nesting in CSS


CSS Nesting is used to call nested element, means child selector or descendant selector from parent. There are two selectors in nesting, child selector and descendant selector.

Descendant Selector

Descendant Selector is used to call children, grand children, great grand children and so on from parent node. Means all elements inside parent element can be called using descendant selector

p tag in header

p tag in subheader


<style>
.header p{ color:red; }      /* Descendant Selector*/
</style>       
   
<div class="header">
    <p>p tag in header</p>
    <div class="subheader">
        <p>p tag in subheader</p> 
    </div>  
</div> 
    

Child Selector

Child selector is used to call child or children of that elements only. Means grand child and great great grand child are not accessible. See example

p tag in header

p tag in subheader

p tag in header


<style>
.header > p{ color:red; }      /* Child Selector*/
</style>       
   
<div class="header">
    <p>p tag in header</p>
    <div class="subheader">
        <p>p tag in subheader</p> 
    </div> 
    <p>p tag in header</p> 
</div>       
    

CSS Child Vs Descendant Selector

This para is inside div.

This para is outside div.

		
<style>
p{ color:red}                 /* all p elements*/
.header p{ color:red; }       /* Descendant Selector*/
.header > p{ color:blue; }    /* Child Selector*/
</style>

<div class="header">
    <p>This para is inside div.</p>
</div>
	
<p>This para is outside div.</p>

Attribute Selector


Attribute Selector select an html element based on their attributes name or value. Brackets [] are used to write attribute or attribute with value.

CSS Attribute Selector






<style>
    input[ type ]{ border:2px solid blue;}
    input[ type="text"]{ background-color:lightblue;}
    input[ type="email"]{ background-color:lightpink;}
</style>
	
    <input type="text">
    <input type="email">

Universal Selector * Asterisk


Universal Selector Asterisk * is used to call all html elements in css. This is a grouping of all html elements including html, head , body etc.

CSS universal Selector

This is Heading 1

This is Heading 3

This is a para

		
<style>
    *{ margin:0}
</style>
	
    <h1>This is Heading 1</h1>
    <h2>This is Heading 2</h2>
    <p>This is a para</p>

CSS Selector Specificity

css selector specificity
CSS Selectors Specificity

CSS Selectors Specificity is calculated on the basis of selector used. Tag selectors is having a value of 1, class selectors is having value of 10, id selector is having value of 100 and inline css is having value of 1000.
!important rule is having maximum value, means no one can break priority of important.


<style>       
    p{ }                        //  value is 1
    div p{ }                    //  value is 1+1, i.e. 2
    .para{ }                    //  value is 10
    p.para{ }                   //  value is 10+1, i.e. 11
    #para{ }                    //  value is 100
    p#para{ }                   //  value is 101
    p{ color:red !important; }  //  value is infinite
</style>

!important

!important rule gives 100% importance to given property. Thus important rule will override all selectors specificity. Only important cab break important. Use Important with caution as even JavaScript cannot change important.

For Detailed list of CSS3 Selectors, click here CSS3 Selectors