Learn CSS
Learn CSS
Audience
This tutorial has been designed to meet the requirements of all
those readers who are keen to learn the basics of CSS.
Prerequisites
This book assumes you have no prior knowledge on Programming
knowledge and assume you are at a beginner level.
All the content and graphics published in this e-book are the
property of PHPBootcamp.com. The user of this e-book is prohibited
to reuse, retain, copy, distribute or republish any contents or a part
of contents of this e-book in any manner without written consent of
the publisher.
contact@phpbootcamp.com
Support
You can reach me for technical discussions and other support
related queries from here.
support@phpbootcamp.com
Free Courses
Learn JAVASCRIPT in 1
Hour
Table of Contents
1. CSS Basics
1 CSS Basics
What is CSS?
CSS stands for Cascading Style Sheet. It is used to describe how the
content should be displayed on the browser, print or screen.
With CSS Language, you can control the layout of the page, color of
the text, size of the font, spacing between the text, width and height
of the elements and complete presentation of the web page.
In Short, CSS handles the look and feel of the web page.
Usage of CSS
CSS are written in a file with extension .css and it is linked into the
HTML page.
Define the style once and then use it any where on your site.
Load the CSS once per page and it will manage the entire page layout
and presentation.
Helps to change the page layout based on the screen the site is
viewed on like Mobile, Tablet or Computer Screen.
CSS helps to separate the presentation work from the HTML page
and the developer can focus on building the content and displayed it
separately.
Global Standards also suggest to use CSS and do not use any HTML
attributes to style the tag.
Reuse the same CSS for multiple WordPress site to have the same
look and feel.
Visit W3.org
History of CSS
CSS was invented by Håkon Wium Lie on October 10, 1994 and
maintained through a group of people within the W3C called the CSS
Working Group.
Without CSS
Without CSS, HTML page will be displayed as per the browser default
formatting and coloring. Most of the time is plain black and white
with some browser defined font size.
With CSS
With CSS, you can add styles to the each elements of the HTML tag.
CSS Syntax
property represents the name of Selectors are the HTML tags that
you want to apply the style on.
Custom selectors are the selectors which name does not matches
with the HTML tag name.
{ } is called as block.
You can separate the each declaration with ; inside the block.
CSS Summary
One of the method of defining the CSS is inside the same HTML
page.
This type of CSS includes is restricted to page level only means you
cannot reuse this code in some other pages.
Benefit of using this internal css is when you want specific changes to
apply for that page level only.
QUICK SYNTAX:
<head>
<style type=”text/css”>
h1 { color: red; }
</style>
</head>
Data written inside the <style> tag is not displayed in the browser
but it is used as instruction to the browser to do something on the
page.
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
h1 { color: red; }
</style>
</head>
<body>
<h1>Heading</h1>
<p>Paragraph Text</p>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</body>
</html>
Live Preview
Exercise 1
Live Preview
Exercise 2
h1 – { font-size: 12 px; }
p – { font-size: 24 px; }
li – { font-size: 48 px; }
Follow the correct syntax to add the internal CSS on the HTML page.
Live Preview
Usage of Comments
You can use the special notation to comment the code inside the
CSS.
HTML comments and CSS comments are not same. Don’t get
confused with the comments in CSS vs comments in HTML.
SYNTAX:
<head>
<style type=”text/css”>
h1 { color: red; }
</style>
</head>
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
/*
h1 { color: red; }
*/
</style>
</head>
<body>
<h1>Heading</h1>
<p>Paragraph Text</p>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</body>
</html>
Live Preview
Exercise 1
Live Preview
Exercise 2
Live Preview
One of the method of defining the CSS is externally which means you
can write CSS inside a file and import it into the HTML page.
External CSS means the CSS is written externally into another file
which is later linked in the page.
Benefit of using this external css is that you have one CSS file that is
included in all the website pages.
By just changing at one place in the CSS it will impact the overall site
design look and feel.
This is one of the best practice to separate the design with the html
tags and store them in a external file and include it in all the HTML
pages.
SYNTAX:
<head>
<!– Make sure styles.css file exists in the same folder –>
<link rel=”stylesheet” type=”text/css” href=”styles.css”
media=”screen” />
</head>
<link> tag is used to link the resource to the HTML page. The
attribute of link tag will let the browser knows what type of resource
it is.
rel attribute is used to tell browser what kind of resource it is. rel =
“stylesheet” means it is a file with CSS inside it.
type attribute tells the type of the content in the file. In this case, it is
text/css
href attribute is similar to <a> tag href to map the location of the file
in the server with the path and filename.
media attribute tells the browser to embed the file for screen
purpose.
Sample Example
index.html
styles.css
styles.css file is linked inside the index.html file with <link> tag in the
<head> section.
FileName: index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<!-- Make sure styles.css file exists in the same folder -->
</head>
<body>
<h1>Heading</h1>
<p>Paragraph Text</p>
</body>
</html>
FileName: styles.css
*{
border-style: solid;
border-color: red;
Live Preview
Exercise 1
Filename: style1.css
h1 { color: red; }
Filename: style2.css
h1 { color: blue; }
Live Preview
Exercise 2
Exercise 2: Change the font size of h1, p and li tags in style.css and
embed the CSS file in the HTML page.
h1 – { font-size: 12 px; }
p – { font-size: 24 px; }
li – { font-size: 48 px; }
Follow the correct syntax to add the external CSS on the HTML page.
Live Preview
Inline CSS overrides all the styles defined in internal CSS and External
CSS.
SYNTAX:
<style> tag is used to write the CSS as a attribute in the HTML tag.
The syntax for CSS is similar in inline, external and internal CSS.
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<!-- style attribute is used to apply the CSS style to any html
tag. -->
<p>Paragraph Text</p>
</body>
</html>
Paragraph Text
Live Preview
Exercise 1
Exercise 1: Write Inline CSS and change the h1 tags color as red,
paragraph as blue and list as green.
Live Preview
Exercise 2
Exercise 2: Change the font size of h1, p and li tags as inline CSS
along with the color.
h1 – { font-size: 12 px; }
p – { font-size: 24 px; }
li – { font-size: 48 px; }
Live Preview
CSS files can be linked together and embed into one another.
Linking one CSS into another CSS file can help to split the
functionality into smaller units.
You can create the multiple CSS file and import the one css file into
another css file.
SYNTAX:
<head>
</head>
Sample Example
• index.html
• mynewstyles.css
• anotherstylesfile.css
FileName: index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<!-- Make sure mynewstyles.css file exists in the same folder -->
</head>
<body>
<h1>Heading</h1>
<p>Paragraph Text</p>
</body>
</html>
FileName: mynewstyles.css
/*
*/
@import "anotherstylesfile.css";
FileName: anotherstylesfile.css
*{
border-style: solid;
border-color: red;
Live Preview
Exercise 1
Live Preview
Exercise 2
h1 – { font-size: 12 px; }
p – { font-size: 24 px; }
li – { font-size: 48 px; }
Follow the correct syntax to add the external CSS on the HTML page.
Live Preview
In this simple example, you will combine all the ways of using CSS
• Internal CSS
• External CSS
• Inline CSS
• Linking CSS
Sample Example
index.html
mynewstyles.css
anotherstylesfile.css
FileName: index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<!-- Make sure mynewstyles.css file exists in the same folder -->
<style type="text/css">
p {
font-size: 20px;
font-weight: bold;
font-style: italic;
text-align: center;
</style>
</head>
<body>
<p>This is a Paragraph Text with 20px Font Size, Bold, Italics and
Aligned Center.</p>
<p>Power of CSS!!!</p>
</body>
</html>
FileName: mynewstyles.css
/*
*/
@import "anotherstylesfile.css";
FileName: anotherstylesfile.css
*{
border-style: solid;
border-color: red;
Live Preview
Exercise 1
Exercise 1: Copy all the style in inline css in a single HTML file
without any external CSS and no <script> tag
Live Preview
Exercise 2
Exercise 2: Copy all the CSS in styles.css file and link it from the
index.html
Live Preview
2. CSS Selectors
2 CSS Selectors
Selector
Declaration
h1, p, li elements will be red in color with this one css rule.
SYNTAX:
<head>
<style type=”text/css”>
h1 { color: red; }
p,
li { color: blue; font-size: 12px }
</style>
</head>
EXAMPLE:
/*
selector
{
property1: value1; => Declaration
property2: value2; => Declaration
}
*/
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
/*
selector
*/
color: red;
</style>
</head>
<body>
<h1>Heading</h1>
</body>
</html>
Live Preview
Exercise 1
Exercise 1: Write the CSS selector and Declaration into another css
and link them in the index.html.
Make all the text on the body blue in color with one CSS rule.
Live Preview
Exercise 2
Exercise 2: Add two declaration for each selector and change the
CSS to see the following output.
Live Preview
Universal selector is the rule that you want to apply for all the
elements of the page.
SYNTAX:
<head>
<style type=”text/css”>
*
{
border-style: solid;
border-color: red;
}
</style>
</head>
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
/*
selector
*/
border-style: solid;
border-color: red;
</style>
</head>
<body>
</body>
</html>
Live Preview
Exercise 1
Live Preview
Exercise 2
Put the universal selector into another css file and change the color
to red
Live Preview
When you find any selector that has rule applied on the HTML tags
then it is called as Type Selector.
SYNTAX:
<head>
<style type=”text/css”>
h1
{
border-style: solid;
border-color: red;
}
</style>
</head>
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
/*
selector
*/
color: red;
h1
color: blue;
p, h1
font-style: italic;
</style>
</head>
</body>
</html>
Live Preview
Exercise 1
Live Preview
Exercise 2
Live Preview
Class Selector is custom selector that you can create in the CSS rules
and apply it to any HTML tag with class attribute.
Class selector names are custom so you should NOT use the
predefined HTML tag names.
Custom Selector names are class selectors that can be applied to any
HTML tag.
Class selector can be defined to any specific HTML tags and applied
to any specify the tags.
If the selector starts with “.” and it name is not HTML tag then it is
called as Class Selector.
SYNTAX:
<head>
<style type=”text/css”>
</style>
</head>
Remove the “.” when the class selector is applied to the HTML tag
attribute.
If you want to create class selector only specific to HTML tags then
use this notation.
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
/*
.selector
*/
.color-red
color: red;
.color-blue
color: blue;
.align-center
text-align: center;
</style>
</head>
<body>
</body>
</html>
Exercise 1
Create one rule called as “.headings” and make the color as blue,
align center and italics.
Create one more rule with same name “.headings” and change the
color to red.
Exercise 2
Exercise 2: Create a class selector only for h1 tag. Even if this class
selector is applied to other tags like p it should not work.
Example:
Live Preview
You can apply the selector to the parent HTML tag and all the child
elements will inherit the property from the parent tags properties.
SYNTAX:
<head>
<style type=”text/css”>
</style>
</head>
<body>
<div class=”redcolor”> <p>This is red color paragraph</p> </div>
</body>
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
selector.class-selector
*/
div.color-red
color: red;
#color-red.align-center
color: red;
text-align: center;
#color-blue .text-underline
color: blue;
text-decoration: underline;
</style>
</head>
<body>
<div class="color-red">
</div>
<hr>
<div id="color-blue">
<h1 class="text-underline">
</h1>
</div>
</body>
</html>
Exercise 1
Exercise 2
Apply the class to h1 tag and see if p tag inside get affected
Apply the class to p tag inside the h1 tag. Write some text inside the
h1 tag.
Apply the class to div tag inside the h1 tag and write p tag inside the
div tag.
2.6 ID Selector
Usage of ID Selector
ID selector names are custom so you should NOT use the predefined
HTML tag names.
If the selector starts with “#” and it name is not HTML tag then it is
called as ID Selector.
SYNTAX:
<head>
<style type=”text/css”>
</style>
</head>
<body>
Remove the “#” when the class selector is applied to the HTML tag
attribute.
If you want to create ID selector only specific to HTML tags then use
this notation.
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
/*
*/
#color-red
color: red;
#color-blue
color: blue;
#align-center
text-align: center;
</style>
</head>
<body>
</body>
</html>
Live Preview
Create one rule called as “#headings” and make the color as blue,
align center and italics.
Create one more rule with same name “#headings” and change the
color to red.
Live Preview
Exercise 2
Example:
You can apply the selector to the parent HTML tag and all the child
elements will inherit the property from the parent tags properties.
SYNTAX:
<head>
<style type=”text/css”>
</style>
</head>
<body>
<div id=”redcolor”> <p>This is red color paragraph</p> </div>
</body>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
/*
selector.class-selector
*/
div.color-red
color: red;
#color-red.align-center
color: red;
text-align: center;
#color-blue .text-underline
color: blue;
text-decoration: underline;
</style>
</head>
<body>
<div class="color-red">
</div>
<hr>
<div id="color-blue">
<h1 class="text-underline">
</h1>
</body>
</html>
Live Preview
Exercise 1
Live Preview
Exercise 2
Apply the class to h1 tag and see if p tag inside get affected
Apply the class to p tag inside the h1 tag. Write some text inside the
h1 tag.
Apply the class to div tag inside the h1 tag and write p tag inside the
div tag.
Attribute selector helps to apply the CSS rule when some specific
condition is met on the HTML attribute.
SYNTAX:
<head>
<style type=”text/css”>
p[class]{
color: red;
}
</style>
</head>
<body>
<p class=”something”>This is RED Color Text</p>
</body>
p[class] this attribute selector tell the browser to apply the rule to
the <p> HTML Tag when it has the class attribute.
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
/*
*/
p[class]{
color: red;
font-weight: bold;
p[class=color-blue]{
color: blue;
/*
*/
</style>
</head>
<body>
</body>
</html>
Live Preview
Exercise 1
Apply the color red to all <h1> tag that has id attribute.
Exercise 2
Live Preview
Child Selector are used to pin point the location of the HTML tag in
the nested HTML sections.
“>” greater than symbol is used to point the location of the HTML
tag.
If you want to apply the rule to <a> anchor tag inside the <p>
paragraph tag then you can use the child selector to apply the style
to only <a> tag inside the <p> tag.
means apply the rule to <a> tag when it is inside the <p> tag.
SYNTAX:
<head>
<style type=”text/css”>
p>a
{
color: red;
}
</style>
</head>
<body>
<p><a href=”#”>This is Red</a></p>
</body>
Sample Example
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
/*
selector>selector
*/
div>a{
color: red;
font-weight: bold;
</style>
</head>
<body>
<hr>
<hr>
<div>
<p>
</p>
</div>
</body>
</html>
Exercise 1
Apply the color red rule to <a> tag when it is inside the <div> and <p>
Live Preview
Exercise 2
Apply the CSS color red when <a> tag is inside the <p> and it has the
class and id attribute only.
Live Preview
Descendant Selector rule is used when you want to apply rule to for
every element under one parent element.
means apply the rule to ALL <a> tag under the <body> tag.
SYNTAX:
<head>
<style type=”text/css”>
</style>
</head>
<body>
<p><a href=”#”>This is Red</a></p>
</body>
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
/*
selector selector
*/
/* All the <a> tag inside the <body> tags are affected. */
body a
color: red;
font-weight: bold;
</style>
</head>
<body>
<hr>
</body>
</html>
Exercise 1
Then make all the <a> tag as red inside the <body>
Exercise 2
Apply the CSS color red when <a> tag is inside the <p> with
descendant and also child selectors.
Adjacent Sibling Selector rule is used when you want to apply rule to
for the first element which is one after the other.
Selectors are separated with ‘+’ (plus) sign to indicate the rule is
applied one after the other
h1 + a { color: red; }
means apply the rule to only the first adjacent sibling <a> tag after
h1.
SYNTAX:
<head>
<style type=”text/css”>
h1 + a
{
color: red;
}
</style>
</head>
<body>
<h1>Test</h1>
<a href=”#”>Red1</a>
<a href=”#”>Red2</a>
</body>
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
/*
selector + selector
*/
/* First <a> tag after the <h1> tags are affected. Not Inside
but after. */
h1 + a
color: red;
font-weight: bold;
</style>
</head>
<body>
<a href="#">About</a>
<hr>
</body>
</html>
Live Preview
Apply this rule only when <a> is adjacent to another <a> tag
Live Preview
Exercise 2
Apply the CSS color red when <a> tag is inside the <p> with
descendant and also child selectors and also with Adjacent Siblings
Selector.
Selectors are separated with ‘~’ (plus) sign to indicate the rule is
applied one after the other.
h1 ~ a { color: red; }
means apply the rule to all the adjacent sibling <a> tag after h1.
It is Adjacent (right after) and sibling and at the same level. All the
next <a> will also be affected
<h1>Test</h1>
<a href=”#”>Red1</a>
<a href=”#”>Red2</a>
SYNTAX:
<head>
<style type=”text/css”>
h1 ~ a
{
color: red;
}
</style>
</head>
<body>
<h1>Test</h1>
<a href=”#”>Red1</a>
<a href=”#”>Red2</a>
</body>
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
/*
*/
h1 ~ a
color: red;
font-weight: bold;
</style>
</head>
<body>
<a href="#">Contact</a>
<a href="#">About</a>
<hr>
</body>
</html>
Live Preview
Exercise 1
Make the all the paragraph bold after the h1 tag which are adjacent.
Exercise 2
Apply the CSS color red when <a> tag is inside the <p> with
descendant and also child selectors and also with Adjacent Siblings
Selector and also the general sibling selector.
Pseudo Class Selector are predefined class that are available to use
for the HTML tags.
For Example, You want to change the color of link when someone
mouse over the link.
This is done by predefined class for the HTML tags called as Pseudo
Class Selectors.
<a> anchor tags has hover, visited pseudo class that we can use.
Pseudo class are separated with “:” along with the HTML tags.
SYNTAX:
<head>
<style type=”text/css”>
</style>
</head>
<body>
<a href=”#”>Hover on me and I will turn red in color</a>
</body>
You don’t have to mention the Pseudo class to the HTML tags
attribute.
These are the properties of the HTML tags that you are changing.
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
/*
*/
a:link
color: blue;
/* Visited Link */
a:visited
color: gray;
a:hover
color: red;
a:active
color: green;
</style>
</head>
<body>
<a href="#3">About</a>
</body>
</html>
Live Preview
Exercise 1
Change the <a> color to red when mouse is over the link.
Apply the Pseudo class only for the first <a> tag after the h1.
Exercise 2
Exercise 2: Apply the hover effect to all the <a> anchor tag only in
<p> tag anywhere in the body.
All the <a> tags outside the <p> are not affected.
Live Preview
Pseudo Elements Selector are rules that you want to add right after
the element is closed. Irrespective of what is there after the tag.
For Example, You want to add “!!!” after every paragraph ending.
Then you can use the Pseudo Element Selector.
Pseudo class are separated with “::” along with the HTML tags.
SYNTAX:
<head>
<style type=”text/css”>
</style>
</head>
<body>
<p>See the !!! right after the paragraph</p>
</body>
You don’t have to mention the Pseudo element to the HTML tags
attribute.
These are the addition data you are inserting before/ after the HTML
tags.
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
/*
*/
h1::after{
content: "!!!";
</style>
</head>
<body>
<h1>Pseudo Elements</h1>
<a href="#2">Contact</a>
<a href="#3">About</a>
</body>
</html>
Live Preview
Exercise 1
Exercise 2
Live Preview
3 CSS Rules
3.1 Precedence
Usage of Precedence
There are many ways to write the same rule in different ways but
which one will be applied on the browser depends on the
precedence of the rule.
Some rule has higher priority or precedence over the other rules.
CSS rule that is more specific has more priority and applied.
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
h1 {
color: blue;
p {
color: green;
p, h1
color: red;
div
color: red;
#text-blue{
color: blue;
.text-green{
color: green;
div#text-blue{
color: lightblue;
</style>
</head>
<body>
<h1>Heading</h1>
<p>Paragraph Text</p>
</div>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</body>
</html>
Live Preview
Exercise 1
Change the color of h1 with two selector one is ID selector with blue
color and another one is class selector with red color.
Live Preview
Exercise 2
h1 + p { color: blue; }
h1 ~ p { color: red; }
Live Preview
3.2 Inheritance
Usage of Inheritance
All the styles are inherited from the parent styles. This is very
important concept to understand.
Lets say you set the background color of body as red color then
every elements get the red background
If you make the font color as red in body then all the text on the page
will red in color.
Example:
<head>
<style type=”text/css”>
</style>
</head>
<body>
<h1>This is red color heading</h1>
<p>This is red color Paragraph</p>
</body>
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Inheritance in CSS</title>
<style type="text/css">
body{
color: white;
background-color: red;
h1{
text-decoration: underline;
.blackscreen{
background-color: black;
</style>
</head>
<body>
<h1>Heading!</h1>
<p>Paragraph Text</p>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
<hr>
<hr>
</body>
</html>
Exercise 1
Exercise 1: Apply the default style to all the content on the page.
Font Size 12 px
Color Red
Text is italics
Exercise 2
See which one take the precedence and the style is inherited or not.
Live Preview
When working with CSS you will often find situation when you want
to apply that last rule and it should work everytime irrespective of all
the other inheritance rules applied on that tag.
You can specify the last rule to the tag using the inline css on the tag
itself.
This last rule can be applied with style attribute on the tag.
All the elements will have this style attribute using which you can
apply the desired style which will override all the styles mentioned in
the internal or external css.
SYNTAX:
<head>
<style type=”text/css”>
p { color: red; }
</style>
</head>
<body>
<p style=”color: blue;”>This text color will be BLUE</p>
</body>
Sample Example
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
body{
color: red;
h1
color: red;
color: red;
#text-red {
color:red;
.text-red {
color: red;
h1#text-red{
h1.text-red{
color:red;
</style>
</head>
<body>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</body>
</html>
Exercise 1
Exercise 1: Apply color red with tag, class, id and blue color with
inline to see the impact of the rules on the tag.
Exercise 2
Exercise 2: Define <p> tag inline as color red. Check if for the next
iteration of <p> will it be red or black.
Will the inline rule is limited to that tag or it is inherited to next tag?
Live Preview
If Inline is the last rule that applied on the HTML tags and it overrides
all the rule then this important rule will even overwrites the inline
rule.
Browser will always give important to the rules that has !important at
the end of the rule.
Observe !important is used after the value and before the “;”
SYNTAX:
<head>
<style type=”text/css”>
p { color: red!important; }
</style>
</head>
<body>
<p style=”color: blue;”>This text will still be red</p>
</body>
Sample Example
Download the Example
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
p,h1
color: red!important;
#text-blue {
color:blue;
.text-blue {
color: blue;
h1#text-blue{
color: blue;
h1.text-blue{
color:blue;
</head>
<body>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</body>
</html>
Exercise 1
Exercise 1: Use the !important for <p> as inline css. Check for next
element is it inherited.
Live Preview
Exercise 2
Exercise 2: Apply the !important for <p> tag as red and for one p tag
use the inline color as blue with !important.
Live Preview
4.1 Colors
Usage of Colors
RGB Value
RGB is represented with the short form rgb(red, green, blue) and
numbers inside it.
Hex Code Value is also used to represent the specific colors. The
value starts with # and then followed by numbers & characters in
Hexadecials. It is typically 6 digits long.
background-color: #ff0000;
SYNTAX:
<head>
<style type=”text/css”>
</style>
</head>
<body>
<a href=”#”>Hover on me and I will turn red in color</a>
</body>
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>CSS Colors</title>
<style type="text/css">
div{
padding: 10px;
width: 50%;
font-size: 40px;
.redboxRGB {
background-color: rgb(255,0,0);
color: white;
.redboxHEX {
background-color: #FF0000;
color: white;
.redboxNAME {
background-color: red;
color: white;
.redboxDARKNAME {
background-color: darkred;
color: white;
</style>
</head>
<body>
<div class="redboxRGB">
</div>
<hr>
<div class="redboxHEX">
</div>
<hr>
<div class="redboxNAME">
</div>
<hr>
<div class="redboxDARKNAME">
</div>
</body>
</html>
Live Preview
Exercise 1
Exercise 1: Fill the body with GREEN color and all the tags
background colors with White and Text as Black.
Live Preview
Exercise 2
Exercise 2: Replace the RGB value with Hex Values for the Exercise 1.
4.2 Text
Usage of Text
Font or Text word is used change the behavior of text on the page.
font-size – Specify the size of the font in pixels or px. It is the same
pixel used in MS Word.
Most of the words will match with the keywords we use in Microsoft
Word Software.
Sample Example
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>CSS Fonts</title>
<style type="text/css">
line-height: 2em;
letter-spacing: 0.2em;
word-spacing: 1em;
text-decoration: underline;
text-indent: 500px;
p::first-letter{
font-size: 200%;
text-shadow: none;
</style>
</head>
<body>
</body>
</html>
Exercise 1
Exercise 1: Use the same text as above sample and apply the
following rules without seeing the properties.
Live Preview
Exercise 2
Exercise 2: Add Text Shadow to First Letter and Make the First Letter
Big. Handle all the changes via the CSS only.
Live Preview
5 CSS Box
5.1 Borders
Usage of Borders
To see the BOX around every HTML tag apply this rule.
*{
border-style: solid;
border-color: red;
}
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
div{
border-style: solid;
border-color: red;
border-width: 4px;
border-top-style: dashed;
border-bottom-style: dotted;
width: 50%;
.blue-border{
.green-border{
</style>
</head>
<body>
<div>
Border Example with Diff line of 4px width and red in color.
</div>
<div class="blue-border">
Border Example with Solid line of 5px width and blue in color.
</div>
<div class="green-border">
Border Example with Solid line of 5px width and green in color.
</div>
</body>
</html>
Exercise 1
Exercise 2
Exercise 2: Add Border Style Class Selector with blue color and apply
it to h1 to h6.
5.2 Margin
Usage of Margin
With Margin, you can push the boxes around the HTML tag.
TOP
RIGHT
BOTTOM
LEFT
Margin can be used to add spaces between the boxes and push the
boxes around.
Margin Properties:
margin
margin-top
margin-right
margin-bottom
margin-left
h1{
margin-top: 5px;
margin-right: 5px;
margin-bottom: 5px;
margin-left: 5px;
}
OR
h1{
margin: 5px; /*All the 4 sides will have margin of 5 px;*/
}
OR
h1{
/* margin: top right bottom left */
margin: 5px 5px 5px 5px;
}
h1{
/* margin: top left+right bottom */
margin: 5px 5px 5px;
}
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
.red-border{
margin-bottom: 25px;
.blue-border{
margin-top: 25px;
margin-left: 20px;
.green-border{
margin-left:50px;
margin-top: 25px;
</style>
</head>
<body>
<div class="red-border">
</div>
</div>
</div>
<div class="blue-border">
</div>
</div>
</div>
<div class="green-border">
</div>
</div>
</div>
</body>
</html>
Exercise 1
Exercise 1: Add h1 and p tag and add a red color border. Add
negative value to <p> margin-top: -50px;
Live Preview
Exercise 2
5.3 Padding
Usage of Padding
Padding add extra space inside the box to make it look bigger.
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
.red-border{
padding-top: 10px;
.blue-border{
padding-left: 10px;
.green-border{
padding: 10px;
</style>
</head>
<body>
<div class="red-border">
</div>
<div class="red-border">
</div>
<div class="red-border">
</div>
<div class="blue-border">
</div>
<div class="blue-border">
</div>
<div class="blue-border">
</div>
<div class="green-border">
</div>
<div class="green-border">
</div>
<div class="green-border">
</div>
</body>
</html>
Live Preview
Exercise 1
Exercise 1: Create RED Box of Width 250px and Height 100px and
add margin of 100px all sides and padding of 50px all sides.
Exercise 2
How big or small you want the box size is defined with this property.
You can even set the minimum and maximum height and width of
the box.
width
height
minwidth
minheight
maxwidth
maxheight
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
.box250 {
height: 250px;
width: 250px;
margin: 10px;
min-width: 100px;
.minbox100 {
margin: 10px;
min-width: 300px;
min-height: 300px;
.maxbox500 {
margin: 10px;
max-width: 500px;
max-height: 500px;
</style>
</head>
<body>
<div class="box250">
This is a box with 250x250 size and green border and margin of
10px.
</div>
This is a box with 250x250 size and green border and margin of
10px. <strong>It also has Padding of 10px all sides.</strong>
</div>
<div class="box250">
This is a box with 250x250 size and green border and margin of
10px.
</div>
<div class="minbox100">
<div class="maxbox500">
</div>
</body>
</html>
Exercise 1
Live Preview
Exercise 2
Live Preview
6 Floating Columns
float property to specify the element to float left or right or follow the
existing flow of box arrangement.
float: none|left|right|initial|inherit;
none – This will follow the default floating of box and it also breaks
the existing floating property set by it siblings or parent.
clear: both – property will clear the floating of boxes next to each
other.
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
.box{
height: 130px;
width: 130px;
margin-bottom: 10px;
margin-right: 10px;
.blue{
background-color: #5bc0de;
float: left;
.green{
background-color: #5cb85c;
float: right;
</style>
</head>
<body>
</body>
</html>
Exercise 1
Exercise 1: Draw 2 box one row and another two box another row.
Like a 2 x 2 matrix.
BOX 1 BOX 2
BOX 3 BOX 4
Exercise 2
Text beside the images can be floated to right or left with the float
property.
Like the articles in the newspaper, you have the image and then text
running sometime or left side or right side.
This is achieved by floating the image to left or right and then next
text element will float with it.
.clearfix::after {
content: “.”;
/* This display property you will learn in next lesson */
This code snippet is a famous hack that many developers use to clear
any floating objects after the box.
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Floating Example</title>
<style type="text/css">
h1 {
text-align: center;
text-decoration: underline;
img {
margin-right: 20px;
.clearfix::after {
/*
content: "";
display: table;
*/
content: ".";
display: block;
clear: both;
height: 0;
visibility: hidden;
.float-left {
float: left;
.float-right{
float: right;
</style>
</head>
<body>
<h1>German Shepherd</h1>
<div class="clearfix">
</div>
<hr>
<div class="clearfix">
</div>
<hr>
<div class="clearfix">
</div>
<hr>
</body>
</html>
Exercise 1
Live Preview
Exercise 2: Put the Image in the center and text in the center without
table.
Live Preview
Floating the <li> elements left will align all the block to stick together.
With display: inline-block allows to set the height and width of the
block where as display: inline does not.
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Floating Example</title>
ul{
background-color: #333;
list-style-type: none;
overflow: hidden;
li{
float: left;
li a{
display: inline-block;
color: white;
text-decoration: none;
</style>
</head>
<body>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Blog</a></li>
<li><a href="#">Contact</a></li>
<li><a href="#">About</a></li>
</ul>
</body>
</html>
Exercise 1
Exercise 1: Write a simple html page with h1, p and li tags and with
display property hide everything on the page.
Live Preview
Live Preview
NOT Floating the <li> elements left/right will align all the block stack
one top of each other.
Remove the float: left from previous horizontal menu and you can
find all the elements stack vertically.
Sample Example
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<title>Floating Example</title>
<style type="text/css">
ul{
background-color: #333;
list-style-type: none;
width: 200px;
li a{
color: white;
text-decoration: none;
</style>
</head>
<body>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Blog</a></li>
<li><a href="#">Contact</a></li>
<li><a href="#">About</a></li>
</ul>
</html>
Live Preview
Exercise 1
Exercise 1: Change the Color of the Link when hover over it.
Exercise 2
Live Preview
7 Positioning Elements
top
right
bottom
left
Element will be fixed and will will not move on the page.
Sample Example
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<style type="text/css">
/* Default Page & Font Styles - Because we love to see nice design.
*/
body, html {
height: 100%;
p {
margin: 0 auto;
max-width: 600px;
margin-top: 40px;
line-height: 1.5;
body {
h1 {
text-align: center;
background-color: red;
opacity: .85;
padding: 20px;
color: rgba(255,255,255,.9);
position: fixed;
left: 0;
right: 0;
text-align: center;
.announcement-top{
background: #d4765d;
position: fixed;
left: 0;
right: 0;
/* z-index: 2; */
/* opacity: .85; */
text-align: center;
color: white;
padding: 20px;
</style>
</head>
<body>
<br><br>
<h1>Position: Fixed</h1>
</p>
</body>
</html>
Live Preview
Exercise 1
Live Preview
Exercise 2
By default without CSS, browser will lay all the element one after the
other.
Example:
<h1>This is h1</h1>
<p>This is paragraph</p>
Without CSS, they both appear one after the other. h1 tags starts
from the absolute position the parent position and <p> tag will follow
the flow and sit after <h1>
If you want to break the <p> flow and want to position somewhere
else and follow the new location then we set the position of that
element as absolute.
position: absolute tells the browser to take this element out of the
flow and start putting from the absolute body position not the
current flow.
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
/* Page Styles */
.container {
background: rgba(0,0,0,.4);
height: 250px;
width: 250px;
margin-left: 300px;
margin-top: 150px;
.box
height: 150px;
width: 150px;
background-color: #5bc0de;
top: 50px;
left: 50px;
</style>
</head>
<body>
<div class="container">
<div class="box"></div>
</div>
</body>
</html>
Exercise 1
Exercise 2
Exercise 2: Draw 2 box (box1 and box2) within one container to and
lay them on top of each other.
Where ever the parent element is, relative property will make the
child stack them relative to parent element.
position: relative helps to group the parent and child together and
flow them next to each other.
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
/* Page Styles */
body {
background: rgba(0,0,0,.1);
/* Exercise Section */
.container {
background: rgba(0,0,0,.4);
height: 250px;
width: 250px;
margin-top: 150px;
.box
height: 150px;
width: 150px;
background-color: #5bc0de;
top: 50px;
left: 50px;
position: absolute;
</style>
</head>
<body>
<div class="container">
<div class="box"></div>
</div>
</body>
</html>
Exercise 1
Exercise 1: Draw two box inside the container stack them vertically.
Exercise 2
8. Display inline
Block
Become PHP Full Stack Web Developer in Just 30 Days
8 Display Inline and Block
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
/* Page Styles */
/* Exercise Section */
span
display: block;
div
display: inline;
</style>
<body>
</body>
</html>
Live Preview
Exercise 1
Exercise 1: Display the images side by side and one after the other
with display inline and block.
Exercise 2
Exercise 2: Draw two menu one with inline and another one with
block.
Live Preview
display:inline will not allow to change the height and width of the
box.
Sample Example
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<style type="text/css">
/* Page Styles */
/* Exercise Section */
body{
color: white;
li{
display: block;
li {
display: inline;
background-color: blue;
height: 100px;
li{
display: inline-block;
background-color: blue;
height: 100px;
</style>
</head>
<body>
<ul>
<li>Home</li>
<li>Blog</li>
<li>Contact</li>
<li>About</li>
</ul>
</body>
</html>
Live Preview
Exercise 1
Live Preview
Exercise 2
Exercise 2: Draw two boxes and align them side by side with
display:inline. Try to change the size of the box with width and
height.
Live Preview
9 Layouts
9.1 Layout 1
Usage of Layout 1
Header
Content
Footer
Sample Example
<!DOCTYPE html>
<html>
<head>
<style>
body {
width: 940px;
margin: 0 auto;
header,section,article,aside,footer{
display: block;
header, footer
padding: 0px;
text-align:center;
</style>
</head>
<body>
<header>
</header>
<hr>
<section>
<header><h1><u>Article 1</u></h1></header>
<div>
</div>
</article>
<article>
<header><h1><u>Article 2</u></h1></header>
<div>
</div>
</article>
</section>
<footer>
<hr>
</footer>
</body>
</html>
Project Work 1:
9.2 Layout 2
Usage of Layout 2
Header
Content
Left Sidebar
Footer
<!DOCTYPE html>
<html>
<head>
<style>
body {
width: 940px;
margin: 0 auto;
header,section,article,aside,footer{
display: block;
header, footer
padding:0px;
text-align:center;
aside{
float: left;
section{
float: right;
width: 700px;
padding-left: 20px;
footer{
clear:both !important;
width:940px;
height: 100px;
padding: 10px;
</style>
</head>
<body>
<header>
</header>
<hr>
<aside>
<figure>
<figcaption>German Breed</figcaption>
</figure>
<div>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
</aside>
<section>
<article>
<header><h1><u>Article 1</u></h1></header>
<div>
</div>
</article>
<article>
<header><h1><u>Article 2</u></h1></header>
<div>
</div>
</article>
</section>
<hr>
</footer>
</body>
</html>
Live Preview
Project Work 1:
9.3 Layout 3
Usage of Layout 3
Content
Footer
Sample Example
<!DOCTYPE html>
<html>
<head>
<style>
body {
width: 940px;
margin: 0 auto;
display: block;
header, footer
padding: 0px;
text-align:center;
</style>
</head>
<body>
<header>
</header>
<hr>
<section>
<article>
<header><h1><u>Article 1</u></h1></header>
<div>
</div>
</article>
<article>
<header><h1><u>Article 2</u></h1></header>
<div>
</div>
</article>
</section>
<footer>
<hr>
</footer>
</body>
</html>
Live Preview
Project Work 1:
Build the same layout from scratch by adding a right side sidebar
along with the left side.
Usage of Layout 4
Header
Content
Footer
Sample Example
<html>
<head>
<style>
body {
width: 940px;
margin: 0 auto;
header,section,article,aside,footer{
display: block;
header, footer
padding:0px;
text-align:center;
nav
text-align:center;
nav ul
list-style-type: none;
padding: 0;
margin-bottom: 0;
nav li
display: inline-block;
margin: 0 0 0 50px;
width: 100px;
aside, .aleft{
float: left;
aside, .aright{
float: right;
section{
float: left;
width: 500px;
padding-left: 20px;
padding-right: 20px;
footer{
clear:both !important;
width:940px;
height: 100px;
padding: 10px;
</style>
</head>
<body>
<header>
<hr>
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
<hr>
<aside class="aleft">
<figure>
<figcaption>German Breed</figcaption>
</figure>
<div>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
</aside>
<section>
<article>
<header><h1><u>Article 1</u></h1></header>
<div>
</div>
</article>
<article>
<header><h1><u>Article 2</u></h1></header>
<div>
</div>
</article>
</section>
<aside class="aright">
<figure>
<figcaption>German Breed</figcaption>
</figure>
<div>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
</aside>
<footer>
<hr>
</footer>
</body>
</html>
Project Work 1:
Build the same layout from scratch by adding your social profiles in
the right side bar section.