Professional Documents
Culture Documents
Chapter Three: Cascaded Style Sheets (CSS)
Chapter Three: Cascaded Style Sheets (CSS)
1
CSS
CSS stands for “Cascading Style Sheets”
CSS stands for “Cascading Style Sheets”
CSS is a method of presenting data in a web page.
CSS offers web developers methods to control the presentation
of data with many more options offered by HTML alone.
Used to specify the presentation of elements separately from
the structure of the document.
One of its major strengths is that a web developer can create a
style sheet (filename.css) save it with the other web pages
which are then linked to this style sheet.
A web developer can update or change one style sheet and
thereby alter the presentation of all the web pages on a web
site -- it is particularly useful in constructing large or corporate
web sites.
2
CSS
• Cascading Style Sheets (CSS) form the
presentation layer of the user interface.
Structure (XHTML)
Behavior (Client-Side Scripting)
Presentation (CSS)
• Tells the browser agent how the element is to be
presented to the user.
• CSS has some limitations, the first is that not all
browsers support all the features so it is
important to check your web pages on several
browsers or at least the one used by your clients.
3
CSS Inheritance
• Allows elements to “inherit” styles from
parent elements.
• Helpful in reducing the amount of CSS to set
styles for child elements.
• Unless a more specific style is set on a child
element, the element looks to the parent
element for its styles.
Why use style sheets?
• Separate structure from appearance
• This separation of structure from presentation
simplifies maintaining and modifying a Webpage
document’s layout.
• Create consistent appearance Structure
Style-
formatting
</table>
</body>
6
Here in CSS Format
<head>
<style type=”text/css”>
.subtext { font-family: arial;
font-size: 10px;
color: red;
}
</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
Basic Syntax
• Made up of three parts:
body {
font-family: arial;
font-size: 100%;
}
11
CSS Style Rule Syntax
12
CSS Style Rule Syntax
16
CSS Style Rule Syntax
17
CSS Style Rule Syntax
Grouping Selectors:
• You can combine various class selectors together
as shown below:
#content, #footer, #supplement {
position: absolute;
left: 510px;
width: 200px;
}
21
CSS Style Rule Syntax
CSS Pseudo-classes: are used to add special effects to some selectors.
• The syntax of pseudo-classes:
selector:pseudo-class {property:value;}
• CSS classes can also be used with pseudo-classes:
selector.class:pseudo-class {property:value;}
• Anchor Pseudo-classes
• Links can be displayed in different ways in a CSS-supporting
browser:
Example
a:link {color:#FF0000;} /* unvisited link */
a:visited {color:#00FF00;} /* visited link */
a:hover {color:#FF00FF;} /* mouse over link */
a:active {color:#0000FF;} /* selected link */
22
CSS Style Rule Syntax
id vs class
• There are two attributes with special meaning
to CSS
Id:- Marks a unique block within the document
for styling (use only once)
Class:- Marks a non-unique tag within the
document for styling (multi-use)
23
CSS Style Rule Syntax
id vs class
• The difference between an ID and a class is that
an ID can be used to identify one element,
whereas a class can be used to identify more
than one.
• You can also apply a selector to a specific HTML
element by simply stating the HTML selector
first, so p.jam { whatever } will only be applied to
paragraph elements that have the class 'jam'.
24
CSS Style Rule Syntax
This class function can then be used to apply
this style to several different HTML tags in the
body of your web page.
25
CSS Style Rule Syntax
• When referring to a Class selector you simply
add the class to an HTML tag like in the above
example (class="classname") do not include the
period before the name.
• id selectors are used to define unique parts of a
web page e.g. footer, header, table or other
unique element.
• Each Id element is unique and can only be used
once.
26
CSS Style Rule Syntax
Id selectors are preceded with the # hash symbol
and are unique – in other words you can only
have one specific id selector on a web page.
They are used to define specific “boxes” on the
page or layers.
27
CSS Inclusion - Associating Styles
28
CSS Inclusion - Associating Styles
Example:
• Following is the example of embed CSS based on
above syntax:
<head>
<style type="text/css">
h1{
color: #36C;
}
</style>
</head>
31
CSS Inclusion - Associating Styles
32
CSS Inclusion - Associating Styles
Attributes:
Attribute Value Description
33
CSS Inclusion - Associating Styles
Example:
• Following is the example of inline CSS based on
above syntax:
34
CSS Inclusion - Associating Styles
35
CSS Inclusion - Associating Styles
36
CSS Inclusion - Associating Styles
screen
tty
tv
projection
Specifies the device the document will be displayed
media handheld
on. Default value is all. This is optional attribute.
print
braille
aural
all
37
CSS Inclusion - Associating Styles
Example:
• Consider a simple style sheet file with a name
mystyle.css having the following rules:
h1, h2, h3 {
color: #36C;
font-weight: normal;
letter-spacing: .4em;
margin-bottom: 1em;
text-transform: lowercase;
}
38
CSS Inclusion - Associating Styles
Example:
• Now you can include this file mystyle.css in any
HTML document as follows:
<head>
<link type="text/css" href="mystyle.css" />
</head>
39
Main.html
<!DOCTYPE html>
<html>
<head>
<meta charset = "utf-8">
<title>Inline Styles</title>
</head>
Inline CSS <body>
<p style = "font-size: 20pt;">
This text has font-size style applied to it
</p>
</body>
</html>
Main.html
<!DOCTYPE html>
<html>
<head>
<meta charset = "utf-8">
<title>Inline Styles</title>
<style type = "text/css">
p { font-size: 20pt;} Embedded CSS
</style>
</head>
<body>
<p>This text has font-size style applied to it </p>
</body>
</html> CS100: Internet and Web Technologies 40
Main.html
<!DOCTYPE html>
<html>
<head>
<meta charset = "utf-8">
<title>Inline Styles</title>
<link rel = "stylesheet" type = "text/css"
href = “Styles.css">
</head>
<body>
<p>This text has font-size style applied to it </p> External
</body>
</html>
CSS
Styles.css
p { font-size: 20pt;}
CSS Comments:
• You simple put your comments inside
/*.....this is a comment in style sheet.....*/.
• You can use /* ....*/ to comment multi-line
blocks in similar way you do in C and C++
programming languages.
43
Common CSS properties: Background
CSS Background
• CSS background properties are used to style the
background of any HTML element.
• CSS properties used for background effects:
a) background-color
b) background-image
c) background-repeat
d) background-attachment
e) background-position
44
Common CSS properties: Background
background-color
• The background-color property specifies the
background color of an element.
• The background color of a page is defined in the
body selector:
Example:
body {
background-color:#b0c4de;
}
45
Common CSS properties: Background
background-image
• To add a background image to an element .
• By default, the image is repeated so it covers the
entire element.
• The background image for a page can be set like
this:
Example
body {
background-image: url(‘image.jpg');
}
46
Common CSS properties: Background
background-repeat
• Determines whether the background image repeats (tiles)
or not.
• Possible Values:
repeat (background repeats both horizontally and vertically)
repeat-x (background repeats horizontally only)
repeat-y (background repeats vertically only)
no-repeat (background is not repeated)
Example
body
{
background-image: url(“image.jpg”);
background-repeat: repeat-x;
} 47
Common CSS properties: Background
Background-position
• Example : set the background image position 100
pixels away from the left side.
body
{
background- image: url(/images/pattern1.gif);
background-position:100px;
}
48
Common CSS properties: Background
• Example 2: set the background image position
100 pixels away from the left side and 200 pixels
down from the top.
table
{
background-image: url(/images/pattern1.gif);
background-position:100px 200px;
}
49
Common CSS properties
Background-attachment
• determines whether a background image is
fixed or scrolls with the rest of the page.
• Possible Values:
fixed
scroll
P
{
background-image:url(/images/pattern1.gif);
background-attachment:fixed;
}
50
Common CSS properties: Background
background
• Property is used as shorthand to specify a
number of other background properties.
P
{
background: url(/images/pattern1.gif) repeat fixed;
}
51
Common CSS Properties: Fonts
• You can set following font properties of an
element:
font-family :- is used to change the face of a font.
• Possible value could be any font family name.
font-style:- is used to make a font italic or oblique.
Following is the example which demonstrates
how to set the font style of an element.
• Possible values are normal, italic and oblique.
font-variant:- is used to create a small-caps effect.
• Possible values are normal and small-caps.
52
Common CSS Properties: Fonts
font-weight:- is used to increase or decrease how
bold or light a font appears.
• Possible values could be normal, bold, bolder,
lighter, 100, 200, 300, 400, 500, 600, 700, 800, 900.
font-size:- is used to increase or decrease the size of
a font.
• Possible values could be xx-small, x-small, small,
medium, large, x-large, xx-large, smaller, larger, size
in pixels or in %
Font:- is used as shorthand to specify a number of
other font properties.
53
Common CSS Properties: Fonts
P {
font-family: georgia, garamond, serif;
font-size:20px;
font-style: italic;
font-weight: bold;
}
• You can use the font property to set all the font
properties at once. For example:
P{
font: italic small-caps bold 15px georgia;
}
54
Common CSS Properties: Text
• You can set following text properties of an
element:
color :- is used to set the color of a text.
direction:- is used to set the text direction.
• Possible values are ltr or rtl.
letter-spacing:- is used to add or subtract space
between the letters that make up a word.
• Possible values are normal or a number
specifying space..
55
Common CSS Properties: Text
word-spacing:- is used to add or subtract space
between the words of a sentence.
• Possible values are normal or a number
specifying space..
text-indent:- is used to indent the text of a
paragraph.
• Possible values are % or a number specifying
indent space.
text-align:- is used to align the text of a
document.
• Possible values are left, right, center, justify.. 56
Common CSS Properties: Text
text-decoration:- is used to underline, overline, and
strikethrough text.
• Possible values are none, underline, overline, line-
through, blink.
text-transform:- is used to capitalize text or convert
text to uppercase or lowercase letters.
• Possible values are none, capitalize, uppercase,
lowercase.
white-space:- is used to control the flow and
formatting of text.
• Possible values are normal, pre, nowrap.
57
Common CSS Properties: Text
P{
color: red;
direction: rtl;
letter-spacing:5px;
word-spacing:5px;
text-indent:1cm;
text-decoration: blink;
}
58
Common CSS Properties: Links
59
Common CSS Properties: Links
60
Common CSS Properties: Links
Example:
<style type="text/css">
a:link {color: #000000}
a:visited {color: #006600}
a:hover {color: #FFCC00}
a:active {color: #FF00CC}
</style>
61
Common CSS Properties: Lists
64
Common CSS Properties: Lists
65
Common CSS Properties: Lists
67
Common CSS Properties: Borders
The border-color Property:
• The border-color property allows you to change the
color of the border surrounding an element.
• You can individually change the color of the bottom,
left, top and right sides of an element's border using
the properties:
• border-bottom-color changes the color of bottom
border.
• border-top-color changes the color of top border.
• border-left-color changes the color of left border.
• border-right-color changes the color of right border.
68
Common CSS Properties: Borders
Border Properties
• The border property allows you to specify
color, style, and width of lines in one
property:
P{
border: 4px solid red;
}
71
Common CSS Properties: Margins
72
Common CSS Properties: Margins
73
Common CSS Properties: Margins
76
Common CSS Properties: Paddings
78
Common CSS Properties: Paddings
81
Common CSS Properties: Positioning
Absolute Positioning:
• An element with position: absolute is positioned at the
specified coordinates relative to your screen top left corner.
• You can use two values top and left along with position property
to move an HTML element anywhere in HTML document.
Here's a quick reference when moving HTML elements in CSS.
Move Left - Use a negative value for left.
Move Right - Use a positive value for left.
Move Up - Use a negative value for top.
Move Down - Use a positive value for top.
NOTE: You can use bottom or right values as well in the same
way as top and left.
82
Common CSS Properties: Positioning
• Here is the example:
83
Common CSS Properties: Positioning
Fixed Positioning:
• Fixed positioning allows you to fix the position of an element
to a particular spot on the page - regardless of scrolling.
• Specified coordinates will be relative to the browser window.
• You can use two values top and left along with position
property to move an HTML element anywhere in HTML
document.
Move Left - Use a negative value for left.
Move Right - Use a positive value for left.
Move Up - Use a negative value for top.
Move Down - Use a positive value for top.
NOTE: You can use bottom or right values as well in the
same way as top and left.
84
Common CSS Properties: Positioning
• Here is the example:
85