Css Box Model
Css Box Model
by Susan L. Miertschin
Based on:
http://www.html.net/tutorials/css/
Lessons 9 through 14
Positioning Elements ‐ Layout
• Before CSS – tables were used to position
elements and lay out pages
• CSS introduced
– the position property
– and
d absolute
b l positioning
ii i
Absolution Positioning
• Normally content of elements flows onto page
sequentially
• Absolute positioning removes the element
from the normal flow
• Instead
I d positions
ii iit relative
l i to the
h top, lleft,
f
right, or bottom margins of its containing
bl k l l element
block‐level l (e.g.
( <p></p>
/ or
<body></body>)
Absolute Positioning
Example
Things to note:
¾This is an XHTML document
¾This is an example of an embedded
style sheet
¾The style definition is in the
<head></head> section
¾Z‐index property controls layering –
when items overlap, which one do you
want on top?
Absolute Positioning Example
• Things to note:
¾ Each <p></p>
p /p element is usingg its class attribute
¾ Change the z‐index element values in the style
definition and observe the results
Result
Relative Position
• Elements are positioned relative to other
elements in the flow of the page
• Keeps elements in the general flow of
elements on the page
Relative Positioning
Example
Things to note:
¾Embedded style sheet (again)
¾ex refers to x‐height
¾X‐height is a relative‐length
measurement equal to the height of a
lowercase x in the applied font
Inline versus Block
Block‐Level
Level Elements
Inline‐Level Elements Block‐level Elements
¾ Do not change the flow of • Displayed on its own line
elements within the • Has a virtual “box”
box around it
document • div
¾ span • p
¾ img • h1 through h6
¾a
¾ em
¾ strong
Relative Positioning
Example
Things to note:
¾Embedded style sheet (again)
¾ex refers to x‐height
¾X‐height is a relative‐length
measurement equal to the height of a
lowercase x in the applied font
Relative Positioning
Example
Things to note:
¾Span is a grouping element
¾It does not imply any structure to its
contents
¾Its purpose is to apply CSS rules or id
attributes to its contents
¾Span is an inline‐level element
Result
The Box Model in CSS
Example Markup
<h1>Article 1:</h1>
<p>Allll human
h beings
b i
are born free and equal
in dignity and rights.
Theyy are endowed with
reason and conscience
and should act towards
one another in a spirit of
brotherhood</p>
What the Browser Does
• Each element is in its
own box
• Each box has top,
margin, border,
b d padding dd
properties ‐ and content
• Each element has four
sides – right, left, top,
and bottom
• Property values are set
with css
Example ‐ Set Margins of the
<body></body>
b d /b d Element
l Withh CSS
body
{
margin‐top: 100px;
margin‐right:
margin right: 40px;
margin‐bottom: 10px;
margin‐left:
margin left: 70px; }
p
{border‐width: 1px;
b d
border‐style:
t l
dashed;
border‐color: blue; }
What it Looks Like ‐ <ul></ul> element
ul
{border‐width: thin;
b d
border‐style:
t l solid;
lid
border‐color:
orange; }
Get Complicated if You Like
h1
{ border‐top‐width:
border top width: thick;
border‐top‐style: solid;
border‐top‐color: red;
border‐bottom‐width:
thick; border‐bottom‐style:
solid;; border‐bottom‐color:
blue; border‐right‐width:
thick; border‐right‐style:
solid; border‐right‐color:
border right color:
green; border‐left‐width:
thick; border‐left‐style:
solid; border‐left‐color:
orange; }
Element Dimensions
• Width and Height
Make a Box With a Div and CSS
div.Box
{ height: 500px; width: 200px;
border: 1px solid black;
background: orange; }
<div class=
class="box">volgus
box >volgus rectum videt,
videt …
plagosum mihi parvo Orbilium </div>
What the Browser Does
Floating Elements
• Float to the right
• Float to the left
• Property: float
fl
• The element box with contents floats either
right or left in the containing element’s box
Float a Box Within a Box With CSS
#picture
p
{ float:left; width: 100px; }
<body>
<div id="picture">
p <imgg src="bill.jpg"
jpg
alt="Bill Gates"> </div>
<p>causas naturales et antecedentes,
antecedentes
idciro etiam nostrarum
voluntatum...</p>
l t t </ >
</body>
What the Browser Does
Create Structure with XHTML
<div id="column1">
id= column1 > <p>Haec disserens
qua de re agatur et in quo causa consistat
non videt...</p>
videt </p> </div>
<div id="column2"> <p>causas naturales
et antecedentes,
antecedentes idciro etiam nostrarum
voluntatum...</p> </div>
<div id="column3">
id column3 > <p>nam nihil esset in
nostra potestate si res ita se
haberet </p> </div>
haberet...</p>
Layout Columns with CSS Floats
#column1
{ float:left;width: 33%; }
#column2
{ float:left;width: 33%; }
#column3
{ float:left;width: 33%;}
Clear Property
• Used to control how the elements that follow
floated elements behave
• By default,
default elements that follow are moved up to
fill available space resulting from the float
• Clear values: left,
left right,
right both or none
• The principle is, if clear, for example, is set to
both for a box,
box the top margin border of this box
will always be under the lower margin border for
possible floating boxes coming from above.
above
Example of Clear
<div id="picture"> <img src="bill.jpg"
alt "Bill Gates"> </div>
alt="Bill
<h1>Bill Gates</h1>
<p class="floatstop">causas naturales
et …etiam nostrarum voluntatum...</p>
#picture
{ float:left; width: 100px; }
.floatstopp
{ clear:both; }
What the Browser Does
Positioning Elements With CSS
• Place an element exactly where you want it on
your page
• Positioning + Floats = many possibilities
• Basic Principle: top‐left coordinates with (0,0)
b i the
being h upper lleftf corner
Example – Position a Heading Element
h1
{ position:absolute;
top: 100px;
left: 200px; }
Absolute Positioning
• An element which is positioned absolute does
not leave empty space after being positioned
Example – Absolute Position
#box1
{position:absolute;
p 50px;
top: p left: 50px;
p }
#box2 { position:absolute;
top: 50px; right: 50px; }
#box3 { position:absolute;
bottom: 50px; right: 50px; }
#box4 { position:absolute;
B
Bottom:50px;
50 lleft:
f 50
50px; }
What the Browser Does
What the Browser Does
Relative Positioning
• The difference between absolute and relative
positioning is how the position is calculated
• An element which is positioned relative has its
position calculated from the original position
in the document (think text document here)
• The element obtains a space in the document
after
f it i iis positioned
ii d
Example – Relative Position With CSS
#dog1
#d 1 { position:relative;
ii l i left:
l f 350
350px;
bottom: 150px; }
#dog2 { position:relative; left: 150px;
bottom: 500px;
p ;}
#dog3 { position:relative; left: 50px;
bottom: 700px; }
<body>
<h1>The Tinder‐Box</h1>
<p><strong>By Hans Christian Andersen</strong></p>
<p>A soldier came … home. </p>
<p>As he walked … </p>
<p>"Thank you, old witch," said the soldier. </p>
<p>"Do you see that large tree, …</p>
<p>"But what am I to do, down there in the tree?" asked the soldier. </p>
<div id="dog1"><img src="dog1.gif"></div>
<p>"Get money," she replied;… </p>
<p>"This is not a bad story," said the soldier; …</p>
<div id="dog2"><img src="dog2.gif"></div>
<p>"No," said the witch; … </p>
<p>"Very
& V well; ll I promise.
i …</p>
/
<p>"Here it is," replied the witch; …</p>
<p>As soon as the rope was tied, … </p>
<p>"You're
p q ; apprettyy fellow,"
, q ; said the soldier,, … </p>
/p
<p>"You had better not look at me in that way… </p>
<div id="dog3"><img src="dog3.gif"></div>
<p>Then he went into the third room, …</p>
</body>
What the Browser Does
• http://www.tech.uh.edu/faculty/miertschin/outtabox/index.html