1 introduction to web application introduction to cascading style sheet

64
1 Introduction to Web Application Introduction to Cascading Style Sheet

Upload: beryl-cannon

Post on 17-Jan-2016

220 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 1 Introduction to Web Application Introduction to Cascading Style Sheet

1

Introduction to Web Application

Introduction to Cascading Style Sheet

Page 2: 1 Introduction to Web Application Introduction to Cascading Style Sheet

2

Topics• Introduction• Inline Styles• Embedded Style Sheets• Conflicting Styles• Linking External Style Sheets• Positioning Elements• Element Dimensions• Text Flow and the Box Model• User Style Sheets

Page 3: 1 Introduction to Web Application Introduction to Cascading Style Sheet

3

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.

Page 4: 1 Introduction to Web Application Introduction to Cascading Style Sheet

4

Introduction

• Cascading Style Sheets (CSS)– Separation of structure from presentation

• Relationship with HTML

Page 5: 1 Introduction to Web Application Introduction to Cascading Style Sheet

5

Different between HTML tags and CSS

<font size=“7”>Hello</font>

<div style=“font-size:18pt”>Hello</div>

Page 6: 1 Introduction to Web Application Introduction to Cascading Style Sheet

6

Ways to Change HTML Default Presentation

• Presentation Elements or Properties– Font, bgcolor, etc.

• Using some Elements to control the page layout– Table, img, etc.

• Cascading style Sheets (CSS) !!

Page 7: 1 Introduction to Web Application Introduction to Cascading Style Sheet

7

Why CSS?

• HTML is primarily concerned with content, rather than style

• Power, Flexibility, Rendering, Accessibility…

• http://www.macromedia.com/devnet/mx/dreamweaver/articles/why_css.html

Page 8: 1 Introduction to Web Application Introduction to Cascading Style Sheet

8

Evolution of CSS

• 1996 W3C Bert Bos&Hakon Lie CSS Level1

• 1998 CSS2

• 2001 CSS Mobile Profile

• Now CSS2.1 CSS3

Page 9: 1 Introduction to Web Application Introduction to Cascading Style Sheet

9

CSS Grammar

p {font-size: 12pt; font-style: italic; color: green}

Selector

Property list separated by semicolons

Property Property value

Colon

Page 10: 1 Introduction to Web Application Introduction to Cascading Style Sheet

10

• Relative units:– em height of letter m– en height of letter n– ex height of letter x– px pixels– % percent

Units

Page 11: 1 Introduction to Web Application Introduction to Cascading Style Sheet

11

Units

• Absolute units:– in inch

– cm centimeter

– mm millimeter

– pt point(1pt=1/72inch)

– pc 12points(1pc=12pt)

Page 12: 1 Introduction to Web Application Introduction to Cascading Style Sheet

12

Inline Styles

• Declare an individual element’s format– Attribute style– CSS property

• Followed by a colon and a value

– Example:• <p style=“font-size: 20pt”> hello

world</p>• <p style=“font-size: 20pt; background-

color: #8000ff; color:red”> hello world</p>

Page 13: 1 Introduction to Web Application Introduction to Cascading Style Sheet

13

Page 14: 1 Introduction to Web Application Introduction to Cascading Style Sheet

14

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 15: 1 Introduction to Web Application Introduction to Cascading Style Sheet

15

Example of Embedded CSS<html>

<head><style type="text/css">

p {font-size: 14 pt; color:red}.special {color: blue}

</style></head><body>

<p>Hello World!</p><h1 class="special">School of Software</h1>

</body></html>

Page 16: 1 Introduction to Web Application Introduction to Cascading Style Sheet

16

Page 17: 1 Introduction to Web Application Introduction to Cascading Style Sheet

17

Conflicting Styles

• Inheritance– Descendant’s properties have greater

specificity than ancestor’s properties

Page 18: 1 Introduction to Web Application Introduction to Cascading Style Sheet

18

Example of Conflicting Styles<html><head>

<style type = "text/css">li em {color: red;

font-weight: bold}ul {margine-left:75pt}ul ul {text-decoration: underline;

margine-left:15pt}</style>

</head><body>

<ul><li>Milk</li><ul><li>White bread</li></ul><li> Pizza <em> with mushrooms</em></li>

</ul></body></html>

Page 19: 1 Introduction to Web Application Introduction to Cascading Style Sheet

19

Page 20: 1 Introduction to Web Application Introduction to Cascading Style Sheet

20

Example of Conflicting Styles (cont.)<html>

<head>

<style type = "text/css">a.nodec {text-decoration: none;

font-size: 18pt}a:hover {text-decoration:

underline; color: red; background-color:

#ccffcc;}

</style></head><body>

<a class="nodec" href="#"> hello world</a></body>

</html>

noneoverline

line-throughunderline

blink

noneoverline

line-throughunderline

blink

Page 21: 1 Introduction to Web Application Introduction to Cascading Style Sheet

21

Page 22: 1 Introduction to Web Application Introduction to Cascading Style Sheet

22

Linking External Style Sheets

• External style sheets– Can provide uniform look and feel to entire site

• Example:<html>

<head><link rel=“stylesheet” type=“text/css”

href=“styles.css”></head><body></body>

</html>

Page 23: 1 Introduction to Web Application Introduction to Cascading Style Sheet

23

Content of styles.css/* 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 }

Page 24: 1 Introduction to Web Application Introduction to Cascading Style Sheet

24

Positioning Elements • Absolute positioning

– z-index attribute• Layer overlapping elements properly

• Relative positioning– Elements are positioned relative to other

elements

Page 25: 1 Introduction to Web Application Introduction to Cascading Style Sheet

25

Example of Position<html><head><title>Absolute Positioning</title></head> <body>

<p><img src = "i.gif" style = "position: absolute; top: 0px; left: 0px; z-index: 1" alt = "First

positioned image" /></p> <p style = "position: absolute;

top: 50px; left: 50px; z-index: 3; font-size: 20pt">Positioned Text</p>

<p><img src = "circle.gif" style = "position: absolute; top: 25px; left: 100px; z-index:

2" alt = "Second positioned image" /></p>

</body></html>

Page 26: 1 Introduction to Web Application Introduction to Cascading Style Sheet

26

Page 27: 1 Introduction to Web Application Introduction to Cascading Style Sheet

27

Example of Position (cont.)

<html><head> <title>Relative Positioning</title>

<style type = "text/css"> p { font-size: 1.3em; font-family: verdana, arial, sans-serif } span { color: red; font-size: .6em; height: 1em } .super { position: relative; top: -1ex } .sub { position: relative; bottom: -1ex } .shiftleft { position: relative; left: -1ex } .shiftright { position: relative; right: -1ex } </style> </head>

Page 28: 1 Introduction to Web Application Introduction to Cascading Style Sheet

28

<body> <p>The text at the end of this sentence <span class = "super">is in superscript</span>.

</p> <p>The text at the end of this sentence <span class = "sub">is in subscript</span>. </p> <p>The text at the end of this sentence <span class = "shiftleft">is shifted left</span>

. </p> <p>The text at the end of this sentence <span class = "shiftright">is shifted right</span>.

</p> </body></html>

Page 29: 1 Introduction to Web Application Introduction to Cascading Style Sheet

29

<span class = "super">is in superscript</span>

<span class = "super">is in superscript</span>

<span class = "sub">is in subscript</span>.

<span class = "sub">is in subscript</span>.

<span class = "shiftleft">is shifted left</span>

<span class = "shiftleft">is shifted left</span>

<span class = "shiftright">is shifted right</span>.

<span class = "shiftright">is shifted right</span>.

Page 30: 1 Introduction to Web Application Introduction to Cascading Style Sheet

30

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

Page 31: 1 Introduction to Web Application Introduction to Cascading Style Sheet

31

Example of Backgrounds<html> <head> <title>Background Images</title> <style type = "text/css"> body { background-image: url(logo.gif); background-position: bottom right; background-repeat: no-repeat; background-attachment: fixed; } p { font-size: 18pt; color: #aa5588; text-indent: 1em; font-family: arial, sans-serif; } .dark { font-weight: bold } </style> </head>

Page 32: 1 Introduction to Web Application Introduction to Cascading Style Sheet

32

<body>

<p>

This example uses the background-image, background-position and background-attachment styles to place the <span class = "dark">Deitel

&amp; Associates, Inc.</span> logo in the bottom, right corner of the page. Notice how the logo stays in the proper position when you resize the browser window.

</p>

</body>

</html>

Page 33: 1 Introduction to Web Application Introduction to Cascading Style Sheet

33

Page 34: 1 Introduction to Web Application Introduction to Cascading Style Sheet

34

Element Dimensions

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

Page 35: 1 Introduction to Web Application Introduction to Cascading Style Sheet

35

Example of Element Dimension P 158

<html><head>

<title>Box Dimensions</title> <style type = "text/css"> div { background-color: #ffccff; margin-bottom: .5em } </style> </head> <body> <div style = "width: 20%"></div> <div style = "width: 80%; text-align: center"/> <div style = "width: 20%; height: 30%; overflow: scroll“/> </body></html>

Page 36: 1 Introduction to Web Application Introduction to Cascading Style Sheet

36

Page 37: 1 Introduction to Web Application Introduction to Cascading Style Sheet

37

Text Flow and the Box Model • Floating

– Move an element to one side of the screen

Page 38: 1 Introduction to Web Application Introduction to Cascading Style Sheet

38

Text flow p160

<html> <head> <title>Flowing Text Around Floating Elements</title> <style type = "text/css"> div { background-color: #ffccff; margin-bottom: .5em; font-size: 1.5em; width: 50% } p { text-align: justify } </style> </head> <body>

<p></p> <div style = "text-align: center"></div> <div style = "float: right; margin: .5em; text-align: right"></div> <div style = "float: right; padding: .5em; text-align: right"></div> <p></p> <p style = "clear: right"></p>

</body></html>

Page 39: 1 Introduction to Web Application Introduction to Cascading Style Sheet

39

Page 40: 1 Introduction to Web Application Introduction to Cascading Style Sheet

40

Text Flow and the Box Model • 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 41: 1 Introduction to Web Application Introduction to Cascading Style Sheet

41

Text Flow and the Box Model

Content

Margin

Border

Padding

Fig. 6.13 Box model for block-level elements.

Page 42: 1 Introduction to Web Application Introduction to Cascading Style Sheet

42

Page 43: 1 Introduction to Web Application Introduction to Cascading Style Sheet

43

Page 44: 1 Introduction to Web Application Introduction to Cascading Style Sheet

44

User Style Sheets • Format pages based on preferences

• User style sheets are not linked to a document;

• Rather, they are set in the browser’s options

Page 45: 1 Introduction to Web Application Introduction to Cascading Style Sheet

45

References

Page 46: 1 Introduction to Web Application Introduction to Cascading Style Sheet

46

Boxes and Layout

• border– border

• Group property, the same below

– border-color– border-width– border-style– example

Page 47: 1 Introduction to Web Application Introduction to Cascading Style Sheet

47

Boxes and Layout

• margin– margin-top, margin-bottom, margin-right,

margin-left– example

• padding– padding-top, padding-bottom, padding-right,

padding-left– example

Page 48: 1 Introduction to Web Application Introduction to Cascading Style Sheet

48

Fonts and Texts

• Fonts– font:

• Group property

– font-family• Sets or retrieves the name of the font used for text in the object

– font-size• Sets or retrieves a value that indicates the font size used for text

in the object

– font-style• Sets or retrieves the font style of the object as italic, normal, or

oblique

Page 49: 1 Introduction to Web Application Introduction to Cascading Style Sheet

49

Fonts and Texts

• Fonts– font-variant

• Sets or retrieves the font style of the object as italic, normal, or oblique

– font-weight• Sets or retrieves the weight of the font of the object

– example

Page 50: 1 Introduction to Web Application Introduction to Cascading Style Sheet

50

Fonts and Texts

• Generic Fonts:– serif Times New Roman– sans-serif Arial– cursive Script– fantasy Comic– monospace Courier New– example

Page 51: 1 Introduction to Web Application Introduction to Cascading Style Sheet

51

Fonts and Texts

• Texts:– letter-spacing

• Sets or retrieves the amount of additional space between letters in the object

– word-spacing• Sets or retrieves the amount of additional space between

words in the object

– white-space• Sets or retrieves a value that indicates whether lines are

automatically broken inside the object

Page 52: 1 Introduction to Web Application Introduction to Cascading Style Sheet

52

Fonts and Texts

• Texts:– line-height

• Sets or retrieves the distance between lines in the object

– text-align• Sets or retrieves whether the text in the object is

left-aligned, right-aligned, centered, or justified

– vertical-align• Sets or retrieves the vertical alignment of the object

Page 53: 1 Introduction to Web Application Introduction to Cascading Style Sheet

53

Fonts and Texts

• Texts– text-decoration

• Sets or retrieves a value that indicates whether the text in the object has blink, line-through, overline, or underline decorations

– text-indent• Sets or retrieves the indentation of the first line of

text in the object

Page 54: 1 Introduction to Web Application Introduction to Cascading Style Sheet

54

Fonts and Texts

• Texts– text-transform

• Sets or retrieves the rendering of the text in the object

– example

Page 55: 1 Introduction to Web Application Introduction to Cascading Style Sheet

55

Lists

• Lists– list-style

• Sets or retrieves up to three separate listStyle properties of the object

– list-style-image• Sets or retrieves up to three separate listStyle

properties of the object

Page 56: 1 Introduction to Web Application Introduction to Cascading Style Sheet

56

Lists

• Lists– list-style-position

• Sets or retrieves a variable that indicates how the list-item marker is drawn relative to the content of the object.

– list-style-type• Sets or retrieves a variable that indicates how the list-

item marker is drawn relative to the content of the object.

– example

Page 57: 1 Introduction to Web Application Introduction to Cascading Style Sheet

57

Hypertext

• Hypertext– a:link

• Sets the style of an a element when the link has not been visited recently.

– a:visited• Sets the style of an a element when the link has been

visited recently

– a:active• Sets the style of an a element when the link is engaged or

active

– example

Page 58: 1 Introduction to Web Application Introduction to Cascading Style Sheet

58

Position• Absolute position

– position=absolute– example

• Relative position– position=relative– example

Page 59: 1 Introduction to Web Application Introduction to Cascading Style Sheet

59

Position

• Layer– z-index

• Sets or retrieves the stacking order of positioned objects

• top, bottom, left, right

• other examples

Page 60: 1 Introduction to Web Application Introduction to Cascading Style Sheet

60

Position• Floating

– floating– example

Page 61: 1 Introduction to Web Application Introduction to Cascading Style Sheet

61

Filters

• Filters:– 翻转 : flipv fliph example– 透明 : chroma example– 反转 : invert– 灰度 : gray– x 光 : xray example– 阴影 : shadow example– 发光 : glow example– 渐变 : alpha example

Page 62: 1 Introduction to Web Application Introduction to Cascading Style Sheet

62

Filters

• Filters:– 模糊 : blur example– 弦波 : wave example– … example

Page 63: 1 Introduction to Web Application Introduction to Cascading Style Sheet

63

Filters

• Transition– Transition event happens when visibility

property value change from hidden to visible– example1– example2

Page 64: 1 Introduction to Web Application Introduction to Cascading Style Sheet

64

Web Resources • http://www.w3.org/TR/css3-roadmap

• http://www.ddj.com/webreview/style• http://tech.irt.org/articles/css.htm