Block Level Elements
Parent Element
width:auto
If no width declared all block
level elements within the parent
element will be set to 100%.
24
Block Level Elements
Parent Element
width:200px
If you declare a width...well
your block level element will
have that width, regardless of
the parent element
25
Absolute Elements
Parent Element
well hello there
well hello there...the box expands
If you don’t specify a width, the
box will expand with the
content. It will expand until
100% of the parent, then wrap.
26
Floated Elements
Parent Element
well hello there
well hello there...the box expands
Mimics the behavior of
positioned elements.
Doesn’t depend on
relative positioning
27
Rule of Thumb
Specify the width on:
• floated elements
• positioned elements either
fixed or absolute
28
Inline Elements
Parent Element
well hello there
well hello there...the box expands
well hello there
well hello there...the box expands
Just really long, skinny boxes
29
CSS Rule Set
Tells a browser how to render HTML boxes
Selector Declaration Block
Declaration Declaration
Property Value Property Value
body { color :black ; padding:1em ; }
31
CSS Rule Set
Tells a browser how to render HTML elements
Selector Declaration Block
Declaration Declaration
Property Value Property Value
body { color :black ; padding:1em ; }
32
Selector Declaration Block
Declaration Declaration
Property Value Property Value
body { color :black ; padding:1em ; }
Selector: “selects” an HTML
element that should be affected
by a rule set
33
Selector Declaration Block
Declaration Declaration
Property Value Property Value
body { color :black ; padding:1em ; }
Declaration Block: anything
between the curly brackets
34
Selector Declaration Block
Declaration Declaration
Property Value Property Value
body { color :black ; padding:1em ; }
Declaration: Tells the browser
how to draw any element on a
page that is selected
35
Selector Declaration Block
Declaration Declaration
Property Value Property Value
body { color :black ; padding:1em ; }
Property: The aspects of the
HTML element that you are
choosing to style.
36
Selector Declaration Block
Declaration Declaration
Property Value Property Value
body { color :black ; padding:1em ; }
Value: The exact style you want
to set for the property
37
Type Selectors
li{color: red;}
body
div div
h1 p p ul
em li li li
61
Class Selectors
.red{color: red;}
body
div div
h1 p p.red ul
em li li li.red
62
Class + Type Selectors
p.red{color: red;}
body
div div
h1 p p.red ul
em li li li.red
63
Rule of Thumb
Don’t use classes to style HTML elements to
look like other elements
<div class="heading">Main Heading</div>
.heading{
font-weight: bold;
font-size: 140%;
}
64
Rule of Thumb
Do HTML elements that already exist
<h1>Main Heading</h1>
h1{
font-size: 140%;
}
65
Think before you class
1. Is there an existing HTML element that I
can use instead?
2. Is there a class or ID further up the
document tree that can be used?
66
ID Selectors
#nav{color: red;}
body
div div
h1 p p.red ul nav
#
em li li li.red
67
ID vs .class
ID’s
• They are unique
• Each element can only have one ID
• Each page can only have one element
with the same ID
• ID’s have special browser functionality
• Javascript loves ID’s
<div id="nav header"></div>
68
ID vs .class
Classes
• They aren’t unique
• The same class can be used
on multiple elements
• You can use multiple classes on the
same element
<div class="nav header"></div>
69
Descendant Selectors
p em{color: red;}
body
div div
h1 p p.red ul nav
#
em li li li.red
70
Universal Selectors
* {color: red;}
body
div div
h1 p p.red ul nav
#
em li li li.red
71
Child Selectors
div > em {color: red;}
body
div div
h1 p p.red ul nav em
#
em li li li.red
72
Adjacent Sibling
Selectors
h1 + p {color: red;}
body
div div
h1 p p.red ul nav em
#
em li li li.red
73
Attribute Selectors
Select based on the attribute’s value
<img src="image.png" width="100"
height="100" title="Main Image"
alt="Main Image"/>
img[src="image.png"] {
border: 1px solid #000;
}
76
Attribute Selectors
Select based on the space separated
instances of a value
<img src="image.png" width="100"
height="100" title="Main Image"
alt="Main Image"/>
img[alt~="Main"] {
border: 1px solid #000;
}
77
Attribute Selectors
Select based on the hyphen separated
instances of a value
<img src="image.png" width="100"
height="100" title="Main Image"
alt="Main Image"/>
img[alt|="Main"] {
border: 1px solid #000;
}
78
:first-line
p:first-line {color: red;}
Lorem ipsum dolor sit amet, consectetur
adipiscing elit. Vestibulum rutrum purus
non risus porttitor convallis. In vitae nulla
id felis.
82
:before and :after
p:before {content: “ extra stuff”}
Lorem ipsum dolor sit amet, consectetur
adipiscing elit. Vestibulum rutrum purus
non risus porttitor convallis. In vitae nulla
id felis. extra stuff
83
:first-line
p:first-line {color: red;}
Lorem ipsum dolor sit amet, consectetur
adipiscing elit. Vestibulum rutrum purus
non risus porttitor convallis. In vitae nulla
id felis.
87
:before and :after
p:before {content: “ extra stuff”}
Lorem ipsum dolor sit amet, consectetur
adipiscing elit. Vestibulum rutrum purus
non risus porttitor convallis. In vitae nulla
id felis. extra stuff
88
CSS 3
I shall now cheat...I’m also lazy
http://www.css3.info/
89