position:relative
I started this morning learning something I should have known for ages. I have been coding HTML for well over a decade, and CSS for nearly a decade. I have even taught HTML and CSS both at the University of Bergen and for student groups at Bergen University College.
Of course, that’s no guarantee of how much I actually know about the subject, but I think I have a pretty good knowledge of HTML and CSS. Even then, how position:relative actually works has managed to slip completely under my radar. I find this rather embarrassing, but I write about it here, because if I haven’t managed to get this before now, there is probably several other webdevelopers that also don’t know this (I hope).
The functionality of position:relative is not complicated, it’s actually very simple, you just have to know this.
Positioning in general
Ok, let’s start with how the positioning in CSS works. If you only want to know about position:relative. You should jump to the position:absolute section and read on from there
You have two sorts of elements in CSS (actually there’s more, but there’s two significant sorts of elements): block level elements and inline elements. Inline elements flow with the text, and is always positioned using the flow around them.
Block elements may also flow with the text, but they always breaks up the flow, and is placed on a seperate line.
Okay, I wrote that they always occupies a seperate line. That’s as long as you don’t tell it to do otherwise. This can be done using the CSS keywords float, position, top, left, right, and bottom.. The simplest of these, and the most widely used are float.
float
You can float elements to the left or to the right. Flowing a block level element to the right will place it as far right as the parent element allows, and if the element is narrower than the parent element the text (or other elements) will flow around it.
example:
div.floating { width: 100px; height:100px; float:right; background:#CCC; margin:0 10px; } |
will make a div-element of the class “floating” look like the grey box on the right. Notice that this text will wrap around it. The floated element is anchored to the location in the text where it is coded. The grey box will always be located to the right of this paragraph even if I add thousands of lines of text above it.
position:absolute
Position:absolute takes the element out of the regular text flow, and places the element on the page according to coordinates. The coordinates are given by the top, left, right and bottom CSS attributes.
Here is an example:
div.absolutepositioned { position:absolute; top:100px; right:100px; width: 100px; height:100px; background:#CCC; margin:0 10px; } |
Imagine that the box below is a page.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce hendrerit lobortis magna pulvinar placerat. Maecenas vel nisi at quam posuere sagittis id vel purus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus eu eros tellus, et malesuada nunc. Etiam iaculis blandit laoreet. Vivamus eu metus odio, quis egestas ante. Cras non turpis quis sapien vestibulum aliquam. Nullam tellus massa, fringilla ut dictum eu, malesuada sit amet diam. Nulla facilisi. Nam congue facilisis risus ac luctus. Etiam ullamcorper pulvinar risus, vitae bibendum metus condimentum id. Proin porttitor neque quis neque ultricies nec varius leo tincidunt. Mauris cursus turpis eu velit tempor dapibus. Cras turpis tortor, tincidunt a varius eget, bibendum commodo lorem. Proin vestibulum, enim nec mattis viverra, odio lectus ultrices ante, at euismod lacus sapien a odio. Aliquam at semper sapien. Vestibulum eu sapien eros, tristique vulputate nisl. Etiam blandit viverra tellus, et consectetur risus suscipit quis.
Pellentesque commodo urna eu mauris tristique sollicitudin. Sed bibendum mauris at felis facilisis vestibulum sit amet sit amet dui. Curabitur dui dolor, tempus eu fringilla at, sagittis in neque. Praesent at tristique nulla. Donec ut ligula et nunc mollis elementum vitae non orci. Nulla volutpat tortor ut diam fermentum hendrerit. Donec sit amet elit ac neque viverra ultrices. Nulla facilisi. Nulla porta pellentesque turpis id mattis. Proin sollicitudin convallis malesuada. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum et molestie eros. Nulla convallis porta augue a porttitor. Pellentesque pharetra facilisis pretium. Phasellus vel luctus lacus.
Donec sagittis diam et orci venenatis mollis. Donec nibh nisl, euismod a vulputate eu, cursus sit amet mi. Suspendisse et vehicula nisi. Nullam accumsan ultricies ligula vel lacinia. Sed lacinia elit eu quam mattis ullamcorper. Maecenas dignissim turpis eros, id facilisis nulla. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed rutrum gravida metus id tempor. Fusce erat tellus, luctus at ultricies nec, imperdiet vel turpis. Integer non tellus justo, id bibendum augue. Nam quam sapien, luctus in molestie et, posuere in justo. Phasellus tincidunt cursus turpis eget viverra. Ut pulvinar, enim ac mattis luctus, odio leo ullamcorper arcu, vitae tincidunt turpis eros a enim. In sit amet turpis in massa lacinia laoreet. Aenean varius tincidunt tortor, sed ullamcorper dui sodales a. Morbi lacus lorem, dictum dignissim placerat eu, scelerisque at orci. Vestibulum ac tortor ac felis posuere aliquam. Proin eget quam sit amet est elementum ornare vel ut mi. Aenean vel dolor facilisis nunc tincidunt condimentum ut non elit.
Notice how the grey box is placed 100px from the top side of the “page” and 100px from the right side of the “page”. Also notice that the text doesn’t flow around the box, but is placed behind the box. This is an easy way to place elements around the screen, but it has low fault tolerance, since elements may end up being placed on top of eachother.
The top attribute controls the offset between the outer top side of the element, to the inner top side of the “page”.
The left attribute controls the offset between the outer left side of the element to the inner left side of the “page”.
The right attribute controls the offset between the outer right side of the element to the inner right side of the page.
The bottom attribute controls the offset between the outer lower side of the element to the inner lower side of the page.
position:fixed
Position:fixed works pretty much like the position:absolute, with the difference that the coordinates is relative to the sides of the viewing window in your browser. Elements that are positioned with fixed positioning will stick to the screen, and stay visible on the same place on the screen when you scroll down on the page. Internet Explorer 6 has no support for this, and this feature is therefore not used as much as you could think. I won’t provide an example for this, because it won’t fit on the page.
position:relative
Now to the thing I had wrong. position:absolute and position:fixed both modifies what the top, left, right and bottom attributes control. I thought this was the case for position:relative as well, and that the “relative” part meant that it would be relative to the location it’s coded (more like the float attribute). It is probably written a lot of places, but I thought I understood this so I never bothered to look. Of course I found it odd that not even Firefox or Opera worked the way I expected, but I didn’t think more about that. I have just avoided using this feature.
Therefore it was a great revelation this morning when I finally got how to use this. I was reading through some old “read-later” bookmarks, and came over this article in Smashing Magazine: Ask SM: CSS Quick-Question Edition. There @_Zapp (funny how similar this is to my nick, but it’s not me, promise.) had asked “How do you position stuff at the bottom of a div with (or without) position absolute and relative, so that it will look the same on all browsers.”. This caught my attention, because I had just had the same problem (since I had the position:relative stuff wrong).
Smashing Magazines answer was to use position relative on the box you want to place elements inside, and then use position:absolute inside.
In fact, I had to do this to show the position:absolute example above. The “page” here has style=”postition:relative”, and then the absolute positioned element inside is relative to the “page”. Without position relative, the grey box would have been placed somewhere around the header of this page.
div.virtualpage { border: 1px solid #cccccc; margin: auto; width: 400px; position: relative; height: 500px; font-size: 10px } div.absolutepositioned { position:absolute; top:100px; right:100px; width: 100px; height:100px; background:#CCC; margin:0 10px; } |
You got to study the specs
http://www.w3.org/TR/CSS21/visudet.html#containing-block-details
Yep.. I should read through it once more, and not skip the parts I think I already know… I can’t really understand how I could have missed this. I also have a feeling that I have known this at some point, but forgotten about this, because I know I have created pages that did this