65
Chapter 3 Cascading Style Sheets

Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

Embed Size (px)

Citation preview

Page 1: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

Chapter 3Cascading Style Sheets

Page 2: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-2

3.1 Introduction

- The CSS1 specification was developed in 1996

- CSS2 was released in 1998

- CSS3 is on its way

- CSSs provide the means to control and change presentation of HTML documents

- CSS is not technically HTML, but can be embedded in HTML documents

- Style sheets allow you to impose a standard style on a whole document, or even a whole collection of documents

- Style is specified for a tag by the values of its properties

Page 3: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-3

3.2 Levels of Style Sheets

- There are three levels of style sheets

1. Inline - specified for a specific occurrence of a tag and apply only to that tag - This is fine-grain style, which defeats the purpose of style sheets - uniform style

2. Document-level style sheets - apply to the whole document in which they appear

3. External style sheets - can be applied to any number of documents

- When more than one style sheet applies to a specific tag in a document, the lowest level style sheet has precedence

- In a sense, the browser searches for a style property spec, starting with inline, until it finds one (or there isn’t one)

Page 4: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-4

3.2 Levels of Style Sheets (continued)

- Inline style sheets appear in the tag itself: 바람직하지 못함 .

- Document-level style sheets appear in the head of the document

- External style sheets are in separate files, potentially on any server on the Internet

- Written as text files with the MIME type text/css

- A <link> tag is used to specify that the browser is to fetch and use an external style sheet file

<link rel = "stylesheet" type = "text/css" href = "http://www.wherever.org/termpaper.css"></link>

- External style sheets can be validated

http://jigsaw.w3.org/css-validator/ validator-upload.html

Page 5: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-5

3.3 Style Specification Formats

- Format depends on the level of the style sheet

- Inline: - Style sheet appears as the value of the style attribute - General form: style = "property_1: value_1; property_2: value_2; … property_n: value_n;"

마지막 ‘ ;’ 은 optional - Document-level:

- Style sheet appears as a list of rules that are the content of a <style> tag

- The <style> tag must include the type attribute, set to "text/css"

- The list of rules must be placed in an HTML comment, because it is not HTML

- Comments in the rule list must have a different form - use C comments (/*…*/)

Page 6: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-6

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

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>

Page 7: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-7

FONT 는 순서대로 , 보통 뒤에는 오는 FONT 는 좀 더 일반적인 것을 씀 ,sans-serif : Helvetica, verdanaserif: times new roman, Georgia cursive: script fantasy: crittermonospace : Courier, fixedsys

Page 8: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-8

3.3 Style Specification Formats (continued)

- General form: <style type = "text/css"> <!-- rule list --> </style> - Form of the rules: selector {list of property/values}

- Each property/value pair has the form: property: value

- Pairs are separated by semicolons, just as in the value of a <style> tag

- External style sheets

- Form is a list of style rules, as in the content of a <style> tag for document-level style sheets

Page 9: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-9

  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

Page 10: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-10

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>

Page 11: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-11

26

27 <body>

28

29 <!-- this class attribute applies the .special style -->

30 <h1 class = "special">Deitel & Associates, Inc.</h1>

31

32 <p>Deitel &amp; 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 &amp; 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 &amp; 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

51 </body>

52 </html>

Page 12: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-12

Page 13: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-13

Conflicting Styles

• Inheritance– Descendant’s properties have greater

specificity than ancestor’s properties

Page 14: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-14

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

Page 15: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-15

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

49 <p><a class = "nodec" href = "http://www.food.com">

50 Go to the Grocery store</a></p>

51

52 </body>

53 </html>

Page 16: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-16

Page 17: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-17

간단한 설명

• text-decoration – overline, line-trough, underline (default), blink

(IE 는 지원 안 함 ), none

Page 18: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-18

3.4 Selector Forms

1. Simple Selector Forms

- The selector is a tag name or a list of tag names, separated by commas - Examples:

h1, h3 p

- Contextual selectors

ol ol li

2. Class Selectors

- Used to allow different occurrences of the same tag to use different style specifications

- A style class has a name, which is attached to a tag name

- For example,

p.narrow {property/value list} p.wide {property/value list}

Page 19: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-19

3.4 Selector Forms (continued) 2. Class Selectors (continued)

- The class you want on a particular occurrence of a tag is specified with the class attribute of the tag

- For example,

<p class = "narrow"> ... </p> ... <p class = "wide"> ... </p>

3. Generic Selectors

- A generic class can be defined if you want a style to apply to more than one kind of tag

- A generic class must be named, and the name must begin with a period

Page 20: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-20

3.4 Selector Forms (continued) 3. Generic Selectors (continued)

- Example,

.really-big { … }

- Use it as if it were a normal style class

<h1 class = "really-big"> … </h1> ... <p class = "really-big"> … </p>

4. id Selectors - An id selector allow the application of a style to one specific element - General form:

#specific-id {property-value list}

- Example:

#section14 {font-size: 20}

Page 21: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-21

3.4 Selector Forms (continued)

5. Pseudo Classes

- Pseudo classes are styles that apply when something happens, rather than because the target element simply exists

- Names begin with colons

- hover classes apply when the mouse cursor is over the element

- focus classes apply when an element has focus

<!-- pseudo.html --><html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title> Checkboxes </title> <style type = "text/css"> input:hover {color: red;} input:focus {color: green;} </style> </head> <body> <form action = ""> <p> Your name: <input type = "text" /> </p> </form> </body> </html>

Page 22: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-22

3.5 Property Value Forms

- There are 60 different properties in 7 categories: - Fonts - Lists - Alignment of text - Margins - Colors - Backgrounds - Borders - Property Value Forms

- Keywords - left, small, … - Not case sensitive

- Length - numbers, maybe with decimal points - Units: px - pixels in - inches cm - centimeters mm - millimeters pt - points pc - picas (12 points) em - height of the letter ‘m’ ex-height - height of the letter ‘x’

- No space is allowed between the number and the unit specification e.g., 1.5 in is illegal!

Page 23: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-23

3.5 Property Value Forms (continued)

- Percentage - just a number followed immediately by a percent sign

- URL values - url(protocol://server/pathname)

- Colors - Color name - rgb(n1, n2, n3) - Numbers can be decimal or percentages - Hex form: #XXXXXX

- Property values are inherited by all nested tags, unless overriden

3.6 Font Properties

- font-family - Value is a list of font names - browser uses the first in the list it has

- font-family: Arial, Helvetica, Courier

- Generic fonts: serif, sans-serif, cursive, fantasy, and monospace (defined in CSS) - Browser has a specific font for each

Page 24: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-24

3.6 Font Properties (continued)

- If a font name has more than one word, it should be single-quoted

- font-size

- Possible values: a length number or a name, such as smaller, xx-large, etc.

- font-style

- italic, oblique (useless), normal

- font-weight - degrees of boldness

- bolder, lighter, bold, normal

- Could specify as a multiple of 100 (100 – 900) - font

- For specifying a list of font properties

font: bolder 14pt Arial Helvetica

- Order must be: style, weight, size, name(s)

Page 25: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-25

3.6 Font Properties (continued)

-> SHOW fonts.html and display -> SHOW fonts2.html and display

- The text-decoration property - line-through, overline, underline, none

- letter-spacing – value is any length property value

3.7 List properties

- list-style-type

- Unordered lists - Bullet can be a disc (default), a square, or a circle - Set it on either the <ul> or <li> tag - On <ul>, it applies to list items

<h3> Some Common Single-Engine Aircraft </h3> <ul style = "list-style-type: square"> <li> Cessna Skyhawk </li> <li> Beechcraft Bonanza </li> <li> Piper Cherokee </li> </ul>

Page 26: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-26

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>

Page 27: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-27

Page 28: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-28

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

Page 29: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-29

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>

Page 30: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-30

Page 31: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-31

3.7 List properties (continued)

- On <li>, list-style-type applies to just that item

<h3> Some Common Single-Engine Aircraft </h3> <ul> <li style = "list-style-type: disc"> Cessna Skyhawk </li> <li style = "list-style-type: square"> Beechcraft Bonanza </li> <li style = "list-style-type: circle"> Piper Cherokee </li> </ul>

Page 32: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-32

3.7 List properties (continued)

- Could use an image for the bullets in an unordered list

- Example:

<li style = "list-style-image: url(bird.jpg)">

- On ordered lists - list-style-type can be used to change the sequence values

Property value Sequence type First four

decimal Arabic numerals 1, 2, 3, 4 upper-alpha Uc letters A, B, C, D lower-alpha Lc letters a, b, c, d upper-roman Uc Roman I, II, III, IV lower-roman Lc Roman i, ii, iii, iv

SHOW sequence_types.html and display

- CSS2 has more, like lower-greek and hebrew

Page 33: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-33

3.8 Colors

- Color is a problem for the Web for two reasons:

1. Monitors vary widely 2. Browsers vary widely - There are three color collections

1. There is a set of 16 colors that are guaranteed to be displayable by all graphical browsers on all color monitors

black 000000 green 008000 silver C0C0C0 lime 00FF00 gray 808080 olive 808000 white FFFFFF yellow FFFF00 maroon 800000 navy 000080 red FF0000 blue 0000FF purple 800080 teal 008080 fuchia FF00FF aqua 00FFFF

2. There is a much larger set, the Web Palette

- 216 colors

- Use hex color values of 00, 33, 66, 99, CC, and FF

- Inside back cover of this book has them!

Page 34: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-34

3.8 Colors (continued)

3. Any one of 16 million different colors___________________________________________

- The color property specifies the foreground color of elements

<style type = “text/css”> th.red {color: red} th.orange {color: orange} </style> … <table border = "5"> <tr> <th class = "red"> Apple </th> <th class = "orange"> Orange </th> <th class = "orange"> Screwdriver </th> </tr> </table>

- The background-color property specifies the background color of elements

SHOW back_color.html and display

Page 35: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-35

3.9 Alignment of Text - The text-indent property allows indentation

- Takes either a length or a % value

- The text-align property has the possible values, left (the default), center, right, or justify

- Sometimes we want text to flow around another element - the float property

- The float property has the possible values, left, right, and none (the default)

- If we have an element we want on the right, with text flowing on its left, we use the default text-align value (left) for the text and the right value for float on the element we want on the right

Page 36: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-36

3.9 Alignment of Text (continued)

<img src = "c210.jpg" style = "float: right" /> -- Some text with the default alignment - left

Page 37: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-37

Element Dimensions

• CSS rules can specify the actual dimensions of each page element

Page 38: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-38

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>

Page 39: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-39

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>

Page 40: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-40

3.10 The Box Model

- Borders – every element has a border-style property

- Controls whether the element has a border and if so, the style of the border

- border-style values: none, dotted, dashed, and double - border-width – thin, medium (default), thick, or a length value in pixels

- Border width can be specified for any of the four borders (e.g., border-top-width)

- border-color – any color

- Border color can be specified for any of the four borders (e.g., border-top-color)

SHOW borders.html and display

Page 41: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-41

3.10 The Box Model (continued)

- Margin – the space between the border of an element and its neighbor element

- The margins around an element can be set with margin-left, etc. - just assign them a length value

<img src = "c210.jpg " style = "float: right; margin-left: 0.35in; margin-bottom: 0.35in" />

Page 42: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-42

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

Page 43: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-43

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

Page 44: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-44

25 <body>

26

27 <div style = "text-align: center">

28 Deitel &amp; 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 &amp; 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

Page 45: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-45

49 <p style = "clear: right">Through its publishing

50 partnership with Prentice Hall, Deitel &amp; 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>

Page 46: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-46

Text Flow and the Box Model

Content

Margin

Border

Padding

Page 47: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-47

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

Page 48: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-48

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>

Page 49: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-49

Page 50: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-50

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>

Page 51: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-51

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>

Page 52: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-52

3.10 The Box Model (continued)

- Padding – the distance between the content of an element and its border

- Controlled by padding, padding-left, etc.

SHOW marpads.html and display

3.11 Background Images

- The background-image property

SHOW back_image.html and display

- Repetition can be controlled

- background-repeat property

- Possible values: repeat (default), no-repeat, repeat-x, or repeat-y

- background-position property

- Possible values: top, center, bottom, left, or right

Page 53: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-53

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

Page 54: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-54

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 &amp; 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>

Page 55: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-55

3.12 The <span> and <div> tags

- One problem with the font properties is that they apply to whole elements, which are often too large

- Solution: a new tag to define an element in the content of a larger element - <span>

- The default meaning of <span> is to leave the content as it is <p> Now is the <span> best time </span> ever! </p>

- Use <span> to apply a document style sheet to its content

<style type = "text/css">? bigred {font-size: 24pt; font-family: Ariel; color: red} </style> <p> Now is the <span class = "bigred"> best time </span> ever! </p>

Page 56: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-56

3.12 The <span> and <div> tags (continued)

- The <span> tag is similar to other HTML tags, they can be nested and they have id and class attributes

- Another tag that is useful for style specifications: <div>

- Used to create document sections (or divisions) for which style can be specified

- e.g., A section of five paragraphs for which you want some particular style

Page 57: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-57

User Style Sheets

• Format pages based on preferences

Page 58: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-58

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>

24

25 </body>

26 </html>

Page 59: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-59

Page 60: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-60

1 /* Fig. 6.17: userstyles.css */

2 /* A user stylesheet */

3

4 body { font-size: 20pt;

5 color: yellow;

6 background-color: #000080 }

Page 61: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-61

6.11  User Style Sheets

Fig. 6.18 User style sheet in Internet Explorer 6.

Page 62: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-62

Page 63: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-63

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>

24

25 </body>

26 </html>

Page 64: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-64

Page 65: Chapter 3 Cascading Style Sheets. © 2006 Pearson Addison-Wesley. All rights reserved. 3-2 3.1 Introduction - The CSS1 specification was developed in 1996

© 2006 Pearson Addison-Wesley. All rights reserved. 3-65