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 trialJohn Moran
3,536 PointsI'm confused about the relationship between relative and absolute positioning. Can I please get an explanation?
I'm not sure how they work together and complement each other.
1 Answer
Jonathan Grieve
Treehouse Moderator 91,253 PointsHi there John,
There are several different types of display and positioning properties. Let's, for now, concentrate on the relative
and absolute
positioning properties you mention.
When you're talking about positioning, the location of the element in the document tree, that is to say the HTML document is important.
Let's say you have an HTML document with the following simple structure.
<!doctype html>
<html>
<head>
<title>Page Tigle</title>
</head>
<body>
<header>
<img src="image.jpeg" />
</header>
<article>
<!-- descendent elements go here
</article>
</body>
</html>
And you want to target the header and give it relative positioning. Header is a child element of the root body
tag. If you gave it relative
positioning value that would indicate it's position relative to the body element.
In this example, the img
element is a descendent of the header element. You can give this element a position property of absolute
. That would tell the browser that you want to position the element within the confines of the header
element that has a relative position property.
Example
header{
position: relative;
}
header img {
position: absolute;
}
So... relative position properties are relative to its parent element, and absolute properties are relative to the parent element that has a relative position property.