CSS Introduction

Course Navigation

Cascading Style Sheets, fondly referred to as CSS, is a simply designed language intended to simplify the process of making web pages presentable. CSS allows you to apply styles to web pages. More importantly, CSS enables you to do this independent of the HTML that makes up each web page.
CSS is easy to learn and understood but it provides powerful control over the presentation of an HTML document.

WHY CSS?



  • CSS saves time : You can write CSS once and reuse same sheet in multiple HTML pages.
  • Easy Maintainence : To make a global change simply change the style, and all elements in all the webpages will be updated automatically.
  • Search Engines : CSS is considered as clean coding technique, which means search engines won’t have to struggle to “read” its content.
  • Superior styles to HTML : CSS has a much wider array of attributes than HTML, so you can give a far better look to your HTML page in comparison to HTML attributes.
  • Offline Browsing : CSS can store web applications locally with the help of offline catche.Using of this we can view offline websites.
  • CSS Syntax

    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 set consists of a selector and declaration block.

    Selector => h1
    Declaration => {color:blue;font size:12px;} 
    • The selector points to the HTML element you want to style.
    • The declaration block contains one or more declarations separated by semicolons.
    • Each declaration includes a CSS property name and a value, separated by a colon.

      For Example:
      -> color is property and blue is value.
      -> font size is property and 12px is value.

    • A CSS declaration always ends with a semicolon, and declaration blocks are surrounded by curly braces.

    Example :

    In the following example all p elements will be center-aligned, with a blue text color:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    CSS Selectors
    CSS selectors are used to “find” (or select) HTML elements based on their element name, id, class, attribute, and more.

    1. THE UNIVERSAL SELECTORS : Rather than selecting elements of a specific type, the universal selector quite simply matches the name of any element type
      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      * { 
         color: #000000; 
      }

      chevron_right

      
      

      This rule renders the content of every element in our document in black.

    2. THE ELEMENT SELECTOR : The element selector selects elements based on the element name. You can select all p elements on a page like this (in this case, all p elements will be center-aligned, with a red text color) :
      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

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

      chevron_right

      
      

    3. THE DESCENDANT SELECTOR : Suppose you want to apply a style rule to a particular element only when it lies inside a particular element. As given in the following example, style rule will apply to em element only when it lies inside ul tag.
      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      ul em {
         color: #000000; 
      }

      chevron_right

      
      

    4. THE ID SELECTOR :
      • The id selector uses the id attribute of an HTML element to select a specific element.
      • The id of an element should be 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.
      • The style rule below will be applied to the HTML element with id=”para1″:
      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

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

      chevron_right

      
      


      The true power of id selectors is when they are used as the foundation for descendant selectors, For example:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      #black h2 {
         color: #000000; 
      }

      chevron_right

      
      

      In this example all level 2 headings will be displayed in black color when those headings will lie with in tags having id attribute set to black.
      NOTE: An id name cannot start with a number.

    5. THE CLASS SELECTORS :
      • The class selector selects elements with a specific class attribute.
      • To select elements with a specific class, write a period (.) character, followed by the name of the class.
      • In the example below, all HTML elements with class=”center” will be red and center-aligned:
      • filter_none

        edit
        close

        play_arrow

        link
        brightness_4
        code

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

        chevron_right

        
        

    You can apply more than one class selectors to given element. Consider the following example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    GROUPING SELECTORS


    If you have elements with the same style definitions, like this:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    It will be better to group the selectors, to minimize the code. To group selectors, separate each selector with a comma. In the example below we have grouped the selectors from the code above:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Before CSS

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html
    <head>
    <title>Example</title>
    </head>
    <body>
    <main>
    <h1>HTML Page</h1>
    <p>This is a basic web page.</p>
    </main>
    </body>
    </html>

    chevron_right

    
    

    After CSS

    In this example we add some CSS.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html
    <head>
    <title>Example</title>
    <style>
    main {
      width: 200px;
      height: 200px;
      padding: 10px;
      background: beige;
      }
    h1 {
      font-family: fantasy, cursive, serif;
      color: olivedrab;
      border-bottom: 1px dotted darkgreen;
      }
    p {
      font-family: sans-serif;
      color: orange;
      }
    </style>
    </head>
    <body>
    <main>
    <h1>HTML Page</h1>
    <p>This is a basic web page.</p>
    </main>
    </body>
    </html>

    chevron_right

    
    

    All we did was add the following code to the example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <style>
    main {
      width: 200px;
      height: 200px;
      padding: 10px;
      background: beige;
      }
    h1 {
      font-family: cursive;
      color: olivedrab;
      border-bottom: 1px dotted darkgreen;
      }
    p {
      font-family: sans-serif;
      color: orange;
      }
    </style>

    chevron_right

    
    

    CSS Versions

    1. CSS1
    2. CSS2
    3. CSS3
    4. CSS4
      Version 4 comes with:-

      • CSS-Pro
      • CSS-Mobile


    My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



    Improved By : JyotiGoyal1



    Article Tags :
    Practice Tags :


    12


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.