Skip to main content
  1. Web Development Course/
  2. Intermediate CSS/

Absolute Positioning

Alejandro AO
Author
Alejandro AO
I’m a software engineer building AI applications. I publish weekly video tutorials where I show you how to build real-world projects. Feel free to visit my YouTube channel or Discord and join the community.
Intermediate CSS - This article is part of a series.
Part 5: This Article

Absolute positioning is a powerful technique in CSS that allows you to precisely position an element relative to its nearest positioned ancestor. This technique is often used to create complex layouts and unique design elements on web pages.

In this article, we’ll introduce you to absolute positioning in CSS and explain how it works.

When an element is positioned absolutely, it is removed from the normal document flow and placed at a specific position on the page. This position is determined by the values you set for the top, right, bottom, and left properties.

Absolute positioning is useful for creating elements that need to be positioned precisely, such as overlays or tooltips. It can also be used to create unique design elements, such as text that flows around an image or a logo that sits on top of a hero image.

The Position Property
#

The first step to using absolute positioning is to set the position property to absolute on the element you want to position. Here’s an example:

.element {
  position: absolute;
}

This code will position the element absolutely but will not move it from its default position.

The Top, Right, Bottom, and Left Properties
#

Once you’ve set the position property to absolute, you can use the top, right, bottom, and left properties to position the element. These properties determine the distance between the element and its nearest positioned ancestor.

Here’s an example:

.element {
  position: absolute;
  top: 20px;
  left: 50px;
}

This code will position the element 20 pixels from the top of its nearest positioned ancestor and 50 pixels from the left of its nearest positioned ancestor.

Finding the Nearest Positioned Ancestor
#

In order for absolute positioning to work correctly, the element must have a positioned ancestor. A positioned ancestor is an element that has its position property set to anything other than static.

If no positioned ancestor exists, the element will be positioned relative to the initial containing block, which is usually the body element of the HTML document. In other words, the element will be positioned relative to the entire page if you don’t have a positioned ancestor.

Here’s an example:

<!DOCTYPE html>
<html>
  <head>
    <style>
      .element {
        position: absolute;
        top: 20px;
        left: 50px;
      }
    </style>
  </head>
  <body>
    <div class="element">This element is positioned absolutely.</div>
  </body>
</html>

In this example, the element is positioned absolutely and has a top value of 20px and a left value of 50px. However, since the element has no positioned ancestor, it will be positioned relative to the initial containing block, which is the body element. This means that the element will be positioned 20 pixels from the top of the page and 50 pixels from the left of the page.

Adding a Positioned Ancestor
#

Now that we know that the element must have a positioned ancestor, let’s add one to our example. We’ll add a div element with a class of container and set its position property to relative.

Here’s an example:

<!DOCTYPE html>
<html>
  <head>
    <style>
      .container {
        position: relative;

        background-color: #eee;
        width: 300px;
        height: 300px;
      }
      .element {
        position: absolute;
        top: 20px;
        right: 50px;

        background-color: #ccc;
        width: 100px;
        height: 100px;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="element">This element is positioned absolutely.</div>
    </div>
  </body>
</html>

In this example, we’ve added a div element with a class of container and set its position property to relative. We’ve also added a div element with a class of element and set its position property to absolute. We’ve also set the top and right properties to 20px and 50px, respectively. I added some background colors to the elements so you can see them on the page.

Now that the element has a positioned ancestor, it will be positioned relative to the container element. This means that the element will be positioned 20 pixels from the top of the container element and 50 pixels from the right of the container element.

Using Negative Values
#

You can also use negative values for the top, right, bottom, and left properties to position the element in the opposite direction.

Here’s an example:

.element {
  position: absolute;
  top: -20px;
  left: -50px;
}

This code will position the element 20 pixels above its nearest positioned ancestor and 50 pixels to the left of its nearest positioned ancestor.

However, be careful when using negative values. If you set the top or bottom property to a negative value, the element will be positioned outside of its nearest positioned ancestor. This can cause the element to overlap other elements on the page.

Conclusion
#

In conclusion, absolute positioning is a powerful technique in CSS that allows you to precisely position an element on a web page. By setting the position property to absolute and using the top, right, bottom, and left properties, you can create unique design elements and complex layouts. However, it’s important to remember that absolute positioning can be challenging to control and position correctly, so it should be used sparingly and with caution.

Intermediate CSS - This article is part of a series.
Part 5: This Article