cmpt241 web programming

48
CMPT241 Web Programming Intro to CSS

Upload: nhu

Post on 23-Feb-2016

30 views

Category:

Documents


1 download

DESCRIPTION

CMPT241 Web Programming. Intro to CSS. T ags such as b, i, u, and font are discouraged in HTML Why is this bad ?. The good, the bad and the… ugly!. Shashdot . News for < b> nerds!! You will never , EVER be - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CMPT241 Web Programming

CMPT241 Web ProgrammingIntro to CSS

Page 2: CMPT241 Web Programming

2

The good, the bad and the… ugly!

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

• 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 3: CMPT241 Web Programming

3

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 4: CMPT241 Web Programming

4

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 5: CMPT241 Web Programming

5

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 6: CMPT241 Web Programming

6

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 7: CMPT241 Web Programming

7

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 8: CMPT241 Web Programming

8

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 9: CMPT241 Web Programming

9

Specifying colorsp { 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

• • RGB codes: red, green, and blue values from 0 (none) to 255 (full)• RGBA(alpha) codes: rgba(255, 165, 0, 0.5)• hex codes: RGB values in base-16 from 00 (0, none) to FF (255,

full)• hue-saturation-luminance codes: hsl(128, 128, 64)

Page 10: CMPT241 Web Programming

10

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

This paragraph uses the above style.

output

This h2 uses the above styles.

• A style can select multiple elements separated by commas

• The individual elements can also have their own styles

Page 11: CMPT241 Web Programming

11

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

• 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

Page 12: CMPT241 Web Programming

12

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/cssref/default.asp)

Page 13: CMPT241 Web Programming

13

font-familyp {font-family: Georgia;}h2 {font-family: "Courier New";} CSS

This paragraph uses the first style above.

This h2 uses the second style above.

output• Enclose multi-word font names in quotes

Page 14: CMPT241 Web Programming

14

More about font-familyp {font-family: Garamond, "Times New Roman", serif;} CSS

This paragraph uses the above style.

output• 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

Page 15: CMPT241 Web Programming

15

font-sizep {

font-size: 24pt;} CSS

This paragraph uses the style above.

output• 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%

Page 16: CMPT241 Web Programming

16

font-sizep {

font-size: 24pt;} CSS

This paragraph uses the style above.

output• 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

Page 17: CMPT241 Web Programming

17

font-weight, font-stylep {font-weight: bold;font-style: italic;} CSS

This paragraph uses the style above.

output

• Either of the above can be set to normal to turn them off (e.g. headings)

Page 18: CMPT241 Web Programming

Embedding Fonts (new in HTML5)@font-face{

font-family: “Kimberley”;src:url(

http://www.princexml.com/fonts/ kimberle.ttf) format(“TrueType”);}

Page 19: CMPT241 Web Programming

19

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 20: CMPT241 Web Programming

20

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_text.asp)

Page 21: CMPT241 Web Programming

21

text-alignblockquote { 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• text-align can be left, right, center, or justify

Page 22: CMPT241 Web Programming

22

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

output• can also be overline, line-through, or none• effects can be combined:text-decoration: overline underline;• none is useful for removing a decoration:

e.g., remove the underline from links

Page 23: CMPT241 Web Programming

line-height, word-spacing, letter-spacing letter-spacing: 0.25em;line-height: 1.5em;word-spacing: 3em;

CSS

http://www.w3schools.com/cssref/default.asp#textdecor

Page 24: CMPT241 Web Programming

24

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 (scroll, fixed)

Page 25: CMPT241 Web Programming

25

background-image body {background-image: url("images/draft.jpg");} CSS

• background image/color fills the element's content area

Page 26: CMPT241 Web Programming

26

background-repeat body {background-image: url("images/draft.jpg");background-repeat: repeat-x;} CSS

• can be repeat (default), repeat-x, repeat-y, or no-repeat

Page 27: CMPT241 Web Programming

27

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

• 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.

• background-size: a size (px, pt, %, em): new in HTML 5

Page 28: CMPT241 Web Programming

background-attachment •background-attachment: scroll; /*

default*/•background-attachment: fixed;

Page 29: CMPT241 Web Programming

29

The list-style-type propertyol { list-style-type: lower-roman; }

CSS• 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…

Page 30: CMPT241 Web Programming

Styling tables

•all standard CSS styles can be applied to a table, row, or cell

table {border: 2px solid black; caption-side: bottom; }tr {font-style: italic; }td {background-color: yellow; text-align: center;}

CSS

Page 31: CMPT241 Web Programming

The border-collapse property

• by default, the overall table has a separate border from each cell inside

• the border-collapse property merges these borders into one

table, td, th { border: 2px solid black; }table { border-collapse: collapse; }

CSS

Page 32: CMPT241 Web Programming

Column styles: <col>, <colgroup>

• col tag can be used to define styles that apply to an entire column (self-closing)

• colgroup tag applies a style to a group of columns (NOT self-closing)

<table> <col class="urgent" /> <colgroup class="highlight" span="2"></colgroup> <tr><th>Column 1</th><th>Column 2</th><th>Column 3</th></tr> <tr><td>1,1</td><td>1,2</td><td>1,3</td></tr> <tr><td>2,1</td><td>2,2</td><td>2,3</td></tr></table>

HTML

Page 33: CMPT241 Web Programming

33

HTML id attribute

Coding Horror! Coding Horror!

Our mission is to combine programming and “human” factors with geekiness! output• A unique ID for an element on a page• Each ID must be unique; can only be used once in the page• id must begin with a letter followed by letters, digits,hyphens, underscores, colons and periods.

<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 34: CMPT241 Web Programming

34

CSS ID selectors#mission {

font-style: italic;font-family: "Garamond", "Century Gothic", serif;

} CSS

• Applies style only to the paragraph that has the ID of mission

Coding Horror! Coding Horror!

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

output

Page 35: CMPT241 Web Programming

35

HTML class attribute

Coding Horror! Coding Horror!

See our special deal on Droids!

Today only! output

• 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 page

<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 36: CMPT241 Web Programming

36

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 37: CMPT241 Web Programming

37

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 38: CMPT241 Web Programming

38

Linking to sections of a web page

View our Mission Statement output

• Link target can include an ID at the end, preceded by a #

• Browser will load that page and scroll to element with given ID

<p><a href="#mission">View our Mission Statement</a></p> HTML

Page 39: CMPT241 Web Programming

39

CSS Pseudo-class Selectors

class 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

• Special kind of selector that targets an element only under certain conditions

Page 40: CMPT241 Web Programming

40

CSS ID selectorsa:hover{

background-color: cyan;font-style: normal;

} /* mouse over link */a:link { color: #FF0000; } /* unvisited link */a:visited { color: #00FF00; } /* visited link */

CSSBuy Early Buy Often!

output

Page 41: CMPT241 Web Programming

41

CSS Pseudo-class Selectorsclass description

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

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

:first-child:nth-child (param) an element that is the first

one to appear inside another li:nth-child(odd){

background-color: grey;} CSS

Page 42: CMPT241 Web Programming

42

Body stylesbody {

font-size: 16px;}

CSS

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

each element

Page 43: CMPT241 Web Programming

43

Inheriting stylesbody { 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

• when multiple styles apply to an element, they are inherited

• The more specific or closely matching selector wins.

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

Page 44: CMPT241 Web Programming

44

Styles that conflictp, h1, h2 { color: blue; font-style: italic; }h2 { color: red; background-color: yellow; }

CSSThis paragraph uses the first style above.

output

• when two styles set conflicting values for the same property, the latter style takes precedence

This heading uses both styles above.

Page 45: CMPT241 Web Programming

45

W3C CSS Validator<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

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

official CSS specifications

Page 46: CMPT241 Web Programming

Project 1•Due February 26 Friday

▫Working links on the turing server▫Source code uploaded to Moodle

index.html, pie.html, recipe.css, index.css Do not upload any other supporting files

•pie.html must match the appearance specified

•Text file provided•Pictures provided •HTML and CSS validation (important!)

Page 47: CMPT241 Web Programming

Project 1•Do not overuse <br/>•Do not overuse class and id•Avoid redundant styles•Do not express style information in HTML•Style is important!•At least 2 out of 4 extra features required

Page 48: CMPT241 Web Programming

Homework 3•Summary of what you have learned

▫Paragraph▫List

•Can I build my new homework based on the previous one?▫Yes, but there should be major changes

either in the content or in the style.