Introduction aux Feuilles de Style CSS
Introduction aux Feuilles de Style CSS
CHAPITRE 2
CASCADING STYLE SHEETS (CSS)
Title
Lorem ipsum dolor sit amet,
Bold
consectetuer adipiscing elit.
Suspendisse at pede ut purus Italics
malesuada dictum. Donec vitae
neque non magna aliquam
dictum.
Indent
• Vestibulum et odio et ipsum
• accumsan accumsan. Morbi at
• arcu vel elit ultricies porta. Proin
tortor purus, luctus non, aliquam
nec, interdum vel, mi. Sed nec
quam nec odio lacinia molestie.
Praesent augue tortor, convallis
eget, euismod nonummy, lacinia
ut, risus.
3
La page résultante
Title
Lorem ipsum dolor sit amet,
consectetuer adipiscing elit.
Suspendisse at pede ut purus
malesuada dictum. Donec vitae neque
non magna aliquam dictum.
• Vestibulum et odio et ipsum
• accumsan accumsan. Morbi at
• arcu vel elit ultricies porta. Proin
Tortor purus, luctus non, aliquam nec,
interdum vel, mi. Sed nec quam nec
odio lacinia molestie. Praesent augue
tortor, convallis eget, euismod
nonummy, lacinia ut, risus. 4
5
CSS INTRODUCTION
Styling with Cascading Stylesheets
CSS Introduction
Feuilles de style en cascade (CSS)
Utilisé pour décrire la présentation des documents
Définissez les tailles, l'espacement, les polices, les
couleurs, la mise en page, etc.
Améliorer l'accessibilité du contenu
Améliorer la flexibilité
6
CSS Introduction (2)
CSS peut être appliqué à n'importe quel document
XML
Pas seulement en HTML / XHTML
CSS peut spécifier différents styles pour différents
médias
À l'écran
Sur papier
8
Pourquoi “en cascade”?
9
Pourquoi “en cascade”?
10
Syntaxe des feuilles de style
Les feuilles de style se composent de règles, de
sélecteurs, de déclarations, de propriétés et de
valeurs
[Link]
Les sélecteurs sont séparés par des virgules
Les déclarations sont séparées par des points-virgules
Les propriétés et les valeurs sont séparées par des
deux-points
h1,h2,h3 { color: green; font-weight:
11
bold; }
Sélecteurs
12
14
Sélecteurs (4)
15
20
Styles Inline: Exemple
21
[Link]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "[Link]
DTD/[Link]">
<html xmlns="[Link]
<head>
<title>Inline Styles</title>
</head>
<body>
<p>Here is some text</p>
<!--Separate multiple styles with a semicolon-->
<p style="font-size: 20pt">Here is some
more text</p>
<p style="font-size: 20pt;color:
#0000FF" >Even more text</p>
</body>
</html>
Styles Inline: Exemple
[Link]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "[Link]
DTD/[Link]">
<html xmlns="[Link]
<head>
<title>Inline Styles</title>
</head>
<body>
<p>Here is some text</p>
<!--Separate multiple styles with a semicolon-->
<p style="font-size: 20pt">Here is some
more text</p>
<p style="font-size: 20pt;color:
#0000FF" >Even more text</p>
</body>
22
</html>
Cascade CSS (priorité)
Il existe des feuilles de style navigateur, utilisateur
et auteur avec des déclarations "normal" et
"important"
Styles de navigateur (priorité la plus basse)
Styles utilisateur normaux
[Link]
23
Spécificité CSS
La spécificité CSS est utilisée pour déterminer la
priorité des déclarations de style CSS avec la
même origine. Les sélecteurs sont ce qui compte
Calcul simple : #id = 100, .class = 10, :pseudo = 10,
[attr] = 10, tag = 1, * = 0
Même nombre de points ? L'ordre compte.
Voir également:
[Link]
know/
[Link]
24
Styles intégrés
25
Styles intégrés: Exemple
[Link]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN"
"[Link]
[Link]">
<html xmlns="[Link]
<head>
<title>Style Sheets</title>
<style type="text/css">
em {background-color:#8000FF; color:white}
h1 {font-family:Arial, sans-serif}
p {font-size:18pt}
.blue {color:blue}
</style>
<head> 26
Styles intégrés: Exemple(2)
…
<body>
<h1 class="blue">A Heading</h1>
<p>Here is some text. Here is some text. Here
is some text. Here is some text. Here is some
text.</p>
<h1>Another Heading</h1>
<p class="blue">Here is some more text.
Here is some more text.</p>
<p class="blue">Here is some <em>more</em>
text. Here is some more text.</p>
</body>
</html>
27
Styles intégrés: Exemple(3)
…
<body>
<h1 class="blue">A Heading</h1>
<p>Here is some text. Here is some text. Here
is some text. Here is some text. Here is some
text.</p>
<h1>Another Heading</h1>
<p class="blue">Here is some more text.
Here is some more text.</p>
<p class="blue">Here is some <em>more</em>
text. Here is some more text.</p>
</body>
</html>
28
Styles CSS externes
Liaison externe
Des pages séparées peuvent toutes utiliser une feuille de style
partagée
Ne modifiez qu'un seul fichier pour changer les styles
sur l'ensemble de votre site Web(see
[Link]
[Link]
/* CSS Document */
a { text-decoration: none }
li em { color: red;
font-weight: bold }
ul { margin-left: 2cm }
ul ul { text-decoration: underline;
margin-left: .5cm } 30
Styles externes: exemple(2)
[Link]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN"
"[Link]
[Link]">
<html xmlns="[Link]
<head>
<title>Importing style sheets</title>
<link type="text/css" rel="stylesheet"
href="[Link]" />
</head>
<body>
<h1>Shopping list for <em>Monday</em>:</h1>
<li>Milk</li>
… 31
Styles externes: exemple(3)
…
<li>Bread
<ul>
<li>White bread</li>
<li>Rye bread</li>
<li>Whole wheat bread</li>
</ul>
</li>
<li>Rice</li>
<li>Potatoes</li>
<li>Pizza <em>with mushrooms</em></li>
</ul>
<a href="[Link] title="grocery
store">Go to the Grocery store</a>
</body>
</html>
32
Styles externes: exemple(4)
…
<li>Bread
<ul>
<li>White bread</li>
<li>Rye bread</li>
<li>Whole wheat bread</li>
</ul>
</li>
<li>Rice</li>
<li>Potatoes</li>
<li>Pizza <em>with mushrooms</em></li>
</ul>
<a href="[Link] title="grocery
store">Go to the Grocery store</a>
</body>
</html>
33
Text-related CSS Properties
color – specifies the color of the text
font-size – size of font: xx-small, x-small,
small, medium, large, x-large, xx-large,
smaller, larger or numeric value
font-family – comma separated font names
Example: verdana, sans-serif, etc.
The browser loads the first one that is available
There should always be at least one generic font
35
Shorthand Font Property
font
Shorthand rule for setting multiple font properties at
the same time
font:italic normal bold 12px/16px verdana
is equal to writing this:
font-style: italic;
font-variant: normal;
font-weight: bold;
font-size: 12px;
line-height: 16px;
font-family: verdana;
36
Backgrounds
background-image
URL of image to be used as background, e.g.:
background-image:url("[Link]");
background-color
Using color and image and the same time
background-repeat
repeat-x, repeat-y, repeat, no-repeat
background-attachment
fixed / scroll
37
Backgrounds (2)
background-position: specifies vertical and
horizontal position of the background image
Verticalposition: top, center, bottom
Horizontal position: left, center, right
is equal to writing:
background-color: #FFF0C0;
background-image: url("[Link]");
background-repeat: no-repeat;
background-attachment: fixed;
background-position: top;
Some browsers will not apply BOTH color and image
for background if using shorthand rule
39
Background-image or <img>?
Background images allow you to save many image
tags from the HTML
Leads to less code
More content-oriented approach
40
Borders
border-width: thin, medium, thick or
numerical value (e.g. 10px)
border-color: color alias or RGB value
border-style: none, hidden, dotted,
dashed, solid, double, groove, ridge,
inset, outset
Each property can be defined separately for left,
top, bottom and right
border-top-style, border-left-color, …
41
Border Shorthand Property
border: shorthand rule for setting border properties at
once:
border: 1px solid red
is equal to writing:
border-width:1px;
border-color:red;
border-style:solid;
Specify different borders for the sides via shorthand
rules: border-top, border-left, border-right,
border-bottom
When to avoid border:0
42
Width and Height
width – defines numerical value for the width of
element, e.g. 200px
height – defines numerical value for the height of
element, e.g. 100px
By default the height of an element is defined by its
content
Inline elements do not apply height, unless you change
43
Margin and Padding
margin and padding define the spacing around
the element
Numericalvalue, e.g. 10px or -5px
Can be defined for each of the four sides separately -
margin-top, padding-left, …
margin is the spacing outside of the border
44
Margin and Padding: Short Rules
margin: 5px;
Sets all four sides to have margin of 5 px;
margin: 10px 20px;
top and bottom to 10px, left and right to 20px;
margin: 5px 3px 8px;
top 5px, left/right 3px, bottom 8px
margin: 1px 3px 5px 7px;
top, right, bottom, left (clockwise from top)
Same for padding
45
The Box Model
46
IE Quirks Mode
47
Positioning
position: defines the positioning of the element in
the page content flow
The value is one of:
static (default)
relative – relative position according to where
the element would appear with static position
absolute – position according to the innermost
positioned parent element
fixed – same as absolute, but ignores page
scrolling
48
Positioning (2)
Margin VS relative positioning
Fixed and absolutely positioned elements do not
influence the page normal flow and usually stay on
top of other elements
Their position and size is ignored when calculating the
size of parent element or position of surrounding
elements
Overlaid according to their z-index
Inline fixed or absolutely positioned elements can
apply height like block-level elements
49
Positioning (3)
top, left, bottom, right: specifies offset of
absolute/fixed/relative positioned element as
numerical values
z-index : specifies the stack level of positioned
elements
Understanding stacking context
Each positioned element creates a stacking
context.
Elements in different stacking contexts are
overlapped according to the stacking order of
their containers. For example, there is no way
for #A1 and #A2 (children of #A) to be placed
over #B without increasing the z-index of #A. 50
Inline element positioning
vertical-align: sets the vertical-alignment of
an inline element, according to the line height
Values: baseline, sub, super, top, text-top,
middle, bottom, text-bottom or numeric
Also used for content of table cells (which apply middle
alignment by default)
51
Float
float: the element “floats” to one side
left: places the element on the left and following
content on the right
right: places the element on the right and following
content on the left
floated elements should come before the content that
will wrap around them in the code
margins of floated elements do not collapse
52
Float (2)
How floated elements are positioned
53
Clear
clear
Sets the sides of the element where other floating
elements are NOT allowed
Used to "drop" elements below floated ones or expand
a container, which contains only floated children
Possible values: left, right, both
Clearing floats
additional element (<div>) with a clear style
54
Clear (2)
Clearing floats (continued)
:after { content: ""; display: block;
clear: both; height: 0; }
Triggering hasLayout in IE expands a container of
floated elements
display:inline-block;
zoom: 1;
55
Opacity
opacity: specifies the opacity of the element
Floating point number from 0 to 1
For old Mozilla browsers use –moz-opacity
56
Visibility
visibility
Determines whether the element is visible
hidden: element is not rendered, but still occupies
place on the page (similar to opacity:0)
visible: element is rendered normally
57
Display
display: controls the display of the element and
the way it is rendered and if breaks should be
placed before and after the element
inline: no breaks are placed before and after
(<span> is an inline element)
block: breaks are placed before AND after the
element (<div> is a block element)
58
Display (2)
display: controls the display of the element and
the way it is rendered and if breaks should be
placed before and after the element
none: element is hidden and its dimensions are not
used to calculate the surrounding elements rendering
(differs from visibility: hidden!)
There are some more possible values, but not all
browsers support them
Specific displays like table-cell and table-row
59
Overflow
60
Other CSS Properties
cursor: specifies the look of the mouse cursor
when placed over the element
Values: crosshair, help, pointer, progress,
move, hair, col-resize, row-resize, text,
wait, copy, drop, and others
white-space – controls the line breaking of text.
Value is one of:
nowrap – keeps the text on one line
normal (default) – browser decides whether to brake
the lines if needed
61
Benefits of using CSS
62
Maintenance Example
Title Title
Some random Title Some random
Title text here. You Some random text here. You
Title can’t read it text here. You Title can’t read it
Some random
anyway! Har har can’t read it anyway! Har har
Title text here. You Title Some random har! Use Css. Some random
anyway! Har har har! Use Css.
can’t read it text here. You text here. You
Some random Some random har! Use Css.
anyway! Har har can’t read it can’t read it
text here. You text here. You Title
har! Use Css. anyway! Har har anyway! Har har
can’t read it can’t read it Title Title
har! Use Css. Some random har! Use Css.
anyway! Har har anyway! Har har
text here. You Some random Some random
har! Use Css. har! Use Css.
can’t read it text here. You text here. You Title
anyway! Har har can’t read it can’t read it
Title Some random
har! Use Css. anyway! Har har anyway! Har har
Some random Title Title Title text here. You
Title har! Use Css. har! Use Css.
text here. You can’t read it
Some random Some random Some random
can’t read it Some random anyway! Har har
text here. You text here. You Title text here. You
anyway! Har har text here. You har! Use Css.
can’t read it can’t read it can’t read it Title
har! Use Css. can’t read it Some random
anyway! Har har anyway! Har har anyway! Har har
anyway! Har har text here. You Title Some random
har! Use Css. har! Use Css. har! Use Css.
Title har! Use Css. Title can’t read it text here. You
Some random
anyway! Har har can’t read it
Some random Some random Title text here. You
har! Use Css. anyway! Har har
text here. You text here. You can’t read it
Some random har! Use Css.
can’t read it can’t read it anyway! Har har
text here. You
anyway! Har har anyway! Har har har! Use Css.
Title can’t read it Title
har! Use Css. har! Use Css. anyway! Har har
Some random har! Use Css. Some random
Title text here. You text here. You Title
CSS
Title can’t read it can’t read it
Some random Some random
anyway! Har har anyway! Har har
text here. You Some random Title Title Title text here. You
har! Use Css. har! Use Css.
can’t read it text here. You can’t read it
Some random Some random Some random
anyway! Har har can’t read it anyway! Har har
Title text here. You text here. You text here. You
har! Use Css. anyway! Har har har! Use Css.
can’t read it can’t read it can’t read it
har! Use Css. Some random
anyway! Har har anyway! Har har anyway! Har har
text here. YouTitle Title
har! Use Css. har! Use Css. har! Use Css.
can’t read it
file
Some random Some random
anyway! Har har
Title text here. You Title Title text here. You
har! Use Css.
can’t read it can’t read it
Some random Some random Some random
anyway! Har har anyway! Har har
Title text here. You text here. You text here. You
har! Use Css. har! Use Css.
can’t read it can’t read it can’t read it
Some random
anyway! Har har anyway! Har har anyway! Har har
Title text here. You Title Title
har! Use Css. har! Use Css. har! Use Css.
can’t read it Title Title Title
Some random Some random Some random
anyway! Har har
text here. You Some random text here. You Some random text here. You Some random
har! Use Css.
can’t read it text here. You can’t read it text here. You can’t read it Title text here. You
anyway! Har har can’t read it anyway! Har har can’t read it anyway! Har har can’t read it
har! Use Css. har! Use Css. har! Use Css. Some random
anyway! Har har anyway! Har har anyway! Har har
text here. You
har! Use Css. har! Use Css. har! Use Css.
Title Title can’t read it
anyway! Har har
Some random Title Some random Title
har! Use Css.
text here. You Some random text here. You Some random
Title can’t read it text here. You can’t read it text here. You
anyway! Har har can’t read it anyway! Har har can’t read it
Some random Title
har! Use Css. anyway! Har har har! Use Css. anyway! Har har
text here. You
Title Some random
har! Use Css. har! Use Css.
can’t read it Title text here. You Title
Some random
anyway! Har har can’t read it
text here. You Some random Some random Title
har! Use Css. anyway! Har har
can’t read it text here. You text here. You
har! Use Css. Some random
anyway! Har har can’t read it can’t read it
Title Title text here. You
har! Use Css. anyway! Har har anyway! Har har can’t read it
Some random har! Use Css. Some random har! Use Css. anyway! Har har
text here. You text here. You Title Title
har! Use Css.
can’t read it can’t read it
Some random Some random
anyway! Har har anyway! Har har
text here. You text here. You
har! Use Css. har! Use Css.
can’t read it can’t read it
anyway! Har har anyway! Har har
har! Use Css. har! Use Css.
63
CSS Development Tools
Visual Studio – CSS Editor
64
CSS Development Tools (3)
Firebug – add-on to Firefox used to examine and
adjust CSS and HTML
65
CSS Development Tools (4)
IE Developer Toolbar – add-on to IE used to
examine CSS and HTML (press [F12])
66