IP - Lecture 6, 7 Chapter-3
IP - Lecture 6, 7 Chapter-3
Cascading Style
Sheets
1
CASCADING STYLE SHEETS
Objectives:
Ease of maintenance
5
COMPARE CLASSIC HTML WITH CSS
<body>
<table>
<tr><td bgcolor="#FFCC00" align="left"><font
face="arial" size=“2" color="red"><b>this is line
1</b></font></td></tr>
<tr><td bgcolor="#FFCC00" align="left"><font
face="arial" size=“2" ><b>this is line
2</b></font></td></tr>
<tr><td bgcolor="#FFCC00" align="left"><font
face="arial" size=“2" color="red"><b>this is line
3</b></font></td></tr>
</table>
6
</body>
HERE IN CSS FORMAT
<head>
<style type=”text/css”>
.subtext { font-family: arial;
font-size: 10px;
color: red;
background-color:#FFCC00;
}
</style>
</head>
<body>
<table>
<tr><td class="subtext">this is line 1</td></tr>
<tr><td class="subtext">this is line 2</td></tr>
<tr><td class="subtext">this is line 3</td></tr>
</table>
</body> 7
ANATOMY OF A CSS RULE
body{
font-family: arial, sans-serif;
font-size: 100%; }
Some examples:
H2 {color: blue;}
P{
font-size: 12pt;
font-family: Verdana, sans-serif;
}
h1, h2, h3 {
color: yellow;
background-color: black;
} 9
CSS STRUCTURE
HTML Selector { property: value;
property2: value2; etc }
p { font-family: times; …}
Note: The property is followed by a colon (:) and
the value is followed by a semicolon(;)
Selector- HTML elements you wish to style
tells a browser which elements in a page will be
affected by the rule.
Property- attribute you wish to change
Value-the value that the property takes
10
Types of Selectors
11
Class selectors-uses the HTML class attribute
which find elements with the specific class.
used to apply the same style to many different
HTML tags on the same class
.ClassSelector { Property: Value; }
Class selectors are preceded by dot followed by
a name and then a series of property: values.
Class selectors can have almost any name, usually
you should pick a name that describes it function.
e.g.
Inline Styles
20
<html >
<head>
<title>Inline Styles</title>
</head>
<body>
<p>This text does not have any style applied to it.</p>
<!-- The style attribute allows you to declare -->
<!-- inline styles. Separate multiple styles -->
<!-- with a semicolon. -->
<p style = "font-size: 20pt">This text has the <em>font-
size</em> style applied to it, making it 20pt.
</p>
<p style = "font-size: 20pt; color: #0000ff">
This text has the <em>font-size</em> and <em>color</em>
styles applied to it, making it 20pt. and blue.</p>
</body>
</html> 21
22
EMBEDDED STYLE SHEETS
print: Printer
projection: Projector
tv: Television
30
/* An external stylesheet */
a { text-decoration: none }
a:hover { text-decoration: underline; color: red;
background-color: #ccffcc }
li em { color: red; font-weight: bold; background-
color: #ffffff }
ul { margin-left: 2cm }
ul ul { text-decoration: underline; margin-left: .5cm
}
Once we wrote the style we need to save it with
filename.css format E.g styles.css 31
<!-- Linking external style sheets -->
<html >
<head>
<title>Linking External Style Sheets</title>
<link rel = "stylesheet" type = "text/css"
href = "styles.css" />
</head>
<body>
<h1>Shopping list for <em>Monday</em>:</h1>
<ul>
<li>Milk</li>
<li>Bread
32
<ul>
<li>White bread</li>
<li>Rye bread</li>
<li>Whole wheat bread</li>
</ul>
</li>
<li>Rice</li>
<li>Potatoes</li>
<li>Pizza <em>with mushrooms</em></li>
</ul>
<p>
<a href = "http://www.food.com">Go to the Grocery
store</a>
</p>
</body>
33
</html>
34
THE CSS BOX MODEL
AllHTML 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 HTML elements, and it
consists of: margins, borders, padding, and
the actual content.
The box model allows us to place a border
around elements and to define space
between elements.
35
The image below illustrates the box model:
36
Border - A border that goes around the padding and
content.
Padding - Clears an area around the content. The
padding is affected by the background color of the box
Content - The content of the box, where text and
images appear
In order to set the width and height of an element
correctly in all browsers, you need to know how the box
model works.
37
Important: When you specify the width and height
properties of an element with CSS, you are just
setting the width and height of the content area.
To know the full size of the element, you must also
add the padding, border and margin.
The total width of the element in the example below
is 300px:
width:250px;
padding:10px;
border:5px solid gray;
margin:10px;
Let's do the math:
250px (width)
+ 20px (left and right padding)
+ 10px (left and right border)
+ 20px (left and right margin) 38
= 300px
Thetotal width of an element should
always be calculated like this:
Total element width = width + left padding + right
padding + left border + right border + left margin +
right margin
Thetotal height of an element should
always be calculated like this:
Total element height = height + top padding + bottom
padding + top border + bottom border + top margin +
bottom margin
39
<html>
<head>
<style type="text/css">
div.ex
{
width:220px;
padding:10px;
border:5px solid gray;
margin:0px;
}
</style>
</head>
40
CSS BORDER PROPERTIES
The CSS border properties allow you to specify
the style and color of an element's border.
Border-style
Border-color
Border-width
Border Style
The border-style property specifies what kind of
border to display.
None of the border properties will have ANY effect
unless the border-style property is set!
border-style values:
none: Defines no border
dotted: Defines a dotted border
dashed: Defines a dashed border 41
42
<!DOCTYPE html>
<html>
<head><style>
p.none {border-style: none;border-color: Green;}
p.dotted {border-style: dotted;border-color: Green;}
p.dashed {border-style: dashed;border-color: Green;}
p.solid {border-style: solid;border-color: Green;}
p.double {border-style: double;border-color: Green;}
p.groove {border-style: groove; border-color: Green;}
p.ridge {border-style: ridge; border-color: Green;}
p.inset {border-style: inset; border-color: Green;}
p.outset {border-style: outset;border-color: Green;}
p.hidden {border-style: hidden;border-color: Green;}
</style>
43
</head>
<body>
<p class="none">No border.</p>
<p class="dotted">A dotted border.</p>
<p class="dashed">A dashed border.</p>
<p class="solid">A solid border.</p>
<p class="double">A double border.</p>
<p class="groove">A groove border.</p>
<p class="ridge">A ridge border.</p>
<p class="inset">An inset border.</p>
<p class="outset">An outset border.</p>
<p class="hidden">A hidden border.</p>
</body>
</html>
44
45
Border Width
The border-width property is used to set the width of the
border.
The width is set in pixels, or by using one of the three pre-
defined values: thin, medium, or thick.
Note: The "border-width" property does not work if it is
used alone. Use the "border-style" property to set the
borders first.
p.one
{
border-style:solid;
border-width:5px;
}
p.two
{
border-style:solid; 46
border-width:medium;
}
Imagine that you only had 250px of space. Let's
make an element with a total width of 250px:
Example
width:220px;
padding:10px;
border:5px solid gray;
margin:0px;
47
READING ASSIGNMENT- READ ABOUT THE
FOLLOWING HTML5 ELEMENTS AND INCLUDE THEM IN
YOUR PROJECT IMPLEMENTATION
<article> <main>
<aside> <mark>
<details> <nav>
<figcaption> <section>
<figure> <summary>
<footer> <time>
<header>
<video>
<audio>
48