chapter 3 style sheets: csssallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf ·...

74
CHAPTER 3 STYLE SHEETS: CSS Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0 WEB TECHNOLOGIES A COMPUTER SCIENCE PERSPECTIVE Modified by Ahmed Sallam Based on original slides by Jeffrey C. Jackson

Upload: others

Post on 20-Jul-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

CHAPTER 3STYLE SHEETS:

CSS

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0

WEB TECHNOLOGIESA COMPUTER SCIENCE PERSPECTIVE

Modified by Ahmed Sallam

Based on original slides by Jeffrey C. Jackson

Page 2: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

MOTIVATIONHTML markup can be used to represent

• Semantics: h1 means that an element is a top-level heading

• Presentation: h1 elements look a certain wayIt’s advisable to separate semantics from presentation because:

• It’s easier to present documents on multiple platforms (browser, cell phone, spoken, …)

• It’s easier to generate documents with consistent look

• Semantic and presentation changes can be made independently of one another (division of labor)

• User control of presentation is facilitated

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0

Page 3: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

STYLE SHEET LANGUAGESCascading Style Sheets (CSS)

• Applies to (X)HTML as well as XML documents in general• Focus of this chapter

Extensible Stylesheet Language (XSL)• Often used to transform one XML document to another form,

but can also add style• XSL Transformations covered in later chapter

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0

Page 4: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

CSS INTRODUCTIONA styled HTML document

produced by the style sheet style1.css:

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0

Page 5: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

CSS SYNTAXParts of a style rule (or statement)

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0

Page 6: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

CSS SYNTAX:SELECTOR STRINGSSingle element type:

Multiple element types:

All element types:

Specific elements by id:

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0

Page 7: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

CSS SYNTAX:SELECTOR STRINGSSingle element type:

Multiple element types:

All element types:

Specific elements by id:

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0

type selector

Page 8: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

CSS SYNTAX:SELECTOR STRINGSSingle element type:

Multiple element types:

All element types:

Specific elements by id:

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0

universal selector

Page 9: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

CSS SYNTAX:SELECTOR STRINGSSingle element type:

Multiple element types:

All element types:

Specific elements by id:

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0

ID selector

Page 10: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

CSS SYNTAX:SELECTOR STRINGS

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0

Page 11: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

CSS SYNTAX:SELECTOR STRINGS

Elements belonging to a style class:

• Referencing a style class in HTML:

Elements of a certain type and class:

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0

class selector

Page 12: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

CSS SYNTAX:SELECTOR STRINGS

Elements belonging to a style class:

• Referencing a style class in HTML:

Elements of a certain type and class:

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0

this span belongs to three style classes

Page 13: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

CSS SYNTAX:SELECTOR STRINGS

Elements belonging to a style class:

• Referencing a style class in HTML:

Elements of a certain type and class:

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

this rule applies only to span’s belonging to class special

Page 14: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

CSS SYNTAX:SELECTOR STRINGS

Source anchor elements:

Element types that are descendents:

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

pseudo-classes

Page 15: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

CSS SYNTAX:SELECTOR STRINGS

Source anchor elements:

Element types that are descendants:

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

rule applies to li element that is

Page 16: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

CSS SYNTAX:SELECTOR STRINGS

Source anchor elements:

Element types that are descendants:

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

rule applies to li element that ispart of the content of an ol element

Page 17: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

CSS SYNTAX:SELECTOR STRINGS

Source anchor elements:

Element types that are descendants:

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

rule applies to li element that ispart of the content of an ol elementthat is part of the content of a ul element

Page 18: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

STYLE SHEETS AND HTMLStyle sheets referenced by link HTML element are called external style sheetsStyle sheets can be embedded directly in HTML document using style element

Most HTML elements have style attribute (value is list of style declarations)

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

Page 19: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

STYLE SHEETS AND HTMLRules of thumb:

• Use external style sheets to define site-wide style• Prefer style sheets (either external or embedded) to style

attributes• XML special characters

• Must use references in embedded style sheets and styleattribute

• Must not use references in external style sheets

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0

Page 20: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

CSS BOX MODELEvery rendered element occupies a box:

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

(or inner edge)

(or outer edge)

Page 21: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

CSS BOX MODEL

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

Page 22: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

CSS BOX MODEL

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

Page 23: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

CSS BOX MODEL

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

Page 24: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

CSS BOX MODEL

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

Page 25: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

CSS BOX MODEL

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

Page 26: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

CSS BOX MODEL

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

Page 27: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

CSS BOX MODEL

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

Page 28: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

CSS BOX MODEL

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

Page 29: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

CSS BOX MODELIf multiple declarations apply to a property, the last declaration overrides earlier specifications

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

Left border is 30px wide, inset style, and red

Page 30: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

BACKGROUNDSbackground-color

• Specifies background color for content, padding, and border areas

• Margin area is always transparent• Not inherited; initial value transparent

background-image

• Specifies (using url() function) image that will be tiled over an element

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

Page 31: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

BACKGROUNDS

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

<body style="background-image:url('CucumberFlowerPot.png')">

Page 32: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

NORMAL FLOW LAYOUTIn normal flow processing, each displayed element has a corresponding box

• html element box is called initial containing block and corresponds to entire document

• Boxes of child elements are contained in boxes of parent• Sibling block elements are laid out one on top of the other• Sibling inline elements are one after the other

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

Page 33: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

NORMAL FLOW LAYOUT

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

(body)

(html)

Page 34: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

NORMAL FLOW LAYOUT

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

Blockelementsonly

Page 35: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

NORMAL FLOW LAYOUT

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

htmlbodydiv d1div d2

div d3

div d4

Top edges ofblock boxes arein document order

Page 36: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

NORMAL FLOW LAYOUTWhat is a “block element”?

• Element with value block specified for its display property• User agent style sheet (not CSS) specifies default values;

typical block elements include html, body, p, pre, div, form, ol, ul, dl, hr, h1 through h6

• Most other elements except li and table-related have inlinespecified for display

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

Page 37: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

NORMAL FLOW LAYOUTWhen blocks stack, adjacent margins are collapsed to the size of the larger margin

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

Page 38: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

NORMAL FLOW LAYOUTInitial value of width property is auto, which for block boxes means to make the content area as wide as possible within margin/padding constraints:

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

Width of block boxesincreases as browserclient area is widened

Page 39: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

NORMAL FLOW LAYOUTCan also specify CSS length or percentage (of parent’s content width) for width property

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

By default, width of right margin is adjusted to accommodate a change to width

Page 40: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

NORMAL FLOW LAYOUTCan also specify CSS length or percentage (of parent’s content width) for width property

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

Centering can be achieved by settingboth margins to auto

Page 41: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

NORMAL FLOW LAYOUTBoxes corresponding to character cells and inline elementsare laid out side by side in line boxes that are stacked one on top of the other

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

Character cells aligned by baseline

Heights based oncontent

Page 42: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

NORMAL FLOW LAYOUTPadding/borders/margins affect width but not height of inline boxes

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

Page 43: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

NORMAL FLOW LAYOUTSpecify value for vertical-align to position an inline element within line box:

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

initial value of vertical-align

Page 44: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

BEYOND NORMAL FLOWCSS allows for boxes to be positioned outside the normal flow:

• Relative positioning

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

span’s shifted backwards relative to normal flow

Page 45: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

BEYOND NORMAL FLOWCSS allows for boxes to be positioned outside the normal flow:

• Relative positioning

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

other span’s are not affected

Page 46: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

BEYOND NORMAL FLOWCSS allows for boxes to be positioned outside the normal flow:

• Relative positioning

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

<span style="background-color:red">&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="right">Red</span> <span style="background-color:yellow">&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="right">Yellow</span> <span style="background-color:lime">&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="right">Green</span>

style rules that move span’saway from normal-flow right edge

Page 47: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

BEYOND NORMAL FLOWCSS allows for boxes to be positioned outside the normal flow:

• Float positioning

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

style rule that “floats” left

Page 48: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

BEYOND NORMAL FLOWCSS allows for boxes to be positioned outside the normal flow:

• Float positioning

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

span taken out of normalflow and “floated” to theleft of its line box

Page 49: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

BEYOND NORMAL FLOWCSS allows for boxes to be positioned outside the normal flow:

• Absolute positioning

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

style rule that moves span relative toupper left corner of containingp element’s box

Page 50: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

BEYOND NORMAL FLOWCSS allows for boxes to be positioned outside the normal flow:

• Absolute positioning

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

span’s removed fromnormal flow andpositioned relativeto another box

Page 51: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

BEYOND NORMAL FLOWProperties used to specify positioning:

• position: static (initial value), relative, or absolute• Element is positioned if this property not static• Properties left, right, top, bottom apply to positioned

elements• Primary values are auto (initial value) or CSS length

• float: none, left, or right• Applies to elements with static and relative positioning

only

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

Page 52: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

BEYOND NORMAL FLOWRelative positioning

• Specifying positive value for right property of relatively positioned box moves it to left

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

<span style="background-color:red">&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="right">Red</span>

spancontainingtext movesleft

Page 53: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

BEYOND NORMAL FLOWRelative positioning

• Specifying negative value for left property also moves box to left

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

<span style="background-color:red">&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="right">Red</span>

sameeffect asbefore

Page 54: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

BEYOND NORMAL FLOWFloat positioning

• Specify value for float property

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

Page 55: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

BEYOND NORMAL FLOWFloat positioning

• Specify value for float property

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

Floated element becomes a CSS blockelement (e.g., can set height and width)

Page 56: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

BEYOND NORMAL FLOWAbsolute positioning

• Specify location for corner of box relative to positioned containing block

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

margin areapadding area

containingblock

This second paragraph has anote.

p elements are positioned (but don’t move!)

Page 57: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

BEYOND NORMAL FLOWAbsolute positioning

• Specify location for edges of box relative to positioned containing block

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

Page 58: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

BEYOND NORMAL FLOWAbsolute positioning

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

10em padding topedge

padding leftedge

Page 59: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

BEYOND NORMAL FLOWAbsolute positioning

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

8em

Page 60: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

BEYOND NORMAL FLOWAbsolutely positioned box does not affect positioning of other boxes!

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

Second absolutelypositioned boxobscures first

Page 61: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

CSS POSITION-RELATED PROPERTIESz-index: drawing order for overlaid boxes (largest number drawn last)

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

Page 62: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

CSS POSITION-RELATED PROPERTIESdisplay: value none means that element and its descendants are not rendered and do not affect normal flow

visibility: value hidden (initial value is visible) means that element and its descendants are not rendered but still doaffect normal flow

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

Page 63: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

CSS RULE CASCADEWhat if more than one style declaration applies to a property of an element?

The CSS rule cascade determines which style rule’s declaration applies

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0

Page 64: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

CSS INHERITANCEWhat if no style declaration applies to a property of an element?Generally, the property value is inherited from the nearest ancestor element that has a value for the propertyIf no ancestor has a value (or the property does not inherit) then CSS defines an initial value that is used

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

Page 65: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

CSS INHERITANCE

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

Page 66: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

CSS INHERITANCEProperty values:

• Specified: value contained in declaration• Absolute: value can be determined without reference to

context (e.g., 2cm)• Relative: value depends on context (e.g., larger)

• Computed: absolute representation of relative value (e.g., larger might be 1.2 x parent font size)

• Actual: value actually used by browser (e.g., computed value might be rounded)

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

Page 67: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

CSS INHERITANCEMost properties inherit computed value

• Exception discussed later: line-heightA little thought can usually tell you whether a property inherits or not

• Example: height does not inherit

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights

Page 68: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

EXTERNAL CSS

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0

link element associates style sheet with doc.

Page 69: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

EXTERNAL CSS

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0

type attribute specifies style language used

Page 70: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

EXTERNAL CSS

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0

href attribute provides style sheet URL

Page 71: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

EXTERNAL CSS

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0

title attribute provides style sheet name

Page 72: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

EXTERNAL CSS

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0

Page 73: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

CSS INTRODUCTION

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0

Alternative, user selectable style sheetscan be specified

Page 74: CHAPTER 3 STYLE SHEETS: CSSsallamah.weebly.com/uploads/6/9/3/5/6935631/webtech-2015-f-03.pdf · Cascading Style Sheets (CSS) • Applies to (X)HTML as well as XML documents in general

CSS & MEDIASingle document can be displayed on multiple media platforms by tailoring style sheets:

This document will be printed differently than it is displayed.

Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0