CBSE CS

cbse cs logo

Home

CSS Syntax and selectors

A CSS comprises of style rules that are interpreted by the browser and then applied to the corresponding elements in your document. A style rule is made of three parts −

  • Selector − A selector is an HTML tag at which a style will be applied. This could be any tag like <h1> or <table> etc.

  • Property − A property is a type of attribute of HTML tag. Put simply, all the HTML attributes are converted into CSS properties. They could be colorborder etc.

  • Value − Values are assigned to properties. For example, color property can have value either red or #F1F1F1 etc.

You can put CSS Style Rule Syntax as follows −

selector { property: value; }

Example − You can define a table border as follows −

table{ border :1px solid #C00; }

Here table is a selector and border is a property and given value 1px solid #C00 is the value of that property.

CSS selectors are used to select the content you want to style. Selectors are the part of CSS rule set. CSS selectors select HTML elements according to its id, class, type, attribute etc.

There are several different types of selectors in CSS.

  1. CSS type Selector

  2. CSS Id Selector

  3. CSS Class Selector

  4. CSS Universal Selector

  5. CSS Group Selector

 

1) CSS type Selector

The element selector selects the HTML element by name.

Example:
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <style>  
  5. p{  
  6.     text-align: center;  
  7.     color: blue;  
  8. }   
  9. </style>  
  10. </head>  
  11. <body>  
  12. <p>This style will be applied on every paragraph.</p>  
  13. <p id=“para1”>Me too!</p>  
  14. <p>And me!</p>  
  15. </body>  
  16. </html>    

2) CSS Id Selector

The id selector selects the id attribute of an HTML element to select a specific element. An id is always unique within the page so it is chosen to select a single, unique element.

It is written with the hash character (#), followed by the id of the element.

Let?s take an example with the id “para1”.

 
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <style>  
  5. #para1 {  
  6.     text-align: center;  
  7.     color: blue;  
  8. }  
  9. </style>  
  10. </head>  
  11. <body>  
  12. <p id=“para1”>Hello Javatpoint.com</p>  
  13. <p>This paragraph will not be affected.</p>  
  14. </body>  
  15. </html>   

 

3) CSS Class Selector

The class selector selects HTML elements with a specific class attribute. It is used with a period character . (full stop symbol) followed by the class name.

Note: A class name should not be started with a number.

Let’s take an example with a class “center”.

 
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <style>  
  5. .center {  
  6.     text-align: center;  
  7.     color: blue;  
  8. }  
  9. </style>  
  10. </head>  
  11. <body>  
  12. <h1 class=“center”>This heading is blue and center-aligned.</h1>  
  13. <p class=“center”>This paragraph is blue and center-aligned.</p>   
  14. </body>  
  15. </html>  

4) CSS Universal Selector

The universal selector is used as a wildcard character. It selects all the elements on the pages.

 
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <style>  
  5. * {  
  6.    color: green;  
  7.    font-size: 20px;  
  8. }   
  9. </style>  
  10. </head>  
  11. <body>  
  12. <h2>This is heading</h2>  
  13. <p>This style will be applied on every paragraph.</p>  
  14. <p id=“para1”>Me too!</p>  
  15. <p>And me!</p>  
  16. </body>  
  17. </html>
error: Content is protected !!