Lecture 3: CSS
Lecture 3: CSS
Lecture 3: CSS
Objectives
Topics covered:
using a style sheet to give all the pages of a Web site the
same look and feel.
style sheet types and cascade precedence
CSS syntax for assigning style properties
Class selector and using the class attribute to apply
styles.
using style sheets to separate presentation from content.
Introduction
Cascading
page
Same style sheet can be applied to the multiple Web
page
vs. User
Inline Styles
style
Attribute style
CSS (style) property
Followed
Outline
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4
5
-->
7
8
9
10
11
inline.html
(1 of 2)
12
13
<body>
14
15
16
17
18
-->
19
-->
20
21
22
</p>
23
24
25
26
27
28
29
</body>
30 </html>
Outline
this method can only specify style information for the current
document:
1:1 relationship
However, the same document may have other style definitions
applied to it
1:M relationship
The CSS syntax is made up of three parts: a selector, a property and a value:
selector {property: value}
A selector can be:
the HTML element/tag you wish to define. In this example:
body {color: black} , element body is the selector
The class selector, which can be tied to a specific element, such as:
p.right {text-align: right}
The class selector applicable to any element, such as:
.loud {font-family: fantasy; font-size: xx-large}
Group selectors are possible for a group of HTML elements. For example:
span element
div element (block level element)
Outline
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4
5
7
8
9
10
-->
declared.html
(1 of 3)
11
12
13
14
15
em
16
{ background-color: #8000ff;
color: white }
17
18
h1
{ font-size: 14pt }
19
20
21
22
23
24
25
</style>
</head>
26
27
Outline
<body>
28
29
30
31
32
33
34
35
36
37
38
39
declared.html
(2 of 3)
40
41
<h1>Clients</h1>
42
43
44
45
46
47
48
49
50
51
</body>
52 </html>
Outline
Outline
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4
5
-->
7
8
9
10
<head>
<title>More Styles</title>
11
12
advance.html
(1 of 3)
13
14
a.nodec
{ text-decoration: none }
a:hover
{ text-decoration: underline;
15
16
17
color: red;
18
background-color: #ccffcc }
19
20
li em
21
{ color: red;
font-weight: bold }
22
23
24
ul
{ margin-left: 75px }
25
ul ul
{ text-decoration: underline;
26
margin-left: 15px }
27
28
29
</style>
</head>
30
31
<body>
advance.html
(2 of 3)
32
33
34
35
<ul>
36
<li>Milk</li>
37
<li>Bread
38
<ul>
39
<li>White bread</li>
40
<li>Rye bread</li>
41
42
</ul>
43
</li>
44
<li>Rice</li>
45
<li>Potatoes</li>
46
47
48
</ul>
Outline
49
50
51
52
</body>
53 </html>
Outline
Location (href) and type (type) of the external style sheet are specified
as attributes of a link element in the head portion of an XHTML
document
In addition, the rel attribute specifies the nature of the relationship
between the stylesheet and the document that is referencing it
Persistent stylesheets must be applied to the document
Preferred style sheet should be applied the unless the user has
selected a different alternate
Alternate style sheets may be selected by users depending on their
preferences
/* An external stylesheet */
*/
Outline
3
4
{ text-decoration: none }
5
6
color: red;
background-color: #ccffcc }
9
10 li em
{ color: red;
11
font-weight: bold;
12
background-color: #ffffff }
13
14 ul
{ margin-left: 2cm }
15
16 ul ul
17
{ text-decoration: underline;
margin-left: .5cm }
styles.css
(1 of 1)
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4
5
-->
-->
7
8
9
external.html
(1 of 2)
10
11
12
13
14
15
<body>
16
17
18
<ul>
19
<li>Milk</li>
20
<li>Bread
21
<ul>
22
<li>White bread</li>
23
<li>Rye bread</li>
24
25
</ul>
Outline
26
</li>
27
<li>Rice</li>
28
<li>Potatoes</li>
29
30
</ul>
31
32
<p>
33
34
</p>
35
36
</body>
37 </html>
Outline
Floating
Move an element to one side of the screen
Box model
Margins
Padding
Border
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4
5
7
8
9
10
floating.html
(1 of 3)
11
12
-->
13
14
15
margin-bottom: .5em;
16
font-size: 1.5em;
17
width: 50% }
18
19
{ text-align: justify }
20
21
</style>
22
23
24
</head>
Outline
25
<body>
26
27
28
29
30
31
text-align: right">
32
33
floating.html
(2 of 3)
34
35
36
37
38
39
40
41
42
text-align: right">
43
44
45
46
47
48
Outline
49
50
51
52
53
54
courses.</p>
55
56
</body>
57 </html>
Outline
Content
Border
Padding
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4
5
7
8
9
10
borders.html
(1 of 2)
11
12
-->
13
14
body
{ background-color: #ccffcc }
div
{ text-align: center;
15
16
17
margin-bottom: 1em;
18
padding: .5em }
19
20
.thick
{ border-width: thick }
21
22
23
24
25
.thin
{ border-width: thin }
Outline
26
27
28
.inset
{ border-style: inset }
29
30
31
32
.red
{ border-color: red }
.blue
{ border-color: blue }
33
34
35
36
37
</style>
borders.html
(2 of 2)
</head>
38
39
<body>
40
41
42
43
44
45
46
47
48
49
</body>
50 </html>
Outline
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4
5
-->
-->
7
8
9
10
User_absolute.html
(1 of 2)
<title>User Styles</title>
11
12
13
14
15
16
17
</style>
</head>
18
19
<body>
20
21
22
23
Outline
24
25
</body>
26 </html>
Outline
/* A user stylesheet
*/
3
4
body
{ font-size: 20pt;
color: yellow;
background-color: #000080 }
Outline
Fig. 6.18
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4
5
-->
-->
7
8
9
10
User_relative.html
(1 of 2)
<title>User Styles</title>
11
12
13
14
15
16
17
</style>
</head>
18
19
<body>
20
21
22
23
Outline
24
25
</body>
26 </html>
Outline
Fig. 6.21