Fixing Anchor Links Hidden by a Sticky Header
Content
A common issue for WordPress site builders is anchor links that jump to a section of a page, only to have that section's title hidden behind a fixed ("sticky") header. This guide explains why this happens and provides the most effective CSS solutions to fix it.
Why This Happens
When you click a jump link (e.g., #section-name), the browser instantly repositions the page so the element with that ID is at the very top of the viewport. If you have a header that is always fixed to the top of the screen, it will cover the content you've just jumped to, creating a poor user experience.
The Modern CSS Solution: scroll-margin-top
The cleanest and most modern way to solve this issue is by using the scroll-margin-top CSS property. This property tells the browser to add a margin (or offset) to the top of an element when it is scrolled into view via a jump link.
Here is the basic code you can adapt. You will need to target the CSS selector for your anchor elements (the elements with the IDs you are linking to) and set the scroll-margin-top value to match the height of your sticky header.
[id] {
scroll-margin-top: 100px;
}
How to implement this:
- Identify the height of your sticky header in pixels. You can often find this using your browser's developer tools (right-click the header and select "Inspect").
- In your WordPress dashboard, navigate to Appearance > Customize > Additional CSS.
- Paste the code above, replacing
100pxwith the actual height of your header (e.g.,80px). The[id]selector is a broad target that will apply to most elements with an ID. - Publish your changes.
For a more targeted approach, you can apply the property to specific elements:
.your-section-class {
scroll-margin-top: 100px;
}
Alternative Solution: scroll-padding-top
Another modern property, scroll-padding-top, can be applied to the entire scrolling container (usually the <html> element) instead of the target elements. This can be useful if you have many different types of anchor elements.
html {
scroll-padding-top: 100px;
}
Important Considerations
- Specificity is Key: The exact CSS selector you need to use (
[id], a class, or thehtmlelement) depends entirely on your theme's structure. You may need to experiment to find the most effective selector for your site. - Test Thoroughly: After applying any CSS, test your anchor links on both desktop and mobile views, as header heights can change on different devices.
- Plugin-Specific Issues: If you built your page with a page builder like Elementor and are experiencing other anchor link issues (e.g., links not working at all when shared), the problem may be specific to that plugin's implementation. In such cases, it is often best to seek help from the plugin's dedicated support community on WordPress.org.
By using these CSS techniques, you can ensure a smooth and professional navigation experience for your visitors, with content perfectly positioned below your header.
Related Support Threads Support
-
Override CSS on the First Pagehttps://wordpress.org/support/topic/override-css-on-the-first-page/
-
Anchor Tag Positioninghttps://wordpress.org/support/topic/anchor-tag-positioning/
-
Optimise a Post within an Accordionhttps://wordpress.org/support/topic/optimise-a-post-within-an-accordion/
-
How to Add Hyperlnk to Borderless Plugin Marquee Texthttps://wordpress.org/support/topic/how-to-add-hyperlnk-to-borderless-marquee-text/
-
Font not showing correctly.https://wordpress.org/support/topic/font-not-showing-correctly-3/
-
How to link a menu item to a subdomain?https://wordpress.org/support/topic/how-to-link-a-menu-item-to-a-subdomain/
-
Content display issue after Elements Kit installationhttps://wordpress.org/support/topic/content-display-issue-after-elements-kit-installation/
-
Anchor Links not Working Properlyhttps://wordpress.org/support/topic/anchor-links-not-working-properly-4/
-
FAQ Schema Appears Twice – Conflict Between Rank Math and Elementorhttps://wordpress.org/support/topic/faq-schema-appears-twice-conflict-between-rank-math-and-elementor/
-
website Animations not working for mobilehttps://wordpress.org/support/topic/website-animations-not-working-for-mobile/
-
How to Embed Form fields directly within HTML contenthttps://wordpress.org/support/topic/how-to-embed-gravity-forms/