liveBook · Manning

Absolute positioning

As explained in CSS in Depth, Second Edition

Absolute positioning removes an element from normal flow and positions it relative to a containing block, typically established by a positioned ancestor.

A dropdown should open over the page, not push the heading below it downward. Absolute positioning makes that possible: the positioned element leaves normal flow, and its edges can be placed within a containing block. Unlike fixed positioning, which uses the viewport, absolute positioning ordinarily uses the nearest positioned ancestor. section 6.2 section 6.3.1

Set position: absolute on the element, then use inset properties such as top, right, bottom, and left to place its edges. To choose its reference point, set position: relative on the intended ancestor. The browser searches past any unpositioned parent to find the nearest positioned ancestor. If it finds none, it uses the initial containing block: a viewport-sized area anchored at the top of the page. section 6.2 section 6.2.1

Place a Close button on a modal

A fixed-positioned modal body provides the containing block for its Close button. Give the button position: absolute; top: 0.3em; right: 0.3em to place it near the modal’s upper-right corner. If the visible button should be an ×, keep meaningful Close text in the HTML for screen readers. Hide the text visually, then add the × with an absolutely positioned ::after pseudo-element inside the button. section 6.2.1 section 6.2.2

Open a dropdown without moving the page

Put a toggle button and menu inside a dropdown container. Set the container to position: relative and the menu to position: absolute; left: 0; top: 2.1em. The menu then opens below the toggle, in front of nearby content, without shifting it. Hide the menu by default; a click handler toggles an is-open class that displays it. A click-controlled menu also works for keyboard and touchscreen users, unlike a hover-only approach. section 6.3.1

Listing 6.9 Displaying the open dropdown menu
.dropdown {
  display: inline-block;
  position: relative;         #1
}

.dropdown-toggle {
  padding: 0.5em 1.5em;
  border: 1px solid #ccc;
  background-color: #eee;
  border-radius: 0;
}

.dropdown-menu {
  display: none;          #2
  position: absolute;
  left: 0;
  top: 2.1em;                  #3
  inline-size: max-content;
  min-inline-size: 100%;
  background-color: #eee;
}

.dropdown.is-open .dropdown-menu {   #4
  display: block;                    #4
}                                    #4

.submenu {
  padding-inline-start: 0;
  margin: 0;
  list-style-type: none;
  border: 1px solid #999;
}

.submenu > li + li {
  border-top: 1px solid #999;
}

.submenu > li > a {
  display: block;
  padding: 0.5em 1.5em;
  background-color: #eee;
  color: #369;
  text-decoration: none;
}

.submenu > li > a:hover {
  background-color: #fff;
}

The dropdown styles show the relative-positioned container, absolute-positioned menu, inset values, and open-state rule together. section 6.3.1

The same pattern can pull a mobile menu button upward so it appears inside a header even when its navigation element follows the header in the markup. Placement does not settle which overlapping element appears on top, though. If a dropdown covers a modal unexpectedly, inspect stacking order and z-index rather than assuming a different top or right value will fix it. section 7.1.1 section 6.4.1

How CSS in Depth, Second Edition covers it

How deep it goes
Explains containing-block selection and inset placement through worked UI examples, then addresses overlapping elements through stacking order.
Examples
Uses a modal Close button, a click-controlled dropdown, and a mobile navigation button.
What it assumes
Assumes basic HTML and CSS familiarity while defining positioned elements and their containing blocks.
What you can do after
Build it from scratch: anchor an overlay to a positioned ancestor and diagnose unexpected placement or overlap.
How it connects
Connects relative positioning, fixed positioning, containing blocks, pseudo-elements, and z-index.

Also explained in

Compare how 2 books explain absolute positioning →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of CSS in Depth, Second Edition. The section links point to the parts of the book it draws on.