k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

44
Cascading Style Sheets Part three Marko Boon http://www.win.tue.nl/~marko/ cursusCSS
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    214
  • download

    0

Transcript of k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

Page 1: k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

Cascading Style

SheetsPart three

Marko Boonhttp://www.win.tue.nl/~marko/

cursusCSS

Page 2: k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

Positioning

• Margins• Padding• Dimensions• Positioning• Layers and Visibility• Pseudo-Classes• Connection to JavaScript

Part 3

Page 3: k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

Margins

Property Values

margin-leftmargin-rightmargin-topmargin-bottom

autolength%

Margins define the space around elements.

Page 4: k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

Margins - Example

body { margin-top:10px; margin-bottom:10px; margin-left:10%; margin-right:auto;}

Page 5: k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

Padding

Property Values

padding-leftpadding-rightpadding-toppadding-bottom

length%

Padding defines the space between theelement border and the element content.

Page 6: k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

Padding - Example

td { padding-left:3px; padding-right:3px; padding-top:3px; padding-bottom:3px;}

Page 7: k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

Padding, Margin and Border shortcutstd { padding-left:1px; padding-right:2px; padding-top:3px; padding-bottom:4px;}

Also applies to:• border-color• border-width• margin

td { padding:3px 2px 4px 1px;}

Page 8: k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

Dimensions

Property Values

heightwidth

autolength%

line-height normalnumberlength%

Page 9: k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

Dimensions - Example

body { line-height:1.5;}img { width:60%; border-width:1px; border-style:solid;}

Page 10: k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

Positioning

Property Values

float left, rightnone

position staticabsoluterelative

Page 11: k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

Positioning (position = absolute/relative)Property Values

leftrighttopbottom

auto%length

Page 12: k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

Positioning - Example

<img src="leftupper.jpg" style= "position:absolute;left:5px;top:5px">

<img src="rightupper.jpg" style= "position:absolute;right:5px;top:5px">

Page 13: k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

Layers and Visibility

Property Values

z-index autonumber

visibility visiblehidden

clip autorect(top right bottom left )

Page 14: k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

Layers and Visibility - Example 1

<span style="width:200;height:100; background-color:blue; position:relative;z-index:5"></span>

Necessary! Otherwisez-index is disregarded!

Page 15: k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

Layers and Visibility - Example 1

<span style="width:200;height:100; background-color:red"></span>

Page 16: k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

Layers and Visibility - Example 1

<span style="width:200;height:100; background-color:red; position:relative;left:-100;top:50"></span>

Page 17: k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

Layers and Visibility - Example 1

<span style="width:200;height:100; background-color:red; position:relative;left:-100;top:50"></span>

Page 18: k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

Layers and Visibility - Example 1

<span style="width:200;height:100; background-color:red; position:relative;left:-100;top:50; z-index:4"></span>

Page 19: k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

Layers and Visibility - Example 1

<span style="width:200;height:100; background-color:red; position:relative;left:-100;top:50; z-index:4"></span>

Page 20: k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

Layers and Visibility - Example 2

<span style="width:200;height:100; background-color:red">Text</span>

Text

Page 21: k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

Layers and Visibility - Example 2

<span style="width:200;height:100; background-color:red; visibility:hidden">Text</span>

Text

Page 22: k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

Layers and Visibility - Example 2

<span style="width:200;height:100; background-color:red; visibility:hidden">Text</span>

Page 23: k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

Layers and Visibility - Example 2

<span style="width:200;height:100; background-color:red;">Text</span>

Text

Page 24: k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

Layers and Visibility - Example 2

<span style="width:200;height:100; background-color:red; position: absolute; clip:rect(25 175 75 25">Text</span>

TextClipping needsabsolute position!(but no top, left, …)

Page 25: k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

Layers and Visibility - Example 2

<span style="width:200;height:100; background-color:red; position: absolute; clip:rect(25 175 75 25">Text</span>

Text

Page 26: k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

Pseudo-Classes

Used in CSS to add different effects to some selectors, or to a part of some selectors

Page 27: k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

Pseudo-Classes

selector:pseudo-class { property:value;}

Page 28: k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

Pseudo-Classesa { text-decoration:none; color:blue;}a:hover { text-decoration:underline; color:blue;}

Other pseudo-classes: a:active a:visited

Page 29: k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

How to link JavaScript to StyleSheets

Any HTML tag that has an ID, is an object inJavaScript.

<p id="par1">Text</p>HTML:

JavaScript:

p = document.getElementById("par1")

Page 30: k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

Every HTML object in JavaScript, has thestyle property. This property, which is anobject itself, enables you to use any CSSstyle command:

CSS: border-width, font-sizeJavaScript: borderWidth, fontSizeetc.

How to link JavaScript to StyleSheets

Page 31: k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

Example of CSS in JavaScript:

<p style="position:relative;left:0"

id="par1">

Some text in a paragraph. </p>

p = document.getElementById("par1");

indent = parseInt(p.style.left);

p.style.left = indent + 10;

Page 32: k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

Content of the tags P, DIV, SPAN, H2 etc. can be changed in the following way:als volgt gewijzigd worden:<div id="div1">Text </div>

p = document.getElementById("div1");

p.innerHTML = "New text";

How to link JavaScript to StyleSheets

Page 33: k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

There is a method that returns all elementswith a given tag name:

pArray =

document.getElementsByTagName("p");

for (i = 0; i < pArray.length; i++)

pArray[i].style.left = 10;

How to link JavaScript to StyleSheets

Page 34: k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

Example: Menus1. Create the menus in a 2 × n table

File Edit Help

Page 35: k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

Example: Menus2. Add an invisible span

(visibility=hidden) in every lower table cell. Give each span a unique id.File Edit Help

Page 36: k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

Example: Menus3. Add a table in each span,containing

the menu items.

File Edit Help

Open

Save

Save As

Close

Copy

Cut

Paste

Index

Contents

About

Page 37: k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

Example: Menus4. Add JavaScript event handlers to the

menus:<a href='javascript:clickMenu(0)' onmouseover='overMenu(0)'>File Edit Help

Open

Save

Save As

Close

Copy

Cut

Paste

Index

Contents

About

Page 38: k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

Example: Menus5. Add JavaScript event handlers to the

spans:

File Edit Help

Open

Save

Save As

Close

Copy

Cut

Paste

Index

Contents

About

Page 39: k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

Example: Menus5. Add JavaScript event handlers to the

spans:<span id='menu0'

style='position:absolute; visibility:hidden' onmouseover='overMenu(0)' onmouseout='outMenu(0)'>

Page 40: k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

Example: Menus

5. Add event handlers to the menu items:<a href='javascript:clickMenuItem(0, 0)'>Open</a>File Edit Help

Open

Save

Save As

Close

Copy

Cut

Paste

Index

Contents

About

Page 41: k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

Example: Menus

6. Write the JavaScript code:function clickMenu(n) {}

function clickMenuItem(n, m) { outMenu(n); // whatever you want to do // e.g. open a new page}

Page 42: k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

Example: Menus

6. Write the JavaScript code:function overMenu(n) { hideMenus(); m = document.getElementById("menu"+n); m.style.visibility="visible"; }

function outMenu(n) { m = document.getElementById("menu"+n); m.style.visibility="hidden"; }

Page 43: k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

Example: Menus

Result in an HTML file:• Click here if you downloaded

everything on your local disk• Click here if you have an Internet connection

Page 44: k/k 1212 Cascading Style Sheets Part three Marko Boon marko/cursusCSS.

Warnings

• almost none of this works in Netscape 4

• there can (and will) always be little differences between Netscape 6+ and Internet Explorer

Test everything in multiple browsers!