1. CSS
Jussi
Pohjolainen
Tampere
University
of
Applied
Sciences
2. CSS
• CSS
is
a
stylesheet
language
used
to
describe
the
presenta>on
of
a
document
wri@en
in
markup
language
• Usually
used
with
(x)html
• Defining
fonts,
colors
and
layout
• Separates
document
content
and
presenta>on
3. Some
History
• CSS1
spec
finished
1996,
it
took
three
years
before
browsers
supported
CSS1
• 2000
IE
5.0
for
Mac
was
the
first
browser
to
fully
support
CSS1
• =>
CSS
Filtering
• S>ll
hundreds
of
bugs
in
browsers
4. CSS
Versions
• CSS1
– W3C
Recommenda>on
1996
– Typography,
fonts,
text
alignment
– No
layout
and
design
• CSS2
– W3C
Recommenda>on
1998
– Layout,
style
sheets
could
be
imported,
selectors
– Revision
to
CSS2
-‐>
CSS2.1
• CSS3
– Several
specifica>ons:
modules
– Not
ready
yet,
four
modules
are
published
under
recommenda>on:
media
queries,
namespaces,
selectors
level
3,
Color
– Some
func>onality
already
implemented
in
browsers
6. <blink>
again!?
• April
3rd,
2013
Google
launches
Blink,
a
fork
of
the
WebKit
browser
engine
– h@p://www.chromium.org/blink
• Apple
and
Google
will
have
their
own
implementa>ons
of
WebKit…
• Mozilla
and
Samsung
collabora>ng
on
new
web
browser
engine
– h@p://blog.mozilla.org/blog/2013/04/03/mozilla-‐and-‐
samsung-‐collaborate-‐on-‐next-‐genera>on-‐web-‐
browser-‐engine/
7. Before
CSS
• Presenta>on
was
part
of
the
document
structure:
<h2 align="center">
<font color="red" size="+4" face="Times New Roman, serif">
<i>Usage of CSS</i>
</font>
</h2>
• Many
problems…
8. Using
CSS
• Structure
is
clear:
<h2>Usage of CSS</h2>
• Presenta>on
is
clear:
h2 {
text-align: center;
color: red;
font: italic large "Times New Roman", serif;
}
9. CSS
Possibili>es
• Colors,
fonts,
layout,
sizes,
borders..
• Possible
to
do
different
css
–
files
for
prin>ng,
mobile
devices,
desktop
computers.
• See
– h@p://www.csszengarden.com/
12. Linking
• Linking
to
external
CSS
– <link
rel="stylesheet"
type="text/css"
href="/
style.css"
media="screen">
• The
same
using
@import
rule
and
"internal"
style
sheet
– <style
type="text/css">
–
@import
url(/style.css);
– </style>
13. Internal
CSS
• To
use
internal:
– <style
type="text/css"
media="screen,projec>on">
–
⋮
CSS
rules…
– </style>
• And
to
use
inline
– <ul
style="font-‐size:120%;
color:#060">
–
⋮
list
items…
– </ul>
14. CSS3
Media
Queries
• Media
Queries
is
CSS3
extension
– <link
rel="stylesheet"
type="text/css"
href="/
style.css"
media="handheld
and
(min-‐width:
20em)">
21. Class
• XHTML:
<h1 class="tarkea">Otsikko</h1>!
<p>Tässä tekstiä ja tämä <span
class="tarkea">erityisen tärkeää
tekstiä</span></p>!
<p class="tarkea">Ja tämä vasta tärkeää
onkin</p>!
• Css:
.tarkea {!
color: RGB(255,0,0);!
}!
22. Class
• Css:
h1.tarkea {!
color: RGB(255,0,0);!
}!
28. Lengths
and
Units
• The
value
type
for
many
CSS
proper>es
is
specified
as
<length>.
• Can
be
rela>ve
or
absolute
• Rela>ve
units
– em
–
current
font
size
(1
em)
– ex
–
x
height
of
the
current
font
(heigth
of
char
x)
– px
–
pixels,
considered
as
rela>ve
since
no
fixed
physical
measure
• Absolute
units
– mm,
cm,
in,
pt,
pc
29. font-‐size
• And
even
more:
– font-size: x-small; /* absolute */
– font-size: larger; /* relative */
– font-size: 12pt; /* unit of length */
– font-size: 80%; /* percentage */
31. Background
• Background
color:
– background-‐color:
#C0C0C0;
• Background
image:
– background-‐image:
url("marble.gif");
– Repeat
• repeat
(default):
repeat
both
direc>ons
• repeat-‐x:
repeat
horizontally
• repeat-‐y:
repeat
ver>cally
• no-‐repeat:
no
repeat
– background-‐a@achment:
fixed
|
scroll
• Is
the
image
s>ll
or
does
it
move
when
scrolling
32. Background
Image
Posi>on
background-position: right top; /* right upper-corner */
background-position: 100% 0%; /* right upper-corner */
background-position: 100%; /* right upper-corner */
background-position: center bottom;/* center bottom */
background-position: 50% 100%; /* center bottom */
background-position: 100%; /* right top */
background-position: 50% 10px; /* center 10 pixels from top */
33. Text
Features
• word-‐spacing
• le@er-‐spacing
• text-‐decora>on
– underline
– overline
– line-‐through
– blink
38. Process
of
Resolu>on
1. For
a
given
property,
find
all
declara>ons
that
apply
to
a
specific
element.
2. Sort
the
declara>ons
according
to
their
levels
of
importance,
and
origins.
– !important statement
– origin?
user-‐agent
css,
author
css
(link),
user
css
3. Sort
declara>ons
with
the
same
level
of
importance
and
origin
by
selector
specificity.
– h@p://reference.sitepoint.com/css/specificity
4. Finally,
if
declara>ons
have
the
same
level
of
importance,
origin,
and
specificity,
sort
them
by
the
order
in
which
they’re
specified;
the
last
declara>on
wins.
39. Inheritance
• Pass
proper>es
from
parent
to
child
• CSS
div {
font-size: 20px;
}
• HTML
<div>
<p>
This <em>sentence</em> will have a 20px
<a href="#">font-size</a>.
</p>
</div>
41. XHTML
Related
CSS
Elements
• You
can
group
XHTML
–
page
using
div
and
span
elements
• div
and
span
elements
do
not
do
anything,
unless
they
are
connected
to
css
• xhtml:
– <p>Text text text <span class="important">text
text</span>. Text text text.</p>
• CSS:
– span.important { color: RGB(255,0,0); }
42. span
and
div?
• By
using
div,
you
can
divide
xhtml
–
page
in
to
logical
pieces.
The
posi>on
of
these
are
defined
in
CSS
– Example:
naviga>on,
header,
footer,
contents
• Span
is
used
to
give
layout
for
text
43. span
and
div
<div class="header">
<h1>Title</h1>
</div>
<div class="contents">
<p>Contents: <span
class="highlight">this is different</span></p>
</div>
<div class="footer">
<p>Copyright 2008</p>
</div>
44. Layout
Basics
• XHTML
page
is
divided
into
“boxes”
by
using
the
div-‐element
• Box
consists
of
– Width
– Padding
– Border
– Marginal
54. Posi>oning
• PosiJon
property
is
used
to
define
whether
box
is
absolute,
rela>ve,
sta>c
or
fixed
– staJc
–
default
value
– relaJve
–
box
can
be
offset
(top,
right,
bo@om,
ley)
– absolute
–
pulls
the
box
out
of
normal
flow,
can
be
placed
anywhere
(top,
right,
bo@om,
ley)
– fixed
–
like
absolute,
but
when
scrolling
stays
in
the
same
place
56. Floa>ng
• Problem
in
previous
example
is
that
there
is
now
way
to
determine
when
absolute
box
ends
– How
to
put
footer
below
these
boxes?
• Use
floaJng
box
– Floa>ng
a
box
will
shiy
it
to
the
right
or
ley
of
a
line,
with
surrounding
content
flowing
around
it.
60. Clear
• If
you
don't
want
next
box
to
wrap
around
the
floa>ng
objects,
use
clear
– clear:
ley
– clear:
right
– clear:
both
• Let's
add
– footer
{
–
border:
1px
solid
black;
–
clear:
both;
– }
70. A@ribute
selectors
• abbr[title] { border-bottom: 1px dotted #ccc }
• input[type=text] { width: 200px; }
• [this^=that] will match a the value of an attribute (“this”) that
begins with something (“that”)
• [this$=that] will match a the value of an attribute (“this”) that
ends with something (“that”)
• [this*=that] will match a the value of an attribute (“this”) that
contains with something (“that”), be it in the beginning, middle,
or end.