Lecture Notes-CSS(1)
Lecture Notes-CSS(1)
Learning Objectives:
• Introduction to CSS
• CSS Selectors
• CSS Rules
• Colors and Text
• CSS Box
• Floating Columns
• Positioning elements
• Display inline block
• Layouts
1. Introduction To CSS
What is CSS? CSS Stands for Cascading Style Sheet, it is used to describe how the content should be
displayed on the browser, print on screen.
In short, CSS handle the looks and feel of the web page. HTML is used to describe the content and CSS
is used to display the content in a presentable way.
Usage of CSS: CSS are written in file with extension .css and it is linked into HTML Page
Help to define the style once and then use it any where on your site,
Help to change the page layout based on the screen site is viewed on like Mobile, Tablet or Computer
Screen
CSS help to separate the presentation work from the HTML page and the developer can focus on building
the content and displayed separately.
1|Page
CSS Syntax
In order to start work with CSS can be written in many different places in HTML page.
Internal CSS:
Internal CSS means inside the same HTML page, it is restricted to only specific html page and you cannot
reuse it on other page, <style> tag is used to write the CSS inside this tag.
This tag <style> is defined inside the <head>
It is used as instruction to do something on the page but it doesn’t display it mainly added on <head>
2|Page
Example 2: Change the font size of h1, p and li tags
<html><head>
<style type="text/css">
h1{color: red;
font-size:12px;}
p{color: blue;
font-size:24px;}
li {color: green;
font-size:48px;}
</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>
CSS Comments: You can use the Special notation to comment the code inside the CSS
External CSS:
3|Page
External CSS filename should be .css and it is included in the <head> section
with <link> tag.
1) <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.
2) rel attribute is used to tell browser what kind of resource it is. rel =
“stylesheet” means it is a file with CSS inside it.
3) type attribute tells the type of the content in the file. In this case, it is text/css
4) href attribute is similar to <a> tag href to map the location of the file in the
server with the path and filename.
Change the font size and color of h1, p and li tags in style23.css and embed the CSS file in the HTML
page.
4|Page
Inline CSS:
Usage of Inline CSS Inline CSS is defined inside the tag itself like an attribute.
Inline CSS overrides all the styles defined in internal CSS and External CSS.
This is defined in the HTML tag as an attribute.
Example: Write Inline CSS and change the h1 tags color and font size as red, paragraph
as blue and list as green
<html>
<head>
</head>
<body>
<h1 style="color: red; font-size:12px;"> Heading </h1>
<p style="color: blue; font-size:24px;"> Paragraph
Text</p>
<ul style="color: green; font-size:48px;"><li> Item 1</li>
<li> Item 2 </li> <li> Item 3 </li></ul>
</body></html>
Linking CSS: Sometimes there is some font style or format on another CSS page you need to add it on
your page to make more presentable so in order to find suitable font style the below link contains several
font styles
https://fonts.google.com/
for example, if I need to add Poppins font family search for it once you choose the style
5|Page
You can create the multiple CSS file and import the one css file into another css file.
Take the import code from the format you choose on the plus sign choose @ import code to take it to CSS
page and add for the tag you want the font family you choose
6|Page
Example 2 :
HTML Page CSS Page
<html> /*CSS Comments*/
<head> @import
<link rel="stylesheet" type="text/css" url('https://fonts.googleapis.com/css2?family=C
href="style23.css" aveat&family=Playpen+Sans:wght@300&famil
y=Poppins:ital,wght@0,200;0,400;1,300&displ
</head> ay=swap');
<body>
<h1 style="color: red; text-align: center;">This is *{border-style: solid;
Inline Style with Heading and Center </h1>
border-color: red;}
<p>Paragraph Text with 20px font size, bold,
italics and aligned center</p> <p>Power of CSS!! p {font-size:20px; font-weight:bold;
</p> font-style: italic;
</body></html> text-align: center;}
Output:
2. CSS Selectors:
7|Page
Selector is used to indicate which element this rule should be applied to.
P, h1, li elements will be red in color with this one CSS rule.
Declaration indicates what is the rule to be applied on that element. The rule is indicated with
Property: value.
8|Page
Example2: Add two declarations for each selector and change the CSS to see the following output.
HTML Page CSS Page Output
<html> /*CSS Comments*/
<head> @import
<link rel="stylesheet" url('https://fonts.googleapis.com/css2?family=
type="text/css" Caveat&family=Playpen+Sans:wght@300&fami
href="style23.css"> ly=Poppins:ital,wght@0,200;0,400;1,300&displ
ay=swap');
</head><body>
h1{color: red;
<h1> Heading </h1>
font-size:12px;}
<p> Paragraph Text </p>
p {color: blue;
<ul><li> Item 1</li><li> Item
2 </li><li> Item 3 font-size:24px;}
</li></ul></body>
li{ color:green;
</html>
font-size:48px;}
2) Universal Selectors
Universal Selectors is the rule that you want to apply for all elements of the page.
Wild Character “*” is used to represent the universal selector
Universal selector is applied on all the elements of the page. Here all page tags will be red.
Example: Write two CSS rule Using Universal Selector change the color to red Change the h1 tag color
to blue. Observe the result.
9|Page
HTML page CSS Page Output
<head> @import
url('https://fonts.googleapis.com/
<link rel="stylesheet"
css2?family=Caveat&family=Playp
type="text/css" href="style23.css">
en+Sans:wght@300&family=Poppi
</head><body> ns:ital,wght@0,200;0,400;1,300&
display=swap');
<h1> Heading </h1>
*{color: red; }
<p> Paragraph Text </p>
<ul><li> Item 1</li><li> Item 2 h1{color: blue;}
</li><li> Item 3 </li></ul></body>
</html>
<head> @import
url('https://fonts.googleapis.com/
<link rel="stylesheet"
css2?family=Caveat&family=Playp
type="text/css" href="style23.css"
en+Sans:wght@300&family=Poppi
</head> ns:ital,wght@0,200;0,400;1,300&
display=swap');
<body>
h1{font-size:12px;}
<h1> Heading </h1>
<h6> Heading6 </h6> h6{font-size:48px;}
</body></html>
10 | P a g e
Example 2: Do the following steps: Make h1 and h2 same size and color. h3 and h4 same size and
color h5 and h6 same size and color. All the headings should be in Italics
<html> @import
url('https://fonts.googleapis.com/
<head>
css2?family=Caveat&family=Playp
<link rel="stylesheet" en+Sans:wght@300&family=Poppi
type="text/css" href="style23.css"> ns:ital,wght@0,200;0,400;1,300&
display=swap');
</head>
<body> *{ font-style: italic;}
4) Class Selector
Usage of Class Selector 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.
11 | P a g e
If you want to create class selector only specific to HTML tags then use this notation
Example: Write the CSS rule with Class Selector Create one rule called as “. headings” and make the
color as blue, align center and italics.
HTML Page : CSS Page Output
<html> /*CSS Comments*/
<head> @import
<link rel="stylesheet" url('https://fonts.googleapis.com/
type="text/css" href="style23.css"> css2?family=Caveat&family=Playp
en+Sans:wght@300&family=Poppi
</head> ns:ital,wght@0,200;0,400;1,300&
<body> display=swap');
12 | P a g e
Example 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.
HTML Page CSS Page
Output
13 | P a g e
Example:
HTML Page CSS Page
Output
</body></html>
Example 2: Apply the class top 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. Guess the output
14 | P a g e
HTML Page CSS Page
Output
6) ID Selectors
ID Selector is custom selector that you can create in the CSS rules and apply it to any HTML tag with id
attribute.
ID selector names are custom so you should NOT use the predefined HTML tag names.
Custom Selector names can be ID selectors that can be applied to any HTML tag.
You define the ID selector with “#” in-front of it.
15 | P a g e
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
Example: Create one rule called as “#headings” and make the color as red, align center
and italics
HTML Page CSS Page Output
16 | P a g e
Example: Create an ID selector only for h1 tag. Even if this ID selector is applied to other tags like p it
should not work.
and italics
HTML Page CSS Page
Output
7) Multiple IS Selectors
You can apply the selector to the parent HTML tag and all the child elements
will inherit the property from the parent tags properties.
17 | P a g e
Example: Write the CSS rule with Multiple ID Selector Create one rule called as “. align-center” for p
tag and h1 tag. Write p tag inside the h1 tag..
HTML Page CSS Page
<h1>Welcome<p id="align- }
center">BIT Students</p></h1>
</body></html>
Output
Example2:
18 | P a g e
HTML Page CSS Page
<h1> }
<div id="align-center">
<p> Paragraph text </p>
</div> </h1> </body></html>
Output:
8) Attribute Selector:
Attribute selector helps to apply the CSS rule when some specific condition is met on the HTML
attribute. One example is when you want apply color red on all the <p> tag when it has class attribute
with any value
p[class] this attribute selector tells the browser to apply the rule to the HTML Tag when it has
the class attribute.
19 | P a g e
HTML Page CSS Page Output
20 | P a g e
Example: Make border radius for input form type text by 5pt
HTML Page CSS Page
Output
21 | P a g e
9) Child Selector
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 anchor tag inside the paragraph tag then you can use the child
selector to apply the style to only tag inside the tag.
Example2: Apply the CSS color red when tag is inside the and it has the class and id attribute
only. HTML Page CSS Page
Output
<html> /*CSS Comments*/
<head> @import
url('https://fonts.googleapis.com/css2?fa
<link rel="stylesheet" type="text/css"
href="style23.css"> mily=Caveat&family=Playpen+Sans:wght
@300&family=Poppins:ital,wght@0,200;
</head> 0,400;1,300&display=swap');
<body> p>a[class][id]{
<p> <a href="#" class="red" id="red"> Click color:red;
1 </a> </p>
}
<p> <a href="#"> Click 2 </a></p>
</body></html>
22 | P a g e
10) Descendant Selector
Descendant Selector rule is used when you want to apply rule to for every
element under one parent element.
Selectors are separated with ‘ ‘ (space) to indicate they are parent and child relation.
Example: make all a tags as green inside the body
HTML Page CSS Page Output
<html> /*CSS Comments*/
<head> @import
url('https://fonts.googleapis.com/css2?fa
<link rel="stylesheet" type="text/css"
href="style23.css"> mily=Caveat&family=Playpen+Sans:wght
@300&family=Poppins:ital,wght@0,200;
</head> 0,400;1,300&display=swap');
<body> body a{
<p> <a href="#"> Click 1 </a> </p> color: green;}
<p> <a href="#"> Click 2 </a></p>
</body></html>
Example2: Apply the CSS color red when tag is inside the with descendant and also child
selectors. Observe which rule is applied.(it will apply the last rule in css )
HTML Page CSS Page Output
<html> /*CSS Comments*/
<head> @import
url('https://fonts.googleapis.com/css2?fa
<link rel="stylesheet" type="text/css"
href="style23.css"> mily=Caveat&family=Playpen+Sans:wght
@300&family=Poppins:ital,wght@0,200;
</head> 0,400;1,300&display=swap');
<body> p a{
<p> <a href="#"> Click 1 </a> </p> color:red;}
<p> <a href="#"> Click 2 </a></p> p>a{
</body></html> color:blue;}
23 | P a g e
11) Adjacent Sibling Selector ( )األقرب
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.
Example: Write the CSS rule with Adjacent Selectors First make the <a> tag as blue Apply this
rule only when <h1> is adjacent to another <a> tag
here the first a apply blue color only.
HTML Page CSS Page Output
<html> /*CSS Comments*/
<head> @import
url('https://fonts.googleapis.com/css2?fa
<link rel="stylesheet" type="text/css"
href="style23.css"> mily=Caveat&family=Playpen+Sans:wght
@300&family=Poppins:ital,wght@0,200;
</head> 0,400;1,300&display=swap');
<body> h1+a{
<h1> Hello</h1> color:blue;
<a href="#"> Click 1 </a> <br> }
<a href="#"> Click 2 </a>
</body></html>
Example2: Apply the CSS color red when tag is inside the with descendant and also child
selectors and also with Adjacent Siblings Selector. (Child selector will be applied)
<head> @import
url('https://fonts.googleapis.com/css2?fa
<link rel="stylesheet" type="text/css"
mily=Caveat&family=Playpen+Sans:wght
href="style23.css">
@300&family=Poppins:ital,wght@0,200;
</head> 0,400;1,300&display=swap');
<body> p a{color: red;}
<p><a href="#"> Click 1 </a></p> p>a{color: blue;}
<p><a href="#"> Click 2 </a></p> p+a{color: green;}
</body></html>
24 | P a g e
12) General Sibling Selector
General Adjacent Sibling Selector rule is used when you want to apply rule to
for every 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 all the adjacent sibling <a> tag after h1.
<h1>Test</h1>
<a href=” #”>Red1</a>
<a href=” #”>Red2</a>
<a> with Red1 is adjacent to <h1> and sibling as well.
<a> with Red2 is also near adjacent to <h1>
Example: Apply the CSS color 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.(Child selector will be applied)
HTML Page CSS Page Output
<html> /*CSS Comments*/
<head> @import
url('https://fonts.googleapis.com/css2?fa
<link rel="stylesheet" type="text/css"
href="style23.css"> mily=Caveat&family=Playpen+Sans:wght
@300&family=Poppins:ital,wght@0,200;
</head> 0,400;1,300&display=swap');
<body> p a{ color:red;}
<p><a href="#"> Click 1 </a></p> p>a{color:blue; }
<p><a href="#"> Click 2 </a></p> p+a{color:green;}
</body> p~a{color: orange;}
</html>
25 | P a g e
13) Pseudo Classes
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.
Example1:
HTML Page CSS Page Output
<html> /*CSS Comments*/
<head> @import
url('https://fonts.googleapis.com/css2?fa
<link rel="stylesheet" type="text/css"
href="style23.css"> mily=Caveat&family=Playpen+Sans:wght
@300&family=Poppins:ital,wght@0,200;
</head> 0,400;1,300&display=swap');
<body> a: hover {
<p><a href="#"> Click 1 </a></p> color: red;}
<p><a href="#"> Click 2 </a></p>
</body></html>
26 | P a g e
Output
<head> @Import
url('https://fonts.googleapis.com/css2?fa
<link rel="stylesheet" type="text/css"
mily=Caveat&family=Playpen+Sans:wght
href="style23.css">
@300&family=Poppins:ital,wght@0,200;
</head> 0,400;1,300&display=swap');
<body> p::before{ content:"!!!";}
<p> Power CSS</p></body></html>
27 | P a g e