We use cookies to make your viewing experience better. By accepting you consent, you agree to our Cookie policy

Accept
Improve your Craft CMS skills

How To Remove White Space In Craft CMS

10 min read
Remove White Space In Craft CMS

Excess whitespace destroying your Craft CMS templates' aesthetics? Those gaps between elements can ruin layouts and styling if left unchecked. This guide provides proven techniques to identify, eliminate, and test whitespace fixes in Craft. Developers will discover targeted solutions to resolve whitespace woes for good, from global CSS tricks to template-specific tweaks. With actionable tips covered, you’ll learn how to banish unwanted whitespace for good.

Excess whitespace in Craft CMS templates can be eliminated by using browser developer tools to identify CSS styling adding gaps, manually reviewing frontend and backend code for empty containers and structural tags introducing white space, overriding default browser styling and framework CSS rules with targeted CSS, and comprehensively testing fixes across site pages and devices to address any new layout issues.

Identifying White Space Problems in Craft CMS

Using Browser Developer Tools

Unwanted white space in Craft CMS templates can create an unsightly gap between elements, misalign content sections, and generally create a disjointed aesthetic for site visitors.

Thankfully, identifying extra white space issues in Craft is straightforward with browser developer tools like Chrome DevTools or Firefox DevTools. These built-in web developer toolkits allow us to visually inspect page elements, analyse applied CSS styling, and pinpoint any CSS code adding unnecessary white space.

To identify white space problems in Craft CMS with Chrome DevTools, first open the page in Chrome and access the DevTools panel (CTRL+SHIFT+I on Windows/Linux, CMD+OPT+I on Mac). Craft CMS pagination: The Elements tab lets you visually browse page elements and their styling. Click on any section with excess white space and the CSS panel will display applied styles. Look for margins, padding, and other spacing properties that may be adding unwanted gaps. Toggling CSS properties on and off can help isolate the culprit.

Similarly, Firefox DevTools provides element inspection and style analysis to debug white space issues in Craft templates. On the Inspector panel click an element then view Box Model diagrams and CSS rules adding white space. The 3D View also lets you visualize spatial relationships between elements that may be causing gaps. For instance, a relatively positioned element can add white space without applying margins/padding.

Both Chrome and Firefox DevTools offer useful visualisation and inspection tools to identify whitespace added by frontend Craft CMS templates. Developers can quickly pinpoint styling problems and modify templates accordingly to remove excess white space.

Checking Templates Manually

Alongside browser developer tools, manually reviewing frontend and backend Craft CMS templates can help identify whitespace issues before they affect the live site. Unlike automated inspection, manual code reviews let developers closely examine both frontend templates and backend PHP/Twig code for any whitespace problems.

For frontend templates, view source on any page with excess white space and scan the raw code for structural tags or styling that may introduce gaps. Check for empty containers and sections, styling such as margins and padding applied to wrappers or rows, and framework CSS that adds default spacing. In the Craft CMS control panel, also review individual Twig templates for tags like {% spaceless %} that may strip whitespace.

On the Craft CMS backend, carefully check PHP includes, global CSS/JS files, and other core templates for coding patterns that could cascade whitespace issues across the site. For example, an empty <div> tag in a reusable header/footer template will add white space site-wide. Also watch for multiple or nested containers that introduce padding and margins.

While more time consuming than automated inspection, hands-on template reviews ensure developers notice every potential whitespace issue in Craft CMS templates. Both frontend and backend code should be checked for optimal whitespace control.

Identifying Common Sources

When hunting for white space problems in Craft, developers should watch for a few common culprits that frequently cause unintended gaps in templates.

A common source of excess white space is browser default styling for structural HTML tags. Browsers apply basic CSS rules including margins for elements like <p>, <h1>-<h6>, and <ul>. Resetting default browser CSS values through a normalization stylesheet prevents inherited white space.

Frontend frameworks like Bootstrap and Foundation also introduce whitespace through their column/grid systems and UI element styling. For example, Bootstrap's .container class applies 15px side padding by default. Developers should override framework defaults where needed.

Finally, custom template code can inadvertently add white space through overuse of gap utilities like margin and padding. Nesting divs and sections with spacing applied can quickly compound whitespace. Keeping templates modular with reusable spacing classes helps reduce these issues.

By learning where whitespace most commonly stems from in Craft CMS - browser styling, CSS frameworks, custom code - developers can more efficiently track down and eliminate the source while optimizing templates.

Techniques for Removing White Space in Craft CMS

Global CSS Adjustments

Once whitespace issues have been identified in Craft CMS templates, developers can make a few key CSS adjustments to globally remove excess gaps across all site pages and templates.

The simplest global fix is adding the * { margin: 0; padding: 0; } ruleset to reset default margin and padding for all elements to zero. Since browsers apply built-in spacing to common tags like <p>, <h1-6>, and <ul>, resetting these ensures no inherited whitespace sneaks in.

Additionally, explicitly setting the global box-sizing property to border-box prevents unintended padding or margin causing elements to overflow their containers. The universal selector (*) can also be used to globally normalize properties like font-size and line-height that influence whitespace.

For more surgical control, developers should override the CSS rules for individual elements prone to adding whitespace like <rows>, <sections>, <divs> etc. For example, rows { margin: 0; } removes the default gap between Bootstrap rows.

Targeting global CSS resolves whitespace across all Craft CMS templates in one go. But fine-tuning may be needed for specific templates.

Targeting Specific Templates

While global adjustments fix universal whitespace issues, surgical changes are sometimes needed to remove excess gaps in individual Craft templates. Developers can target unique templates and elements to refine whitespace.

First, inspect the compiled front-end code for any template with extra whitespace, like a blog post or landing page. Identify unnecessary <divs>, container elements, or sections causing gaps. Then write targeted CSS overrides in the main stylesheet to reduce or remove spacing for those elements, e.g. .blog-header { padding: 0; }.

For surgical adjustments in backend templates, utilise the {% spaceless %} tag in Twig around problem code blocks. This strips all whitespace between HTML tags in that block.

Spaceless tags can be wrapped around specific divs or entire template sections as needed.

Template-specific code tweaks like removing container elements, modifying gap utilities, and adding negative margins can also fix whitespace issues. The Craft CMS control panel displays margins, making it easy to visually adjust spacing.

With a precision approach, developers can dial in white space control for unique templates without affecting site-wide styling. Both frontend and backend adjustments should be leveraged.

Fixing Common HTML Elements

A few ubiquitous HTML elements are particularly prone to introducing excess whitespace from browser default styling - namely <div>, <section>, <p>, <h1-6> and more. Resetting whitespace for these common tags is key.

The <div> tag has no semantic meaning and is purely for styling, so resetting its margin and padding to zero typically won't affect layouts. Similarly, <section> tags are frequently overused, so removing any applied margin-bottom prevents compounding whitespace when sections stack vertically.

Paragraphs <p> receive default browser styling including margin-bottom spacing. Setting p { margin: 0 } is recommended to avoid gaps between paragraphs. The same technique applies to heading tags <h1> through <h6> which carry bottom margins.

Tables <table>, lists <ul>/<ol> and other common tags also gain browser whitespace. A basic CSS reset targeting just these common elements goes a long way towards controlling whitespace in Craft CMS templates.

Testing and Verifying Whitespace Changes

Checking on Different Pages

After making whitespace adjustments in Craft CMS templates, thorough testing across all site pages is crucial to catch any unintended side effects. Both global CSS changes and template-specific tweaks should be validated across pages.

For global whitespace fixes, browse through the entire frontend site checking for alignment issues, clipped content, or other layout quirks the changes may have introduced. Scroll through home, landing, blog, contact pages etc. Both visual inspection and DevTools analysis should be leveraged to verify nothing broke.

For template-specific fixes, open every page using that template to ensure the whitespace adjustments didn't negatively impact other styles. For example, if sidebar padding was reduced on the blog template, check it on blog category, tag, and author pages.

Ideally, carry out automated checks using QA tools like Selenium to quickly validate whitespace across many site pages. Automated testing quickly identifies the breadth of impact global and specific fixes may have.

With rigorous multi-page testing, developers can feel confident that whitespace fixes for Craft CMS won’t lead to unintended consequences on other parts of the site. Both manual reviews and automated checks should validate changes.

Testing Across Devices

Craft's flexible templating lends itself well to responsive design, so it's crucial to test whitespace fixes across multiple devices and viewports. Changes that appear fine on desktop may break responsive layouts on mobile.

Use Chrome DevTools to toggle between device sizes and orientations, ensuring whitespace adjustments hold up. Common breakpoints like mobile portrait/landscape, tablet and desktop should be checked. Verify content reflows appropriately without misalignment.

In addition to emulated mobile testing, preview on actual devices like iPhones, Android phones, and tablets to test real-world results. Resizing the browser window alone doesn't catch all responsive issues. Whitespace changes should display correctly across all physical screen sizes.

To quickly test across many devices at once, utilize responsive viewers on sites like BrowserStack. These tools preview how whitespace fixes render on countless device and browser combinations, flagging any issues.

By thoroughly vetting changes across viewports and devices, developers can feel assured whitespace management won’t compromise responsive design for Craft CMS sites. Both emulated and real-world mobile tests give full coverage.

Addressing Any New Issues

It’s common for whitespace adjustments to necessitate further tweaks once tested, as fixes impact other styles in unforeseen ways. Developers should iterate on changes while resolving any new layout issues that crop up.

For example, resetting page-level padding may unexpectedly clip a fixed header. Tweaking the header positioning would address this while retaining the whitespace fixes. Similarly, reducing text spacing may break responsive wrapping on mobile. Increasing font scaling for mobile could compensate.

Continuously re-testing whitespace changes allows developers to catch every subtle knock-on effect. Leverage DevTools to toggle CSS rules and isolate any problem adjustments. It’s expected that multiple rounds of refinement will be needed to remove all excess whitespace without impacting Craft CMS templates.

With diligent testing and incremental iterations, developers can dial in whitespace adjustments that completely resolve the original issues without introducing any new layout quirks. A willingness to refine CSS and templates is key for clean results.

Shape April 2022 HR 202
Andy Golpys
- Author

Andy has scaled multiple businesses and is a big believer in Craft CMS as a tool that benefits both Designer, Developer and Client. 

Share
Feedback
Show us some love
Email Us
We usually reply within 72 hours
Agency Directory
Submit your agency
Affiliate Partners
Let's chat