Css Introduction
a month ago
6 min read

Css Introduction

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 independently of the HTML that makes up each web page. It describes how a webpage should look: it prescribes colors, fonts, spacing, and much more. In short, you can make your website look however you want. CSS lets developers and designers define how it behaves, including how elements are positioned in the browser.

While html uses tags, css uses rulesets. CSS is easy to learn and understand, 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 Maintenance: 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 versions release years:

CSS Syntax: 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;} 

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

  }

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

  1. ELEMENT SELECTORS: 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;

  }

Output:

  1. DESCENDANT SELECTORS: 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; 

     }

Output:

  1. ID SELECTORS:

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

CSS

#gfg{

    color: green;

    text-align: center;

    }
  1. 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

.gfg{

color: green;

text-align: center;

}
  1. 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;

   }

Output:

CSS EFFESCTS ON HTML ELEMENTS

Before CSS: In this example, we do not add any 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>

Output:

After CSS: In this example, we add some CSS.

html

<!DOCTYPE html>

<html>

<head>

    <title>Example</title>

    <style>

        main {

        width: 600px;

        height: 200px;

        padding: 10px;

        background: beige;

        }

        h1 {

        color: olivedrab;

        border-bottom: 1px dotted darkgreen;

        }

        p {

        font-family: sans-serif;

        color: orange;

        }

    </style>

</head>

<body>

    <main>

        <h1>My first Page</h1>

        <p>This is the first page.</p>

    </main>

</body>

</html>                   

Output

CSS

<style>

     main {

    width: 600px;

    height: 200px;

    padding: 10px;

    background: beige;

    }

     h1 {

    color: olivedrab;

    border-bottom: 1px dotted darkgreen;

    }

     p {

    font-family: sans-serif;

    color: orange;

    }

</style>

Output: