49
1 Cascading Style Sheets (CSS) Part 2

1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

Embed Size (px)

Citation preview

Page 1: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

1

Cascading Style Sheets(CSS)

Part 2

Page 2: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

2

The Sources of Style Sheets

Page 3: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

3

Embedded Style Sheets

<HTML> <HEAD> <STYLE type="text/css"> <!-- BODY {color: red} --> </STYLE> </HEAD> <BODY> ... </BODY> </HTML>

The embedded style sheet appears in the header inside a <STYLE> element.

All the text of the style sheet is inside an HTML comment so that browsers that do not support CSS will ignore the text.

Page 4: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

4

Inline Styles

• Any tag, except the <HTML> tag itself, can have the style attribute

• For example, the following will define the color of the paragraph to be green

<P style="color: green"> and this is green</P>

Page 5: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

5

Imported Style Sheets

• The @import rule imports style rules to the beginning of the style sheet

• Usage: @import url(nameOfFile.css)• Several import rules may appear at

the beginning of the style sheet• Import rules can appear in embedded

style sheets or in external style sheets

Page 6: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

6

Linked Style Sheets

• Links to external style sheets can appear in the header of an HTML page

<HTML> <HEAD> <LINK rel="stylesheet" type="text/css”

href=“name.css“ media=“print handheld”> </HEAD> <BODY> ... </BODY></HTML>

Page 7: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

7

Inheritance and Cascading

Page 8: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

8

Inheritance of Properties

• If an element does not match any rule for some given property, then that property eitherhas an initial value, or inherits the value from the parent of the element

• The CSS2 Specification determines which properties are inherited, for example font-size is inheritedbackground-color is not inherited – its initial

value is transparent

Page 9: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

9

Computed Values are Inherited

• When a property is inherited, the computed value (and not a relative value) is inherited

• See the example on next the slide

Page 10: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

10

Example

• Given the rules:BODY { font-size: 10pt }H1 { font-size: 120% }

• What will be the font size of the EM element in the following HTML segment?

<BODY> <H1>A <EM>large</EM>

heading</H1></BODY>

Page 11: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

11

Cascading Order

• When several rules define the same property and match the same element, then the cascading order is as follows:Primary sort: weight and originSecondary sort: specificity of selectorsFinal sort: order of appearance

• The cascading order determines which rule applies

Page 12: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

12

Weight and Origin

• Author style sheets override user style sheets

• User style sheets override default (browser) style sheets

• ! important declaration overrides normal declaration

• If both author and user declarations have ! important, the user declarations still override the author declarations (in CSS2, but not in CSS1)

Page 13: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

13

Specificity of a Selector

• Let a be the number of ID attributes in the given selector

• Let b be the number of attributes and pseudo-classes in the given selector

• Let c be the number of element names in the given selector

• Sort the triples (a,b,c) lexicographically

• Pseudo-elements are ignored

Page 14: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

14

Example

• Consider the two rulesP {…}.introductory {…}

• If an element matches both, then the second rule has a higher specificity and overrides the first

Page 15: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

15

Order of Appearance

• If two rules have the same weight, origin and specificity, then the one that appears last in the style sheet overrides the others

• Rules in imported style sheets are considered to appear before local rules

Page 16: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

16

Lengths and Colors

How to Specify Them

Page 17: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

17

Colors

• Colors can be specified as namesAqua, Black, Blue, Fuchsia, Gray,

Green, Lime, Maroon, Navy, Olive, Purple, Red, Silver, Teal, White, Yellow

• Colors can be specified as #RRGGBBEach of RR, GG and BB should be a

hexadecimal number ranging from 00 to FF

Page 18: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

18

More Ways to Specify Colors

• RGB(rrr,ggg,bbb)Each of rrr, ggg and bbb should be a

decimal number ranging from 0 to 255

• RGB(rrr%,ggg%,bbb%)Each of rrr, ggg and bbb should be a

decimal number ranging from 0 to 100

Page 19: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

19

Lengths

• Lengths can be specified in absolute unitscm, in, mm, pc (6 picas per inch), pt (72

points per inch)• Length can be specified in relative

unitsem (the height of the current font), ex

(the height of the letter “x” in the current font), px (pixels – relative to the screen resolution and size)

Page 20: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

20

Using%

• Sometimes it is possible to use % to specify length (it is % of the current value of that property, as determined by inheritance and cascading)

• For examplefont-size: 200%

Page 21: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

21

The Box Model

Page 22: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

22

Elements as Boxes

• The element is the yellow area• The border is the thick black line• The padding is the area between the

element & the border• The margin is around the border, on the

outside (the dotted line is imaginary)

<p> This is text. </p>

Page 23: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

23

Don’t Be Mislead by the Diagram

• The padding has the same background color as the element

• The border can get its own color and style• The margin is transparent• Margins may collapse, i.e., adjoining margins of two

or more boxes may combine (overlap) to form a single margin

<p> This is text. </p>

Page 24: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

24

Padding Properties• The following properties determine the

width of the padding on each sidepadding-top padding-right padding-bottom padding-left

• The value isfixed length or % (of the width of the

containing box)• See examples in W3Schools

Alternatively, all four widths can be assigned to the property:

padding

Page 25: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

25

Border Properties

• The properties of the border areThe width (on each side)

•the possible values arethin, medium, thick, or length in some units (not %)

The border styleThe border color

• See examples in W3Schools

Page 26: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

26

Margin Properties

• The margin properties are the width on each side

• The value is one of the following:fixed length % (of the width of the containing box)Auto

• See examples in W3Schools

Page 27: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

27

Visual Formatting Model

Also Known as “Positioning”

Page 28: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

28

Inline-Level Elements

• Inline-level elements do not start a new line, for example<EM><SPAN>

• Inline-level elements generate inline boxes, which are positioned horizontally (i.e., side-by-side on the same line until the line is full and then continuing on the next line)

Page 29: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

29

Block-Level Elements

• Block-level elements start a new line, for example<P><LI><DIV>

• Block-level elements generate block boxes, which are positioned vertically (i.e., each box below the previous one)

Page 30: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

30

The Display Property

• The display property may force an element to become inline-level or block-level

• Some possible values of the display property areinlineblocknone

• See example in W3Schools

Page 31: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

31

Normal Flow

• Normal flow means that boxes are positioned left-to-right, top-to-bottom

• Each block box starts a new line

Page 32: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

32

Floats

• A floating box is first laid out according the normal flow,

andthen it is taken out of the normal flow

and shifted left or right as far as possible

• The normal flow continues along the side of a floating box

• See example in W3Schools

Page 33: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

33

Absolute Positioning

• The declaration position: absolute means that the box is taken out of the normal flow entirely and positioned in some fixed coordinates with respect to the containing block

• See example in W3Schools• More examples

Page 34: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

34

Relative Positioning

• The declaration position: relative means that the box is first laid out according to the normal flow and then it may be shifted relative to this position

• The next box is laid out as if the previous relative box was not shiftedRelative positioning may cause boxes to

overlap• See example in W3Schools

Page 35: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

35

The Clear Property

• The clear property is used to indicate that a box should not be placed next to the side of a float, but rather below that float

• The possible values are:none (the initial value)leftrightboth

Page 36: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

36

More Properties that Effect Positioning

• Clip• Overflow• Vertical-align• V-index• See examples in W3Schools

Page 37: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

37

Media Types

Page 38: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

38

Rules for Specific Media Types

• It is possible to define rules for specific media types

• See example in W3Schools• The visual formatting model may

behave differently for different media types

Page 39: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

39

Examples

A Three-Column Design

Page 40: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

40

Basic Idea

• Split the page into three column• Menu on the left• Main content in the center• Links (e.g., resources) on the right• Possibly some additional text is

positioned after the three columns

Page 41: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

41

The Rule for the Body

body{ background-color: gray; font-family: Tahoma, Verdana,

sans-serif; font-size: 400%; color: yellow; }

Page 42: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

42

The Rule for the Left Column

DIV#left{ float: left; width: 20%; height: 300px; background-color: #000066; }

Page 43: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

43

The Rule for the Center Column

DIV#center{ float: left; width: 60%; height: 350px; background-color: #660000; }

Page 44: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

44

The Rule for the Right Column

DIV.right{ float: left; width: 20%; height: 350px; background-color: #006600; }

Page 45: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

45

The Body

<div id="left"> <p> float: left; </p> </div><div id="center"> <p> float: left;

</p> </div><div class="right"> <p> float: left;

</p> </div>

Page 46: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

46

The Rule for More Text

<div style="position: static">More text goes here.……</div>

Page 47: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

47

In the Following Examples

• Sometimes the width is in px and sometimes the width is in %

• Sometimes the “More Text” DIV has clear: left

• In the following examples, we vary the value of the position property in each DIV

Page 48: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

48

The Examples

• Example 1: All positions are static• Example 2: All positions are

relative• Example 3: All DIVs are float: left• Example 4: DIV.right is float: right• Example 5: The DIV of the “More

Text” has clear: left

Page 49: 1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets

49

Two Right Columns

• Example 6: DIV.right is float: right• Example 7: DIV.right is float: left

(and it also has margins)If the width of DIV.right is 20%, it will

never fit on the right side, because of the width of the margin

• Change the font size in the examples and see what happens