Skip to content
Related Articles

Related Articles

Improve Article
CSS Introduction
  • Difficulty Level : Easy
  • Last Updated : 20 May, 2021

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 the 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 a 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 an offline cache. Using this we can view offline websites.

CSS Syntax:
A CSS comprises 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:
 

CSS




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 
 

CSS




* { 
   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) :
 

CSS




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, the style rule will apply to the em element only when it lies inside the ul tag.
 

CSS




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

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:

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

html




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

6. GROUPING SELECTORS

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

CSS




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:
 

CSS




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



Before CSS:

html




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

html




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

CSS




<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



My Personal Notes arrow_drop_up
Recommended Articles
Page :