Intro to HTML/CSS
<blockquote> is floated to the left, allowing text to wrap around it on the right
- "Floating" an element moves it as far to the left or right of its containing element as possible.
- Any other elements, such as paragraphs or lists, will wrap around the floated element.
- Always specify a
width when floating an element—otherwise the element is likely to take up the whole width of the container and not appear floated.
- You can specify a) whether an element is floated or not, and b) which side it floats on.
Hi, I'm a yellow box with black text.
I like to hang out on the left side.
Not floating element
Not floating element
Not floating element with wrapping text that continues on and on below the the floated box, and then goes on some more.
Placing elements side by side
If you want two block level elements to be side by side, you need to float both elements. One left, and one right.
- Clearing tells the element on which side (
both) other elements cannot appear.
- If you had an image floated left, and you did not want the paragraph to wrap next to it, you would add
clear: left; to the paragraph.
both ensures the element doesn't wrap next to floats on either side.
Element floated to the left
Non-floating element that wraps next to the floated element
Non-floating element with a class of
.clear-left drops below the floated element.
Let's Develop It
float on your sidebar and content areas.
- Experiment with floating other elements, like images.
- What happens when multiple elements are floated to the right?
- When would you want to clear a float?
- All HTML elements are positioned
static by default.
- Static elements are positioned in the normal flow of the page.
- Static elements ignore
left property specifications.
In normal flow,
block elements flow from top to bottom, making a new line after every element.
The "relative" value will still put the element in the normal flow, but then offset it according to
- Takes the element out of the normal flow, allowing it to be moved in relation to the
- Does not affect the elements surrounding it.
- Makes an element a "positioning context" in which to position other elements relative to it.
The "absolute" value will take the element out of the normal flow and position it in relation to the window (or the closest non-static ancestor).
- Positions element outside of the normal flow—other elements act as if it's not there.
- An absolutely positioned element is offset from its container block, set with the properties
- Its container block is the first surrounding element that has any position other than static.
- If no such element is found, the container block is
Example: Absolute Positioning
Here's an example of an image with a caption absolutely positioned in front of it.
div has a
relative, and the caption has a
The "fixed" value will take the element out of the normal flow and position it relative to the viewport.
Do you see the fixed footer in these slides?
Sometimes elements overlap. You can change the order of overlapping with z-index. The element with highest z-index goes on top.
Let's Develop it!
- Create a
div that contains an image and a caption.
- Position the caption absolutely in front of the image.
- Bonus: Use semantic HTML5 elements for your image and caption.
- Bonus: Use fixed positioning for your header or footer.
- What is HTML5? What do we gain by using it?
- What can we use to position elements with CSS?
- What technical struggles did you overcome today?
- What's next to continue your learning?
We love feedback! Please leave us some kind words and actionable suggestions.
Return to Table of Contents