CSS Basics

tags:


CSS is acronym of Cascading Style Sheets. It helps to define the presentation of HTML elements as a separate file known as CSS file having .css extension.

CSS helps to change formatting of any HTML element by just making changes at one place. All changes made would be reflected automatically to all of the web pages of the website in which that element appeared. Also, quality website requires a quality web hosting service provider.

CSS Rules

CSS Rules are the styles that we have to create in order to create style sheets. These rules define appearance of associated HTML element. The general form of CSS syntax is as follows:

Selector {property: value;}

Key Points

  • Selector is HTML element to which CSS rule is applied.

  • Property specifies the attribute that you want to change corresponding to the selector.

  • Property can take specified value.

  • Property and Value are separated by a colon (:).

  • Each declaration is separated by semi colon (;).

Following are examples of CSS rules:

P { color : red;}

h1 (color : green; font-style : italic }

body { color : cyan; font-family : Arial; font- style : 16pt}

Embedding CSS into HTML

Following are the four methods to add CSS to HTML documents.

  1. Inline Style Sheets

  2. Embedded Style Sheets

  3. External Style Sheets

  4. Imported Style Sheets

Inline Style Sheets

Inline Style Sheets are included with HTML element i.e. they are placed inline with the element. To add inline CSS, we have to declare style attribute which can contain any CSS property.

Syntax:

<Tagname STYLE =  Declaration1 ; Declaration2>  …. </Tagname>

Let’s consider the following example using Inline Style Sheets:

<p style="color: blue; text-align: left; font-size: 15pt">
Inline Style Sheets are included with HTML element i.e. they are placed inline with the element.
To add inline CSS, we have to declare style attribute which can contain any CSS property.
</p>

Output −

Inline Style Sheet

Embedded Style Sheets

Embedded Style Sheets are used to apply same appearance to all occurrence of a specific element. These are defined in <head> element by using the <style> element.

The <style> element must include type attribute. The value of type attribute specifies what type of syntax it includes when rendered by the browser.

Syntax

<head> <title> …. </title>
   <style type =”text/css”>
      …….CSS Rules/Styles….
   </style>		
</head>

Let’s consider the following example using Embedded Style Sheets:

<style type="text/css">
   p {color:green; text-align: left; font-size: 10pt}
   h1 { color: red; font-weight: bold}
</style>

Embedded Style Sheet

External Style Sheets

External Style Sheets are the separate .css files that contain the CSS rules. These files can be linked to any HTML documents using <link> tag with rel attribute.

Syntax:

<head> <link rel= “stylesheet”  type=”text/css” href= “url of css file>
</head>

In order to create external css and link it to HTML document, follow the following steps:

  • First of all create a CSS file and define all CSS rules for several HTML elements. Let’s name this file as external.css.

p{ 
   Color: orange;     text-align:  left;        font-size: 10pt;
}
h1{ 
   Color: orange;      font-weight: bold;
}
  • Now create HTML document and name it as externaldemo.html.

<html>
   <head>
      <title> External Style Sheets Demo </title>
      <link rel="stylesheet"  type="text/css" href="external.css">
   </head>
   <body>
      <h1> External Style Sheets</h1>
      <p>External Style Sheets are the separate .css files that contain the CSS rules.</p>
   </body>
</html>

External Style Sheet

Imported Style Sheets

Imported Style Sheets allow us to import style rules from other style sheets. To import CSS rules we have to use @import before all the rules in a style sheet.

Syntax:

<head><title> Title Information </title>
   <style type=”text/css”>
      @import URL (cssfilepath)
       CSS rules
   </style>
</head>

Let’s consider the following example using Inline Style Sheets:

<html>
   <head>
      <title> External Style Sheets Demo </title>
      <style>
         @import url(external.css);
      </style>
   </head>
   <body>
      <h1> External Style Sheets</h1>
      <p>External Style Sheets are the separate .css files that contain the CSS rules.</p>
   </body>
</html>

Imported Style Sheet