How to make a sticky header in a WordPress block theme

A sticky header stays at the top of the screen as you scroll, so your site title and menu are always within reach. If you scroll down this page, you’ll see one in action. I’ve used a sticky header on most redesigns of my personal site over the years.

Block themes can do this without any custom code, thanks to the “Sticky” position setting on the Group block. It’s been in WordPress since 6.2, but it’s easy to miss, and if you set it on the wrong block, nothing happens. In this tutorial, we’ll go through how to make the header sticky in the Site Editor, why it sometimes doesn’t work, and how to do it for every template at once with a few lines of CSS.

How to make the header sticky in the Site Editor

The trick is to make the block around the header sticky, not the header itself. In most block themes, the header is a template part that’s added to each template, so we’ll wrap it in a Group block and make that Group sticky. I’m using Twenty Twenty-Five here, but the steps are the same in most block themes.

  1. Go to “Appearance” → “Editor” → “Templates”, and open the “Single Posts” template.
  2. Click the header to select it, and make sure it’s the Header template part that’s selected, not a block inside it. The List View, which you open with the icon in the top left, makes this easier.
  3. Open the block’s options menu (the three dots in the toolbar), and select “Group”. The header is now wrapped in a Group block, which is selected.
  4. In the block settings on the right, find the “Position” panel, and change it from “Default” to “Sticky”.
  5. Give the Group a background color, under “Background” → “Color”. Pick the same color as your page background, so the header looks the same as before.
  6. Click “Save”.

View a post on your site and scroll down, and the header follows along. If you’re logged in, it stops right below the admin bar, since WordPress adds the height of the admin bar to the position automatically.

The background color in step 5 matters more than it might seem. Without it, the header is transparent, so the content you scroll past shows through it, right behind your site title and menu.

Since each template has its own copy of the header, the change only applies to single posts. To make the header sticky everywhere, repeat the steps for your other templates, like “Pages”, “Blog Home” and “All Archives”. If that sounds tedious, the CSS further down does it for all templates at once.

Why it doesn’t work

  • You made a block inside the header sticky. This is the most common mistake. A sticky block only sticks within the block it’s in, so a sticky Group inside the Header template part stops as soon as the header itself scrolls out of view, which is right away. The editor hints at this: when a nested block is set to “Sticky”, the help text says it will stick to the scrollable area of its parent block. Make sure the sticky Group is at the top level of the template, with the template part inside it.
  • There’s no “Position” panel. Only the Group block supports it (along with its Row and Stack variations), so check that you’ve selected a Group and not the template part. The theme also needs to allow it. Themes that have appearanceTools turned on in theme.json, like Twenty Twenty-Five, allow it automatically. Otherwise, the theme needs position.sticky set to true in its settings.
  • The header sticks, but you can see the content through it. Give the sticky Group a background color, as in step 5 above.
  • Something on top of the header. WordPress gives sticky blocks a z-index of 10. If a slider, map or other embed still ends up on top when you scroll, it has a higher z-index of its own.
  • It still doesn’t stick. If one of the blocks or elements around the sticky Group has overflow: hidden in its CSS, sticky positioning stops working. That’s a CSS limitation rather than a WordPress one, and it’s usually caused by custom CSS in the theme or a plugin.

Making the header sticky on every template with CSS

If you’d rather not edit every template, a few lines of CSS do the same thing everywhere. Add them under “Appearance” → “Editor” → “Styles” → “Additional CSS”. I have a separate tutorial on how to add custom CSS in a WordPress block theme if you haven’t used it before.

header.wp-block-template-part {
background-color: var(--wp--preset--color--base);
position: sticky;
top: var(--wp-admin--admin-bar--height, 0px);
z-index: 10;
}
@media (max-width: 600px) {
header.wp-block-template-part {
top: 0;
}
}

The rule makes the header template part sticky, and gives it the theme’s base color as a background. If your theme uses a different color slug, replace base with it. The top value keeps the header below the admin bar when you’re logged in. WordPress only sets the admin bar height variable when the admin bar is shown, so for everyone else, it falls back to 0px. On small screens, the admin bar scrolls away with the page, so the header can go all the way to the top.

A sticky header has one side effect. When you follow a link to a heading further down the page, like the ones in a table of contents, the browser scrolls the heading to the very top of the screen, where it ends up hidden behind the header. The same thing happens to links and buttons when you move back up the page with the keyboard. This happens whichever way you made the header sticky, so add this to “Additional CSS” as well:

html {
scroll-padding-top: calc(var(--wp-admin--admin-bar--height, 0px) + 100px);
}

This tells the browser to leave some space at the top whenever it scrolls to something. Set 100px to a little more than the height of your header. WordPress already uses scroll-padding-top to make room for the admin bar, so the admin bar height is included in the calc() to keep that working when you’re logged in.

Doing it in the theme files

If you’re building the theme yourself, you can add the sticky Group straight to the template files instead. This is what the Site Editor steps above end up saving, here in templates/single.html:

<!-- wp:group {"style":{"position":{"type":"sticky","top":"0px"}},"backgroundColor":"base","layout":{"type":"default"}} -->
<div class="wp-block-group has-base-background-color has-background">
<!-- wp:template-part {"slug":"header"} /-->
</div>
<!-- /wp:group -->

If your theme doesn’t use appearanceTools, turn on sticky positioning in theme.json too:

{
"settings": {
"position": {
"sticky": true
}
}
}

If you’re using a theme you didn’t make yourself, make these changes in a child theme, so they aren’t lost the next time the theme updates.