SlideShare a Scribd company logo
Cascading Style Sheets
Ms. Yashoda M B
Assistant Professor,
Kristu Jayanti College (Autonomous)
• CSS works by allowing you to associate rules with the elements that appear
in a web page.
• These rules govern how the content of those elements should be rendered.
• CSS rule, which is made of two parts:
h1 { color : green; }
• Selector, which indicates which element or elements the declaration applies
to
• Declaration, which sets out how the elements referred to in the selector
should be styled.
Selector
property value
Declaration
17-02-2024 Yashoda M B 2
• <!DOCTYPE html>
• <html>
• <head>
• <style>
• p {
• color: red;
• text-align: center;
• }
• </style>
• </head>
• <body>
• <p>Hello World!</p>
• <p>These paragraphs are styled with CSS.</p>
• </body>
• </html>
17-02-2024 Yashoda M B 3
The CSS element Selector
• The element selector selects HTML elements based on the element
name.
• Example
• Here, all <p> elements on the page will be center-aligned, with a red
text color:
p {
text-align: center;
color: red;
}
17-02-2024 Yashoda M B 4
The CSS 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.
• Example
• The CSS rule below will be applied to the HTML element with id="para1":
• #para1 {
text-align: center;
color: red;
}
17-02-2024 Yashoda M B 5
The CSS 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.
• Example
• In this example all HTML elements with class="center" will be red and
center-aligned:
• .center {
text-align: center;
color: red;
}
17-02-2024 Yashoda M B 6
• You can also specify that only specific HTML elements should be
affected by a class.
• Example
• In this example only <p> elements with class="center" will be red and
center-aligned:
• p.center {
text-align: center;
color: red;
}
17-02-2024 Yashoda M B 7
The CSS Universal Selector
• The universal selector (*) selects all HTML elements on the page.
• Example
• The CSS rule below will affect every HTML element on the page:
• * {
text-align: center;
color: blue;
}
17-02-2024 Yashoda M B 8
The CSS Grouping Selector
• The grouping selector selects all the HTML elements with the same style definitions.
• Look at the following CSS code (the h1, h2, and p elements have the same style definitions):
h1 {
text-align: center;
color: red;
}
h2 {
text-align: center;
color: red;
}
p {
text-align: center;
color: red;
}
• It will be better to group the selectors, to minimize the code.
• To group selectors, separate each selector with a comma.
• Example
• In this example we have grouped the selectors from the code above:
h1, h2, p {
text-align: center;
color: red;
}
17-02-2024 Yashoda M B 9
• Set the color of all <p> elements to red.
• <style>
• p
• {
•
• color:
• red;
• }
• </style>
17-02-2024 Yashoda M B 10
• Exercise:
• Set the text color to red, for the element with id="para1".
• <style>
• {
•
• red;
• }
• </style>
• <body>
• <h1>This is a heading</h1>
• <p id="para1">This is a paragraph</p>
• </body>
17-02-2024 Yashoda M B 11
• Exercise:
• Set the text color to red, for elements with class="colortext".
• <style>
• {
•
• red;
• }
• </style>
• <body>
• <h1>This is a heading</h1>
• <p>This is a paragraph</p>
• <p class="colortext">This is a paragraph</p>
• <p class="colortext">This is a paragraph</p>
• </body>
17-02-2024 Yashoda M B 12
• Set the text color to red, for all <p> and <h1> elements. Group the selectors to minimize code.
• <style>
• {
•
• red;
• }
• </style>
• <body>
• <h1>This is a heading</h1>
• <h2>This is a smaller heading</h2>
• <p>This is a paragraph</p>
• <p>This is a paragraph</p>
• </body>
17-02-2024 Yashoda M B 13
Internal style sheet
• An internal style sheet may be
used if one single HTML page has
a unique style.
• The internal style sheet is used to
add a unique style for a single
document
• The internal style is defined
inside the <style> element, inside
the head section.
Example
Internal styles are defined within
the <style> element, inside the
<head> section of an HTML page:
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: linen;
}
h1 {
color: maroon;
margin-left: 40px;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
17-02-2024 Yashoda M B 14
External Style Sheets
• An external style sheet is used to define the style for many HTML pages
• To use an external style sheet, add a link to it in the <head> section of each
HTML page
• Each HTML page must include a reference to the external style sheet file
inside the <link> element, inside the head section.
• The external style sheet may be written in any text editor but must be
saved with a .css extension
17-02-2024 Yashoda M B 15
External styles are defined within the <link> element,
inside the <head> section of an HTML page:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="mystyle.c
ss">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
• An external style sheet can be written in any text
editor and must be saved with a .css extension.
• The external .css file should not contain any HTML
tags.
Here is how the "mystyle.css"
file looks like:
"mystyle.css"
body {
background-color: lightblue;
}
h1 {
color: navy;
margin-left: 20px;
}
17-02-2024 Yashoda M B 16
Controlling text
Text Color
The color property is used to set the color of the text. The color is
specified by:
a color name - like "red"
a HEX value - like "#ff0000"
an RGB value - like "rgb(255,0,0)"
17-02-2024 Yashoda M B 17
<body>
<h1>This is heading 1</h1>
<p>This is an ordinary paragraph. Notice
that this text is blue. The default text
color for a page is defined in the body
selector.</p>
<p>Another paragraph.</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
body {
color: blue;
}
h1 {
color: green;
}
</style>
</head>
17-02-2024 Yashoda M B 18
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: lightgrey;
color: blue;
}
h1 {
background-color: black;
color: white;
}
</style>
</head>
Text color and Background color
<body>
<h1>This Heading is Black with White Text</h1>
<p>This page has a grey background color and a blue text.</p>
<p>Another paragraph.</p>
</body>
</html>
17-02-2024 Yashoda M B 19
Text Alignment
• The text-align property is used to set the horizontal alignment of a text.
• A text can be left or right-aligned, centered, or justified.
h1 {
text-align: center;
}
h2 {
text-align: left;
}
h3 {
text-align: right;
}
div {
text-align: justify;
}
17-02-2024 Yashoda M B 20
The text-align-last property specifies how to align the last line of a text.
Example
Align the last line of text in three <p> elements:
p.a {
text-align-last: right;
}
p.b {
text-align-last: center;
}
p.c {
text-align-last: justify;
}
17-02-2024 Yashoda M B 21
Text Decoration
The text-decoration property is used to set
decorations from text.
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
text-decoration: overline;
}
h2 {
text-decoration: line-through;
}
h3 {
text-decoration: underline;
}
</style>
</head>
<body>
<h1>This is heading 1</h1>
<h2>This is heading 2</h2>
<h3>This is heading 3</h3>
</body>
</html>
17-02-2024 Yashoda M B 22
CSS background-image
The background-image property specifies an image to use as the background of an element.
Example
The background image for a page can be set like this:
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("paper.gif");
}
</style>
</head>
<body>
<h1>Hello World!</h1>
<p>This page has an image as the background!</p>
</body>
</html>
17-02-2024 Yashoda M B 23
CSS background-repeat Property
• The background-repeat property sets if/how a background image will
be repeated.
• By default, a background-image is repeated both vertically and
horizontally.
Example
Repeat a background image both vertically and horizontally (this is the
default):
body {
background-image: url("paper.gif");
background-repeat: repeat;
}
17-02-2024 Yashoda M B 24
Example
• Repeat a background image only horizontally:
body {
background-image: url("paper.gif");
background-repeat: repeat-x;
}
Example
• Do not repeat a background image. The image will only be shown once:
body {
background-image: url("paper.gif");
background-repeat: no-repeat;
}
17-02-2024 Yashoda M B 25
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 CSS box model is essentially a box that wraps around every HTML element. It consists of: margins, borders, padding, and the
actual content.
The image below illustrates the box model:
Explanation of the different parts:
Margin - Clears an area outside the border. The margin is transparent
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
17-02-2024 Yashoda M B 26
Example
Demonstration of the box model:
div {
width: 300px;
border: 15px solid green;
padding: 50px;
margin: 20px;
}
Here is the calculation:
320px (width of content area)
+ 20px (left padding + right padding)
+ 10px (left border + right border)
= 350px (total width)
50px (height of content area)
+ 20px (top padding + bottom padding)
+ 10px (top border + bottom border)
= 80px (total height)
17-02-2024 Yashoda M B 27
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: lightblue;
width: 300px;
border: 15px solid red;
padding: 50px;
margin: 20px;
}
</style>
</head>
<body>
<h2>Demonstrating the Box Model</h2>
<p>The CSS box model is essentially a box that wraps around every HTML element. It consists of: borders, padding, margins, and the actual
content.</p>
<div><h1 align=center>KJC</h1>. We have added a 50px padding, 20px margin and a 15px green border. Ut enim ad minim veniam, quis nostrud
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore
eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
</body>
</html>
17-02-2024 Yashoda M B 28
Write the CSS to create above web page
17-02-2024 Yashoda M B 29
Solution:
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 320px;
height: 50px;
padding: 10px;
border: 5px solid gray;
margin: 0;
}
</style>
</head>
<body>
<h2>Calculate the total width:</h2>
<img src="klematis4_big.jpg" width="350" height="263" alt="Klematis">
<div>The picture above is 350px wide. The total width of this element is also 350px. The total height of this element is 80px.</div>
</body>
</html>
17-02-2024 Yashoda M B 30

More Related Content

Similar to Cascading style sheet: complete explanation with some examples (20)

PPTX
Cascading style sheet, CSS Box model, Table in CSS
SherinRappai
 
PPTX
CSS – CASCADING STYLE SHEET - MY_PPT.pptx
IorlahaSamuel1
 
PPTX
CSS____4563276__HTML___________0989.pptx
Ajanya5
 
PPTX
Cascading Styling Sheets(CSS) simple design language intended to transform th...
JebaRaj26
 
DOCX
CSS Tutorial For Basic Students Studying
nirmala119429
 
PPTX
Introduction to CSS
Folasade Adedeji
 
PPTX
BITM3730 9-19.pptx
MattMarino13
 
DOC
Css introduction
vishnu murthy
 
PDF
TUTORIAL DE CSS 2.0
Vladimir Valencia
 
PDF
Web Development Using CSS3
Anjan Mahanta
 
PPTX
Web Development Using CSS3
Anjan Mahanta
 
PPT
Make Css easy : easy tips for css
shabab shihan
 
PPTX
cascading style sheets- About cascading style sheets on the selectors
JayanthiM19
 
PPTX
chitra
sweet chitra
 
PPT
Css siva
ch samaram
 
PPT
Css siva
ch samaram
 
PPTX
BITM3730 9-20.pptx
MattMarino13
 
PPTX
v5-introduction to html-css-210321161444.pptx
hannahroseline2
 
PDF
Web Design Course: CSS lecture 1
Gheyath M. Othman
 
Cascading style sheet, CSS Box model, Table in CSS
SherinRappai
 
CSS – CASCADING STYLE SHEET - MY_PPT.pptx
IorlahaSamuel1
 
CSS____4563276__HTML___________0989.pptx
Ajanya5
 
Cascading Styling Sheets(CSS) simple design language intended to transform th...
JebaRaj26
 
CSS Tutorial For Basic Students Studying
nirmala119429
 
Introduction to CSS
Folasade Adedeji
 
BITM3730 9-19.pptx
MattMarino13
 
Css introduction
vishnu murthy
 
TUTORIAL DE CSS 2.0
Vladimir Valencia
 
Web Development Using CSS3
Anjan Mahanta
 
Web Development Using CSS3
Anjan Mahanta
 
Make Css easy : easy tips for css
shabab shihan
 
cascading style sheets- About cascading style sheets on the selectors
JayanthiM19
 
chitra
sweet chitra
 
Css siva
ch samaram
 
Css siva
ch samaram
 
BITM3730 9-20.pptx
MattMarino13
 
v5-introduction to html-css-210321161444.pptx
hannahroseline2
 
Web Design Course: CSS lecture 1
Gheyath M. Othman
 

More from yashodamb (10)

PPTX
conversion of Infix to Postfix conversion using stack
yashodamb
 
PPTX
Introduction to Cloud computing concept.pptx
yashodamb
 
PPTX
Introduction to Database Management System.pptx
yashodamb
 
PDF
Unit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdf
yashodamb
 
PPTX
applets.pptx
yashodamb
 
PPTX
Navigation between the worksheets.pptx
yashodamb
 
PPTX
Implementing a Java Program.pptx
yashodamb
 
PPTX
Thread life cycle.pptx
yashodamb
 
PPTX
DECISION MAKING STATEMENTS.pptx
yashodamb
 
PPTX
History of C programming.pptx
yashodamb
 
conversion of Infix to Postfix conversion using stack
yashodamb
 
Introduction to Cloud computing concept.pptx
yashodamb
 
Introduction to Database Management System.pptx
yashodamb
 
Unit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdf
yashodamb
 
applets.pptx
yashodamb
 
Navigation between the worksheets.pptx
yashodamb
 
Implementing a Java Program.pptx
yashodamb
 
Thread life cycle.pptx
yashodamb
 
DECISION MAKING STATEMENTS.pptx
yashodamb
 
History of C programming.pptx
yashodamb
 
Ad

Recently uploaded (20)

PPTX
How to Create Rental Orders in Odoo 18 Rental
Celine George
 
PPTX
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
PDF
IMP NAAC-Reforms-Stakeholder-Consultation-Presentation-on-Draft-Metrics-Unive...
BHARTIWADEKAR
 
PDF
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PPTX
Capitol Doctoral Presentation -July 2025.pptx
CapitolTechU
 
PPTX
Quarter1-English3-W4-Identifying Elements of the Story
FLORRACHELSANTOS
 
PPTX
Pyhton with Mysql to perform CRUD operations.pptx
Ramakrishna Reddy Bijjam
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PPTX
Gall bladder, Small intestine and Large intestine.pptx
rekhapositivity
 
PPTX
How to Configure Lost Reasons in Odoo 18 CRM
Celine George
 
PDF
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
PPTX
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PDF
1, 2, 3… E MAIS UM CICLO CHEGA AO FIM!.pdf
Colégio Santa Teresinha
 
PPTX
How to Manage Promotions in Odoo 18 Sales
Celine George
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PPTX
Views on Education of Indian Thinkers Mahatma Gandhi.pptx
ShrutiMahanta1
 
How to Create Rental Orders in Odoo 18 Rental
Celine George
 
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
IMP NAAC-Reforms-Stakeholder-Consultation-Presentation-on-Draft-Metrics-Unive...
BHARTIWADEKAR
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
Dimensions of Societal Planning in Commonism
StefanMz
 
Capitol Doctoral Presentation -July 2025.pptx
CapitolTechU
 
Quarter1-English3-W4-Identifying Elements of the Story
FLORRACHELSANTOS
 
Pyhton with Mysql to perform CRUD operations.pptx
Ramakrishna Reddy Bijjam
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
Gall bladder, Small intestine and Large intestine.pptx
rekhapositivity
 
How to Configure Lost Reasons in Odoo 18 CRM
Celine George
 
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
1, 2, 3… E MAIS UM CICLO CHEGA AO FIM!.pdf
Colégio Santa Teresinha
 
How to Manage Promotions in Odoo 18 Sales
Celine George
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
Views on Education of Indian Thinkers Mahatma Gandhi.pptx
ShrutiMahanta1
 
Ad

Cascading style sheet: complete explanation with some examples

  • 1. Cascading Style Sheets Ms. Yashoda M B Assistant Professor, Kristu Jayanti College (Autonomous)
  • 2. • CSS works by allowing you to associate rules with the elements that appear in a web page. • These rules govern how the content of those elements should be rendered. • CSS rule, which is made of two parts: h1 { color : green; } • Selector, which indicates which element or elements the declaration applies to • Declaration, which sets out how the elements referred to in the selector should be styled. Selector property value Declaration 17-02-2024 Yashoda M B 2
  • 3. • <!DOCTYPE html> • <html> • <head> • <style> • p { • color: red; • text-align: center; • } • </style> • </head> • <body> • <p>Hello World!</p> • <p>These paragraphs are styled with CSS.</p> • </body> • </html> 17-02-2024 Yashoda M B 3
  • 4. The CSS element Selector • The element selector selects HTML elements based on the element name. • Example • Here, all <p> elements on the page will be center-aligned, with a red text color: p { text-align: center; color: red; } 17-02-2024 Yashoda M B 4
  • 5. The CSS 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. • Example • The CSS rule below will be applied to the HTML element with id="para1": • #para1 { text-align: center; color: red; } 17-02-2024 Yashoda M B 5
  • 6. The CSS 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. • Example • In this example all HTML elements with class="center" will be red and center-aligned: • .center { text-align: center; color: red; } 17-02-2024 Yashoda M B 6
  • 7. • You can also specify that only specific HTML elements should be affected by a class. • Example • In this example only <p> elements with class="center" will be red and center-aligned: • p.center { text-align: center; color: red; } 17-02-2024 Yashoda M B 7
  • 8. The CSS Universal Selector • The universal selector (*) selects all HTML elements on the page. • Example • The CSS rule below will affect every HTML element on the page: • * { text-align: center; color: blue; } 17-02-2024 Yashoda M B 8
  • 9. The CSS Grouping Selector • The grouping selector selects all the HTML elements with the same style definitions. • Look at the following CSS code (the h1, h2, and p elements have the same style definitions): h1 { text-align: center; color: red; } h2 { text-align: center; color: red; } p { text-align: center; color: red; } • It will be better to group the selectors, to minimize the code. • To group selectors, separate each selector with a comma. • Example • In this example we have grouped the selectors from the code above: h1, h2, p { text-align: center; color: red; } 17-02-2024 Yashoda M B 9
  • 10. • Set the color of all <p> elements to red. • <style> • p • { • • color: • red; • } • </style> 17-02-2024 Yashoda M B 10
  • 11. • Exercise: • Set the text color to red, for the element with id="para1". • <style> • { • • red; • } • </style> • <body> • <h1>This is a heading</h1> • <p id="para1">This is a paragraph</p> • </body> 17-02-2024 Yashoda M B 11
  • 12. • Exercise: • Set the text color to red, for elements with class="colortext". • <style> • { • • red; • } • </style> • <body> • <h1>This is a heading</h1> • <p>This is a paragraph</p> • <p class="colortext">This is a paragraph</p> • <p class="colortext">This is a paragraph</p> • </body> 17-02-2024 Yashoda M B 12
  • 13. • Set the text color to red, for all <p> and <h1> elements. Group the selectors to minimize code. • <style> • { • • red; • } • </style> • <body> • <h1>This is a heading</h1> • <h2>This is a smaller heading</h2> • <p>This is a paragraph</p> • <p>This is a paragraph</p> • </body> 17-02-2024 Yashoda M B 13
  • 14. Internal style sheet • An internal style sheet may be used if one single HTML page has a unique style. • The internal style sheet is used to add a unique style for a single document • The internal style is defined inside the <style> element, inside the head section. Example Internal styles are defined within the <style> element, inside the <head> section of an HTML page: <!DOCTYPE html> <html> <head> <style> body { background-color: linen; } h1 { color: maroon; margin-left: 40px; } </style> </head> <body> <h1>This is a heading</h1> <p>This is a paragraph.</p> </body> </html> 17-02-2024 Yashoda M B 14
  • 15. External Style Sheets • An external style sheet is used to define the style for many HTML pages • To use an external style sheet, add a link to it in the <head> section of each HTML page • Each HTML page must include a reference to the external style sheet file inside the <link> element, inside the head section. • The external style sheet may be written in any text editor but must be saved with a .css extension 17-02-2024 Yashoda M B 15
  • 16. External styles are defined within the <link> element, inside the <head> section of an HTML page: <!DOCTYPE html> <html> <head> <link rel="stylesheet" type="text/css" href="mystyle.c ss"> </head> <body> <h1>This is a heading</h1> <p>This is a paragraph.</p> </body> </html> • An external style sheet can be written in any text editor and must be saved with a .css extension. • The external .css file should not contain any HTML tags. Here is how the "mystyle.css" file looks like: "mystyle.css" body { background-color: lightblue; } h1 { color: navy; margin-left: 20px; } 17-02-2024 Yashoda M B 16
  • 17. Controlling text Text Color The color property is used to set the color of the text. The color is specified by: a color name - like "red" a HEX value - like "#ff0000" an RGB value - like "rgb(255,0,0)" 17-02-2024 Yashoda M B 17
  • 18. <body> <h1>This is heading 1</h1> <p>This is an ordinary paragraph. Notice that this text is blue. The default text color for a page is defined in the body selector.</p> <p>Another paragraph.</p> </body> </html> <!DOCTYPE html> <html> <head> <style> body { color: blue; } h1 { color: green; } </style> </head> 17-02-2024 Yashoda M B 18
  • 19. <!DOCTYPE html> <html> <head> <style> body { background-color: lightgrey; color: blue; } h1 { background-color: black; color: white; } </style> </head> Text color and Background color <body> <h1>This Heading is Black with White Text</h1> <p>This page has a grey background color and a blue text.</p> <p>Another paragraph.</p> </body> </html> 17-02-2024 Yashoda M B 19
  • 20. Text Alignment • The text-align property is used to set the horizontal alignment of a text. • A text can be left or right-aligned, centered, or justified. h1 { text-align: center; } h2 { text-align: left; } h3 { text-align: right; } div { text-align: justify; } 17-02-2024 Yashoda M B 20
  • 21. The text-align-last property specifies how to align the last line of a text. Example Align the last line of text in three <p> elements: p.a { text-align-last: right; } p.b { text-align-last: center; } p.c { text-align-last: justify; } 17-02-2024 Yashoda M B 21
  • 22. Text Decoration The text-decoration property is used to set decorations from text. <!DOCTYPE html> <html> <head> <style> h1 { text-decoration: overline; } h2 { text-decoration: line-through; } h3 { text-decoration: underline; } </style> </head> <body> <h1>This is heading 1</h1> <h2>This is heading 2</h2> <h3>This is heading 3</h3> </body> </html> 17-02-2024 Yashoda M B 22
  • 23. CSS background-image The background-image property specifies an image to use as the background of an element. Example The background image for a page can be set like this: <!DOCTYPE html> <html> <head> <style> body { background-image: url("paper.gif"); } </style> </head> <body> <h1>Hello World!</h1> <p>This page has an image as the background!</p> </body> </html> 17-02-2024 Yashoda M B 23
  • 24. CSS background-repeat Property • The background-repeat property sets if/how a background image will be repeated. • By default, a background-image is repeated both vertically and horizontally. Example Repeat a background image both vertically and horizontally (this is the default): body { background-image: url("paper.gif"); background-repeat: repeat; } 17-02-2024 Yashoda M B 24
  • 25. Example • Repeat a background image only horizontally: body { background-image: url("paper.gif"); background-repeat: repeat-x; } Example • Do not repeat a background image. The image will only be shown once: body { background-image: url("paper.gif"); background-repeat: no-repeat; } 17-02-2024 Yashoda M B 25
  • 26. 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 CSS box model is essentially a box that wraps around every HTML element. It consists of: margins, borders, padding, and the actual content. The image below illustrates the box model: Explanation of the different parts: Margin - Clears an area outside the border. The margin is transparent 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 17-02-2024 Yashoda M B 26
  • 27. Example Demonstration of the box model: div { width: 300px; border: 15px solid green; padding: 50px; margin: 20px; } Here is the calculation: 320px (width of content area) + 20px (left padding + right padding) + 10px (left border + right border) = 350px (total width) 50px (height of content area) + 20px (top padding + bottom padding) + 10px (top border + bottom border) = 80px (total height) 17-02-2024 Yashoda M B 27
  • 28. <!DOCTYPE html> <html> <head> <style> div { background-color: lightblue; width: 300px; border: 15px solid red; padding: 50px; margin: 20px; } </style> </head> <body> <h2>Demonstrating the Box Model</h2> <p>The CSS box model is essentially a box that wraps around every HTML element. It consists of: borders, padding, margins, and the actual content.</p> <div><h1 align=center>KJC</h1>. We have added a 50px padding, 20px margin and a 15px green border. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div> </body> </html> 17-02-2024 Yashoda M B 28
  • 29. Write the CSS to create above web page 17-02-2024 Yashoda M B 29
  • 30. Solution: <!DOCTYPE html> <html> <head> <style> div { width: 320px; height: 50px; padding: 10px; border: 5px solid gray; margin: 0; } </style> </head> <body> <h2>Calculate the total width:</h2> <img src="klematis4_big.jpg" width="350" height="263" alt="Klematis"> <div>The picture above is 350px wide. The total width of this element is also 350px. The total height of this element is 80px.</div> </body> </html> 17-02-2024 Yashoda M B 30