Remove unwanted blank space at top of mobile WordPress site

I successfully hid the header navigation and top bar on my WordPress site using CSS, but there’s still an annoying white gap showing up at the very top when viewing on phones and tablets. The desktop version looks fine, but mobile users see this empty space where the header used to be.

I tried using this CSS code:

.page .main-header {
  visibility: hidden;
}

.page .top-navigation {
  visibility: hidden;
}

The elements are hidden but the spacing remains. Is there a better way to completely remove this top margin or padding on mobile screens? Maybe I need to target different CSS properties or add some mobile-specific rules?

This is super common with WordPress themes. When you hide the header with display: none, the main content area often still has margins or padding that were meant to account for the fixed header.

Try adding this CSS with your display: none rules:

@media (max-width: 768px) {
  .site-content,
  .main-content,
  #content {
    margin-top: 0 !important;
    padding-top: 0 !important;
  }
}

Your theme might use different class names, so inspect the elements to find the right selectors. I’ve run into this exact problem on Genesis and Divi sites - the theme keeps compensating for header height even when it’s hidden. You might also need to target any sticky header classes specifically.

c-check if your theme’s adding top margin to the body or html element on mobile. WordPress themes sometimes throw extra spacing there that sticks around even when you hide headers. Pop open dev tools and look for something like body { margin-top: 60px } - just override it with zero.

Yeah, visibility hides elements but keeps their space - that’s why you’re getting the gap. Switch to display: none instead to completely remove them from the layout:

@media (max-width: 768px) {
  .page .main-header {
    display: none !important;
  }
  .page .top-navigation {
    display: none !important;
  }
}

Ran into this exact problem last year on a client site. The media query targets mobile only, and !important forces it over any theme styles. If there’s still spacing after this, check your theme for margin/padding on the body or main wrapper at mobile breakpoints.

The Problem:

You’re experiencing frustrating CSS debugging related to spacing issues across different screen sizes and themes in WordPress. You’ve tried hiding header elements using visibility: hidden, but this leaves an unwanted white gap on mobile devices. Manually adjusting CSS for every theme and screen size is time-consuming and prone to breakage with updates.

:thinking: Understanding the “Why” (The Root Cause):

The visibility: hidden property hides an element but preserves its space in the layout. This is why you still see a gap where your header used to be. Manually fixing this by adjusting margins and padding for each screen size and theme becomes a maintenance nightmare. Theme updates, plugin conflicts, or even slight changes in your site’s structure can easily break your custom CSS, forcing you to repeat the process.

:gear: Step-by-Step Guide:

  1. Automate CSS Maintenance: The most effective long-term solution involves automating the process of detecting and correcting layout problems. Rather than manually writing and updating media queries, build a system that automatically monitors your site’s mobile layout and applies the necessary CSS fixes. This system would continuously check for unintended spacing and apply the correct display properties and margin/padding resets, ensuring your mobile layout remains consistent regardless of theme updates or structural changes. This proactive approach saves significant time and prevents the frustration of repeatedly debugging the same layout issue.

  2. (If Automation Isn’t Feasible): Implement a Targeted CSS Solution: If fully automating CSS maintenance isn’t immediately possible, focus on a targeted solution using display: none; instead of visibility: hidden;. This completely removes the element from the layout. Add the following CSS to your theme’s style.css file or a custom CSS file. Ensure it’s added in a way that it won’t be overwritten during future updates (consider using a child theme if you’re uncomfortable making direct edits).

@media (max-width: 768px) {
  .page .main-header {
    display: none !important;
  }
  .page .top-navigation {
    display: none !important;
  }
  /* Target parent containers to remove extra margins/padding */
  .site-content,
  .main-content,
  #content {
    margin-top: 0 !important;
    padding-top: 0 !important;
  }
  body, html { /* Check for theme-level margins/padding */
    margin-top: 0 !important;
    padding-top: 0 !important;
  }
}

Remember to replace .page .main-header, .page .top-navigation, .site-content, .main-content, and #content with the actual CSS selectors for your header and main content area. Use your browser’s developer tools (usually accessed by pressing F12) to inspect the HTML and find the correct class or ID names.

  1. Verify the Solution: After implementing the CSS changes, clear your browser’s cache and test on multiple mobile devices and emulators. Use your browser’s developer tools to ensure that the header elements are hidden and that no extra margins or padding are affecting the layout.

:mag: Common Pitfalls & What to Check Next:

  • Caching: Browser caching, CDN caching, or WordPress caching plugins can prevent CSS changes from taking effect. Clear your browser’s cache, purge your CDN cache, and temporarily deactivate caching plugins.
  • Theme Conflicts: Your theme might have CSS rules with higher specificity that override your custom CSS. Use your browser’s developer tools to identify conflicting styles and adjust your selectors accordingly. Using a child theme will help prevent future conflicts with theme updates.
  • Incorrect Selector: Double-check that you are using the correct CSS selectors to target the header elements and the main content area. Inspect the HTML structure using your browser’s developer tools to ensure accuracy.
  • Additional Spacing Sources: Check the body and html elements for any default margins or padding that could be causing the extra space. Also, examine any wrappers or containers surrounding your main content.

:speech_balloon: Still running into issues? Share your (sanitized) config files, the exact command you ran, and any other relevant details. The community is here to help!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.