CSSE 290 Web Programming

Lecture 5: Floating and Positioning

Reading: 4.2 - 4.5

Attribution:Except where otherwise noted, the contents of this document are Copyright 2012 Marty Stepp, Jessica Miller, and Victoria Kirst. All rights reserved. Any redistribution, reproduction, transmission, or storage of part or all of the contents in any form is prohibited without the author's expressed written permission.

Otherwise noted: Claude Anderson was given permission to modify the slides for CSSE 290 at Rose-Hulman by author Jessica Miller. The authors' original slides, based on Web Programming Step by Step, can be seen at http://webstepbook.com.
Some of the examples in some days' slides are from David Fisher at Rose-Hulman, who was kind enough to allow me to use them. My intention is to mark these examples with [DSF].

Valid HTML Valid CSS!

Front-end stuff

4.2: Introduction to Layout

Document Flow - Block Elements

flow

Document Flow - Block and Inline elements

flow

The CSS Box Model

box model

Rounded corners with border-radius css3

p {
	border: 3px solid blue;
	border-radius: 12px;
	padding: 0.5em;
}

This is a paragraph.

This is another paragraph.
It spans multiple lines.

CSS Properties for Padding

property description
padding padding on all 4 sides
padding-bottom padding on bottom side only
padding-left padding on left side only
padding-right padding on right side only
padding-top padding on top side only
padding properties

Padding Example 1

p { padding: 20px; border: 3px solid black; }
h2 { padding: 0px; background-color: yellow; }

This is the first paragraph

This is the second paragraph

This is a heading

Padding example 2

p {
	padding-left: 200px; padding-top: 30px;
	background-color: fuchsia;
}

This is the first paragraph

This is the second paragraph

CSS properties for margins

property description
margin margin on all 4 sides
margin-bottom margin on bottom side only
margin-left margin on left side only
margin-right margin on right side only
margin-top margin on top side only
margin-property arguments

Margin example 1

p {
	margin: 50px;
	background-color: fuchsia;
}

This is the first paragraph

This is the second paragraph

Margin example 2

p {
	margin-left: 8em;
	background-color: fuchsia;
}

This is the first paragraph

This is the second paragraph

CSS Properties for Dimensions

p { width: 350px; background-color: yellow; }
h2 { width: 50%; background-color: aqua; }

This paragraph uses the first style above.

An h2 heading

property description
width, height How wide or tall to make this element
(block elements and images only)
max-width, max-height,
min-width, min-height
max/min size of this element in given dimension

Notice what happens if we make the browser window wider or narrower

Centering a Block Element: auto Margins

p {
	margin-left: auto;
	margin-right: auto;
	width: 550px;
}

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Top/bottom Margin Collapse

margin collapse
margin collapse

Chrome developer tools

appearance CSS code HTML Code

4.3: Floating Elements

The CSS float Property (reference)

property description
float side to hover on; can be left, right, or none (default)

Float Example

<img src="images/Keaggy.jpg" alt="Phil Keaggy" class="headericon" />
Phil Keaggy has been known for more than 40 years as one ...
img.headericon {
	float: left;
}
Phil Keaggy Phil Keaggy has been known for more than 40 years as one of the world's best electric and acoustic guitarists, even though he only has nine fingers. He has recorded about 50 solo albums, as well as eight albums with power rock trio Glass Harp. His musical styles range from folk to fingerstyle to blues-rock.
One of my favorite Keaggy electric jams begins at time 2:55 in this YouTube video from 1985. This one shows off his amazing acoustic style (starts off slow, but really gets going!).

Float vs. alignment

none 1 before
none 2 before
right #1
right #2
left #1
left #2
none 1 after
none 2 after

Floating Content and Width

I am not floating, no width set

I am floating right, no width set

I am floating right, no width set, but my text is very long so this paragraph doesn't really seem like it's floating at all.

I am not floating, 45% width

I am floating right, 45% width

The clear property

p { background-color: fuchsia; }
h2 { clear: right; background-color: yellow; }

homestar runnerHomestar Runner is a Flash animated Internet cartoon. It mixes surreal humour with ...

My Homestar Runner Fan Site

property description
clear disallows floating elements from overlapping this element;
can be left, right, both, or none (default)

Clear diagram

div#sidebar { float: right; }
p { clear: right; }

float clear

Common error: container too short

<p><img src="images/homestar_runner.png" alt="homestar runner" />
	Homestar Runner is a Flash animated Internet cartoon.
	It mixes surreal humour with ....</p>
p { border: 2px dashed black; }
img { float: right; }

The overflow property

p { border: 2px dashed black; overflow: hidden; }

homestar runner Homestar Runner is a Flash animated Internet cartoon. It mixes surreal humour with ....

property description
overflow specifies what to do if an element's content is too large;
can be auto, visible, hidden, or scroll

Play with various values for overflow.

Multi-column layouts

<div>
	<p>the first paragraph</p>
	<p>the second paragraph</p>
	<p>the third paragraph</p>
	Some other text that is important
</div>
p { float: right; width: 20%; margin: 0.5em;
    border: 2px solid black; }
div { border: 3px dotted green; overflow: hidden; }

4.4: Sizing and Positioning

The position Property

div#ad {
	position: fixed;
	right: 10%;
	top: 45%;
}
property value description
position static default position
relative offset from its default position
absolute a fixed position within its containing element
fixed a fixed position within the browser window
top, bottom,
left, right
positions of box's corners

Resize this window and notice what happens to the "Here I am" ad.

Absolute positioning

#menubar {
	position: absolute;
	left: 400px;
	top: 50px;
}
absolute positioning

Relative Positioning

#area2 { position: relative; }
absolute positioning

Fixed positioning

fixed positioning

Alignment vs. float vs. position

  1. If possible, lay out an element by aligning its content
    • horizontal alignment: text-align
      • set this on a block element; it aligns the content within it (not the block element itself)
    • vertical alignment: vertical-align
      • set this on an inline element, and it aligns it vertically within its containing element
  2. If alignment won't work, try floating the element
  3. If floating won't work, try positioning the element
    • Absolute/fixed positioning are a last resort and should not be overused

The vertical-align property

property description
vertical-align Specifies where an inline element should be aligned vertically, with respect to other content on the same line within its block element's box

vertical-align Example

<p style="background-color: yellow;">
<span style="vertical-align: top; border: 1px solid red;">
Don't be sad!  Turn that frown
<img src="images/sad.jpg" alt="sad" /> upside down!
<img style="vertical-align: bottom" src="images/smiley.jpg" alt="smile" />
Smiling burns calories, you know.
<img style="vertical-align: middle" src="images/puppy.jpg" alt="puppy" />
Anyway, look at this cute puppy; isn't he adorable!  So cheer up,
and have a nice day.  The End.
</span></p>

Common Bug: Space Under an Image

<p style="background-color: red; padding: 0px; margin: 0px">
<img src="images/smiley.png" alt="smile" />
</p>

Details About Inline Boxes

The display pProperty

h2 { display: inline; background-color: yellow; }

This is a heading

This is another heading

property description
display Sets the type of CSS box model that an element is displayed with

Displaying Block Elements as Inline

<ul id="topmenu">
	<li>Item 1</li>
	<li>Item 2</li>
	<li>Item 3</li>
</ul>
#topmenu li {
	display: inline;
	border: 2px solid gray;
	margin-right: 1em;
}

The visibility Property

p.secret {
	visibility: hidden;
}

Since nobody can see this anyway: I am a recovering sugar addict!!!

property description
visibility Sets whether an element should be shown onscreen;
can be visible (default) or hidden

The opacity Property css3

body     { background-image: url("images/marty-mcfly.jpg"); background-repeat: repeat; }
p        { background-color: yellow; margin: 0; padding: 0.25em; }
p.mcfly1 { opacity: 0.75; }
p.mcfly2 { opacity: 0.50; }
p.mcfly3 { opacity: 0.25; }

Marty McFly in 1985

Marty McFly in 1955 fading away, stage 1

Marty McFly in 1955 fading away, stage 2

Marty McFly in 1955 fading away, stage 3

property description
opacity How not-transparent the element is; value ranges from 1.0 (opaque) to 0.0 (transparent)

More Details and Examples