static
by default.In normal flow, inline boxes flow from left to right, wrapping to next line when needed.
<img src="http://placekitten.com/100/100"/>
<img src="http://placekitten.com/100/100"/>
<img src="http://placekitten.com/100/100"/>
...
<img src="http://placekitten.com/100/100"/>
<img src="http://placekitten.com/100/100"/>
In normal flow, block boxes flow from top to bottom, making a new line after every box.
<p>Greetings</p>
<p>Hello</p>
<p>Hi there!</p>
Greetings
Hello
Hi there!
The "relative" value will still put the element in the normal flow, but then offset it according to top/left/right/bottom properties.
.relative{
position: relative;
left: 80px;
top: 20px;
height: 100px;
background-color: yellow;
}
top
, bottom
, right
, and left
.<html>
.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 element).
.top{
position: absolute;
top: -40px;
right: 10px;
background-color: yellow
}
.bottom{
position: absolute;
bottom: -40px;
left:60px;
background-color: green
}
Here's an example of an image with a caption absolutely positioned over top of it.
The containing div has a position of relative, and the caption has a position of absolute.
Bonus: Look up fixed
positioning. What's the difference between fixed
and absolute
?
Sometimes elements overlap. You can change the order of overlapping with z-index. The element with highest z-index goes on top.
.bottom{
position: absolute;
bottom: 10px;
left:60px;
background-color: yellow;
}
.top{
position: absolute;
bottom: 15px;
left:60px;
background-color: green;
z-index: 2;
}
Open the sample codepen, and fork a copy of your own.
Experiment with positioning different elements.
Create a div that contains an image and a caption, and position the caption absolutely in front of the image.
Bonus: Use semantic HTML5 elements for your image and caption.
Below a <blockquote> is floated to the left, allowing text to wrap around it on the right
.float{
float:left;
width:200px;
background:yellow;
}
If you want two block level elements to be side by side, one method is to float both elements. One left, and one right.
width: 300px;
float: left;
width: 400px;
float: right;
clear: left;
to the paragraph.clear: right;
clear: left;
clear: both;
.float{
float:left;
width:150px;
background:yellow;
}
.clear-left{
clear:left
}
.clear-left
drops below the floated element.Experiment with floats.
What heppens when multiple elements are floated to the right?
When would you want to clear a float?