Top Banner
CSS: Cascading CSS: Cascading Style Sheets Style Sheets
54

CSS: Cascading Style Sheets

Jan 11, 2016

Download

Documents

john

CSS: Cascading Style Sheets. What 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. Style Sheets. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CSS: Cascading Style Sheets

CSS: Cascading Style CSS: Cascading Style SheetsSheets

Page 2: CSS: Cascading Style Sheets

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

Style SheetsStyle Sheets

• Usually, a file that ends with .css

• For example:

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

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

• To attach a style sheet to an HTML file, add <link rel="stylesheet" type="text/css" href="css-file"/>

to the head

Page 4: CSS: Cascading Style Sheets

Without a style sheet

Page 5: CSS: Cascading Style Sheets

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
Page 7: CSS: Cascading Style Sheets

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

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
Page 10: CSS: Cascading Style Sheets

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

• In XHTML (or HTML 4.01), styling is very

limited, hence style sheets are necessary

- Transitional versions are richer in style

• Separates content from style

• Reduces download time (how?)

• Allows to easily maintain a consistent

appearance over a whole Web site (how?)

Page 11: CSS: Cascading Style Sheets

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 !important;

letter-spacing:normal;

text-transform: none; }

Declaration

Property

Value(s)

Selector

“important”rule

Page 12: CSS: Cascading Style Sheets

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 13: CSS: Cascading Style Sheets

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

- Look in the Web !!!

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

Page 14: CSS: Cascading Style Sheets

Style PropertiesStyle Properties

Page 15: CSS: Cascading Style Sheets

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 16: CSS: Cascading Style Sheets

Font PropertiesFont Properties

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

span {

font-family: courier;

font-size: 130%;

font-style: italic;

font-weight: bold}

Page 17: CSS: Cascading Style Sheets

Text PropertiesText Properties

• Text properties: color, transform, decoration, …

span {

color: #00cc00;

text-decoration: line-through;

text-transform: uppercase}

Page 18: CSS: Cascading Style Sheets

Background PropertiesBackground Properties

• Background properties: background-color,

background-image, …

span {background-color: #00ff00}

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

Page 19: CSS: Cascading Style Sheets

Layout PropertiesLayout Properties

Page 20: CSS: Cascading Style Sheets

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 21: CSS: Cascading Style Sheets

Inline vs. Block ElementsInline vs. Block Elements

• There are two type 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 22: CSS: Cascading Style Sheets

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 23: CSS: Cascading Style Sheets

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 24: CSS: Cascading Style Sheets

Position ExamplesPosition Examples

span {

position:absolute;

left: 1cm;

top: 1cm;

color: #00cc00;} span {

position:fixed;

left: 1cm;

top: 1cm;

color: #00cc00;}

Page 25: CSS: Cascading Style Sheets

Position ExamplesPosition Examples

span {

position:static;

left: 1cm;

top: 1cm;

color: #00cc00;}

Totally Ignored! This is the

default position type

Page 26: CSS: Cascading Style Sheets

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, …

Page 27: CSS: Cascading Style Sheets

Length UnitsLength Units

• CSS has several types of length units:

- em, ex: height of current fonts

- px, in, cm, mm, pt, pc: international units

- %: ratio of parent’s respective dimension

• A page should remain a proper layout when

windows (and even fonts) resize

- Hence, do not assume anything about default sizes

Page 28: CSS: Cascading Style Sheets

Selector TypesSelector Types

Page 29: CSS: Cascading Style Sheets

Several Kinds of SelectorsSeveral Kinds of Selectors

• Type Selectors

• Class Selectors

• ID Selectors

• Attribute Selectors

• Universal Selector

• Child Selectors

• Adjacent-Sibling Selectors

• Descendant Selectors

• Pseudo-Class Selectors

• Pseudo-Element Selectors

Not supported by IE 5, 5.5 and 6

Page 30: CSS: Cascading Style Sheets

Type SelectorType Selector

• A type selector is the name of an element type

• A type selector matches every instance of the

element typeLI {color: red; font-size: 16px}

Matches:<ol>

<li> An item </li>

<li class="reditem"> Another item </li>

</ol>

Page 31: CSS: Cascading Style Sheets

Class SelectorClass Selector

• A class selector is a selector of the form x.y

• It matches xs that have the class attribute with

value yLI.reditem {color: red}

Matches:<ol>

<li> An item </li>

<li class="reditem"> Another item </li>

</ol>

.reditem { color: red} will also work!

Page 32: CSS: Cascading Style Sheets

ID SelectorsID Selectors

• IDs are identical to classes, except that there can only be

one element with a given ID in a document

LI#23 {color: red}

Matches:<ol>

<li> An item </li>

<li id="23"> Another item </li>

</ol>

#23 { color: red} will also work!

Page 33: CSS: Cascading Style Sheets

Attribute SelectorsAttribute Selectors

• p[attribute]

- matches p when attribute is set to any value

• p[title=intro] or p[title="intro"] (the quotes are optional)

- matches p when its title attribute is set to “intro”

• p[class~=green]

- matches p when the class attribute value includes the

word “green”

Page 34: CSS: Cascading Style Sheets

Universal SelectorUniversal Selector

• The universal selector matches every element

• The following rule means that all the text will have a

size of 40px

* {font-size: 40px }

Page 35: CSS: Cascading Style Sheets

Descendant SelectorDescendant Selector

• A descendant selector has the form S1 S2 where

S1 and S2 are selectors

• It matches all elements that

- match S2, and

- are descendants (nested in) elements that match S1

Page 36: CSS: Cascading Style Sheets

An ExampleAn Example

.href em {color: blue}

Matches: This is <em>not blue</em>.

<p>

This is <em> blue </em>

<span><i>and so is <em> this </em></i></span>.

</p>

What will this match?

p em {color: blue}

Page 37: CSS: Cascading Style Sheets

Pseudo-ClassesPseudo-Classes

• Pseudo class selectors are similar to class

selectors, but they match states rather than class

values

- For example, a link can be in the states: visited,

active, mouse-over (“hover”), etc.

- Another example: a text-area can be focused

Page 38: CSS: Cascading Style Sheets

Examples of Rules for Examples of Rules for Pseudo-ClassesPseudo-Classes

• A:link {color: blue}

• A:visited {color: purple}

• A:hover {font-size: 1.5em}

• A:active {color: red}

• INPUT:focus {background-color: yellow}

Page 39: CSS: Cascading Style Sheets

Grouping SelectorsGrouping Selectors

• We can group several declarations by specifying

several selectors, separated by commas

• For example, the following rule applies to all

elements that match either H1, P B, or

H2[class=largehead]

P B, H1, H2.largehead {font-size: 120%}

Page 40: CSS: Cascading Style Sheets

Inserting Style to a PageInserting Style to a Page

Page 41: CSS: Cascading Style Sheets

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 42: CSS: Cascading Style Sheets

Document-Level StyleDocument-Level Style

<html> <head>

<style type="text/css">

body {color: red; background: skyblue;}

h1 { color: blue }

</style>

</head>

<body>... </body>

</html>

Page 43: CSS: Cascading Style Sheets

Imported Style SheetsImported Style Sheets

• The @import rule imports the style rules of another

style sheet

• Usage: @import url(file.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 44: CSS: Cascading Style Sheets

Imported Style SheetsImported Style Sheets

Why do we need the import

command?

@import url(general.css);

body { color: red; background:skyblue }

h1 { color: blue }

An Example:

Page 45: CSS: Cascading Style Sheets

Inheritance and CascadingInheritance and Cascading

Page 46: CSS: Cascading Style Sheets

Inheritance of PropertiesInheritance of Properties

• Consider a property of an element that does not match

any rule

• For some properties (inherited properties), the

computed value of this property is inherited from the

parent of the element

• For example, color, font and word-spacing are

inherited

• Yet border, margin and padding are not!

Page 47: CSS: Cascading Style Sheets

An ExampleAn Example

• Given the rules:

- body { font-size: 10pt }

- h1 { font-size: 120% }

• What will be the font size of the <em> element?

<body>

<h1>A <em>large</em> heading</h1>

</body>

Computed Value: 12pt

Page 48: CSS: Cascading Style Sheets

Cascading of StylesCascading of Styles

• CSS merges style rules from different places

(inline, document-level, linked and defaults)

• Different places may have conflicting style rules

- conflicts may even arise in a single source

• The process of merging (cascading) styles from

different places determines which style rules

have higher priority

Page 49: CSS: Cascading Style Sheets

Determining Property ValuesDetermining Property Values

• Suppose that we would like to determine the value of

property p for element e

• Choose all declarations that have a selector that matches

e and have the property p

• Choose the first declaration in the cascading order, and

apply the corresponding rule

Page 50: CSS: Cascading Style Sheets

Cascading OrderCascading Order

The cascading order of declarations:

1. Primary sort: importance of origin

2. Secondary sort: specificity of selectors

3. Final sort: order of appearance

Page 51: CSS: Cascading Style Sheets

Importance of OriginImportance of Origin

• There are two rule origins: author and browser

(either defaults or user customizations)

browser !important rules

author !important rules

author rules

browser rules

Page 52: CSS: Cascading Style Sheets

Specificity of SelectorsSpecificity of Selectors

is rule in style attribute?

number of ID attributes

number of attributes and pseudo-classes

number of element names

Page 53: CSS: Cascading Style Sheets

An ExampleAn Example

• Consider the following rules:

- li {…}

- #x34y {…}

- ul ol li.red {…}

• Which is the most specific?

Page 54: CSS: Cascading Style Sheets

Order of AppearanceOrder of Appearance

• If two rules have the same origin importance and

specificity, then the one that appears last in the

style sheet overrides the others

• Rules in imported style sheets (@import) are

considered to appear before local rules