css

88
CSS Cascading Style Sheets

Upload: billie

Post on 23-Feb-2016

39 views

Category:

Documents


0 download

DESCRIPTION

CSS. Cascading Style Sheets. Resources. www.w3schools.com/css/css_reference.asp ( list of all CSS properties) www.w3schools.com/css / www.glish.com/css / www.html.net/tutorials/css / blog.html.it/ layoutgala / Great Book “CSS: The Missing Manual” - by David Sawyer McFarland. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CSS

CSS

Cascading Style Sheets

Page 2: CSS

Resources• www.w3schools.com/css/css_reference.asp

(list of all CSS properties)• www.w3schools.com/css/• www.glish.com/css/• www.html.net/tutorials/css/• blog.html.it/layoutgala/

• Great Book“CSS: The Missing Manual” - by David Sawyer McFarland

Page 3: CSS

Style and CSS Basics• Goal is to separate Style from Structure• Style rules express the style characteristics of an element• A set of style rules in an external document is a style sheet• Example of Style Section (sets <p> elements in the document to 24 point

blue)<head><style>

P {color: blue; font-size: 24pt;}</style>…

3

Page 4: CSS

Style and CSS Basics• A style rule is composed of two parts

– Selector– Declaration

h1 {color: red;}

4

DeclarationSelector

Page 5: CSS

Style and CSS Basics• A declaration is composed of two parts

– Property– Value – Syntax is Property and colon, then value and semicolon to allow

multiple declaration

h1 {color: red;}

5

ValueProperty

Page 6: CSS

Style and CSS Basics• Style rules can be applied to an element• Style rules can be a section in a document• Style rules can be in a document external to the Web pages to

which it can be applied

6

Page 7: CSS

Style and CSS Basics• Style rules can be applied to an element similar to an element

attribute

– Example

<div style="font-family: 'Trebuchet MS'; Color: Navy; ">

7

Page 8: CSS

Style and CSS Basics• Example of a Style Section to be used for a Web page that sets

h1 and h2 to a particular font and color of navy—Type Selector<style>

h1 , h2{ font-family: 'Trebuchet MS'; Color: Navy; }

</style>

8

Page 9: CSS

Style and CSS Basics• Example of an external style sheet—referred to as a cascading

style sheet—note that the content is the same as a Style Section but in a file with a .css extension<style>

h1 , h2{ font-family: 'Trebuchet MS'; example.css Color: Navy; }

</style>

9

Page 10: CSS

Style and CSS Basics• Referencing an external cascading style sheet

<head><title….<link href="example.css" rel="stylesheet"

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

10

Page 11: CSS

Style and CSS Basics• Grouping Selectors

h1 {color: red;}h2 {color: red;}

can be grouped with elements separated by commas

h1, h2 {color: red;}

11

Page 12: CSS

Style and CSS Basics• Combining Declarations

p {color: blue;}p {font-size: 12pt;}

can be expressed as follows

p {color: blue; font-size: 12pt;}

12

Page 13: CSS

Style and CSS Basics• Using Descendant Selectors

– Selects only <b> elements that are within <p> elements

p b {color: blue;}

can be more than 2 selector elements

ul li b {color: blue;}

13

Page 14: CSS

Style and CSS Basics• Using the Class Attribute Selector

.quote {color: blue; margin: 30px;}Reference the class

<p class="quote"> This….

Note that this allows any element to use the style whereas a general style for an element applies to every instance of the element

14

Class NameFlag

Character Declaration

Page 15: CSS

Style and CSS Basics• Making Class Selectors More Specific

h1.quote {color: blue; margin: 30px;}

restricts the use of the quote to the <h1> element

15

Page 16: CSS

Style and CSS Basics• Using the id Attribute Selector

#preface {color: blue; margin: 30px;}reference the element

<p id="preface"> This….

Note that the id value uniquely identifies this one <p> element to which the rule applies

16

Class NameFlag

Character Declaration

Page 17: CSS

Style and CSS Basics

• CSS font measurement units

17

Absolute Units Unit Abbrev Description

Centimeter cm Standard metric cm

Inch in Standard US inch

Millimeter mm Standard metric mm

Pica pc Equal to 12 points

Point Pt 72 points / inch

Page 18: CSS

Style and CSS Basics• CSS font measurement units (cont)

18

Relative Units Unit Abbrev

Description

Em em The width of a capital M in the current font—usually the same as the font size

Ex ex The height of the letter x in the current font

Pixel px The size of a pixel on the current monitor

Percentage 150% Sets a font size relative to the base font size

Page 19: CSS

Style and CSS Basics• Absolute Font Size Keywords

– xx-small– x-small– small– medium– large– x-large– xx-large

• Relative Font Size Keywords– smaller– larger

19

Page 20: CSS

Style and CSS Basics• Fonts

– Family {font-family: san-serif;}• arial, courier …..

– Shortcut property designation• p {font: 12pt arial bold;}

– Text Spacing Properties• text-align• text-indent• …

– Text –decoration properties• none, underline, overline, line-through, blink

20

Page 21: CSS

Style and CSS Basics• Fonts

– Vertical alignment {vertical-align: super;}• baseline, sub, super, top, text-top, middle, bottom, text-bottom, percentage

– Transforming case {text-transformation: uppercase}• capitalize, uppercase, lowercase, none

21

Page 22: CSS

Style and CSS Basics

• Visual Formatting Model– Block elements

• <body> ,<h1>, <div> <p>…• Allows specification of margin, border, and padding for all block-level elements

– Inline elements– List-item

22

Page 23: CSS

Style and CSS Basics

23

Page 24: CSS

Style and CSS Basics• Box specification example

p {margin-left; 2 em;margin-right; 2 em;padding-left; 2 em;padding-right; 2 em;border-right; solid thin black;background: white}

24

Page 25: CSS

Style and CSS Basics• Special Box Properties

– width– height– float– clear

div {float: left; width: 200px;}

25

Page 26: CSS

CS380 26

CSS for Styling

Page 27: CSS

CS380 27

The good, the bad and the… ugly!

• Tags such as b, i, u, and font are discouraged in strict XHTML

• Why is this bad?

<p><font face="Arial">Shashdot.</font>News for <b>nerds!!</b> You will <i>never</i>, <u>EVER</u> be<font size="+4" color="red">BORED</font> here!</p> HTML

Slashdot. News for nerds!! You will never, EVER be BORED here! output

Page 28: CSS

CS380 28

Cascading Style Sheets (CSS)

• Describes the appearance, layout, and presentation of information on a web page– HTML describes the content of the page

• Describes how information is to be displayed, not what is being displayed

• Can be embedded in HTML document or placed into separate .css file

Page 29: CSS

29

Basic CSS rule syntax• A CSS file consists of one or more rules• Each rule starts with a selector • A selector specifies an HTML element(s) and then applies

style properties to them– a selector of * selects all elements

selector {property: value;property: value;...property: value;} CSSp {font-family: sans-serif;color: red;} CSS

Page 30: CSS

CS380 30

Attaching a CSS file <link>• A page can link to multiple style sheet files

– In case of a conflict (two sheets define a style for the same HTML element), the latter sheet's properties will be used

<head>...<link href="filename" type="text/css" rel="stylesheet" />...</head> HTML

<link href="style.css" type="text/css" rel="stylesheet" /><link href="http://www.google.com/uds/css/gsearch.css"rel="stylesheet" type="text/css" />

HTML

Page 31: CSS

CS380 31

Embedding style sheets: <style>• CSS code can be embedded within the head of an HTML page• Bad style and should be avoided when possible (why?)<head><style type="text/css">p { font-family: sans-serif; color: red; }h2 { background-color: yellow; }</style></head>

HTML

Page 32: CSS

CS380 32

Inline styles: the style attribute• Higher precedence than embedded or linked styles• Used for one-time overrides and styling a particular element• Bad style and should be avoided when possible (why?)

<p style="font-family: sans-serif; color: red;">This is a paragraph</p>

HTMLThis is a paragraph

output

Page 33: CSS

CS380 33

CSS properties for colorsp {color: red;background-color: yellow;}

CSSThis paragraph uses the style above output

property description color color of the element's text

background-color color that will appear behind the element

Page 34: CSS

34

Specifying colors• color names: aqua, black, blue, fuchsia, gray, green, lime, maroon,

navy, olive, purple, red, silver, teal, white (white), yellow• RGB codes: red, green, and blue values from 0 (none) to 255 (full)• hex codes: RGB values in base-16 from 00 (0, none) to FF (255, full)

p { color: red; }h2 { color: rgb(128, 0, 196); }h4 { color: #FF8800; }

CSS

This paragraph uses the first style above This h2 uses the second style above.

This h4 uses the third style above. output

Page 35: CSS

CS380 35

Grouping styles• A style can select multiple elements separated by commas• The individual elements can also have their own styles

p, h1, h2 {color: green;}h2 {background-color: yellow;} CSS

This paragraph uses the above style.

output

This h2 uses the above styles.

Page 36: CSS

CS380 36

CSS comments /*…*/• CSS (like HTML) is usually not commented as rigorously as

programming languages such as Java• The // single-line comment style is NOT supported in CSS• The <!-- ... --> HTML comment style is also NOT supported in

CSS

/* This is a comment.It can span many lines in the CSS file. */p {color: red; background-color: aqua;} CSS

Page 37: CSS

CS380 37

CSS properties for fontsproperty description font-family which font will be used font-size how large the letters will be drawn font-style used to enable/disable italic style font-weight used to enable/disable bold style

Complete list of font properties (http://www.w3schools.com/css/css_reference.asp#font)

Page 38: CSS

CS380 38

font-family• Enclose multi-word font names in quotesp {font-family: Georgia;}h2 {font-family: "Courier New";} CSS

This paragraph uses the first style above.

This h2 uses the second style above.

output

Page 39: CSS

CS380 39

More about font-family• We can specify multiple fonts from highest to lowest priority• Generic font names:

– serif, sans-serif, cursive, fantasy, monospace• If the first font is not found on the user's computer, the

next is tried• Placing a generic font name at the end of your font-family

value, ensures that every computer will use a valid font

p {font-family: Garamond, "Times New Roman", serif;} CSS

This paragraph uses the above style.

output

Page 40: CSS

CS380 40

font-size• units: pixels (px) vs. point (pt) vs. m-size (em)16px, 16pt, 1.16em• vague font sizes: xx-small, x-small, small, medium, large, x-large, xx-large, smaller, larger

• percentage font sizes, e.g.: 90%, 120%

p {font-size: 24pt;

} CSS

This paragraph uses the style above.

output

Page 41: CSS

CS380 41

font-size• pt specifies number of point, where a point is

1/72 of an inch onscreen• px specifies a number of pixels on the screen• em specifies number of m-widths, where 1 em is

equal to the font's current size

p {font-size: 24pt;

} CSS

This paragraph uses the style above.

output

Page 42: CSS

CS380 42

font-weight, font-style• Either of the above can be set to normal to turn them off (e.g.

headings)p {font-weight: bold;font-style: italic;} CSS

This paragraph uses the style above.

output

Page 43: CSS

CS380 43

CSS properties for textproperty description text-align alignment of text within its element text-decoration decorations such as underlining line-height, word-spacing, letter-spacing

gaps between the various portions of the text

text-indent indents the first letter of each paragraph

Complete list of text properties (http://www.w3schools.com/css/css_reference.asp#text)

Page 44: CSS

CS380 44

text-align• text-align can be left, right, center, or justify

blockquote { text-align: justify; }h2 { text-align: center; }

CSS

The Gollum’s Quote

We wants it, we needs it. Must have the precious. They stole it from us. Sneaky little hobbitses. Wicked, tricksy, false!

output

Page 45: CSS

CS380 45

text-decoration• can also be overline, line-through, blink, or none

• effects can be combined:text-decoration: overline underline;

p {text-decoration: underline;} CSSThis paragraph uses the style above.

output

Page 46: CSS

CS380 46

The list-style-type property• Possible values:

i. none : No markerii. disc (default), circle, squareiii. Decimal: 1, 2, 3, etc.iv. decimal-leading-zero: 01, 02, 03, etc.v. lower-roman: i, ii, iii, iv, v, etc.vi. upper-roman: I, II, III, IV, V, etc.vii. lower-alpha: a, b, c, d, e, etc.viii. upper-alpha: A, B, C, D, E, etc.x. lower-greek: alpha, beta, gamma, etc.others: hebrew, armenian, georgian, cjk-ideographic, hiragana…

ol { list-style-type: lower-roman; }

CSS

Page 47: CSS

CS380 47

Body styles• Applies a style to the entire body of your page• Saves you from manually applying a style to each element

body {font-size: 16px;}

CSS

Page 48: CSS

CS380 48

Cascading Style Sheets

• Properties of an element cascade together in this order:– browser's default styles– external style sheet files (in a <link> tag)– internal style sheets (inside a <style> tag in the

page's header)– inline style (the style attribute of the HTML

element)

Page 49: CSS

CS380 49

Inheriting styles• when multiple styles apply to an element, they are inherited• a more tightly matching rule can override a more general

inherited rule

body { font-family: sans-serif; background-color: yellow; }p { color: red; background-color: aqua; }a { text-decoration: underline; }h2 { font-weight: bold; text-align: center; }

CSS

This is a heading

• A bulleted list output

A styled paragraph. Previous slides are available on the website.

Page 50: CSS

CS380 50

Styles that conflict• when two styles set conflicting values for the same property,

the latter style takes precedencep, h1, h2 { color: blue; font-style: italic; }h2 { color: red; background-color: yellow; }

CSSThis paragraph uses the first style above.

output

This heading uses both styles above.

Page 51: CSS

CS380 51

W3C CSS Validator• jigsaw.w3.org/css-validator/• checks your CSS to make sure it meets the official CSS

specifications

<p><a href="http://jigsaw.w3.org/css-validator/check/referer"><img src="http://jigsaw.w3.org/css-validator/images/vcss"alt="Valid CSS!" /></a></p> CSS

output

Page 52: CSS

CS380 52

CSS properties for backgroundsproperty description background-color color to fill background background-image image to place in background

background-position placement of bg image within element

background-repeat whether/how bg image should be repeated

background-attachment whether bg image scrolls with page

background shorthand to set all background properties

Page 53: CSS

CS380 53

background-image • background image/color fills the element's content areabody {background-image: url("images/draft.jpg");} CSS

Page 54: CSS

CS380 54

background-repeat • can be repeat (default), repeat-x, repeat-y, or no-repeatbody {background-image: url("images/draft.jpg");background-repeat: repeat-x;} CSS

Page 55: CSS

CS380 55

background-position • value consists of two tokens, each of which can be top, left,

right, bottom, center, a percentage, or a length value in px, pt, etc.

• value can be negative to shift left/up by a given amount

body {background-image: url("images/draft.jpg");background-repeat: no-repeat;background-position: 370px 20px;} CSS

Page 56: CSS

CS380 56

Aside: Favorites icon ("favicon")• The link tag, placed in the HTML page's head section, can

specify an icon– this icon will be placed in the browser title bar and bookmark/favorite

<link href="filename" type="MIME type" rel="shortcut icon" /> HTML

<link href="yahoo.gif" type="image/gif" rel="shortcut icon" />

HTML

Page 57: CSS

CS380 57

More CSS

• <link href="filename" type="text/css" rel="stylesheet" />

Page 58: CSS

CS380 58

HTML id attribute• A unique ID for an element on a page• Each ID must be unique; can only be used once in the page

Coding Horror! Coding Horror!

Our mission is to combine programming and “human” factors with geekiness! output

<p>Coding Horror! Coding Horror!</p><p id="mission">Our mission is to combine programming and <q>human</q> factors with geekiness!</p>

HTML

Page 59: CSS

CS380 59

Linking to sections of a web page• Link target can include an ID at the end, preceded by a #• Browser will load that page and scroll to element with given

ID

Visit textpad.com to get the TextPad editor.

View our Mission Statement output

<p>Visit <a href="http://www.textpad.com/download/index.html#downloads">textpad.com</a> to get the TextPad editor.</p><p><a href="#mission">View our Mission Statement</a></p>

HTML

Page 60: CSS

CS380 60

CSS ID selectors• Applies style only to the paragraph that has the ID of mission#mission {font-style: italic;font-family: "Garamond", "Century Gothic", serif;} CSSCoding Horror! Coding Horror!

Our mission is to combine programming and “human” factors with geekiness!

output

Page 61: CSS

CS380 61

HTML class attribute• A way to group some elements and give a style to only that

group• Unlike an id, a class can be reused as much as you like on the pageCoding Horror! Coding Horror!

See our special deal on Droids!

Today only! output

<p class="shout">Coding Horror! Coding Horror!</p><p class="special">See our special deal on Droids!</p>

<p class="special">Today only!</p> HTML

Page 62: CSS

CS380 62

CSS class selectors

Coding Horror! Coding Horror!

output

.special {background-color: yellow;font-weight: bold;}p.shout {color: red;font-family: cursive;} CSS

See our special deal on Droids!

Today only!

Page 63: CSS

CS380 63

CSS class selectors

Coding Horror! Coding Horror!

output

See our special deal on Droids!

Today only!

<p class="shout">Coding Horror! Coding Horror!</p><p class="special">See our special deal on Droids!</p>

<p class="special shout">Today only!</p> HTML

Page 64: CSS

CS380 64

CSS ID selectorsa:link { color: #FF0000; } /* unvisited link */a:visited { color: #00FF00; } /* visited link */a:hover { color: #FF00FF; } /* mouse over link */

CSS

Buy Early Buy Often!

output

Page 65: CSS

CS380 65

CSS ID selectorsclass description :active an activated or selected element

:focus an element that has the keyboard focus

:hover an element that has the mouse over it

:link a link that has not been visited :visited a link that has already been visited

:first-letter the first letter of text inside an element

:first-line the first line of text inside an element

:first-child an element that is the first one to appear inside another

Page 66: CSS

CS380 66

Styling Page Sections

Page 67: CSS

CS380 67

Why do we need page sections?

• Style individual elements, groups of elements, sections of text or of the page

• Create complex page layouts

Page 68: CSS

CS380 68

Sections of a page <div>• Tag used to indicate a logical section or area of a page• Has no appearance by default, but you can apply styles to it

Coding Horror! Coding Horror!

We’ll beat any advertised price! output

<div class="shout"><h2>Coding Horror! Coding Horror!</h2><p class="special">See our special deal on Droids!</p> <p>We'll beat any advertised price!</p></div> HTML

See our special deal on Droids!

Page 69: CSS

CS380 69

Inline Sections <span>• has no onscreen appearance, but you can apply a style or ID

to it, which will be applied to the text inside the span

Coding Horror! Coding Horror!

See our spectacular deal on Droids!

We’ll beat any advertised price! output

<h2>Coding Horror! Coding Horror!</h2><p>See our <span class="special“>spectacular</span> deal on Droids!</p> <p>We'll beat <span class="shout“> any advertised price</span>!</p> HTML

Page 70: CSS

CS380 70

CSS context selectors• applies the given properties to selector2 only if it is inside a

selector1 on the page

selector1 selector2 {properties} CSS

selector1 > selector2 {properties} CSS applies the given properties to selector2 only if it is directly

inside a selector1 on the page

Page 71: CSS

CS380 71

Context selector example

Eat at Greasy’s Burger…

• The greasiest burgers in town!• Yummy and greasy at the same time!

output

<p>Eat at <strong>Greasy's Burger</strong>...</p><ul><li>The <strong>greasiest</strong> burgers in town!</li><li>Yummy and greasy at the same time!</li></ul> HTMLli strong { text-decoration: underline; }

CSS

Page 72: CSS

CS380 72

More complex example

Eat at Greasy’s Burger…

• The greasiest burgers in town!• Yummy and greasy at the same time!

output

<div id="ad"><p>Eat at <strong>Greasy's Burger</strong>...</p><ul><li class="important">The <strong>greasiest</strong> burgers in town!</li><li>Yummy and <strong>greasy at the same time </strong>!</li></ul> </div> HTML#ad li.important strong { text-decoration: underline; }

CSS

Page 73: CSS

CS380 73

The CSS Box Model

• Every element composed of:– content– a border around the element– padding between the content and the border– a margin between the border and other content

Page 74: CSS

CS380 74

The CSS Box Model (cont.)

• width = content width + L/R padding + L/R border + L/R margin

• height = content height + T/B padding + T/B border + T/B margin

• IE6 doesn't do this right

Page 75: CSS

CS380 75

Document Flow – block elements

Page 76: CSS

CS380 76

Document flow - inline elements

Page 77: CSS

CS380 77

Document flow - a larger example

Page 78: CSS

78

CSS properties for borders

output

h2 { border: 5px solid red; }

CSS

This is a heading.

property description

border thickness/style/size of border on all 4 sides

Thickness: px, pt, em, or thin, medium, thick Style: none, hidden, dotted, dashed, double, groove, inset, outset, ridge, solid

color

Page 79: CSS

79

More border propertiesproperty description border-color, border-width, border-style

specific properties of border on all 4 sides

border-bottom, border-left, border-right, border-top

all properties of border on a particular side

border-bottom-color, border-bottom-style, border-bottom-width, border-left-color, border-left-style, border-left-width, border-right-color, border-right-style, border-right-width, border-top-color, border-top-style, border-top-width

properties of border on a particular side

Complete list of border properties http://www.w3schools.com/css/css_reference.asp#border

Page 80: CSS

CS380 80

Another border example

output

h2 {border-left: thick dotted #CC0088;border-bottom-color: rgb(0, 128, 128);border-bottom-style: double;}

CSS

This is a heading.

each side's border properties can be set individually if you omit some properties, they receive default

Page 81: CSS

CS380 81

CSS properties for paddingproperty description padding padding on all 4 sides padding-bottom padding on bottom side only padding-left padding on left side only padding-right padding on right side only padding-top padding on top side only Complete list of padding propertieshttp://www.w3schools.com/css/css_reference.asp#padding

Page 82: CSS

CS380 82

Padding example 1

This is a first paragraph.

This is a second paragraph.

output

p { padding: 20px; border: 3px solid black; }h2 { padding: 0px; background-color: yellow; }

CSS

This is a heading

Page 83: CSS

83

Padding example 2

This is a first paragraph.

output

p {padding-left: 200px; padding-top: 30px;background-color: fuchsia;} CSS

This is a first paragraph

This is a second paragraph

each side's padding can be set individually notice that padding shares the background color of the

element

Page 84: CSS

CS380 84

CSS properties for marginsproperty description margin margin on all 4 sides margin-bottom margin on bottom side only margin-left margin on left side only margin-right margin on right side only margin-top margin on top side only Complete list of margin propertieshttp://www.w3schools.com/css/css_reference.asp#margin

Page 85: CSS

CS380 85

Margin example 1

output

p {margin: 50px;background-color: fuchsia;} CSS

notice that margins are always transparent

This is a second paragraph

This is a first paragraph

Page 86: CSS

CS380 86

Margin example 2

output

p {margin-left: 8em;background-color: fuchsia;} CSS

each side's margin can be set individually

This is a second paragraph

This is a first paragraph

Page 87: CSS

87

CSS properties for dimensions

output

p { width: 350px; background-color: yellow; }h2 { width: 50%; background-color: aqua; }

CSS

An h2 heading

This paragraph uses the first style above

property description

width, height how wide or tall to make this element (block elements only)

max-width, max-height, min-width, min-height

max/min size of this element in given dimension

Page 88: CSS

CS380 88

Centering a block element: auto margins

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. output

p {margin-left: auto;margin-right: auto;width: 750px;} CSS

works best if width is set (otherwise, may occupy entire width of page)

to center inline elements within a block element, use text-align: center;