Chapter 6 - Cascading Style Sheets™ (CSS)
description
Transcript of Chapter 6 - Cascading Style Sheets™ (CSS)
2004 Prentice Hall, Inc. All rights reserved.
Chapter 6 - Cascading Style Sheets™ (CSS)
Outline6.1 Introduction6.2 Inline Styles6.3 Embedded Style Sheets6.4 Conflicting Styles6.5 Linking External Style Sheets6.6 W3C CSS Validation Service6.7 Positioning Elements6.8 Backgrounds6.9 Element Dimensions6.10 Text Flow and the Box Model6.11 User Style Sheets6.12 Web Resources
2004 Prentice Hall, Inc. All rights reserved.
Objectives
• In this lesson, you will learn:– To control the appearance of a Web site by creating style
sheets.
– To use a style sheet to give all the pages of a Web site the same look and feel.
– To use the class attribute to apply styles.
– To specify the precise font, size, color and other properties of displayed text.
– To specify element backgrounds and colors.
– To understand the box model and how to control the margins, borders and padding.
– To use style sheets to separate presentation from content.
2004 Prentice Hall, Inc. All rights reserved.
6.1 Introduction
• Cascading Style Sheets (CSS)– Separation of structure from presentation
2004 Prentice Hall, Inc. All rights reserved.
6.2 Inline Styles
• Declare an individual element’s format– Attribute style
– CSS property• Followed by a colon and a value
• CSS properties– Background-color
– Font family
– Font size• Absolute : “font-size: 20pt”
• Relative : xx-small, x-small, smaller, medium, large, larger, x-large, xx-large
• Relative : em(‘M’ 의 높이 ), ex(‘x’ 의 높이 )
2004 Prentice Hall, Inc. All rights reserved.
OutlineOutline
inline.html(1 of 2)
1 <?xml version = "1.0"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
3 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4
5 <!-- Fig. 6.1: inline.html -->
6 <!-- Using inline styles -->
7
8 <html xmlns = "http://www.w3.org/1999/xhtml">
9 <head>
10 <title>Inline Styles</title>
11 </head>
12
13 <body>
14
15 <p>This text does not have any style applied to it.</p>
16
17 <!-- The style attribute allows you to declare -->
18 <!-- inline styles. Separate multiple styles -->
19 <!-- with a semicolon. -->
20 <p style = "font-size: 20pt">This text has the
21 <em>font-size</em> style applied to it, making it 20pt.
22 </p>
23
2004 Prentice Hall, Inc. All rights reserved.
OutlineOutline
inline.html(2 of 2)
24 <p style = "font-size: 20pt; color: #0000ff">
25 This text has the <em>font-size</em> and
26 <em>color</em> styles applied to it, making it
27 20pt. and blue.</p>
28
29 </body>
30 </html>
2004 Prentice Hall, Inc. All rights reserved.
6.3 Embedded Style Sheets
• Embed an entire CSS document in an XHTML document’s head section– Multipurpose Internet Mail Extensions (MIME) type
• Describes a file’s content
– Property background-color• Specifies the background color
– Property font-family• Specifies the name of the font to use
– Property font-size• Specifies a 14-point font
2004 Prentice Hall, Inc. All rights reserved.
OutlineOutline
declared.html(1 of 3)
1 <?xml version = "1.0"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
3 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4
5 <!-- Fig. 6.2: declared.html -->
6 <!-- Declaring a style sheet in the header section. -->
7
8 <html xmlns = "http://www.w3.org/1999/xhtml">
9 <head>
10 <title>Style Sheets</title>
11
12 <!-- this begins the style sheet section -->
13 <style type = "text/css">
14
15 em { background-color: #8000ff;
16 color: white }
17
18 h1 { font-family: arial, sans-serif }
19
20 p { font-size: 14pt }
21
22 .special { color: blue }
23
24 </style>
25 </head>
2004 Prentice Hall, Inc. All rights reserved.
OutlineOutline
declared.html(2 of 3)
26
27 <body>
28
29 <!-- this class attribute applies the .special style -->
30 <h1 class = "special">Deitel & Associates, Inc.</h1>
31
32 <p>Deitel & Associates, Inc. is an internationally
33 recognized corporate training and publishing organization
34 specializing in programming languages, Internet/World
35 Wide Web technology and object technology education.
36 Deitel & Associates, Inc. is a member of the World Wide
37 Web Consortium. The company provides courses on Java,
38 C++, Visual Basic, C, Internet and World Wide Web
39 programming, and Object Technology.</p>
40
41 <h1>Clients</h1>
42 <p class = "special"> The company's clients include many
43 <em>Fortune 1000 companies</em>, government agencies,
44 branches of the military and business organizations.
45 Through its publishing partnership with Prentice Hall,
46 Deitel & Associates, Inc. publishes leading-edge
47 programming textbooks, professional books, interactive
48 CD-ROM-based multimedia Cyber Classrooms, satellite
49 courses and World Wide Web courses.</p>
50
2004 Prentice Hall, Inc. All rights reserved.
OutlineOutline
declared.html(3 of 3)
51 </body>
52 </html>
2004 Prentice Hall, Inc. All rights reserved.
6.4 Conflicting Styles
• Inheritance– Descendant’s properties have greater specificity than
ancestor’s properties
2004 Prentice Hall, Inc. All rights reserved.
OutlineOutline
advance.html(1 of 3)
1 <?xml version = "1.0"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
3 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4
5 <!-- Fig 6.3: advanced.html -->
6 <!-- More advanced style sheets -->
7
8 <html xmlns = "http://www.w3.org/1999/xhtml">
9 <head>
10 <title>More Styles</title>
11
12 <style type = "text/css">
13
14 a.nodec { text-decoration: none }
15
16 a:hover { text-decoration: underline;
17 color: red;
18 background-color: #ccffcc }
19
20 li em { color: red;
21 font-weight: bold }
22
23 ul { margin-left: 75px }
24
2004 Prentice Hall, Inc. All rights reserved.
OutlineOutline
advance.html(2 of 3)
25 ul ul { text-decoration: underline;
26 margin-left: 15px }
27
28 </style>
29 </head>
30
31 <body>
32
33 <h1>Shopping list for <em>Monday</em>:</h1>
34
35 <ul>
36 <li>Milk</li>
37 <li>Bread
38 <ul>
39 <li>White bread</li>
40 <li>Rye bread</li>
41 <li>Whole wheat bread</li>
42 </ul>
43 </li>
44 <li>Rice</li>
45 <li>Potatoes</li>
46 <li>Pizza <em>with mushrooms</em></li>
47 </ul>
48
2004 Prentice Hall, Inc. All rights reserved.
OutlineOutline
advance.html(3 of 3)
49 <p><a class = "nodec" href = "http://www.food.com">
50 Go to the Grocery store</a></p>
51
52 </body>
53 </html>
2004 Prentice Hall, Inc. All rights reserved.
6.5 Linking External Style Sheets
• External style sheets– Can provide uniform look and feel to entire site
2004 Prentice Hall, Inc. All rights reserved.
OutlineOutline
styles.css(1 of 1)
1 /* Fig. 6.4: styles.css */
2 /* An external stylesheet */
3
4 a { text-decoration: none }
5
6 a:hover { text-decoration: underline;
7 color: red;
8 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 { text-decoration: underline;
17 margin-left: .5cm }
2004 Prentice Hall, Inc. All rights reserved.
OutlineOutline
external.html(1 of 2)
1 <?xml version = "1.0"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
3 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4
5 <!-- Fig. 6.5: external.html -->
6 <!-- Linking external style sheets -->
7
8 <html xmlns = "http://www.w3.org/1999/xhtml">
9 <head>
10 <title>Linking External Style Sheets</title>
11 <link rel = "stylesheet" type = "text/css"
12 href = "styles.css" />
13 </head>
14
15 <body>
16
17 <h1>Shopping list for <em>Monday</em>:</h1>
18 <ul>
19 <li>Milk</li>
20 <li>Bread
21 <ul>
22 <li>White bread</li>
23 <li>Rye bread</li>
24 <li>Whole wheat bread</li>
25 </ul>
2004 Prentice Hall, Inc. All rights reserved.
OutlineOutline
external.html(2 of 2)
26 </li>
27 <li>Rice</li>
28 <li>Potatoes</li>
29 <li>Pizza <em>with mushrooms</em></li>
30 </ul>
31
32 <p>
33 <a href = "http://www.food.com">Go to the Grocery store</a>
34 </p>
35
36 </body>
37 </html>
2004 Prentice Hall, Inc. All rights reserved.
6.6 W3C CSS Validation Service
• Validates external CSS documents• Ensures that style sheets are syntactically correct
2004 Prentice Hall, Inc. All rights reserved.
6.6 W3C CSS Validation Service
Fig. 6.6 Validating a CSS document. (Courtesy of World Wide Web Consortium (W3C).)
2004 Prentice Hall, Inc. All rights reserved.
6.6 W3C CSS Validation Service
Fig. 6.7 CSS validation results. (Courtesy of World Wide Web Consortium (W3C).)
2004 Prentice Hall, Inc. All rights reserved.
6.7 Positioning Elements
• Absolute positioning– z-index attribute
• Layer overlapping elements properly
• Relative positioning– Elements are positioned relative to other elements
2004 Prentice Hall, Inc. All rights reserved.
OutlineOutline
positioning.html(1 of 1)
1 <?xml version = "1.0"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
3 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4
5 <!-- Fig 6.8: positioning.html -->
6 <!-- Absolute positioning of elements -->
7
8 <html xmlns = "http://www.w3.org/1999/xhtml">
9 <head>
10 <title>Absolute Positioning</title>
11 </head>
12
13 <body>
14
15 <p><img src = "i.gif" style = "position: absolute;
16 top: 0px; left: 0px; z-index: 1"
17 alt = "First positioned image" /></p>
18 <p style = "position: absolute; top: 50px; left: 50px;
19 z-index: 3; font-size: 20pt">Positioned Text</p>
20 <p><img src = "circle.gif" style = "position: absolute;
21 top: 25px; left: 100px; z-index: 2" alt =
22 "Second positioned image" /></p>
23
24 </body>
25 </html>
2004 Prentice Hall, Inc. All rights reserved.
2004 Prentice Hall, Inc. All rights reserved.
OutlineOutline
positioning2.html(1 of 2)
1 <?xml version = "1.0"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
3 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4
5 <!-- Fig. 6.9: positioning2.html -->
6 <!-- Relative positioning of elements -->
7
8 <html xmlns = "http://www.w3.org/1999/xhtml">
9 <head>
10 <title>Relative Positioning</title>
11
12 <style type = "text/css">
13
14 p { font-size: 1.3em;
15 font-family: verdana, arial, sans-serif }
16
17 span { color: red;
18 font-size: .6em;
19 height: 1em }
20
21 .super { position: relative;
22 top: -1ex }
23
24 .sub { position: relative;
25 bottom: -1ex }
26
2004 Prentice Hall, Inc. All rights reserved.
OutlineOutline
positioning2.html2 of 2
27 .shiftleft { position: relative;
28 left: -1ex }
29
30 .shiftright { position: relative;
31 right: -1ex }
32
33 </style>
34 </head>
35
36 <body>
37
38 <p>The text at the end of this sentence
39 <span class = "super">is in superscript</span>.</p>
40
41 <p>The text at the end of this sentence
42 <span class = "sub">is in subscript</span>.</p>
43
44 <p>The text at the end of this sentence
45 <span class = "shiftleft">is shifted left</span>.</p>
46
47 <p>The text at the end of this sentence
48 <span class = "shiftright">is shifted right</span>.</p>
49
50 </body>
51 </html>
2004 Prentice Hall, Inc. All rights reserved.
2004 Prentice Hall, Inc. All rights reserved.
6.8 Backgrounds
• background-image– Specifies the image URL
• background-position– Places the image on the page
• background-repeat– Controls the tiling of the background image
• background-attachment– fixed– scroll
• font-weight– Specify the “boldness” of text
2004 Prentice Hall, Inc. All rights reserved.
OutlineOutline
background.html(1 of 2)
1 <?xml version = "1.0"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
3 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4
5 <!-- Fig. 6.10: background.html -->
6 <!-- Adding background images and indentation -->
7
8 <html xmlns = "http://www.w3 .org/1999/xhtml">
9 <head>
10 <title>Background Images</title>
11
12 <style type = "text/css">
13
14 body { background-image: url(logo.gif);
15 background-position: bottom right;
16 background-repeat: no-repeat;
17 background-attachment: fixed; }
18
19 p { font-size: 18pt;
20 color: #aa5588;
21 text-indent: 1em;
22 font-family: arial, sans-serif; }
23
24 .dark { font-weight: bold; }
25
2004 Prentice Hall, Inc. All rights reserved.
OutlineOutline
background.html(2 of 2)
26 </style>
27 </head>
28
29 <body>
30
31 <p>
32 This example uses the background-image,
33 background-position and background-attachment
34 styles to place the <span class = "dark">Deitel
35 & Associates, Inc.</span> logo in the bottom,
36 right corner of the page. Notice how the logo
37 stays in the proper position when you resize the
38 browser window.
39 </p>
40
41 </body>
42 </html>
2004 Prentice Hall, Inc. All rights reserved.
2004 Prentice Hall, Inc. All rights reserved.
6.9 Element Dimensions
• CSS rules can specify the actual dimensions of each page element
2004 Prentice Hall, Inc. All rights reserved.
OutlineOutline
width.html(1 of 2)
1 <?xml version = "1.0"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
3 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4
5 <!-- Fig. 6.11: width.html -->
6 <!-- Setting box dimensions and aligning text -->
7
8 <html xmlns = "http://www.w3.org/1999/xhtml">
9 <head>
10 <title>Box Dimensions</title>
11
12 <style type = "text/css">
13
14 div { background-color: #ffccff;
15 margin-bottom: .5em }
16 </style>
17
18 </head>
19
20 <body>
21
22 <div style = "width: 20%">Here is some
23 text that goes in a box which is
24 set to stretch across twenty percent
25 of the width of the screen.</div>
2004 Prentice Hall, Inc. All rights reserved.
OutlineOutline
width.html(2 of 2)
26
27 <div style = "width: 80%; text-align: center">
28 Here is some CENTERED text that goes in a box
29 which is set to stretch across eighty percent of
30 the width of the screen.</div>
31
32 <div style = "width: 20%; height: 30%; overflow: scroll">
33 This box is only twenty percent of
34 the width and thirty percent of the height.
35 What do we do if it overflows? Set the
36 overflow property to scroll!</div>
37
38 </body>
39 </html>
2004 Prentice Hall, Inc. All rights reserved.
6.10 Text Flow and the Box Model
• Floating– Move an element to one side of the screen
• Box model– Margins
• margin-top, margin-right, margin-left, margin-bottom
– Padding• padding-top, padding-right, padding-left, and padding-bottom
– Border• border-width
– thin, medium, thick• border-color
– Sets the color• border-style
– none, hidden, dotted, dashed, solid, double, groove, ridge, inset and outset
2004 Prentice Hall, Inc. All rights reserved.
OutlineOutline
floating.html(1 of 3)
1 <?xml version = "1.0"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
3 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4
5 <!-- Fig. 6.12: floating.html -->
6 <!-- Floating elements and element boxes -->
7
8 <html xmlns = "http://www.w3.org/1999/xhtml">
9 <head>
10 <title>Flowing Text Around Floating Elements</title>
11
12 <style type = "text/css">
13
14 div { background-color: #ffccff;
15 margin-bottom: .5em;
16 font-size: 1.5em;
17 width: 50% }
18
19 p { text-align: justify }
20
21 </style>
22
23 </head>
24
2004 Prentice Hall, Inc. All rights reserved.
OutlineOutline
floating.html(2 of 3)
25 <body>
26
27 <div style = "text-align: center">
28 Deitel & Associates, Inc.</div>
29
30 <div style = "float: right; margin: .5em;
31 text-align: right">
32 Corporate Training and Publishing</div>
33
34 <p>Deitel & Associates, Inc. is an internationally
35 recognized corporate training and publishing organization
36 specializing in programming languages, Internet/World
37 Wide Web technology and object technology education.
38 The company provides courses on Java, C++, Visual Basic, C,
39 Internet and World Wide Web programming, and Object Technology.</p>
40
41 <div style = "float: right; padding: .5em;
42 text-align: right">
43 Leading-Edge Programming Textbooks</div>
44
45 <p>The company's clients include many Fortune 1000
46 companies, government agencies, branches of the military
47 and business organizations.</p>
48
2004 Prentice Hall, Inc. All rights reserved.
OutlineOutline49 <p style = "clear: right">Through its publishing
50 partnership with Prentice Hall, Deitel & Associates,
51 Inc. publishes leading-edge programming textbooks,
52 professional books, interactive CD-ROM-based multimedia
53 Cyber Classrooms, satellite courses and World Wide Web
54 courses.</p>
55
56 </body>
57 </html>
floating.html(3 of 3)
2004 Prentice Hall, Inc. All rights reserved.
6.10 Text Flow and the Box Model
Content
Margin
Border
Padding
Fig. 6.13 Box model for block-level elements.
2004 Prentice Hall, Inc. All rights reserved.
OutlineOutline
borders.html(1 of 2)
1 <?xml version = "1.0"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
3 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4
5 <!-- Fig. 6.14: borders.html -->
6 <!-- Setting borders of an element -->
7
8 <html xmlns = "http://www.w3.org/1999/xhtml">
9 <head>
10 <title>Borders</title>
11
12 <style type = "text/css">
13
14 body { background-color: #ccffcc }
15
16 div { text-align: center;
17 margin-bottom: 1em;
18 padding: .5em }
19
20 .thick { border-width: thick }
21
22 .medium { border-width: medium }
23
24 .thin { border-width: thin }
25
2004 Prentice Hall, Inc. All rights reserved.
OutlineOutline
borders.html(2 of 2)
26 .groove { border-style: groove }
27
28 .inset { border-style: inset }
29
30 .outset { border-style: outset }
31
32 .red { border-color: red }
33
34 .blue { border-color: blue }
35
36 </style>
37 </head>
38
39 <body>
40
41 <div class = "thick groove">This text has a border</div>
42 <div class = "medium groove">This text has a border</div>
43 <div class = "thin groove">This text has a border</div>
44
45 <p class = "thin red inset">A thin red line...</p>
46 <p class = "medium blue outset">
47 And a thicker blue line</p>
48
49 </body>
50 </html>
2004 Prentice Hall, Inc. All rights reserved.
2004 Prentice Hall, Inc. All rights reserved.
OutlineOutline
borders2.html(1 of 2)
1 <?xml version = "1.0"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
3 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4
5 <!-- Fig. 6.15: borders2.html -->
6 <!-- Various border-styles -->
7
8 <html xmlns = "http://www.w3.org/1999/xhtml">
9 <head>
10 <title>Borders</title>
11
12 <style type = "text/css">
13
14 body { background-color: #ccffcc }
15
16 div { text-align: center;
17 margin-bottom: .3em;
18 width: 50%;
19 position: relative;
20 left: 25%;
21 padding: .3em }
22 </style>
23 </head>
24
25 <body>
2004 Prentice Hall, Inc. All rights reserved.
OutlineOutline
borders2.html(2 of 2)
26
27 <div style = "border-style: solid">Solid border</div>
28 <div style = "border-style: double">Double border</div>
29 <div style = "border-style: groove">Groove border</div>
30 <div style = "border-style: ridge">Ridge border</div>
31 <div style = "border-style: inset">Inset border</div>
32 <div style = "border-style: outset">Outset border</div>
33
34 </body>
35 </html>
2004 Prentice Hall, Inc. All rights reserved.
6.11 User Style Sheets
• Format pages based on preferences
2004 Prentice Hall, Inc. All rights reserved.
OutlineOutline
User_absolute.html(1 of 2)
1 <?xml version = "1.0"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
3 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4
5 <!-- Fig. 6.16: user_absolute.html -->
6 <!-- User styles -->
7
8 <html xmlns = "http://www.w3.org/1999/xhtml">
9 <head>
10 <title>User Styles</title>
11
12 <style type = "text/css">
13
14 .note { font-size: 9pt }
15
16 </style>
17 </head>
18
19 <body>
20
21 <p>Thanks for visiting my Web site. I hope you enjoy it.
22 </p><p class = "note">Please Note: This site will be
23 moving soon. Please check periodically for updates.</p>
2004 Prentice Hall, Inc. All rights reserved.
OutlineOutline
User_absolute.html(2 of 2)
24
25 </body>
26 </html>
2004 Prentice Hall, Inc. All rights reserved.
OutlineOutline
userstyles.css1 of 1
1 /* Fig. 6.17: userstyles.css */
2 /* A user stylesheet */
3
4 body { font-size: 20pt;
5 color: yellow;
6 background-color: #000080 }
2004 Prentice Hall, Inc. All rights reserved.
6.11 User Style Sheets
Fig. 6.18 User style sheet in Internet Explorer 6.
2004 Prentice Hall, Inc. All rights reserved.
6.11 User Style Sheets
Fig. 6.19 User style sheet applied with pt measurement.
2004 Prentice Hall, Inc. All rights reserved.
OutlineOutline
User_relative.html(1 of 2)
1 <?xml version = "1.0"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
3 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4
5 <!-- Fig. 6.20: user_relative.html -->
6 <!-- User styles -->
7
8 <html xmlns = "http://www.w3.org/1999/xhtml">
9 <head>
10 <title>User Styles</title>
11
12 <style type = "text/css">
13
14 .note { font-size: .75em }
15
16 </style>
17 </head>
18
19 <body>
20
21 <p>Thanks for visiting my Web site. I hope you enjoy it.
22 </p><p class = "note">Please Note: This site will be
23 moving soon. Please check periodically for updates.</p>
2004 Prentice Hall, Inc. All rights reserved.
OutlineOutline
User_relative.html(2 of 2)
24
25 </body>
26 </html>
2004 Prentice Hall, Inc. All rights reserved.
6.11 User Style Sheets
Fig. 6.21 User style sheet applied with em measurement.
2004 Prentice Hall, Inc. All rights reserved.
6.12 Web Resources
• www.w3.org/TR/css3-roadmap
• www.ddj.com/webreview/style• tech.irt.org/articles/css.htm