59
Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

Embed Size (px)

Citation preview

Page 1: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

Cascading Style Sheets (CSS)

ICS213, 1 / 2011Dr. Seung Hwan Kang

Page 2: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

Outline3.1 Introduction3.2 Levels of Style

Sheets3.3 Style

Specification Formats3.4 Selector Forms3.5 Property Value

Forms3.6 Font Properties3.7 List Properties3.8 Color3.9 Alignment of

Text3.10 The Box

Model3.11 Background

Images3.12 The <span>

and <div> Tags3.13 Conflict

Resolution

2

Page 3: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.1 Introduction•T

he CSS 1 specification was developed in 1996•C

SS 2 was released in 1998•C

SS 3 is on its way •C

SSs provide the means to control and change presentation of HTML documents

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

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

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

3

Page 4: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.2 Levels of Style Sheets There are three levels of style sheets, inline,

document-level and external, in order from lowest level to highest level.• Inline - specified for a specific occurrence of a tag and

apply only to that tag– This is fine-grain style, which defeats the purpose of style sheets - uniform style

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

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

When more than one style sheet applies to a specific tag in a document, the lowest level style sheet has precedence In a sense, the browser searches for a style property

spec, starting with inline, until it finds one (or there isn’t one)

4

Page 5: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.3 Style Specification Formats•I

nline• Style sheet appears as the value of the style attribute• General form:

style="property_1: value_1;

property_2: value_2;

property_n: value_n;"

<p style="font-size: 40pt;">On the plains of hesitation bleach the bones of countless millions </p>

5inline.html

Page 6: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.3 Style Specification Formats (cont’d)

•Document-level style sheet

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

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

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

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

6

Page 7: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.3 Style Specification Formats (cont’d)

•Document-level style sheet (cont’d)• General form:…</head><style type="text/css"> <!--

rule list --></style>

<body>

• Form of the rules:selector { list of property/values; }• Each property/value pair has the form: property: value

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

7

h1 { font-size: 24pt; color: blue;

}

doclevel.html

Page 8: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.3 Style Specification Formats (cont’d)

External style sheet A <link> tag is used to specify that the browser

is to fetch and use an external style sheet file

<!– template.html --><!DOCTYPE HTML>

<head>

<title></title>

<link rel="stylesheet" type="text/css" href="mystyle.css" />

</head>

<body>

</body>

</html>

8template.html, mystyle.css

Page 9: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.4 Selector Forms3.4.1 Simple Selector Forms3.4.2 Class Selectors3.4.3 Generic Selectors3.4.4 id Selectors3.4.5 Universal Selectors3.4.6 Pseudo Classes

9

Page 10: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.4 Selector Forms (cont’d)3.4.1 Simple Selector Forms• The selector is a tag name or a list of tag

names, separated by commas

P { font-size: 12pt; }

h1, h3 { font-size: 24pt; color: blue; }

/* more readable */

h1, h3 {

font-size: 24pt;

color: blue;

}

10greet.html

Page 11: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.4 Selector Forms (cont’d)3

.4.2 Class Selectors•U

sed to allow different occurrences of the same tag to use different style specifications

p.normal { font-size: 12pt;

}

<p class="normal">This is a normal sentence.</p>

11

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

Page 12: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.4 Selector Forms (cont’d)3

.4.3 Generic Selectors•A

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

•A generic class must be named, and the name must begin with a period (.)

.really-big { font-size: 40pt;

}

•Use it as if it were a normal style class<h1 class="really-big"> … </h1>...<p class="really-big"> … </p>

12greet3.html

Page 13: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.4 Selector Forms (cont’d)3.

4.4 id Selectors•A

n id selector allow the application of a style to one specific element•G

eneral form:

#specific-id { property-value list; }

• the style specified in the id selector applies to the element with the specific id.#section14 { font-size: 20; }

<h2 id="section14">1.4 Calico Cats</h2>

13template.html, mystyle.css

Page 14: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.4 Selector Forms (cont’d)3.4.5

Universal Selectors•T

he universal selector, denoted by an asterisk (*), applies its style to all elements in the document.

•it makes all elements in the document red.

* {

color: red;

}

•It is not often useful.

14

Page 15: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.4 Selector Forms (cont’d)3.4.6

Pseudo Classes•P

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

•Names begin with colons (:)

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

•focus classes apply when an element has focus

input:hover { color: red; }

input:focus { color: green; }

15pseudo.html

Page 16: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.5 Property Value Forms•T

here are 60 different properties in 7 categories:• Fonts (3.6)• Lists (3.7)• Colors (3.8)• Alignment of text (3.9)• Margins (3.10)• Borders (3.10)• Backgrounds (3.11)

16

Page 17: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.5 Property Value Forms (cont’d)Keywords property values are used - left, small, … Not case sensitive

Length - numbers, maybe with decimal pointsUnits:

px - pixels in - inches cm - centimeters mm - millimeters pt - points pc - picas (12 points) em - height of the letter ‘m’ ex-height - height of the letter ‘x’ No space is allowed between the number and the unit

specification e.g., 1.5 in is illegal!

17

Page 18: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.5 Property Value Forms (cont’d)•P

ercentage - just a number followed immediately by a percent signe.g., 75%

•URL valuesurl(protocol://server/pathname)

•ColorsColor name

rgb(n1, n2, n3)

Numbers can be decimal or percentages

Hex form: #XXXXXX

•Property values are inherited by all nested tags, unless overridden

18

Page 19: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.6 Font Properties3.6.1

Font Families3.6.2

Font Sizes3.6.3

Font Variants3.6.4

Font Styles3.6.5

Font Weights3.6.6

Font Shorthands3.6.7

Text Decoration19

Page 20: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

20

3.6 Font Properties3

.6.1 Font Families• Value is a list of font names - browser uses the first in the list it has

font-family: Arial, Helvetica, Courier;

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

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

font-family: Arial, 'Times New Roman', 'Courier New';

Page 21: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

•3.6.1 Font Families (cont’d)

21

Table 3.1 Generic Fonts

Page 22: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.6 Font Properties (cont’d)3.6.2 Font Sizes

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

Font-size: 10pt;

22

Page 23: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.6 Font Properties (cont’d)3.6.3 Font Variants

Normal, small-caps

Font-variant: small-caps;

23

Page 24: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.6 Font Properties (cont’d)3.6.4 Font Styles

italic, oblique (useless), normal

Font-style: italic;

24

Page 25: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.6 Font Properties (cont’d)3.6.5 Font Weights degrees of boldness

bolder, lighter, bold, normalCould specify as a multiple of 100 (100 – 900)

font-weight: bold;

font-weight: 300;

25

Page 26: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.6 Font Properties (cont’d)3.6.6 Font Shorthands

For specifying a list of font properties

font: bolder 14pt Arial Helvetica;

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

26fonts.html, fonts2.html

Page 27: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.6 Font Properties (cont’d)3

.6.7 Text Decoration• line-through, overline, underline, none• letter-spacing – value is any length property value

Text-decoration: line-through;

27decoration.html

Page 28: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.7 List PropertiesUnordered lists

Bullet can be a disc (default), a square, or a circle Set it on either the <ul> or <li> tag

On <ul>, it applies to list items

<h3>Some Common Single-Engine Aircraft</h3>

<ul style="list-style-type: square">

<li>Cessna Skyhawk</li>

<li>Beechcraft Bonanza</li>

<li>Piper Cherokee</li>

</ul>

28

Page 29: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

29

3.7 List Properties (cont’d)On <li>, list-style-type applies to

just that item

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

Page 30: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.7 List Properties (cont’d)

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

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

30

Page 31: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

List properties (continued)

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

31sequence_types.html

Page 32: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.8 Color•3

.8.1 Color Groups•3

.8.2 Color Properties

32

Page 33: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.8 Colors (cont’d)• Color is a problem for the Web for two

reasons:1. Monitors vary widely

2. Browsers vary widely

33

Page 34: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.8 Colors (cont’d)3.8.1 Color Groups

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

34

Page 35: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.8 Colors (cont’d)3.8.1 Color Groups (cont’d)

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

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

• 216 colors

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

• Inside back cover of this book has them!

3. Any one of 16 million different colors

• The color property specifies the foreground color of elements

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

35

Page 36: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

Colors (continued)3.8.2 Color

Properties•I

t is used to specify the foreground color of (X)HTML elements. <style

type="text/css"> th.red {

color: red; }

th.orange { color: orange; } </style> … <table> <tr> <th

class="red"> Apple </th> <th

class="orange"> Orange </th> <th

class="orange"> Screwdriver </th> </tr> </table>

36color.html

Page 37: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.9 Alignment of Text•T

he text-indent property allows indentation • Takes either a length or a % value

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

•Sometimes we want text to flow around another element - the float property• The float property has the possible values, left, right, and none (the default)

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

37

Page 38: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.9 Alignment of Text (cont’d)<

img src="c210.jpg" style="float: right" />

•Some text with the default alignment - left

38

Page 39: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.10 The Box Model•3

.10.1 Borders•3

.10.2 Margins and Padding

39

Page 40: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.10 The Box Model•3

.10.1 Borders•e

very element has a border-style property• Controls whether the element has a border and if so, the style

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

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

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

40borders.html

Page 41: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.10 The Box Model (cont’d)•3

.10.2 Margins and Padding•M

argin – the space between the border of an element and its neighbor element

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

<img src="c210.jpg" style="float: right;

margin-left: 1in;

margin-bottom: 1in"

/>

41

1 inch

1 inch

Page 42: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.10 The Box Model (cont’d)

•Padding – the distance between the content of an element and its border• Controlled by padding, padding-left, etc.

42

marpads.html

Page 43: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.10 The Box Model (cont’d)

43

Page 44: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.11 Background Images•T

he background-image property is used to place an image in the background of an element.

•Repetition can be controlled•background-repeat property

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

•background-position property• Possible values: top, center, bottom, left, or right

44back_image.html

Page 45: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.12 The <span> and <div> Tags•O

ne problem with the font properties is that they apply to whole elements, which are often too large• Solution: a new tag to define an element in the content of a larger element - <span>

• The default meaning of <span> is to leave the content as it is

<p>

Now is the <span> best time </span> ever!

</p>45

Page 46: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.12 The <span> Tag (cont’d)• Use <span> to apply a document style sheet to its content

<style type="text/css">

.bigred { font-size: 24pt;

font-family: Arial; color: red; }

</style>

<p>

Now is the <span class="bigred"> best time </span> ever!

</p>

46

Page 47: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.12 The <span> Tag (cont’d)•T

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

47

Page 48: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.12 The <div> Tag (cont’d)•Y

ou can apply CSS between <div> tags•C

reate an ID in CSS with . followed by ID

.header {position:

relative;width: 750px;height: 121px;margin: 10px;background-

color: #dcedd1;}

•Then put div tags with IDs around that content you are manipulating

<div class="header">My header</div>

•Can only be used once per page• Good strategy for one-use structural elements (header, body, nav, footer)

48

Page 49: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.12 The <div> Tag (cont’d)•Y

ou can apply CSS between <div> tags•C

reate an ID in CSS with # followed by ID

#header {position:

relative;width: 750px;height:

121px;margin: 10px;background-

color: #dcedd1;}

•Then put div tags with IDs around that content you are manipulating

<div id="header">My header</div>

•Can only be used once per page• Good strategy for one-use structural elements (header, body, nav, footer)

49

Page 50: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.13 Conflict Resolution (cont’d)•W

hen two or more rules apply to the same tag there are rules for deciding which rule applies

•Document level• In-line style sheets have precedence over document style sheets• Document style sheets have precedence over external style sheets

•Within the same level there can be conflicts• A tag may be used twice as a selector• A tag may inherit a property and also be used as a selector

•Style sheets can have different sources• The author of a document may specify styles• The user, through browser settings, may specify styles

•Individual properties can be specified as important

50

Page 51: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.13 Conflict Resolution (cont’d)Precedence Rules from highest to lowest1. Important declarations with user origin2. Important declarations with author origin3. Normal declarations with author origin4. Normal declarations with user origin5. Any declarations with browser (or other user

agent) origin

51

Page 52: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

3.13 Conflict Resolution (cont’d)Tie-Breakers• Specificity

1. id selectors2. Class and pseudo-class selectors3. Contextual selectors4. General selectors

• Position

• Essentially, later has precedence over earlier

52

Page 53: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

HTML 4.01 & CSS 2.1 vs. HTML 5 & CSS 3

•HTML 4.01 & CSS 2.1 (lots of the <div> tags)

• header • section• article• nav• footer

•HTML 5 & CSS 3 ( no the <div> tag)

• header • section• article• nav• footer

53

section

header

nav

footer

HTML 5

article

Page 54: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

HTML 4.01 & CSS 2.1

54

(1024px * auto)

#nav(174px * auto)

#section(800px * auto)

#footer(800px * auto)

#header (800px * 50px)

#article(800px * auto)

Page 55: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

template.html, mystyle.css

55FireFox 4

Page 56: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

HTML 5 & CSS 3

56

(1024px * auto)

nav(174px * auto)

section(800px * auto)

footer(800px * auto)

header (800px * 50px)

article(800px * auto)

Page 57: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

html5.html, html5.css

57FireFox 4

Page 58: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

Free HTML5 Templates (include CSS3)

•http://freehtml5templates.com/template-portfolio/

Page 59: Cascading Style Sheets (CSS) ICS213, 1 / 2011 Dr. Seung Hwan Kang

Reference•C

ascading Style Sheets, level 2 CSS2 Specification (1998) World Wide Web Consortium (W3C), http://www.w3.org/TR/2008/REC-CSS2-20080411/ Accessed: 26/01/2010.

•HTML 4.01 Specification (1999) World Wide Web Consortium (W3C), http://www.w3.org/TR/1999/REC-html401-19991224/ Accessed: 26/01/2010.

•HTML 5 (2010) World Wide Web Consortium (W3C), http://www.w3.org/TR/2010/WD-html5-20100304/ Accessed: 13/05/2010.

•Introduction to CSS3 (2001) World Wide Web Consortium (W3C), http://www.w3.org/TR/2001/WD-css3-roadmap-20010523/ Accessed: 15/05/2010.

•Robert W. Sebesta (2008) Programming the World Wide Web, 4th edn, Pearson/Addison Wesley. (Chapter 3)

59