tutorial 3 working with cascading style sheets. introducing cascading style sheets style sheets are...

59
Tutorial 3 Working with Cascading Style Sheets

Upload: caitlin-strickland

Post on 11-Jan-2016

221 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Tutorial 3Working with

Cascading Style Sheets

Page 2: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Introducing Cascading Style Sheets

• Style sheets are declarations that describe the layout and appearance of a document

• Cascading Style Sheets (CSS) is a style sheet language used on the Web– CSS specifications are maintained by the

World Wide Web Consortium (W3C) www.w3c.org

– Several versions of CSS exist: CSS1, CSS2, CSS 2.1, and CSS3

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

2

Page 3: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Cascading Style Sheets• Cascading Style Sheets (CSS)

specifications are maintained• by the World Wide Web Consortium (W3C)• Several versions of CSS exist:• CSS1 (1996)• CSS2 (1998)• CSS 2.1 (revision of CSS2) the newest

useable version. However, it was returned to Candidate status in 2005 when the W3C decided to more strictly enforce standards.

• CSS3 still in Candidate statusNew Perspectives on HTML, XHTML,

and Dynamic HTML, 4e3

Page 4: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Applying a Style Sheet

• Three ways to apply a style to an HTML or XHTML document:– Inline Styles – Apply to just one tag– Embedded Style Sheet – Apply to

whole Web page– External Style Sheet – Apply to

whole Web site

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

4

Page 5: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Using Inline Styles• Inline styles are easy to use and interpret

because they are applied directly to the elements they affect

<element style=“style1: value1; style2:

value2; style3: value3;…”>

<h1 style=“text-align: center; color: blue;”>

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

5

The style attribute is used to affect an individual HTML element

Note semi-colon

Note colon

Page 6: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Using Embedded Styles

• You can embed style definitions in a document head using the following form:

<head> <style type=“text/css”> style declarations </style> </head>

• Where style declarations are the declarations of the different styles to be applied to the whole document

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

6

Page 7: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Using Embedded Styles

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

7

<head>

</head>

Note the curly braces. You don't use curly braces with inline styles

Note also, the “selector”, you have to let the browser know which element to apply the style to. This is not needed with inline styles, as the style is, by definition, applied to the tag in which it is found.

Page 8: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Using an External Style Sheet• Because an embedded style sheet only applies

to the content of one page, you need to place a style declaration in an external style sheet to apply to an entire Web site

• An external style sheet is a text file that contains style declarations– It can be linked to any page in the site, allowing the

same style declaration to be applied to the entire site

<head><link href=“welcome.css” rel=“stylesheet”

type=“text/css” />

</head> New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

8

Page 9: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Using an External Style Sheet

• You can add style comments as you develop an external style sheet/* comment */

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

9

Page 10: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Understanding Cascading Order• You can link a single style sheet to

multiple documents in your Web site by using the link element

• You can also link a single document to several style sheets

• If you have conflicting styles the one with the highest precedence is used.

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

10

Page 11: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Style Inheritance

• If a style is not specified for an element, it inherits the style of its parent element. This is called style inheritance

body {color: blue}

p {color: red}

• In the above example, the body element is the parent element examples/005_pg-134-style-inheritance.htm

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

11

Page 12: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Applying a Style to a Specific ID• To apply a style to an element marked

with a specific id value, use the declaration#id {style rule}

where id is the value of the element’s id attribute and style rule stands for the styles applied to that specific element

• When specifying an ID, you preface the ID with a pound sign (#)

• examples\007_pg-134-applying-style-to-id.htm

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

12

Page 13: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Working with Color in HTML and CSS

• Any color can be thought of as a combination of three primary colors: red, green, and blue

• By varying the intensity of each primary color, you can create almost any color and any shade of color

• This principle allows a computer monitor to combine pixels of red, green, and blue to create the array of colors you see on your screen

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

13

Page 14: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Working with Color in HTML and CSS

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

14

Page 15: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Working with Color in HTML and CSS

• HTML is a text-based language, requiring you to define your colors in textual terms

• HTML identifies a color in one of three ways:– By the color name, a numeric expression or

RGB value• background-color: orange• background-color: #FFA500• background-color: rgb(255, 165, 0)

• demo_css.htm

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

15

All the same color

Page 16: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Working with Color in HTML and CSS

• Using the basic color names allows you to accurately display them across different browsers and operating systems

• There were originally 16 basic colors, CSS 2.1 added another one. The list of only 17 colors is limiting to Web designers

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

16

Page 17: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Working with Color in HTML and CSS

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

17

Partial list of 140 extended color names

demo_color_names.htm List of valid color names demo_safety_palette.htm List of “Safe” colors – those guaranteed to be safe from dithering

Page 18: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Defining Text and Background Colors

• Background color definition:– background-color: color

• Text color definition:– color: color

where color is either the color value or the color

name

• You can apply text and background colors to any page element

• examples\008_pg-138-controlling-color.htm New Perspectives on HTML, XHTML,

and Dynamic HTML, 4e18

Page 19: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

19

•Go to my Web site and download the files for Tutorial 3

•http://www.kapple01.com/ccm/web2/home/web2_labdata.html

•Unzip the files into a work folder on your flash drive

•Go to pg 121 in text book and start the in-chapter exercise.

•Stop when you get to the end of the section on page 140

Page 20: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Working with Fonts and Text Styles

• A specific font is a font such as Times New Roman, Arial, or Garamond. The font is installed on a user’s computer

• A generic font refers to the font’s general appearance

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

20

Generic fonts

Page 21: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Working with Fonts and Text Styles• CSS allows you to specify a list of specific

fonts along with a generic font• In HTML we used <font face=font> to

change the font. The style version is <font-family: font>

• If the browser cannot find any of the specific fonts listed, it uses the generic fontfont-family: Arial, Helvetica, ‘Trebuchet MS’, sans-serif

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

21

Page 22: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Setting the Font Size• The style to change the font size of

text within an element is:– font-size: lengthwhere length is a number followed by a unit of measure

• Absolute units define a font size using one of five standard units of measurement:–Millimeters (mm)– Centimeters (cm)– Inches (in)– Points (pt)– Picas (pc)

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

22

Page 23: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Setting the Font Size• Many Web page designers opt to use

relative units, which are expressed relative to the size of other objects within the Web page– Em units (have become increasingly popular

because they scale nicely to different pages)

– Percentages– Relative keywords

• xx-small, x-small, small, medium, large, x-large, xx-large

• Larger• Smaller

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

23

Page 24: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Spacing and Indentation• Kerning is the amount of space between

characters– letter-spacing: value

• Tracking is the amount of space between words and phrases– word-spacing: value

• Leading is the space between lines of text– line-height: length

• Indent is the amount of space that you want to indent the first line of a paragraph– text-indent: value

• Value can be negative – creating a hanging indentNew Perspectives on HTML, XHTML,

and Dynamic HTML, 4e24

Page 25: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Applying Font Features

• To specify font styles, use the following style: – font-style: type

• To control font weight for any page element, use the following style: – font-weight: weight

• To change the appearance of your text, use the following style: – text-decoration: type

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

25

Page 26: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Applying Font Features

• Capitalize: –text-transform: type

• Uppercase letters, small font: –font-variant: type

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

26

Page 27: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Aligning Text Vertically

• Use the vertical-align attribute to align text vertically– vertical-align: type

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

27

Page 28: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Combining All Text Formatting in a Single Style

• You can combine most of them into a single declaration, using the style

font: font-style font-variant font-weight font-size

line-height font-family• You don't have to specify all the

formatting, but they must be specified in this order.

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

28

Page 29: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Examples of Font Styles

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

29

012_pg-152-font-example.htm

Page 30: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Working with GIF Images

• GIF (Graphics Interchange Format) is the most commonly used image format on the Web

• Compatible with virtually all browsers• GIF files are limited to displaying 256 colors• Often used for graphics requiring fewer

colors, such as clip art images, line art, logos, and icons

• Images that require more color depth, such as photographs, can appear grainy when saved as GIF files

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

30

Page 31: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Working with GIF Images• A transparent color is a color that is not

displayed when the image is viewed in an application

• A splash screen is a Web page containing interesting animation or graphics that introduces a Web site

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

31

Page 32: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

JPEG Images• JPEG stands for Joint Photographic Experts

Group• Supports up to 16.7 million colors• Most often used for photographs and other

images that cover a wide spectrum of color• Surprisingly, often smaller than their GIF

counterparts. This is due to better compression algorithms.

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

32

• JPEGs do not support transparency as Gifs do.

Page 33: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

PNG Images

• A file format called PNG (Portable Network Graphics) has been gaining wider acceptance

• PNG files use a free and open file format and can display more colors than GIFs

• PNGs do allow transparent colors, but not all browsers support this feature

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

33

Page 34: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Setting the Image Size

• By default, browsers display an image at its saved size

• You can specify a different size by adding the HTML attributeswidth="value" height="value"

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

34

Page 35: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Formatting Backgrounds

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

35

Page 36: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Background Image Options• By default, background images are

tiled both horizontally and vertically until the entire background of the element is filled up

• You can specify the direction of the tiling using the style:– background-repeat: type

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

36

Page 37: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Background Image Options

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

37

013_pg-161-background-placement.htm

Page 38: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

The Background Style

• You can combine the various background styles into the following single style:background: color url(url) repeat attachment horizontal vertical

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

38

•Color defines the background color•URL defines the image to be used•Repeat defines how and if the image repeats (see previous slide)•Attachment specifies if the image scrolls or is fixed as user moves scroll bar•Horizontal specifies Horizontal placement: Left/Center/Right•Vertical specifies Vertical placement: Top/Center/Bottom

015_pg-161-background-placement.htm

Page 39: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

39

•Go to pg 149 in text book and continue the in-chapter exercise.

•Stop when you get to the end of the section on page 161

•To save class-time, I want you to skip the demos on pages 141, 146, 147 and 151. If you finish the section before I start lecturing, you can go back and play with these demos, or you can examine them outside of class. But for now, I don’t want to spend class time on the demos.

Page 40: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Floating an Element

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

40

• The syntax for the float style is:• style=“float: right” or “float: left” or “float: none”

examples/020_pg-163-float.htm

Page 41: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

41

pg-164-clear.htm

Page 42: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Working with the Box Model

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

42 pg-166-box-model.htm

Page 43: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Margin Styles• Control your margins with the following four styles:

– margin-top: length margin-right: length– margin-bottom: length margin-left: length

• Margin values can also be negative. This may create an overlay effect by forcing the browser to render one element on top of another

• You can also combine the four margin styles into a single style:– margin: top right bottom left

• If you use this shorthand, elements must be coded in this order

• If you specify just one value, it will pertain to all margins• If you specify 3values they will affect: Top, Right and Bottom• If you specify 2 values, the first value affects top and

bottom, the second affects left and right (error in book says it applies only to top and bottom)

035_pg-167-margin.htmNew Perspectives on HTML, XHTML, and Dynamic HTML, 4e

43

Page 44: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Margin Style Examples

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

44

:

Page 45: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Padding Styles

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

45

Page 46: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Border Styles• border-top-width: length• border-right-width: length• border-bottom-width: length• border-left-width: length• border-width: top right bottom left

• border-top-color: color• border-right-color: color• border-bottom-color: color• border-left-color: color• border-color: top right bottom left

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

46

Length is specified in a unit of measure like: px, em, pt etc…..

Color is specified with a numeric color value or color name

You can only change the color of the border, you can’t change the color of the margin or padding

Page 47: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Border Styles

47

Important. If you want to see your border you must specify one of these options (except none)

You can combine all of the border styles in one “super style”border: width style color<h1 style="border: 10px dashed blue"> pg-169-border-combined.htm

Do not combine shortcut and long versions: pg_169-dont-combine-short-and-long

Page 48: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Border Styles

48

• Let’s review the example from a few slides back. If you remember when we coded float: right everything looked fine, however with float: left there was no space between the image and the paragraph

043_pg-164,169-float-does-not-chg-size.htm

Page 49: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Width and Height Styles• To set the box model width, use

– width: length

where length is the width of the box content in one of the CSS units of measure.

• Note that Internet Explorer applies the width value to the box model content, padding, and border. But the CSS model applies width only to content. This means that other browsers will render the width differently (see next slide)

• To set the box model height, use– height: length

where length is the height of the box content in one of the CSS units of measure

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

49

Page 50: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Width and Height Styles

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

50

Width applies only to content

Width applies content, padding and borders

Page 51: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Controlling Page Layout with div Containers

• Sections of page content can be enclosed in div containers

• div containers can be resized and floated to create different page layouts

• A div container lets you create a block of page content that you can manipulate using styles

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

51

Page 52: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Controlling Page Layout with div Containers

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

52

Page 53: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

div container example

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

53

Page 54: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

div container example

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

54

pg-174-div.htm

Page 55: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Span for inline style

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

55

047_pg-179-span.htm

Page 56: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Setting the Display Style

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

56

Values of the display style

Page 57: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Setting the Display Style

• To set the display style of an element, use– display: type

where type is the type of display. Use inline for inline elements and block for block-level elements

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

57

Page 58: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

Setting the Display style

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

58

pg-180-display-style.htm

Page 59: Tutorial 3 Working with Cascading Style Sheets. Introducing Cascading Style Sheets Style sheets are declarations that describe the layout and appearance

New Perspectives on HTML, XHTML, and Dynamic HTML, 4e

59

•Review

Then:

•Go to pg 162in text book and complete the in-chapter exercise.

•When you are done make sure you ZIP the entire folder and send it to me in an e-mail

•Don’t forget to do Case Study 2 starting on pg 190