Fulvio Corno, Dario Bonino: 24/04/09 eBWA-www2eB 1
Fulvio Corno, Dario Bonino: 24/04/09 eBWA-www2eB 1
24/04/09
eBWA-www2eB
Basics
CSS: Cascading Style Sheet CSS 1: W3C recommendation 17 Dec 1996 CSS 2.1: W3C Candidate Recommendation 19 July 2007 CSS 3: under construction Resources: CSS2.1 standard
http://www.w3.org/TR/CSS21/
W3C
CSS Tutorial
http://www.w3.org/Style/Examples/011/firstcss
2
24/04/09
24/04/09
em: the font-size of the relevant font ex: the 'x-height' of the relevant font px: pixels, relative to the viewing device
24/04/09
units
in: inches cm: centimeters mm: millimeters pt: points 1/72*1inch pc: picas, (12 points)
24/04/09
24/04/09
Anatomy of a Rule
A rule consists of two parts Selector the part before the left curly brace Declaration the part within the curly braces The selector is the link between the XHTML document and the style The selector specifies what elements are affected by the declaration The declaration sets what the effect will be
h1 {color: green} Selector
24/04/09
Declaration
7
24/04/09
Anatomy of a Declaration
A declaration has two parts separated by a colon: Property the part before the colon Value the part after the colon The property is a quality or a characteristic that something possesses (the color in the example) The value is a precise specification of the property (in the example: green)
h1 {color: green} Property Value
24/04/09
24/04/09
10
24/04/09
11
24/04/09
12
24/04/09
14
test.html
<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Untitled Document</title> <link rel="stylesheet" type="text/css" href="style.css"/> </head> <body> <h1>prova</h1> </body> </html>
24/04/09
15
24/04/09
16
Set
24/04/09
17
Overriding Inheritance
It is possible to override styles assigned to the parent elements of a given tag Example
body {color: green} h1 {color: navy}
The two rules are in conflict!! who wins? The most specific one!!
24/04/09
18
Inheritance Exceptions
Usually properties in CSS inherit from parent to child as in the previous slide However some properties do not! Example: background!! Why? You may end up with a non smooth background surface (if each element has its own background that is equal to all the others) The loading time can sensibly increase
24/04/09
19
24/04/09
21
Example
Want to have a heading1 in Times font, with 24pt height, bold, italic, navy color and a line height that is 150% of the font size
h1 { font-size: 24pt; font-family: Times, Times New Roman; font-style: italic; font-weight: bold; color: navy; line-height: 150%; }
24/04/09
22
24/04/09
23
Example
A blockquote margins'
blockquote { margin-top: 1em; margin-right: 0em; margin-left: 0em; margin-bottom: 1em; font-style: italic; }
There is also a shorthand that allows to specify margins in a single value (top, right, bottom, left):
margin: 1em 0em 1em 0em;
24/04/09
24
Background is a shortcut for 5 different attributes (whose values can be written in a single line, space separated)
24/04/09
25
background-attachment
fixed | scroll
Scroll: follows the document scrolling Fixes the background color, can be a named color or a RGB value
background-color
background-image
24/04/09
26
background-position Establishes the left and top edges of the background image Can be
A percentage (horizontal% height%) (if only 1 value is specified it is interpreted as an horizontal% and height% is fixed at 50% A length (same as for percentage) A mix of top | center | bottom left | center | right
24/04/09
27
Set if a background image shall be repeated and along which axis Allowed values
no-repeat|repeat|repeat-x|repeat-y
24/04/09
28
a:link { color: red } /* unvisited links */ a:visited { color: blue } /* visited links */ a:hover { color: yellow } /* user hovers */ a:active { color: lime } /* active links */
24/04/09
29
Example
A link should be visualized blue on white without being underlined when it is not selected White on blue when the mouse is hover the link And magenta on white when the link has been already selected
24/04/09
30
Example (2)
/* not-visited link: blue on white */ a:link {color: navy; background-color: white;} /* mouse over the link: white on blue */ a:hover{color: white; background-color: navy;} /* already visited link: magenta on white */ a:visited {color: magenta; background-color: white;}
24/04/09
31
In the CSS
p.author {font-style: italic; font-color:red;}
24/04/09
32
24/04/09
33
24/04/09
34
h1, h2, h3 { font-weight: bold} div.section > div.note {} p quote { font-style: italic}
Etc.
24/04/09
35
Examples
e /*matches any e element*/ e f /*matches any f element descendant of an e element*/ e > f /*matches any f element that is child of an e element*/ e + f /*matches any f element immediately preceded by a sibling element e*/ e [foo=warning] /*matches any element e having a foo attribute with value warning*/ #d /* matches any element with an attribute id=d */ .c /* matches any element with an attribute class=c */
36
24/04/09
CSS Selector * E EF E>F E:first-child E:link, E:visited E:active, E:hover, E:focus E:lang(c) E+F E[foo] E[foo="warning"]
Meaning Matches any element. Matches any E element (i.e., an element of type E). Matches any F element that is a descendant of an E element. Matches any F element that is a child of an element E. Matches element E when E is the first child of its parent. Matches element E if E is the source anchor of a hyperlink of which the target is not yet visited (:link) or already visited (:visited). Matches E during certain user actions. Matches element of type E if it is in (human) language c (the document language specifies how language is determined). Matches any F element immediately preceded by an element E. Matches any E element with the "foo" attribute set (whatever the value). Matches any E element whose "foo" attribute value is exactly equal to "warning".
E[foo~="warning"] Matches any E element whose "foo" attribute value is a list of spaceseparated values, one of which is exactly equal to "warning". E[lang|="en"] DIV.warning E#myid
24/04/09
Matches any E element whose "lang" attribute has a hyphen-separated list of values beginning (from the left) with "en". The same as DIV[class~="warning"]. Matches any E element ID equal to "myid".
37 http://www.w3.org/TR/CSS2/selector.html
24/04/09
38
The content edge surrounds the rectangle given by the width and height of the box, which often depend on the elements rendered content.
padding edge
The padding edge surrounds the box padding. If the padding has 0 width, the padding edge is the same as the content edge.
24/04/09
39
The border edge surrounds the boxs border. If the border has 0 width, the border edge is the same as the padding edge. The margin edge surrounds the box margin. If the margin has 0 width, the margin edge is the same as the border edge.
24/04/09
40
24/04/09
41
Examples (results)
24/04/09
42
Run-in boxes
They are useful for example to make run-in headers Example
h3{display:runin}
24/04/09
43
Block elements
The XHTML block element are the <div></div> element and the <span></span> element Example:
<div> Some text <p>More text</p> </div>
24/04/09
44
Box Positioning
A block can be positioned in different ways to which correspond different positioning schemes
Position: static | relative | absolute | fixed | inherit static: normal block relative: the offset values are relative to the block position in the normal flow. If a relative block B follows a relative block A, the offset is respect to the position of A without the offset
24/04/09
45
Box Positioning
absolute: the box position is determined by the top, left, right, bottom properties and is relative to the containing block fixed: the box is fixed with respect to some reference (the viewport as an example)
Examples
@mediascreen{ div.header{position:fixed} } @mediaprint{ div.header{position:static} }
24/04/09
46
Box offset
For absolute and relative position, an offset can be specified
top : length | percentage left: length | percentage right : length | percentage bottom : length | percentage
24/04/09
47
Floats
A float is a box that is shifted to the left or right on the current line. Content may flow along its side (or be prohibited from doing so by the clear property) A floated box is shifted to the left or right until its outer edge touches the containing block edge or the outer edge of another float.
24/04/09
48
Float (2)
If theres a line box, the top of the floated box is aligned with the top of the current line box. If there isnt enough horizontal room for the float, it is shifted downward until either it fits or there are no more floats present. Since a float is not in the flow, non-positioned block boxes created before and after the float box flow vertically as if the float didnt exist.
24/04/09
49
Example
the containing block is too narrow to contain the content next to the float, so the content is moved below the float
p{width:10em;border:solidaqua;} span{float:left;width:5em;height:5em; border:solidblue;} <p> <span></span> Supercalifragilisticexpialidocious </p>
24/04/09
50
Example 2
<html> <head> <title>floatexample</title> <styletype="text/css"> img{float:left} body,p,img{margin:2em} </style> </head> <body> <p><imgsrc=img.pngalt="thisimagewillillustratefloats"> somesampletextthathasnoother... </body> </html>
24/04/09
51
Example 3
Floating along two paragraphs
24/04/09
52
Example 4
The clear property If the p style is
p { clear: left }
24/04/09
53
Page arrangement
Fixed positioning can be used to create some complex frame-like presentations For example, the following:
24/04/09
54
24/04/09
55
24/04/09
56
24/04/09
57
24/04/09
58
24/04/09
59
24/04/09
60
24/04/09
61
RC width RC RC
/* RC
62