CSS CSS Layout Basics Positioning Page Content Create an Image Caption with Absolute Positioning

John Moran
John Moran
2,465 Points

I'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
MOD
Jonathan Grieve
Treehouse Moderator 87,171 Points

Hi 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.