Understanding CSS Position

Written by

The position property controls how an element is placed in the page.
Position Type

position: static;

Static is the default position for all elements. The element follows the normal document flow and cannot be moved using top, left, right, or bottom.

Diagram
static element (stays in normal flow)

This element simply appears where the HTML places it. No special positioning is applied.

Position Type

position: relative;

A relative element stays in the normal document flow, but you can shift it slightly using top, left, right, or bottom. It also becomes the positioning reference for any absolutely positioned children.

Diagram
Normal flow:
element in original position
After position: relative; top: 10px; left: 20px;:
element is visually moved but still keeps its original space

The original layout space is still reserved, even though the box looks shifted.

Position Type

position: absolute;

An absolute element is removed from the normal document flow. It is positioned relative to the nearest ancestor that has position set to relative, absolute, or fixed. If none is found, it uses the page (viewport) as the reference.

Diagram
Parent box (position: relative;)
Normal content inside the parent (still in flow)
absolute element
(top: 12px; right: 12px;)

The absolute element ignores the normal flow and “floats” on top, using the parent as a reference frame.

Position Type

position: fixed;

A fixed element is taken out of the document flow and attached to the viewport. It stays in the same place on the screen even when the page is scrolled (like a floating button or a sticky header).

Diagram
Scrolling content
Fixed element
Imagine this box staying in the same corner of the screen while the content on the left scrolls.

In real code, you would use something like position: fixed; bottom: 20px; right: 20px; for a floating button.

Position Type

position: sticky;

A sticky element starts as relative, scrolling normally with the page. Once it reaches a specified threshold, it behaves like a fixed element and sticks in place.

Diagram
sticky header (scrolls down at first)

As you scroll, the sticky element “sticks” to its position (for example the top of the viewport) and stays visible.

EXAMPLE 🔝
An example of using relative parents and absolute child property controls how an element is placed in the page.

<div class="modal-box">
  <button class="close-btn">✕</button>
  This is a modal box.
</div>

/* CSS */
.modal-box {
 /* parent */
  position: relative;  
  ...
}

.close-btn {
 /* positioned relative */
  position: absolute; 
  top: 10px;
  right: 10px;
  ...
}
This is the parent box
(position: relative;)

Position Details

Little Tips

1. top and left are measured from the element’s top-left corner

When you position an element using top or left, CSS always measures from the element’s top-left corner. It never uses the center unless you explicitly adjust it.

Diagram
Positioned box
(top: 20px; left: 30px;)
Reference point (0,0)

The element’s own top-left corner is moved to (top, left).

2. Aligning elements by their center (circular or geometric “center point”)

To center an element based on its actual center instead of its top-left corner, you must combine percentage positioning with a translation. This shifts the element back by half of its own size.

Diagram
Centered box
top:50%; left:50%;
transform: translate(-50%, -50%);

50% + translate() is the universal formula for perfect center alignment.

3. What negative top / left values mean

A negative value moves the element in the opposite direction. For example:

  • top: -20px; moves the element up by 20px
  • left: -30px; moves the element left by 30px
Diagram
Original position
Moved with negative values
top:-20px; left:-30px;

Negative values pull the element outward from the reference point.

Leave a Reply

Discover more from Daily Learning

Subscribe now to keep reading and get access to the full archive.

Continue reading