Tutorialspoint.dev

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:

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

    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
      * { 
         color: #000000; 
      }

      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) :
      p {
          text-align: center;
          color: red;
      }

    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.
      ul em {
         color: #000000; 
      }

    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″:
      #para1 {
          text-align: center;
          color: red;
      }


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

      #black h2 {
         color: #000000; 
      }

      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:
      • .center {
            text-align: center;
            color: red;
        }

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

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

    GROUPING SELECTORS

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

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

    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:



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

    class=”alignnone size-full wp-image-373729″ />

    Before CSS

    <!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>

    After CSS

    In this example we add some CSS.

    <!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>

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

    <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>

    CSS Versions

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

      • CSS-Pro
      • CSS-Mobile


This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter