Chapter 4 - FSD I
Chapter 4 - FSD I
Significance of CSS
• CSS stands for Cascading Style Sheets.
• CSS is the language we use to style a Web page.
• CSS describes how HTML elements are to be displayed on screen, paper, or in other
media.
• CSS saves a lot of work. It can control the layout of multiple web pages all at once.
• External stylesheets are stored in CSS files.
• In above syntax “selector” is the element(tag) that the rule defines and “property1”,
“property2” are the values assigned to these properties.
Example:
p {
color: blue;
text-align: center;
}
• p is a selector in CSS (it points to the HTML element you want to style: <p>).
• color is a property, and blue is the property value
• text-align is a property, and center is the property value
Types of CSS
1) Inline CSS
Example:
<html>
<body>
</body>
</html>
Output:
2) Internal Style
<html>
<head>
<style>
body {background-color: pink;}
h1{color: blue;}
p{color: purple;}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Output:
3) External CSS
• External CSS contains separate CSS file which contains only style property with the
help of tag attributes (For example class, id, heading, … etc). CSS property written in
a separate file with .css extension and should be linked to the HTML document
using link tag.
• This means that for each element, style can be set only once and that will be applied
across web pages.
Below is the HTML file that is making use of the created external style sheet
• link tag is used to link the external style sheet with the html webpage.
• href attribute is used to specify the location of the external style sheet file.
Example:
demo.html
<html>
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
style.css
body {
background-color: pink;
}
h1 {
color: blue;
}
p {
color: purple;
}
Output:
Various CSS Selectors:
• The element selector selects HTML elements based on the element name.
Syntax:
element-name
Example:
• Here, all <p> elements on the page will be center-aligned, with a red text color:
<html>
<head>
<style>
p {
text-align: center;
color: blue;
}
</style>
</head>
<body>
<p>This is element selector and we have selected paragraph to provide style<p>
<p>Both paragraph will be in blue color</p>
</body>
</html>
Output:
2) Id Selector
• The id selector uses the id attribute of an HTML element to select a specific element.
• The id of an element is unique within a page, so the id selector is used to select one
unique element!
• To select an element with a specific id, write a hash (#) character, followed by the id of
the element.
Synatx:
#element-id{
Example:
<html>
<head>
<style>
#para1 {
text-align: center;
color: green;
}
</style>
</head>
<body>
</body>
</html>
Output:
3) Class Selector
• The class selector selects HTML elements with a specific class attribute.
• To select elements with a specific class, write a period (.) character, followed by the class
name.
Syntax:
.element-classname
{
css property: value;
}
Example 1:
<html>
<head>
<style>
.center{
text-align: center;
color: red;
}
</style>
</head>
<body>
</body>
</html>
Output:
Example 2
• You can also specify that only specific HTML elements should be affected by a class.
• In this example only <p> elements with class="center" will be red and center-aligned:
<html>
<head>
<style>
p.center {
text-align: center;
color: red;
}
</style>
</head>
<body>
</body>
</html>
Output:
Example 3
• HTML elements can also refer to more than one class.
• In this example the <p> element will be styled according to class="center" and to
class="large":
<html>
<head>
<style>
p.center {
text-align: center;
color: red;
}
p.large {
font-size: 200%;
}
</style>
</head>
<body>
</body>
</html>
Output:
4) Universal Selector
• The universal selector (*) selects all HTML elements on the page.
Syntax:
*
{ css property: value;
}
Example:
• The CSS rule below will affect every HTML element on the page:
<html>
<head>
<style>
* {
text-align: center;
color: brown;
}
</style>
</head>
<body>
<h1>Universal Selector</h1>
<p>Using this</p>
<pre>Every element on the page will be affected by the style.</pre>
</body>
</html>
Output:
5) Descendant selector
• If some tag is nested in the other tag then nested tag is called as descendant of parent tag.
Syntax:
element1 element2
{
css property: value;
}
Example:
<html>
<head>
<style>
ul b
{
font-size:20px;
color:blue;
}
</style>
</head>
<body>
<h3>Unordered list</h3>
<ul>
<li><b>abc</b></li>
<li><b>xyz</b></li>
</ul>
</body>
</html>
Output:
6) Child Selector
• The child selector selects all elements that are the children of a specified element.
• To apply CSS, nested tag must be a direct child of previous tag.
Syntax:
element1>element2
{
css property:value;
}
Example 1:
<html>
<head>
<style>
ul>b
{
font-size:20px;
color:blue;
}
</style>
</head>
<body>
<h3>Unordered list</h3>
<ul>
<li><b>abc</b></li>
<li><b>xyz</b></li>
</ul>
</body>
</html>
Output:
Note: CSS will not work for example 1 as nested tag <b> is not a direct child of <ul> tag.
Example 2:
<html>
<head>
<style>
body>h3
{
font-size:20px;
color:blue;
}
</style>
</head>
<body>
<h3>Unordered list</h3>
<ul>
<li><b>abc</b></li>
<li><b>xyz</b></li>
</ul>
</body>
</html>
Output:
7) Attribute selector:
• It is possible to style HTML elements that have specific attributes or attribute values.
• The [attribute] selector is used to select elements with a specified attribute.
• The attribute selectors can be useful for styling forms without class or ID:
Syntax:
element-name[type=”text”]
{
css property: value;
}
Example:
<html>
<head>
<style>
input[type=text] {
width: 150px;
display: block;
margin-bottom: 10px;
background-color: lightpink;
}
input[type=button] {
width: 120px;
margin-left: 35px;
display: block;
background-color: yellow;
}
</style>
</head>
<body>
<form name="input" action="" method="get">
Firstname:<input type="text" name="Name" value="ZALAK" size="20">
Lastname:<input type="text" name="Name" value="BHATT" size="20">
<input type="button" value="Submit Details">
</form>
</body>
</html>
Output:
CSS colors
• Colors are specified using predefined color names, or RGB, HSL, RGBA, HSLA values.
• Colors are specified using HEX code #rr gg bb, Simple #rgb.
• Colors are specified using RGB function: rgb(RRR, GGG, BBB)
• In CSS, a color can be specified by using a predefined color name: Orange, Red, White,
Gray etc.
Example
<html>
<body>
</body>
</html>
Output:
div {
background-color: #FFC0CB;
}
p {
background-color: #bcd;
}
pre {
background-color: aqua;
}
</style>
</head>
<body>
<h1>CSS background-color</h1>
<div>
This is a text inside a div element.
<p>This paragraph has its own background color.</p>
We are still in the div element.
<pre>Preformatted text
with its own
background color
</pre>
</div>
</body>
</html>
Output:
<h1>BACKGROUND IMAGE</h1>
</body>
</html>
Output:
<style>
body {
background-repeat: repeat-X;
}
</style>
</head>
<body>
<h1>BACKGROUND IMAGE(REPEAT-X)</h1>
</body>
</html>
Output:
Example (repeat-y):
<html>
<head>
<style>
body {
</style>
</head>
<body>
<h1>BACKGROUND IMAGE(REPEAT-Y)</h1>
<p>THE IMAGE IS REPEATED VERTICALLY</p>
</body>
</html>
Output:
Example (no-repeat)
<html>
<head>
<style>
body {
}
</style>
</head>
<body>
<h1>BACKGROUND IMAGE(no-repeat)</h1>
</body>
</html>
Output:
Example (Shorthand):
<html>
<head>
<style>
body {
background: pink url("D:/Full Stack/flwr.jpg") repeat-x;
}
</style>
</head>
<body>
<h1>BACKGROUND SHORTHAND</h1>
<p>with image and background color</p>
</body>
</html>
Output:
<h2>text-indent: 50px:</h2>
<div class="a">
<p>Hello</p>
</div>
<h2>text-indent: -2em:</h2>
<div class="b">
<p>Everybody</p>
</div>
<h2>text-indent: 30%:</h2>
<div class="c">
<p>Good Morning</p>
</div>
</body>
</html>
Output:
<head>
<style>
h1 {
text-decoration: overline;
}
h2 {
text-decoration: line-through;
h3 {
text-decoration: underline;
}
h4 {
text-decoration: underline overline;
}
h5 {
text-decoration: none;
</style>
</head>
<body>
<h1>Heading with overline</h1>
</html>
Output:
.b {
text-transform: lowercase;
}
.c {
text-transform: capitalize;
}
.d {
text-transform: none;
}
</style>
</head>
<body>
<h1>The text-transform Property</h1>
<h2>text-transform: Uppercase</h2>
<div class="a">Have a nice day</div>
<h2>text-transform: Lowercase</h2>
<div class="b">Have a nice day</div>
<h2>text-transform: Capitalize</h2>
<div class="c">Have a nice day</div>
<h2>text-transform: None</h2>
<div class="d">Have a nice day</div>
</body>
</html>
Output:
Text Manipulation: text-align
• The text-align property specifies the horizontal alignment of text in an element.
Syntax:
text-align: left|right|center|justify;
Example:
<html>
<head>
<style>
.a {
text-align: center;
}
.b {
text-align: left;
}
.c {
text-align: right;
}
.d {
text-align: justify;
}
</style>
</head>
<body>
<h2>text-align: center:</h2>
<p class="a">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam
semper diam at erat pulvinar, at pulvinar felis blandit. Vestibulum volutpat
tellus diam, consequat gravida libero rhoncus ut.</p>
<h2>text-align: left:</h2>
<p class="b">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam
semper diam at erat pulvinar, at pulvinar felis blandit. Vestibulum volutpat
tellus diam, consequat gravida libero rhoncus ut.</p>
<h2>text-align: right:</h2>
<p class="c">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam
semper diam at erat pulvinar, at pulvinar felis blandit. Vestibulum volutpat
tellus diam, consequat gravida libero rhoncus ut.</p>
<h2>text-align: justify:</h2>
<p class="d">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam
semper diam at erat pulvinar, at pulvinar felis blandit. Vestibulum volutpat
tellus diam, consequat gravida libero rhoncus ut.</p>
</body>
</html>
Output:
CSS Fonts
• Choosing the right font has a huge impact on how the readers experience a website.
• The right font can create a strong identity for your brand.
• Using a font that is easy to read is important. The font adds value to your text. It is also
important to choose the correct color and text size for the font.
Font families:
Note: If the font name is more than one word, it must be in quotation marks, like: "Times New
Roman".
1. Serif fonts have a small stroke at the edges of each letter. They create a sense of
formality and elegance.
2. Sans-serif fonts have clean lines (no small strokes attached). They create a modern and
minimalistic look.
3. Monospace fonts - here all the letters have the same fixed width. They create a
mechanical look.
4. Cursive fonts imitate human handwriting.
5. Fantasy fonts are decorative/playful fonts.
Example:
<html>
<head>
<style>
.p1 {
font-family: "Times New Roman", Times, serif;
}
.p2 {
font-family: Arial, Helvetica, sans-serif;
}
.p3 {
font-family: "Lucida Console", "Courier New", monospace;
}
</style>
</head>
<body>
<h1>CSS font-family</h1>
<p class="p1">This is a paragraph, shown in the Times New Roman font.</p>
<p class="p2">This is a paragraph, shown in the Arial font.</p>
<p class="p3">This is a paragraph, shown in the Lucida Console font.</p>
</body>
</html>
Output:
Font style:
• The font-style property is mostly used to specify italic text.
• Possible values for this properties are:
1) Normal: The text is shown normally
2) Italic: The text is shown in italics
Example:
<html>
<head>
<style>
p.normal {
font-style: normal;
}
p.italic {
font-style: italic;
}
</style>
</head>
<body>
<h1>The font-style property</h1>
<p class="normal">This is a paragraph in normal style.</p>
<p class="italic">This is a paragraph in italic style.</p>
</body>
</html>
Output:
Font weight
Example:
<html>
<head>
<style>
p.normal {
font-weight: normal;
}
p.thick {
font-weight: bold;
}
</style>
</head>
<body>
<h1>The font-weight property</h1>
</body>
</html>
Output:
Font variant:
• The font-variant property specifies whether or not a text should be displayed in a small-
caps font.
• In a small-caps font, all lowercase letters are converted to uppercase letters. However, the
converted uppercase letters appears in a smaller font size than the original uppercase
letters in the text.
Example:
<html>
<head>
<style>
p.normal {
font-variant: normal;
}
p.small {
font-variant: small-caps;
}
</style>
</head>
<body>
Output:
Font size:
Syntax:
font-size: medium|xx-small|x-small|small|large|x-large|xx-large|px|%
Example:
<html>
<head>
<style>
.a {
font-size: 15px;
}
.b {
font-size: large;
}
.c {
font-size: 150%;
}
.d {
font-size: small;
}
.e {
font-size: x-small;
}
.f {
font-size: xx-small;
}
</style>
</head>
<body>
<h1>The font-size Property</h1>
</body>
</html>
Output:
CSS text controls: Letter spacing
• The letter-spacing property is used to specify the space between the characters in
a text.
Example:
<html>
<head>
<style>
h2 {
letter-spacing: 5px;
}
h3 {
letter-spacing: -2px;
}
</style>
</head>
<body>
<h1>Using letter-spacing</h1>
<h2>This is heading 1</h2>
<h3>This is heading 2</h3>
</body>
</html>
Output:
CSS text controls: Word spacing
• The word-spacing property is used to specify the space between the words in a text.
Example:
<html>
<head>
<style>
p.one {
word-spacing: 10px;
}
p.two {
word-spacing: -2px;
}
</style>
</head>
<body>
<h1>Using word-spacing</h1>
</body>
</html>
Output:
CSS Borders:
• The CSS border properties allow you to specify the style, width, and color of an element's
border.
• The border-style property specifies what kind of border to display.
• Possible values are: solid, dotted, dashed, double.
Example:
<html>
<head>
<style>
p.dotted {border-style: dotted;}
p.dashed {border-style: dashed;}
p.solid {border-style: solid;}
p.double {border-style: double;}
p.mix {border-style: dotted dashed solid double;}
</style>
</head>
<body>
Output:
Border color:
• The border-color property is used to set the color of the four borders.
• The border-color property can have from one to four values (for the top border,
right border, bottom border, and the left border).
Example:
<html>
<head>
<style>
p.one {
border-style: solid;
border-color: red green blue yellow; /* red top, green right, blue bottom and
yellow left */
}
p.two {
border-style: double;
border-color: red green blue;
}
p.three {
border-style: dotted;
border-color: red green;
}
p.four {
border-style: dashed;
border-color: blue;
}
</style>
</head>
<body>
Output:
Border width:
• The border-width property specifies the width of the four borders.
• The width can be set as a specific size (in px, pt, cm, em, etc) or by using one of the three
pre-defined values: thin, medium, or thick:
Example:
<html>
<head>
<style>
p.one {
border-style: solid;
border-width: 5px;
}
p.two {
border-style: solid;
border-width: medium;
}
p.three {
border-style: dotted;
border-width: 2px;
}
p.four {
border-style: dotted;
border-width: thick;
}
p.five {
border-style: double;
border-width: 15px;
}
p.six {
border-style: double;
border-width: thick;
}
</style>
</head>
<body>
Output:
Border Shorthand:
• To shorten the code, it is also possible to specify all the individual border properties in
one property.
• The border property is a shorthand property for the following individual border
properties: border-width, border-style(required), border-color.
Example:
<html>
<head>
<style>
.a {
border: 5px solid red;
}
.b{
border-left: 5px solid red;
}
</style>
</head>
<body>
<h1>The border short hand Property</h1>
</body>
</html>
Output:
Pseudo classes
• A pseudo-class is used to define a special state of an element.
• it can be used to:
1) Style an element when a user mouses over it
2) Style visited and unvisited links differently
3) Style an element when it gets focus
Syntax:
selector:pseudo-class {
property: value;
}
Example:
<html>
<head>
<style>
/* unvisited link */
a:link {
color: red;
}
/* visited link */
a:visited {
color: green;
}
/* selected link */
a:active {
color: blue;
}
</style>
</head>
<body>
</body>
</html>
Output:
Example:
<html>
<head>
<style>
a:link
{
color:blue;
}
a:visited
{
color:purple;
}
a:hover
{
color:yellow;
}
a:active
{
color:cyan;
}
ol:hover
{
color:pink;
}
ol:active
{
color:purple;
}
a.highlight:hover
{
font-size:100px;
}
</style>
</head>
<body>
<a href="http://www.facebook.com" class="highlight">FACEBOOK</a>
<ol>
<li>ABC</li>
<li>PQR</li>
</ol>
</body>
</html>
Output:
Pseudo Elements
• A CSS pseudo-element is used to style specified parts of an element.
• it can be used to:
1) Style the first letter, or line, of an element
2) Insert content before, or after, the content of an element
Syntax:
selector::pseudo-element
{
property: value;
}
Example:
<html>
<head>
<style>
p::first-line
{
font-size:30px;
font-family:times new roman,sarif;
font-style:italic;
font-weight:bold;
font-variant:small-caps;
}
</style>
</head>
<body>
<p>Happiness, in the context of mental or emotional states, is
positive or pleasant emotions ranging from contentment to intense joy. Other
forms include life satisfaction, well-being, subjective well-being, flourishing
and eudaimonia.
Since the 1960s, happiness research has been conducted in a wide variety of
scientific disciplines, including gerontology, social psychology and positive
psychology, clinical and medical research and happiness economics.</p>
</body>
</html>
Output:
Output:
CSS - The ::before Pseudo-element
• The ::before pseudo-element can be used to insert some content before the content of an
element.
Example:
<html>
<head>
<style>
h1::before
{
content:url("D:/Full Stack/smile.jfif");
</style>
</head>
<body>
</body>
</html>
Output:
CSS - The ::after Pseudo-element
• The ::after pseudo-element can be used to insert some content after the content of an
element.
Example:
<html>
<head>
<style>
h1::after
{
content:url("D:/Full Stack/smile.jfif");
}
</style>
</head>
<body>
<h1>Spread smile wherever you go.........</h1>
</body>
</html>
Output:
<ul>
<li>ABC</li>
<li>PQR</li>
<li>XYZ</li>
</ul>
<ol>
<li>ABC</li>
<li>PQR</li>
<li>XYZ</li>
<ol>
</body>
</html>
Output:
Output
Before Selection:
After Selection
CSS Box Model:
• All HTML elements can be considered as boxes.
• In CSS, the term "box model" is used when talking about design and layout.
• The box model allows us to add a border around elements, and to define space between
elements.
• The CSS box model is essentially a box that wraps around every HTML element. It
consists of: margins, borders, padding, and the actual content.
• Content - The content of the box, where text and images appear
• Padding - Clears an area around the content. The padding is transparent
• Border - A border that goes around the padding and content
• Margin - Clears an area outside the border. The margin is transparent
Example:
<html>
<head>
</head>
<body>
<p style="margin:auto; padding:50px; border:4px solid blue">
THIS IS P1
</p>
<p style="margin:10px; padding:50px 50px; border:5px dashed red">
THIS IS P2
</p>
<p style="margin:100px 100px; border:15px dotted green">
THIS IS P3
</p>
<p style="margin:15px 20px 25px 30px;border:4px double purple">
THIS IS P4
</p>
</body>
</html>
Output:
CSS positioning:
• The position property specifies the type of positioning method used for an element (static,
relative, fixed, absolute or sticky).
• Possible values for the position property are:
1) Absolute
2) Relative
3) Fixed
4) Static
position: static:
• HTML elements are positioned static by default.
• Static positioned elements are not affected by the top, bottom, left, and right properties.
• An element with position: static; is not positioned in any special way; it is always
positioned according to the normal flow of the page
position :relative
position: fixed
• An element with position: fixed; is positioned relative to the viewport, which means it
always stays in the same place even if the page is scrolled.
• The top, right, bottom, and left properties are used to position the element.
• A fixed element does not leave a gap in the page where it would normally have been
located.
position-absolute:
• An element with position: absolute; is positioned relative to the nearest positioned ancestor
(instead of positioned relative to the viewport, like fixed).
• However; if an absolute positioned element has no positioned ancestors, it uses the
document body, and moves along with page scrolling.
Note: Absolute positioned elements are removed from the normal flow, and can overlap
elements.
Example:
<html>
<head>
<style>
#p1
{
position:static;
border:1px solid green;
height:30%;
}
#p2
{
border:2px solid red;
position:relative;
top:40px;
left:40px;
}
#p3
{
border:2px solid purple;
position:fixed;
top:20px;
left:30px;
}
#p4
{
border:2px solid blue;
position:absolute;
top:100px;
left:100px;
}
</style>
</head>
<body>
<p id="p1">
THIS IS STATIC OR BY DEFAULT POSITIONING
</p>
<p id="p2">
THIS IS RELATIVE POSITIONING TO FIRST PARAGRAPH
</p>
<p id="p3">
THIS IS FIXED POSITIONING
</p>
<p id="p4">
THIS IS ABSOLUTE POSITIONING
</p>
<pre>
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
*
*
*
*
*
*
*
*
*
*
*
*
*
*
</pre>
</body>
</html>
Output:
</body>
</html>
Output:
Note: In this example image will float to the left and text in the paragraph will wrap around the
image.
Z-index Property
The z-index property specifies the stack order of an element. An element with greater stack order is always
in front of an element with a lower stack order.
Note: z-index only works on positioned elements (position: absolute, position: relative, position: fixed)
Note: If two positioned elements overlap without a z-index specified, the element positioned last in the
HTML code will be shown on top.
Syntax:
z-index: auto|number|initial|inherit;
Property values:
• number: It means that the element's stack level is set to the given value. It allows negative values.
Example:
<html>
<head>
<style>
div
{
position:fixed;
width:400px;
height:300px;
background-color:lightgreen;
}
img
{
position:absolute;
left: 0px;
top: 0px;
z-index: 1;
}
h1
{
position:relative;
color:green;
z-index: 2;
}
</style>
</head>
<body>
<div>
<h1>Choose Hope</h1>
<img src="xy.jpg"/>
</div>
</body>
</html>
Output:
Examples:
1) Write HTML and CSS script to display two sections of 40% width using div tag. Both
these sections are having title and description. Both these div’s should be horizontally
adjacent to each other.
<html>
<head>
<title>css float property example</title>
</head>
font-style : normal;
font-size : 120%;
text-align:center;
}
.arial
font-family : arial;
font-weight : bold;
text-indent : 3cm;
color:white;
}
.body1
{
background : url("D:/ZPB_FullStack/flow.jfif") repeat-y;
Output:
3) Write an HTML and CSS script to create a table with 5 rows and 3 columns. Even no of
rows should be displayed in red color and odd no of rows should be displayed in yellow
color.
table.html
<html>
<head>
<title>CSS DEMO</title>
<link type="text/css" rel="stylesheet" href="demo.css"/>
</head>
<body>
<table border="2" width="30%">
<tr align="center" class="tr2">
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr align="center" class="tr1">
<td>y</td>
<td>y</td>
<td>y</td>
</tr><tr align="center" class="tr2">
<td>z</td>
<td>z</td>
<td>z</td>
</tr><tr align="center" class="tr1">
<td>w</td>
<td>w</td>
<td>w</td>
</tr><tr align="center" class="tr2">
<td>v</td>
<td>v</td>
<td>v</td>
</tr>
</table>
</body>
</html>
demo.css
.tr1
{
background-color:red;
}
.tr2
{
background-color:yellow;
}