Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
mrx3
8,742 PointsI'm not fully understanding where to apply absolute, and relative positioning.
I'm going to try to explain how I would use absolute, and relative positioning. If I'm wrong please feel free to correct me. I'll start with absolute position, lets say I have a image that's not right where I want it. No matter where I set the image tag it's just not in the right position for my image. Now I can use absolute positioning to put the picture exactly where I want to by, using left, right, bottom, and top, correct?
Now for relative. Lets say I have another image that just needs to be moved a little bit, nothing major, or I want the image to be relative to another image. I could use relative positioning for this, correct?
Thanks in advance for any help.
7 Answers
Kevin Korte
28,149 PointsBoth relative and absolute positioning allow you to use top, left, right, and bottom to force an element to be exactly where you want it. The difference is how the the browser renders the elements as the page loads.
When you position an element using relative position, but do not give it top, left, right, or bottom adjustments, the element will render in exactly where it would as if the position was set to static. But if you use top, left, right, or bottom to move this relative positioned element, the element gets moved relative to where it would normally be positioned in the flow of the document. When you move a relative positioned element, there will be a space left where the element normally would have been rendered in the page.
When you position something with absolute, it is taken out of the normal flow of the page. There is no space rendered for these elements like there would be with a relative positioned element. Instead an absolute positioned element will be rendered top left of the next parent element that has a position of relative. So in most cases, if an element has a position of absolute, somewhere it has a parent element who simply has a position of relative.
Here is hopefully a super simple codepen to understand.
Ron McCranie
7,837 PointsRelative applies to the parent of an absolute element. You're saying "I want all absolute elements (images) to be relative to this parent element". If you don't use the relative you're telling the absolute elements to be positioned according to the screen, not their parent. The absolute elements will be positioned based on the closest relative parent or the screen itself if no relative parents are present. This and the box model are always the most difficult to grasp at first.
<div style="position:relative;"> <image style="position:absolute;"/> </div>
<div style="position:relative;"> <div style="position:relative;"> <image style="position:absolute;"/> </div> </div>
Matt Campbell
9,767 PointsWhen positioning something absolutely, you are taking it out of the flow of the document and saying I want it exactly here where I'm telling it to go. This is great when you want to do something like have title straddle the top border of a box or something.
Now, you need to give that exactly positioned element some coordinates so it know where it's meant to be but then you need to thinking, what are these coordinates in relation to? This is where the relative positioning comes in.
If you put your heading inside a div with the border on it, then you say that you want the heading to sit, 20px from the left and -10px from the top but you need to tell the browser that you want it to do that in relation to the div with the border.
That's why you'd set the heading to be absolutely positioned, relative to the position of the div with the border.
However, absolute positioning should not be used as a general positioning tool. If you're finding something isn't where you want it, look at the construction of your page and work out what it is you need to do to create the framework around your image so that it sits where you want it to. Absolute positioning is a hard and fast rule and as such, is totally unresponsive and will sit on top of other elements that get in it's way as they're not responding to where each other are.
Hope that helps.
mrx3
8,742 PointsExcellent reply Matthew, that's exactly what I was wondering. I was experimenting with Absolute positioning, and it was totally unresponsive like you said. When I tried to adjust the window, the image would override text, so you couldn't see the text because, the image blocked the text. Thanks again for the reply!!
Shawn Jones
15,579 PointsYou can't with absolute because like mentioned earlier absolute positioning pulls the item out of the flow of the original document. With responsive web design, the document itself is designed to be fluid, your using percent and em values in place of static values like pixels. When you use absolute positioning, it has no reference to the document because its not in flow with the document at all, so while the document will change orientation and size depending on the device it's displayed on, the absolute position item has no reference to change to because it doesn't reference anything in the first place, just the positioning from edge of the device screen. Think of it as a child with no guidance, he simply makes he's own rules based off of what he knows, but child's with parents base their rules off of what the parent knows and responds accordingly.
mrx3
8,742 PointsAwesome explanation Shawn, and thank you very much.
Ron McCranie
7,837 PointsYes, you can. Use relative units (%, em, vw) to position them. The key is think in a relative way. You can start with a design that's in pixels and just convert it to percentages once you get your layout where it needs to be if starting with relative units seems daunting.
Scott Kobewka
1,737 PointsYou are correct on absolute positioning. Relative positioning puts your image in place relative to the other elements in your HTML. So if you have a <p> before your image, the image will be displayed after the </p>. However, the exact image location will be determined by it's float(left right or none).
Or, what Ron said.
mrx3
8,742 PointsHello Ron, and Scott. Thanks for the input, I really appreciate it. I have another quick question. In Nick's lesson about making a site, we put images on the portfolio page, these images are responsive, they resize with the browser window. Can I use absolute or relative positioning on images that resize with the browser window?
mrx3
8,742 Pointsmrx3
8,742 PointsThanks Kev. Much appreciated bro.