Question

CSS Fixed Position Relative to a Centered Container

csspositioncss-positionfixed

Question

How can I keep a <div> fixed at the top of the viewport while aligning it with the right edge of a centered container?

I currently use:

position: fixed;
top: 0;
right: 0;

This correctly keeps the element visible during scrolling, but it positions the element relative to the browser window rather than the centered container that contains it. position: absolute can position relative to the container, but it scrolls away with the page.

Is there a CSS technique or workaround that keeps the element visible while maintaining alignment with the container?

Short Answer

You will learn why position: fixed normally uses the viewport as its positioning reference, how position: sticky differs, and how a fixed full-width wrapper can keep content aligned with a centered layout container.

Concept

position: fixed removes an element from normal document flow and positions it relative to the viewport—the visible browser window. Therefore, right: 0 means “the right edge of the window,” not “the right edge of my parent container.”

A centered container is commonly created with a maximum width and automatic horizontal margins:

.container {
  max-width: 1100px;
  margin-inline: auto;
}

The container's left and right edges change as the viewport grows or shrinks. A fixed element cannot normally use that container as its reference while also remaining fixed to the viewport.

The practical solution is usually to separate the two jobs:

  1. Use a full-width fixed wrapper to attach something to the viewport.
  2. Put a regular centered inner container inside it to align its contents with the page layout.

If you want the item to stay visible only while its section is on screen, use position: sticky instead. Sticky positioning remains part of its container and becomes pinned when it reaches an offset such as top: 0.

Mental Model

Think of the viewport as the glass of a car window and the centered container as a box sitting behind that glass.

  • position: fixed attaches a note directly to the glass. It stays visible when the page moves, but its coordinates are measured from the glass.
  • position: absolute pins a note to the box. It moves away when the box moves during scrolling.
  • position: sticky pins the note to the box until it reaches the glass's top edge, where it temporarily behaves like it is attached to the glass.

A fixed wrapper plus a centered inner container is like attaching a transparent strip to the glass, then placing the note within that strip at the same horizontal position as the box below it.

Take Quiz

Syntax and Examples

A common layout uses a reusable .container class for both page content and the contents of a fixed bar.

<header class="fixed-bar">
  <div class="container fixed-bar__content">
    <a href="/" class="logo">Example</a>
    <button type="button">Menu</button>
  </div>
</header>

<main class="container page-content">
  <h1>Article title</h1>
  <p>Page content goes here.</p>
</main>
:root {
  --container-width: 1100px;
  --page-padding: 24px;
  --bar-height: 64px;
}

.container {
  width: min(var(--container-width), calc(100% - 2 * var(--page-padding)));
  margin-inline: auto;
}

.fixed-bar {
  position: fixed;
  inset: 0 0 auto;
  z-index: 1000;
  min-height: var(--bar-height);
  background: white;
  box-shadow: 0 2px 8px rgb(0 0 0 / 12%);
}

.fixed-bar__content {
  min-height: var(--bar-height);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.page-content {
  padding-top: calc(var(--bar-height) + 24px);
}

fixed-bar is fixed to the viewport and spans the entire screen. Its child uses the same .container sizing rules as the main content, so the logo and button line up with the content below.

The padding on .page-content matters because fixed elements are removed from normal layout. Without it, the beginning of the page could be hidden behind the header.

Step by Step Execution

Consider this smaller example:

<div class="toolbar">
  <div class="container">
    <button class="save-button">Save</button>
  </div>
</div>

<main class="container">
  <h1>Editor</h1>
</main>
.container {
  max-width: 900px;
  margin: 0 auto;
  padding: 0 16px;
}

.toolbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  background: white;
}

.toolbar .container {
  display: flex;
  justify-content: flex-end;
}

main {
  padding-top: 56px;
}

Execution flow:

  1. .toolbar has position: fixed, so it is positioned against the viewport and does not scroll away.
  2. top: 0, left: 0, and right: 0 make it stretch across the viewport at the top.
  3. The nested .container is a normal element inside the toolbar. Its max-width and margin: 0 auto center it.
  4. justify-content: flex-end places the Save button on the right side of that centered container.
  5. The main element receives top padding so its heading begins below the fixed toolbar.
  6. When the user scrolls, the document moves underneath .toolbar, while the toolbar stays at the viewport top.

Real World Use Cases

  • Fixed site headers: Keep navigation visible while aligning the logo, links, and account controls with the page content.
  • Cookie and consent bars: Fix a notice to the bottom of the viewport while keeping its text aligned with the site's content column.
  • Dashboard action bars: Keep Save, Publish, or Filter controls visible at the top of a long admin page.
  • Chat launchers or support panels: Align a fixed control with a desktop content layout while adapting at smaller screen widths.
  • E-commerce checkout summaries: Use a sticky order summary within a checkout column so it remains visible only while the checkout area is relevant.
Take Quiz

Real Codebase Usage

In production CSS, developers typically define one shared container rule and reuse it everywhere:

.container {
  width: min(72rem, calc(100% - 2rem));
  margin-inline: auto;
}

A fixed component then contains that shared container:

<div class="app-header">
  <div class="container">...</div>
</div>

This pattern avoids manually calculating how far the viewport edge is from the container edge. It also keeps alignment correct when the design changes from 1100px to 1200px, or when mobile padding changes.

Common project practices include:

  • Setting a deliberate z-index for fixed layers so they appear above page content.
  • Reserving space with padding or margin for a fixed header or footer.
  • Using CSS custom properties such as --header-height so the header height is defined once.
  • Adding responsive rules when navigation wraps or the fixed bar becomes taller on small screens.
  • Choosing sticky for sidebars and in-content controls that should stop at the boundary of their parent section.

A transformed ancestor can change the containing block for a fixed descendant in modern browsers, but it is usually not a layout technique to rely on. For example, transform: translateZ(0) on an ancestor can make a “fixed” child behave relative to that ancestor and potentially scroll with it. Prefer the wrapper pattern or sticky because their intent is clearer.

Common Mistakes

Expecting a parent to affect ordinary fixed positioning

This does not make the child fixed relative to .container:

.container {
  position: relative;
}

.menu {
  position: fixed;
  top: 0;
  right: 0;
}

position: relative creates a reference for absolutely positioned descendants, not normally for fixed ones. The fixed menu still uses the viewport.

Using position: absolute when the element must remain visible

.menu {
  position: absolute;
  top: 0;
  right: 0;
}

This correctly aligns with a positioned ancestor, but it moves with the document as the user scrolls. Use it only when scrolling away is desired.

Forgetting that fixed elements leave normal flow

.header {
  position: fixed;
  top: 0;
}

Without compensating space, content can appear underneath the header. Add top padding or scroll padding:

:root {
  scroll-padding-top: 64px;
}

main {
  padding-top: 64px;
}

Applying overflow to a sticky element's ancestor

position: sticky can fail unexpectedly when an ancestor has overflow: hidden, overflow: auto, or overflow: scroll. That ancestor may become the sticky element's scrolling boundary. Check parent styles if sticky does not stick as expected.

Hard-coding a desktop offset

.button {
  position: fixed;
  right: 220px;
}

This might align on one screen size but breaks on wider, narrower, or mobile screens. Use a shared container inside a full-width fixed wrapper instead.

Comparisons

TechniquePositioned relative toStays visible on page scroll?Best use
position: staticNormal document flowNoDefault layout
position: relativeIts normal locationNoSmall visual offsets; reference for absolute children
position: absoluteNearest positioned ancestorNoBadges, overlays, controls inside a card
position: fixedUsually the viewportYesGlobal headers, floating controls, modal layers
position: stickyIts container until it reaches a scroll offsetYes, within limitsSection headers, sidebars, in-page tools

Fixed wrapper versus sticky child

Use a fixed wrapper when the element must remain at the viewport edge no matter where the user scrolls. Place a centered container inside it for horizontal alignment.

Use a sticky child when the element belongs to a section and should stop being visible once that section has scrolled away. Sticky is often the better choice for a sidebar in a two-column page.

Take Quiz

Cheat Sheet

/* Shared centered layout */
.container {
  width: min(1100px, calc(100% - 32px));
  margin-inline: auto;
}

/* Fixed viewport bar, aligned internally to the container */
.fixed-bar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
}

/* Reserve space for the fixed bar */
main {
  padding-top: 64px;
}

/* Alternative: pinned within a section */
.sidebar {
  position: sticky;
  top: 16px;
}
  • fixed normally measures top, right, bottom, and left from the viewport.
  • A positioned parent helps absolute, not ordinary fixed positioning.
  • For fixed content aligned to a centered layout, use a full-width fixed parent and a centered child.
  • Add z-index when a fixed layer must appear above content.
  • Add layout space for fixed headers and footers.
  • Use sticky when the element should remain bounded by its parent section.

FAQ

Can position: fixed be relative to its parent?

Normally, no. A fixed element is positioned relative to the viewport. Certain ancestor properties, including transforms, can affect this behavior, but this is generally not the best way to build a centered fixed layout.

How do I align a fixed header with a max-width container?

Make the header full-width and fixed, then place an inner element with the same .container class used by the main page content.

Should I use fixed or sticky for a sidebar?

Use sticky when the sidebar belongs to a content section and should remain within that section. Use fixed only when it should stay visible globally, independent of the section.

Why does my content appear underneath my fixed header?

Fixed elements are removed from normal document flow and do not reserve space. Add padding-top or margin-top equal to the header height on the content area.

Why is position: sticky not working?

Check that it has an offset such as top: 0 and inspect ancestors for overflow settings. Also ensure the sticky element has enough scrollable space inside its container.

Can I use right: 0 to align with a centered container?

No. With position: fixed, right: 0 aligns to the viewport's right edge. A fixed full-width wrapper with a centered inner container avoids fragile manual offsets.

Take Quiz

Mini Project

Description

Build a fixed application header that stays at the top of the viewport. Its logo and navigation controls must align with the same centered content column used by the page.

Goal

Create a responsive fixed header whose inner content remains aligned with the main page container during scrolling.

Requirements

Use one reusable centered container rule for the header and main content.

Keep the header fixed at the top of the viewport.

Place the navigation button at the right edge of the centered container.

Prevent the page heading from being covered by the fixed header.

Keep the layout usable on narrow screens.

Take Quiz

Keep learning

Related questions

View more