Beruflich Dokumente
Kultur Dokumente
Cascading
ascad g Style
S y e Sheets
S ee s ((CSS)
SS)
http://freepdf-books.com
What is CSS?
A set of style rules that tell the web browser how
to present a web page or document.
In earlier versions of HTML,, style
y characteristics,, such
as fonts, backgrounds, and other aspects of the look
of the webpage were controlled from within the page
it lf
itself.
The evolution of cascading style sheets allowed for the
rules of formatting and presentation to be done in a
different manner than before.
http://freepdf-books.com
Top
p 10 Reasons for Usingg CSS
1
1. Build from the ground up to replace traditional
Web design methods
CSS to replace HTML tables, font tags, frames, and other presentational
hacks of HTML elements
http://freepdf-books.com
Top
p 10 Reasons for Usingg CSS
5
5. Greater control over the typography in a Web
page
CSS's ability to control typography better
6. Improvements in accessibility
Using CSS effectively means structuring content with HTML elements e.g.
<P>, <Tables>, <H1> etc.
7. Print designs as well as Web page designs
Create presentations not only for Web browsers, but also for other media
like print or PowerPoint‐like presentations.
http://freepdf-books.com
Top
p 10 Reasons for Usingg CSS
9
9. The design of Web pages is separated from the
content
By keeping the design in separate files linked to the HTML pages, you
reduce the likelihood of your page designs falling apart over time as
different contributors add to the Web page
http://freepdf-books.com
What is CSS?
CONTENT
Style
Web page
http://freepdf-books.com
What is CSS?
Style
y
CONTENT
Web page
Web page
CSS CSS
Physical layout
Text
CSS CSS
Headings
Body
http://freepdf-books.com
What is CSS?
One cascadingg style
y sheet can be used to set the styley and
format for many different web pages.
The biggest advantage is that if the web author wants to
makek whole
h l changes
h to a website,
b i the h style
l editing
di i takesk
place in one location (the CSS), yet is applied to all related
locations (the pages).
Imagine a large site with hundreds of pages. To change even
on item on each page, without a CSS, would require editing of
each of the hundreds of pages
pages.
In contract, a CSS based website would require the editing of
onlyy one file – the CSS.
http://freepdf-books.com
Types
yp of CSS
External
Internal/Embedded
Inline
http://freepdf-books.com
External
Allows for usingg style
y sheets from other sources
Connection made via the LINK tag
Use the optional TYPE attribute to specify a media type
Two Methods
Linking a CSS file: (Recommended)
<head>
h d
<link rel=“stylesheet” type=“text/css” href=“mystyle.css” />
</head>
Importing a CSS file: (Not supported in older browsers)
<head>
y type=”text/css”>@import
<style yp / @ p url(path/to/cssdoc.css);</style>
(p / / ); / y
</head>
http://freepdf-books.com
Internal/Embedded
/
Style characteristics are embedded in the HEAD
section of the webpage
Perhaps
p best used when a single
g p
page
g requires
q a
unique style sheet
<head>
<style type=“text/css”>
hr { color: navy}
body {margin‐left: 20px}
</style>
</head>
/h d
http://freepdf-books.com
Inline
Least flexible
Requires each element to be tagged if you want
them to appear differently
Looses the advantage of using CSS
http://freepdf-books.com
Usingg multiple
p sheets
You can use multiple sheets to define the style of
your document
Internal styles will override external styles, if they
are duplicated
http://freepdf-books.com
Usingg multiple
p sheets
h3 {color: red; text
text‐align:
align: right; font‐size:
font size: 8pt} (external CSS)
h3 {text‐align: center; font‐size: 20pt} (internal CSS)
will yield
http://freepdf-books.com
Usingg multiple
p sheets
h3 {color: red; text
text‐align:
align: right; font‐size:
font size: 8pt} (external CSS)
h3 {text‐align: center; font‐size: 20pt} (internal CSS)
will yield
h3 {color: red; text‐align: center; font‐size: 20pt }
http://freepdf-books.com
Understandingg the Cascade
Cascading
Determining rule weight by order
Based on order of rule within style sheet
Those listed later take precedence over those listed
earlier in the style sheet
http://freepdf-books.com
Understandingg the Cascade
Inheritance
Based
on hierarchical structure of
d
documents
t
CSS rules inherit from parent elements to child
elements:
thus <LI> elements will inherit style rules
from <UL> elements unless a style rule is
specifically set for the <LI> element
http://freepdf-books.com
Basic CSS Syntax
y
Three parts:
selector
property
value
http://freepdf-books.com
Basic CSS Syntax
y
selector
se ec o {property:
{p ope y value}
a ue}
http://freepdf-books.com
Basic CSS Syntax
y
If thee value
a ue has
as multiple
u p e words,
o ds, pu
put thee value
a ue in quo
quotes
es
p {{font‐family:
y “sans serif”}}
http://freepdf-books.com
Basic CSS Syntax
y
You
ou ca
can spec
specifyy multiple
u pep properties
ope t es to
o a ssingle
g e se
selector.
ec o
Properties must be separated by a semicolon.
http://freepdf-books.com
Basic CSS Syntax
y
To
o make
a e properties
p ope es more
o e readable,
eadab e, put
pu eac
each on
o a
separate line.
p { text‐align: center;
color: navy;
font‐family: arial
}
http://freepdf-books.com
Basic CSS Syntax – Grouping
S l t
Selectors
Se ecto s ca
Selectors can be ggrouped
ouped so that
a a common
co o pproperty
ope y
can be specified
<h1>
h1 This is a level 1 heading </h1>
/h1
<h2> This is a level 2 heading </h2>
http://freepdf-books.com
Basic CSS Syntax
y – Descendants
Selectors can be descendants
p b { color: yellow }
This style
y will be applied
pp to onlyy those <p>
element that has a value of class attribute
equal to “right”
http://freepdf-books.com
CSS Syntax
y ‐ class
p.right
p g { text‐align:
e a g rightg }
<p
p class=“right”>
g
This paragraph will be right aligned.
</p>
http://freepdf-books.com
CSS Syntax
y ‐ class
This is improper
p p use of the class selector:
http://freepdf-books.com
CSS Syntax
y – class free of tagg name
You can also create a class selector free of a tagg name if
you want all tags that have a particular class to be
formatted the same. E.g.
http://freepdf-books.com
CSS Syntax – class free of tag name
example
l
.center
ce te { text‐align:
g center }
<h1 class=“center”>
This heading will be centered
</h1>
<p class=“center”>
So will this text
</p>
http://freepdf-books.com
CSS Syntax
y – id Selector
Whilee thee class
c ass selector
se ec o can
ca apply
app y to
o several
se e a different
d ee
elements, the id selector can only apply to one,
unique element with in a document.
This style will apply to only that <p> element that has id
value equal to para1
http://freepdf-books.com
CSS Syntax
y – id Selector
p#para1
p pa a { text‐align:
e a g center;
ce e ; color:
co o green
g ee }
<p
p id=“para1”>
p
This text would be centered and green
</p>
http://freepdf-books.com
CSS Syntax
y ‐ comment
You can insert comments to help you describe the particular style
/* This is a comment */
P { color: red;
/* This is another comment */
Font‐family: verdana }
http://freepdf-books.com
CSS Properties
p
34
Font Properties
Border Properties
p
Margin Properties
http://freepdf-books.com
Background
g properties
p p
Basic syntax
background
background‐color
g
background‐image
background‐repeat
background‐attachment
background‐position
http://freepdf-books.com
Background
g properties
p p
All attributes can be set in a single declaration:
Shorthand Property:
background: #000000 url(‘psumark.gif’)
url(‘psumark gif’) no‐repeat
no repeat
fixed center
Eq i alent to
Equivalent to:
background‐color: #000000; background‐image:
url(‘psumark.gif’);
l(‘ k if’) background‐repeat:
b k d t no‐repeat;
t
background‐attachment: fixed; background‐
position: center
http://freepdf-books.com
Background
g properties
p p
Setting the body background (internal CSS)
<style>
body { background: #000000 url(
url(‘psumark
psumark.gif
gif’)) no
no‐
repeat fixed center }
</style>
http://freepdf-books.com
Background
g properties
p p
Elements
El t can also
l b be sett separately
t l (i
(internal
t l CSS)
<style>
body { background‐color: #000000; background‐
image: url(‘psumark.gif’); background‐repeat: no‐
repeat background‐attachment:
repeat; backgro nd attachment fixed;
fi ed
background‐position: center }
</style>
http://freepdf-books.com
CSS Properties
p
40
Font Properties
Border Properties
p
Margin Properties
http://freepdf-books.com
Text properties
p p
color
sets the color of the text
color can be represented by the color name (red), an
rgb value (rgb(255,255,255)), or by a hexadecimal
number (#ffffff)
Syntax
body {color: #ffffff}
http://freepdf-books.com
Text properties
p p
direction
sets the direction of the text
can be set as left to right (ltr) or right to left (rtl)
Syntax
body {direction: ltr}
http://freepdf-books.com
Text properties
p p
text‐align
text align
aligns the text in an element
possible values are left, right, center and justify
Syntax
p {text‐align:
{text align: justify}
http://freepdf-books.com
Text properties
p p
text‐decoration
text decoration
adds certain decoration elements to the text
possible values are none, underline, overline, line
line‐
through and blink
Syntax
y
p {text‐decoration: underline}
http://freepdf-books.com
Text properties
p p
text‐indent
text indent
indents the first line of text inside an element
possible values are length (defines a fixed value) and
%
Syntax
y
p {text‐indent: 20px} or
p {text‐indent: 5%}
http://freepdf-books.com
CSS Properties
p
47
Font Properties
Border Properties
p
Margin Properties
http://freepdf-books.com
Font properties
p p
49
Shorthand Property:
font: italic small‐caps 900 12px arial
Equivalent to:
font‐style: italic; font‐variant: small‐caps; font‐
weight: 900; font‐size: fixed; font‐family: arial
Italic
oblique
Syntax
body {font-style:
{ o t sty e italic}
ta c}
http://freepdf-books.com
Font properties
p p
font‐variant
font variant
Normal
font displays as is
small‐caps
font displays in all capitals, with lower case letters in smaller size
Syntax:
body {font-variant:
{font variant: small-caps}
small caps}
http://freepdf-books.com
Font properties
p p
font‐weight
font weight
normal
bold range from 100 – 900
400 is the same as normal weight
bolder
700 is the same as bold weight
lighter
weighted
i ht d values
l
Syntax:
body {font‐weight: bold}
http://freepdf-books.com
Font properties
p p
font‐size
font size
xx‐small
x‐small
Small
Medium
Large
x‐large
xx‐large
%
Syntax:
body {font-size: 20px}
{font-size: x-large}
{font-size: 125%}
http://freepdf-books.com
Font properties
p p
font‐familyy
family‐name
“times”, “arial”, “courier”, “verdana”
generic‐family
“serif”, “sans‐serif”, “monospace”
Syntax:
body {font-family: verdana, sans-serif}
http://freepdf-books.com
CSS Properties
p
55
Font Properties
Border Properties
p
Margin Properties
You can specify the width, style, color and on which sides the
border appears
Shorthand
Sh th d P Property:
t
border: medium double rgb(250,0,255)
E i l
Equivalent to:
border‐width: medium; border‐style: double; border‐color:
rgb(255 0 255)
rgb(255,0,255)
http://freepdf-books.com
CSS Properties
p
58
Font Properties
Border Properties
p
Margin Properties
http://freepdf-books.com
Margin
g properties
p p
Properties
margin
margin‐top
margin top
margin‐right
margin‐bottom
g
margin‐left
http://freepdf-books.com
Margin
g properties
p p
Can be set in one declaration
Think clock face (clockwise)
top right,
top, right bottom,
bottom left
http://freepdf-books.com
Margin
g properties
p p
All margins can be set the same
h1 {margin: 40px}
http://freepdf-books.com
Margin
g properties
p p
Margin settings can be paired (left and right, top
and bottom)
http://freepdf-books.com
Margin
g properties
p p
0 size margins do not need to be specified. 0px, 0pt
and 0 are all equivalent.
h1 {margin: 40px 0 5% 0}
In this example, the top margin would be 40 pixels, the left and
right margins would be 0, and the bottom margin would
be 5% of the total height of the document.
http://freepdf-books.com
Summaryy
CSS allows much richer control of document
appearance (than HTML)
CSS simplifies HTML
CSS makes maintenance easier
CSS allows for “cascading”
cascading rules
CSS can help reduce download time
CSS iis th
the ffuture
t off presentation
t ti on th
the web
b
Browser support is spotty, but getting better
http://freepdf-books.com
Thank You
Thank you
http://freepdf-books.com