css: cascading style sheets. 2 what are style sheets a style sheet is a mechanism that allows to...

35
CSS: Cascading CSS: Cascading Style Sheets Style Sheets

Upload: jasmin-chapman

Post on 29-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSS: Cascading Style Sheets. 2 What are Style Sheets A style sheet is a mechanism that allows to specify how HTML (/XHTML/XML) pages should look The style

CSS: Cascading Style CSS: Cascading Style SheetsSheets

Page 2: CSS: Cascading Style Sheets. 2 What are Style Sheets A style sheet is a mechanism that allows to specify how HTML (/XHTML/XML) pages should look The style

2

What are Style SheetsWhat are Style Sheets

• A style sheet is a mechanism that allows to

specify how HTML (/XHTML/XML) pages

should look

• The style is specified by style rules

• The style rules appear either in the document or

in external files, called style sheets

Page 3: CSS: Cascading Style Sheets. 2 What are Style Sheets A style sheet is a mechanism that allows to specify how HTML (/XHTML/XML) pages should look The style

3

Style SheetsStyle Sheets

• Usually, a file that ends with .css

• Contains a list of style rules for HTML elements

• Case insensitive

• Comments are enclosed in /* */

• For example:

- i.a.cnn.net/cnn/.element/ssi/css/1.1/main.css (CNN)

- http://www.huji.ac.il/styles/style.css (HUJI)

• Demo: http://www.w3schools.com/css/css_intro.asp

Page 4: CSS: Cascading Style Sheets. 2 What are Style Sheets A style sheet is a mechanism that allows to specify how HTML (/XHTML/XML) pages should look The style

4

Without a style sheet

Page 5: CSS: Cascading Style Sheets. 2 What are Style Sheets A style sheet is a mechanism that allows to specify how HTML (/XHTML/XML) pages should look The style

5

Simple ExampleSimple Example

<html>

<head><title>A Joke</title></head>

<body>

<div><img src="tomato.gif" alt="joke"/></div>

<h1>A joke</h1>

<p>A mama tomato, a papa tomato and a baby tomato are walking

down the street. The baby tomato keeps falling behind so the papa

tomato goes back, steps on the baby tomato and says, ketchup

("Catch-up!"). </p>

</body>

</html>

Page 6: CSS: Cascading Style Sheets. 2 What are Style Sheets A style sheet is a mechanism that allows to specify how HTML (/XHTML/XML) pages should look The style

6

Page 7: CSS: Cascading Style Sheets. 2 What are Style Sheets A style sheet is a mechanism that allows to specify how HTML (/XHTML/XML) pages should look The style

7

Style File: Style File: joke.cssjoke.css

body {

background-image: url("bg.gif"); }

h1 {

background-color: green;

color: rgb(250, 200, 250); /* pink */

font-family: cursive }

p {

background-color: yellow;

color: purple;

font-size: 200%;}

Page 8: CSS: Cascading Style Sheets. 2 What are Style Sheets A style sheet is a mechanism that allows to specify how HTML (/XHTML/XML) pages should look The style

8

Simple Example (with css)Simple Example (with css)

<html>

<head><title>A Joke</title>

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

</head>

<body>

<div><img src="tomato.gif" alt="joke"></div>

<h1>A joke</h1>

<p>A mama tomato, a papa tomato and a baby tomato are walking

down the street. The baby tomato keeps falling behind so the papa

tomato goes back, steps on the baby tomato and says, ketchup

("Catch-up!"). </p> </body>

</html>

Page 9: CSS: Cascading Style Sheets. 2 What are Style Sheets A style sheet is a mechanism that allows to specify how HTML (/XHTML/XML) pages should look The style

9

Page 10: CSS: Cascading Style Sheets. 2 What are Style Sheets A style sheet is a mechanism that allows to specify how HTML (/XHTML/XML) pages should look The style

10

Why do we Need a Style Sheet?Why do we Need a Style Sheet?

• Separates content from format

• Reduces download time (how?)

• In HTML 4.01 styling is very limited

• Consistent appearance over a site

• Allows to easily change style

- In one page

- In a whole site

Page 11: CSS: Cascading Style Sheets. 2 What are Style Sheets A style sheet is a mechanism that allows to specify how HTML (/XHTML/XML) pages should look The style

Inserting Style to a PageInserting Style to a Page

Page 12: CSS: Cascading Style Sheets. 2 What are Style Sheets A style sheet is a mechanism that allows to specify how HTML (/XHTML/XML) pages should look The style

12

How and Where?How and Where?

• The style rules appear either in the document or

in external files, called style sheets

- Inside a document, there are inline styles and

embedded style sheets

- External style sheets are either linked or

imported

Page 13: CSS: Cascading Style Sheets. 2 What are Style Sheets A style sheet is a mechanism that allows to specify how HTML (/XHTML/XML) pages should look The style

13

Inline StylesInline Styles

• In an inline style, the declaration block appear as

the value of the attribute style of the element

• Almost every tag can have the style attribute

- exceptional: base, head, html, meta, param, script,

style and title

<p style="color: green; font-size: 1.5em; font-style: italic">

This text will be shown in italic green and the size will be 1.5 times the current font size

</p>

Page 14: CSS: Cascading Style Sheets. 2 What are Style Sheets A style sheet is a mechanism that allows to specify how HTML (/XHTML/XML) pages should look The style

14

Document-Level Style (Embedded)Document-Level Style (Embedded)

<html> <head>

<style type="text/css">

body {color: red; background: skyblue;}

h1 { color: blue }

</style>

</head>

<body>... </body>

</html>

Page 15: CSS: Cascading Style Sheets. 2 What are Style Sheets A style sheet is a mechanism that allows to specify how HTML (/XHTML/XML) pages should look The style

15

Link External Style SheetsLink External Style Sheets

<html> <head>

<link rel="stylesheet" type="text/css"

href="mystyle.css" />

</head>

<body>... </body>

</html>

Page 16: CSS: Cascading Style Sheets. 2 What are Style Sheets A style sheet is a mechanism that allows to specify how HTML (/XHTML/XML) pages should look The style

16

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

the style sheet

• The @import rule imports the style rules of another

style sheet

• Several import rules may appear

Imported Style SheetsImported Style Sheets

@import url(general.css);

body { color: red; background:skyblue }

h1 { color: blue }An Example:

Page 17: CSS: Cascading Style Sheets. 2 What are Style Sheets A style sheet is a mechanism that allows to specify how HTML (/XHTML/XML) pages should look The style

17

Cascading of StylesCascading of Styles

• CSS merges style rules from different places

(inline, document-level, imported and linked)

• Different places may have conflicting style rules

• The process of merging (“cascading”) styles

from different places determines which style

rules have priority

• Will be discussed later…

Page 18: CSS: Cascading Style Sheets. 2 What are Style Sheets A style sheet is a mechanism that allows to specify how HTML (/XHTML/XML) pages should look The style

Style SyntaxStyle Syntax

Page 19: CSS: Cascading Style Sheets. 2 What are Style Sheets A style sheet is a mechanism that allows to specify how HTML (/XHTML/XML) pages should look The style

19

Style RulesStyle Rulesh1 {

color: purple;

font-family: Impact, Arial Black;

font-size-adjust: .46;

font-size: 2.33em;

font-weight:400;

font-style:normal;

text-decoration: none;

word-spacing:normal

letter-spacing:normal;

text-transform: none; }

Declaration

Property

Value(s)

Selector

Page 20: CSS: Cascading Style Sheets. 2 What are Style Sheets A style sheet is a mechanism that allows to specify how HTML (/XHTML/XML) pages should look The style

20

Style Rules (cont)Style Rules (cont)

• A rule has the following form

selector {declaration block}

• The selector determines when the rule is applied

• For example, the following rule applies to text

that is inside a <p> tag

p {color: green; font-size: 1.5em; font-style: italic}

Page 21: CSS: Cascading Style Sheets. 2 What are Style Sheets A style sheet is a mechanism that allows to specify how HTML (/XHTML/XML) pages should look The style

21

What Kind of Properties can a CSS What Kind of Properties can a CSS Style Sheet Change?Style Sheet Change?

• Style properties

• Layout properties

• There are many properties and many possible

values

- We will not cover all of them here

- Everything is in the Web !

- A good source: http://www.w3schools.com/css

Page 22: CSS: Cascading Style Sheets. 2 What are Style Sheets A style sheet is a mechanism that allows to specify how HTML (/XHTML/XML) pages should look The style

Style PropertiesStyle Properties

Page 23: CSS: Cascading Style Sheets. 2 What are Style Sheets A style sheet is a mechanism that allows to specify how HTML (/XHTML/XML) pages should look The style

23

Our ExamplesOur Examples

• We use the following HTML example:

This is <span> our example </span> for css.

• The <span> tag is used to group elements for

formatting with styles

- Extremely useful tag...

Page 24: CSS: Cascading Style Sheets. 2 What are Style Sheets A style sheet is a mechanism that allows to specify how HTML (/XHTML/XML) pages should look The style

24

Font PropertiesFont Properties

• Font properties: family, size, weight, style, variant, ...

span {

font-family: courier;

font-size: 130%;

font-style: italic;

font-weight: bold}

Page 25: CSS: Cascading Style Sheets. 2 What are Style Sheets A style sheet is a mechanism that allows to specify how HTML (/XHTML/XML) pages should look The style

25

Text PropertiesText Properties

• Text properties: color, transform, decoration, …

span {

color: #00cc00;

text-decoration: line-through;

text-transform: uppercase}

Page 26: CSS: Cascading Style Sheets. 2 What are Style Sheets A style sheet is a mechanism that allows to specify how HTML (/XHTML/XML) pages should look The style

26

Background PropertiesBackground Properties

• Background properties: background-color,

background-image, …

span {background-color: #00ff00}

span {background-image: url('bg.gif');}

Page 27: CSS: Cascading Style Sheets. 2 What are Style Sheets A style sheet is a mechanism that allows to specify how HTML (/XHTML/XML) pages should look The style

Layout PropertiesLayout Properties

Page 28: CSS: Cascading Style Sheets. 2 What are Style Sheets A style sheet is a mechanism that allows to specify how HTML (/XHTML/XML) pages should look The style

28

Page LayoutPage Layout

• Each HTML element defines a layer (rectangular

box) that is placed in some location on the page

• Layers are nested with correspondence to the

nesting of their elements

Page 29: CSS: Cascading Style Sheets. 2 What are Style Sheets A style sheet is a mechanism that allows to specify how HTML (/XHTML/XML) pages should look The style

29

Inline vs. Block ElementsInline vs. Block Elements

• There are two types of elements:

• Block elements: p, ol, table, div, h1, etc.

• Inline elements: b, i, a, span, cite, etc.

• Layers of block elements are separated from their

adjacent elements (i.e., a new line before and after),

while inline elements are not

• You can turn a block into an inline and vice-versa, using

the display property, e.g., h1 { display: inline }

Page 30: CSS: Cascading Style Sheets. 2 What are Style Sheets A style sheet is a mechanism that allows to specify how HTML (/XHTML/XML) pages should look The style

30

Positioning ElementsPositioning Elements

• Using CSS, you can define the position of an element

inside its parent layer

• For that, use the properties position, left, right, top and

bottom

span {

position:relative;

left: 1cm;

top: 1cm;

color: #00cc00;}

Page 31: CSS: Cascading Style Sheets. 2 What are Style Sheets A style sheet is a mechanism that allows to specify how HTML (/XHTML/XML) pages should look The style

31

Relative vs. AbsoluteRelative vs. Absolute

• In CSS, positions (of boxes) and sizes (of fonts and boxes) could be either relative or absolute

• In an “absolute” style

- Font size is fixed

- Sizes and positions of elements are fixed

• In a “relative” style

- you can change the font size

- The sizes and positions of elements may change when the size of the window is changed

Page 32: CSS: Cascading Style Sheets. 2 What are Style Sheets A style sheet is a mechanism that allows to specify how HTML (/XHTML/XML) pages should look The style

32

Position TypesPosition Types

• But 1cm left to what??

• For that, we have the position property

• Four position types:

- static: the default position

- absolute: relative to the parent layer coordinates

- relative: relative to the static position

- fixed: relative to the window coordinates

Page 33: CSS: Cascading Style Sheets. 2 What are Style Sheets A style sheet is a mechanism that allows to specify how HTML (/XHTML/XML) pages should look The style

33

Position ExamplesPosition Examples

span {

position:absolute;

left: 1cm;

top: 1cm;

color: #00cc00;} span {

position:fixed;

left: 1cm;

top: 1cm;

color: #00cc00;}

Page 34: CSS: Cascading Style Sheets. 2 What are Style Sheets A style sheet is a mechanism that allows to specify how HTML (/XHTML/XML) pages should look The style

34

Position ExamplesPosition Examples

span {

position:static;

left: 1cm;

top: 1cm;

color: #00cc00;}

Totally Ignored! This is the

default position type

Page 35: CSS: Cascading Style Sheets. 2 What are Style Sheets A style sheet is a mechanism that allows to specify how HTML (/XHTML/XML) pages should look The style

35

More Layout PropertiesMore Layout Properties

• Layer properties

- margin-top (-bottom, -left, -right)

- padding-top (-bottom, -left, -right)

- border-width (-color, -style, … )

• Text Layout

- direction, word-spacing, white-space, letter-

spacing, line-height, text-align, text-indent, …