This Q&A shows how to add a sticky header or sidebar in WordPress with only CSS. It walks through finding your element selector and adding position: sticky rules in your theme or custom CSS. You’ll see how to set top offsets and z-index for a neat result.
How do I make a sticky header or sidebar in WordPress using only CSS?
position: sticky doesThe position: sticky property starts with relative positioning, then switches to fixed when you scroll past a set point.
style.css, ideally within a child theme..your-header-class {
position: sticky;
top: 0;
background-color: #fff;
z-index: 100;
}
.your-sidebar-class {
position: sticky;
top: 20px;
}
position: sticky; makes it stick.top: 0; fixes a header at the viewport’s top; adjust (e.g., top: 20px;) to stick a bit lower.background-color: #fff; (for headers) hides content underneath when sticking.z-index: 100; (for headers) keeps it above other elements.overflow: hidden, overflow: auto, or overflow: scroll.top, right, bottom, or left for sticky to work.WP Assistant is a free tool created by Atiba Software, a WordPress design and development company located in Nashville, TN. If you need more personalized WordPress assistance let us know, and we’ll get back to you ASAP!