CSS Introduction

Basic Format of CSS

In CSS we have following things:-

h1{
 color : red;
}

css format

We Can visit following link to see all the CSS Properties :-
MDN CSS Properties

How to Include CSS to our HTML File

We Have three methods to include CSS to HTML :-

  1. Inline CSS :- Writing Styles directly inline on each element. In this every property is separated by Semi-colon( ; ). Its drawback is, in this we have to style every html element.

    <h1 style="color:red; background-color:white;">
      This is the Inline CSS
    </h1>
  2. Internal CSS :- Style is added using the <style>tage in the head section in the same document.It has the drawback that it only add style to only one page.

    <!--This is head section-->
    <head>
        <!--Here CSS is written.>
        <style>
          h1{
            color:red;
            background-color:white;

           }
        </style>
    </head>

    <!--This is body section-->
    <body>
    </body>
  3. External CSS :- Writing CSS in a separate document & linking it with the HTML file. We use <link> tag in "head" tag to link html file to css_file. in link tag "rel" defines what type of relation does our "href" have with html document.

    • "index.html" file

      <!--This is head section-->
      <head>
        <!--Here css_file is linked with html document.-->
        <link rel="stylesheet" href="styles.css">
      </head>

      <!--This is body section-->
      <body>
      </body>
    • "styles.css" file

      h1{
       color:black;
       text-align: center;
      }

    Note :-

    The Cascading Order is given below :-
    Inline CSS > External & Internal CSS > Browser default
    It means Inline CSS has the highest priority and it will override other styles.

Selectors

CSS selectors are like addresses that tell the browser which HTML elements to style. These are used to select a group of elements or a particular element for styling purpose.

Importance order of Selectors :-
ID >class>Element

Types of Selectors

There are different type of selectors :-

CSS Cascading StyleSheets

Qs. :- What is cascade in CSS?
Ans :- The CSS Cascade Algorithm's job is to select CSS decoration in order to determine the correct values for CSS properties.
It is always applied on same element.

cascading properties
Note :- in above example always that style will be aplied which is written in the last.

Selector Specificity

Qs. :- What is Specificity?
Ans :- Specificity is an algorithm that calculates the weight that is applied to a given CSS declaration.
It is a three digit no., Which is calculated by following figure :-

slector sepecificity

In above figure "id" defines the hundredth place, "class" defines the Tenth place & element desides the Unit's place.

It means folling order is used for cascading :-

  1. ID > Class > Element
  2. More Selectors > Less Selectors.
  3. If Specificity = same
    Then apply cascading.


Example :-
selctor_specifity

Inline Specificity

Inline styles are more specific than id.
inline Specificity

QUORA PRACTICE WEBPAGE | DELTA BATCH