Author: M Abo Bakar Aslam

Basic Structure

1. Styling Methods

There are three way to use CSS.

  1. Inline CSS
  2. Internal/Embeded CSS
  3. External CSS

2. Styling Using Inline CSS

Inline CSS, also known as “inline styles” is a way to apply CSS directly to individual HTML elements within an HTML document. Unlike external and internal/embedded stylesheets, which apply styles globally to multiple elements, inline CSS is specific to a single element. It’s defined within the HTML tag itself, using the style attribute.

Example Code 1: color property by using inline-css

<!--File: myFile.html-->
<p style="color: blue;">This is a paragraph with inline CSS.</p>

Output

This is a paragraph with inline CSS.

Example Code 2: background-color property by using inline-css

<!--File: myFile.html-->
<div style="background-color: #D8B08C;">This has a yellow background.</div>

Output

This has a yellow background.

Example Code 3: font-size property by using inline-css

<!--File: myFile.html-->
<p style="font-size: 24px;">This is a larger paragraph.</p>

Output

This is a larger paragraph.

Example Code 4: font-family property by using inline-css

<!--File: myFile.html-->
<p style="font-family: Comic Sans MS;">This text uses the Arial font.</p>

Output

This text uses font as Comic Sans MS

Example Code 5: text-align property by using inline-css

<!--File: myFile.html-->
<div style="text-align: center; border: 1px solid red; width: 200px;">Centered text</div>

Output

Centered text

3. Styling Using Internal/Embedded CSS

This method applies styles globally to multiple elements. All styles are applied under <style> </style> tags.

Example Code 6: Style all HTML elements in internal/embedded CSS

Note following points in this example.

<!--File: myFile.html-->
<style>
p{
    color: red;
    background-color: black;
}
h1{
    font-size: 24px;
}
</style>
  
<h1>Name</h1>
<p>Introductory paragraph</p>
<p>Contact Information</p>
  
<h1>Education History</h1>
<p>Detail about educational journey</p>

Output

Name

Introductory paragraph

Contact Information

Education History

Detail about educational journey

Example Code 7: Styling using IDs in internal/embedded CSS.

Note following points in this example.

IDs are used to apply styles, but this method is not suggested. It is rarely used.

<!--File: myFile.html-->
<style>
#pID{
    color: red;
    background-color: black;
}
#myID{
    font-size: 24px;
}
</style>
  
<h1 id="myID">Name</h1>
<p id="pID">Introductory paragraph</p>
<p>Contact Information</p>
  
<h1>Education History</h1>
<p id="myID">Detail about educational journey</p>

Output

Name

Introductory paragraph

Contact Information

Education History

Detail about educational journey

Example Code 8: Styling using classes in internal/embedded CSS.

Note following points in this example.

Classes are used to apply styles. This method is highly suggested and mostly used.

<!--File: myFile.html-->
<style>
.pID{
    color: red;
    background-color: black;
}
.myID{
    font-size: 24px;
}
</style>
  
<h1 class="myID">Name</h1>
<p class="pID">Introductory paragraph</p>
<p>Contact Information</p>
  
<h1>Education History</h1>
<p class="myID">Detail about educational journey</p>

3. Styling Using External CSS

This method applies styles globally to multiple elements. All styles are applied in separate file having extension as .css

Example Code 9: There are two files. One is myFile.html and other is myStyle.css. Both files are place in same directory. Code for both files are shown below.

Note following points in this example.

  1. The <link> tag is used to link myStyle.css file to current HTML file. It is writteni in header tag because this file is in our computer/server. If this file is in other’s computer/server, then it is suggested to write this link at bottom of HTML file.
  2. The <link> tag is closed properly.
  3. Output will be same as for previous example.
<!--File: myFile.html-->
<header>
    <link rel="stylesheet" href="myStyle.css" />
</header>
<h1 class="myID">Name</h1>
<p class="pID">Introductory paragraph</p>
<p>Contact Information</p>
  
<h1>Education History</h1>
<p class="myID">Detail about educational journey</p>
    /* File: myStyle.css */
.pID{
    color: red;
    background-color: black;
}
.myID{
    font-size: 24px;
}

Example Code 10: Repeat previous example but use IDs instead of classes in both files.

Note following points in this example.

  1. we just replace dot(.) by hash(#) and class by id.
<!--File: myFile.html-->
  
<header>
    <link rel="stylesheet" href="myStyle.css" />
</header>
  
<h1 id="myID">Name</h1>
<p id="pID">Introductory paragraph</p>
<p>Contact Information</p>
  
<h1>Education History</h1>
<p id="myID">Detail about educational journey</p>
/* File: myStyle.css */
#pID{
    color: red;
    background-color: black;
}
#myID{
    font-size: 24px;
}

Preferences

Inline-CSS > Internal-CSS > External-CSS

If you style HTML element in multiple ways, then a method can override the other method’s property.

Example Code 13: Multiple styles to multiple HTML elements are applied here.

Question 1: Why background-color for last paragraph is black, although p is styled as blue background-color?

Question 2: Why text-color for last paragraph is white, although there is no property in mStyle class?

<!--File: myFile.html-->
<style>
p{
    color: white;
    background-color: blue;
}
h1{
    color: red;
}
.mStyle{
    background-color: black;
}
</style>
  
<p>Web Development Using HTML and CSS.</p>
<h1>University of Gujrat.</h1>
<p class="mStyle">Computer Science</p>

Output

Web Development Using HTML and CSS.

University of Gujrat.

Computer Science

Example Code 14: Overriding internal CSS by inline-CSS

Question 1: Why background-color for last paragraph is red, although p is styled as blue background-color in internal CSS?

Question 2: Why text-color for heading is white, although there is no property in mStyle class?

<!--File: myFile.html-->
<style>
p{
    color: white;
    background-color: blue;
}
h1{
    color: red;
}
.mStyle{
    background-color: black;
}
</style>
  
<p>Web Development Using HTML and CSS.</p>
<h1 class="mStyle" style="color: white;">University of Gujrat.</h1>
<p style="background-color: red;">Computer Science</p>

Output

Web Development Using HTML and CSS.

University of Gujrat.

Computer Science

Example Code 15: Overriding external CSS by internal-CSS

Question 1: Why background-color for first paragraph is black, although p is styled as blue background-color in external CSS?

Question 2: Why text-color for heading is orange, although there is no property in mStyle class?

<!--File: myFile.html-->
  
<header>
    <link rel="stylesheet" href="myStyle.css" />
    <style>
        p{
            color: yellow;
            background-color: black;
        }
        h1{
            color: orange;
        }
        .mStyle{
            background-color: black;
        }
    </style>
</header>
  
<h1 class="myStyle">Name</h1>
<p>Introductory paragraph</p>
<p>Contact Information</p>
  
<h1 class="myStyle">Education History</h1>
<p>Detail about educational journey</p>
/* File: myStyle.css */
p{
    color: red;
    background-color: blue;
}
.myStyle{
    font-size: 24px;
}

Output

Name

Introductory paragraph

Contact Information

Education History

Detail about educational journey

Example Code 16: Overriding both internal and external CSS by inline CSS

Question 1: Why background-color for first paragraph is green, although p is styled as blue background-color in external CSS and yellow in internal CSS?

Question 2: Why text-color for last heading is purple, although there is no property in mStyle class?

<!--File: myFile.html-->
  
<header>
    <link rel="stylesheet" href="myStyle.css" />
    <style>
        p{
            color: black;
            background-color: yellow;
        }
        h1{
            color: orange;
        }
        .mStyle{
            background-color: black;
        }
    </style>
</header>
  
<h1 class="myStyle">Name</h1>
<p style="background-color: green;">Introductory paragraph</p>
<p>Contact Information</p>
  
<h1 class="myStyle" style="color: purple; ">Education History</h1>
<p>Detail about educational journey</p>
/* File: myStyle.css */
p{
    color: red;
    background-color: blue;
}
.myStyle{
    font-size: 24px;
}

Output

Name

Introductory paragraph

Contact Information

Education History

Detail about educational journey

:hover pseudo-class

In web development, hover refers to a user interaction where the pointer (usually a mouse cursor) is placed over an element without clicking it. The :hover pseudo-class in CSS allows developers to apply specific styles to an element when it’s hovered over.

Note: It cannot be applied using inline-CSS.

Example Cdoe 17:

<!--File: myFile.html-->
<style>
    p{
        background-color: #D8B08C;
    }
    p:hover{
        opacity: 70%;
    }
    .myParagraph:hover{
        opacity: 100%;
        background-color: #0F6466;
        color: white;
    }
</style>
  
<p>Hover Over Me</p>
<p class="myParagraph">New Paragraph</p>

Output

Hover Effect using External CSS - visualized by edu2skill

Important CSS Properties

Sr.No.Property NamePossible ValuesDescription
1color
black
#fff
using color name
using color code
2background-color
red
#ccc
using color name
using color code
3font-size24pxpixel value
4font-familyVerdana, sans-serifAny font-family
5text-align
center
right
left
Align as center
Align as right
Align as left
6border
1px solid black
2px dashed red
3px dotted blue
1px solid black border
2px dashed red border
3px dotted blue border
7border-radius
40%
10%
15px
Percentage value
Percentage value
Pixel value
8margin
2px
3px 4px
5px 6px 10px 15px
All sides
Top-bottom / left-right
Top right bottom left
9margin-top24pxTop spacing
10margin-bottom24pxBottom spacing
11margin-left24pxLeft spacing
12margin-right24pxRight spacing
13padding-top24pxTop inner spacing
14padding-bottom24pxBottom inner spacing
15padding-left24pxLeft inner spacing
16padding-right24pxRight inner spacing
17padding
2px
3px 4px
5px 6px 10px 15px
All sides
Top-bottom / left-right
Top right bottom left
18text-decoration
underline
none
Apply underline
Remove underline
19line-height1.5Line spacing
20display
none
block
inline-block
flex
Hide element
Block element
Inline-block element
Flex layout
21width
200px
40%
100%
Fixed width
Relative width
Full width
22height
200px
40%
100%
Fixed height
Relative height
Full height
23min-width300pxMinimum width
24max-width700pxMaximum width
25min-height300pxMinimum height
26max-height700pxMaximum height
27order
1
2
3
Position 1
Position 2
Position 3
28float
left
right
none
inherit
Not recommended. Use flexbox.
29position
absolute
fixed
sticky
Avoid excessive use. Used for positioning.
30flex-direction
column
row
row-reverse
Direction of flex items
31flex-wrap
nowrap
wrap
Wrapping behavior
32justify-content
center
flex-start
flex-end
space-between
space-around
Horizontal alignment
33align-items
center
flex-start
flex-end
Vertical alignment
34:hoverChange propertyPseudo-class