How to Center a Fixed Element in CSS

To center a position: fixed element horizontally, use left: 50% to put its left edge at the center of its containing block and transform: translateX(-50%) to shift it left by half its own width. The containing block is usually the viewport, the area where the browser displays the page.

Center a fixed element horizontally

Set top for a bar fixed near the top. The rule below caps its width at 400px and leaves 1rem on each side when space is tight. box-sizing: border-box counts the border and padding in that width.

.fixed-bar {
  position: fixed;
  top: 1rem;
  left: 50%;
  width: min(400px, calc(100% - 2rem));
  box-sizing: border-box;
  transform: translateX(-50%);
}

With a 1000px containing block and a 400px border box, left puts the left edge at 500px. The translation subtracts 200px, leaving the edge at 300px. left uses the containing block. Horizontal translateX() normally uses the element’s own border box. See the CSS positioned layout specification and CSS transform functions specification.

Run the scrollable example

Save the complete code in UTF-8 as fixed-center-example.html and open it in a browser. The bar and content share a wrapper but have separate widths.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Centering a fixed element</title>
  <style>
    body { margin: 0; color: #173447; font: 1rem/1.6 system-ui, sans-serif; }
    .shell {
      width: min(600px, calc(100% - 2rem));
      min-height: 150vh;
      margin-inline: auto;
      padding-block: 7rem 2rem;
    }
    .fixed-bar {
      position: fixed;
      top: 1rem;
      left: 50%;
      width: min(400px, calc(100% - 2rem));
      box-sizing: border-box;
      transform: translateX(-50%);
    }
    .fixed-bar {
      z-index: 1;
      padding: .6rem 1rem;
      border: 1px solid #0b4b56;
      background: #0b4b56;
      color: #fff;
      text-align: center;
      font-weight: 700;
      overflow-wrap: anywhere;
    }
    main { padding: 1rem; background: #e6f3f8; overflow-wrap: anywhere; }
    h1 { margin: 0 0 .5rem; font-size: 1.5rem; }
  </style>
</head>
<body>
  <div class="shell">
    <div class="fixed-bar">Centered fixed bar</div>
    <main>
      <h1>Scrolling content</h1>
      <p>Scroll to see whether the bar stays at the same position.</p>
    </main>
  </div>
</body>
</html>

The teal bar and the content box start centered. Scroll down. The content moves out of view while the bar stays 1rem from the viewport top.

  1. In developer tools, disable only transform on .fixed-bar. The bar’s left edge should sit on the center line. Re-enable it afterward.
  2. Narrow the viewport. With a 16px root font size and 390px of available width, the bar is 358px wide, leaving 16px on both sides. Changing the root font size changes the rem gaps.

Center horizontally without a transform

If you can set a width, set both horizontal insets to 0 and use automatic horizontal margins. Replace the first .fixed-bar rule in the demo with this one. The two margins split the leftover space. This also leaves the transform property available for another effect. See the auto margin rules.

.fixed-bar {
  position: fixed;
  top: 1rem;
  left: 0;
  right: 0;
  width: min(400px, calc(100% - 2rem));
  box-sizing: border-box;
  margin-inline: auto;
}

Center on both axes

For a box centered in the viewport on both axes, replace the original first .fixed-bar rule with this version. top: 50% sets the vertical starting point, and the second translate() value moves the box up by half its height. Taller content scrolls inside the box.

.fixed-bar {
  position: fixed;
  top: 50%;
  left: 50%;
  width: min(400px, calc(100% - 2rem));
  max-height: calc(100% - 2rem);
  box-sizing: border-box;
  overflow: auto;
  transform: translate(-50%, -50%);
}

Diagnose drift or covered content

An ancestor with only position: relative leaves viewport-based fixed positioning intact. An ancestor with a transform such as transform: translateZ(0) can become the containing block. Add it to .shell and scroll to see the difference. For viewport positioning, place the element outside the transformed ancestor or remove an unneeded transform. See the transform rendering rules.

A fixed box is out of normal flow. Leave space above the initial content for the actual bar height and check for overlap on narrow screens and at increased zoom. If the bar should take up space until it reaches a scroll threshold, position: sticky may fit better.

Compare the positioning modes in the CSS positioning guide.