Custom font causing vertical gaps in masonry grid layout plugin

My front page layout is arranged using the "Masonry" jQuery plugin, with each div containing an upper image div and a lower text div. The plugin is initialized like this:

function use_masonry() {
        var container = document.querySelector('#container');
        var msnry = new Masonry(container, {
            transitionDuration: '0.2s',
            isResizeBound: false,
            gutter: 10,
            gutterY: 0,
            itemSelector: '.divs_to_be_arranged'
        });
}

EDIT: This function is called as follows:

//called when page loaded/reloaded
$("img").load(function() {
    use_masonry();
});

$(window).resize(function() {
    use_masonry();
});

Custom font CSS implementation:

@font-face {
  font-family: 'leaguegothic-regular';
  src: url('fonts/leaguegothic-regular-webfont.eot'); /* IE9 Compat */
  src: url('fonts/leaguegothic-regular-webfont.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
       url('fonts/leaguegothic-regular-webfont.woff') format('woff'), /* Modern Browsers */
       url('fonts/leaguegothic-regular-webfont.ttf')  format('truetype'), /* Safari, Android, iOS */
       url('fonts/leaguegothic-regular-webfont.svg#svgFontName') format('svg'); /* Legacy iOS */
} 

Sometimes, on initial load (after cache clear), the vertical gutters in the grid generated by Masonry are inconsistent. However, this issue is resolved upon window resize or reload. It appears that the problem is related to the custom font usage.

Any suggestions on what might be causing this and how to fix it?

Answer №1

If your custom font is wider and/or taller than the standard font, it can cause issues with Masonry's calculations for box width/height. This happens because Masonry calculates dimensions before your font is fully downloaded/applied.

To resolve this, you can bind your use_masonry function to the document load event instead of the ready event. This will ensure that Masonry works correctly with your custom font. Here is an example using jQuery:

$(document).on('load',function(){
    use_masonry();
});

You can also use vanilla JavaScript like this:

document.onload = use_masonry;

Alternatively, you can utilize the waitForWebfonts() function and include use_masonry() inside it. There are other methods mentioned in the same question, such as WebFont Loader, for confirming when the font has been successfully loaded.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The Organization of Tabs in the AngularJS ui-select Component

As a newcomer to angularjs, I am currently working on a web application using the ui-select library for select2-style dropdowns. While developing my forms with appropriate tabindex values, I noticed a user experience issue with tab ordering. When tabbing ...

Modify section background color for every iteration in an image carousel

Is it possible to dynamically change the background color of the cd-hero section each time a new image is loaded in a simple slider on the home page? Can this be achieved by storing predefined colors in an array so that different images trigger different b ...

Enhance User Experience: Revise Custom Fields Post Checkout Validation Error in WooCommerce

For my current project, I am making some customizations to the WooCommerce features. Specifically, I have set up two "shipping methods": 1. delivery 2. take away In addition, I have included a custom field on the checkout page which is a <select& ...

Attempting to retrieve an image from a specified div ID within Android Studio

I find myself at a loss for where to even begin with this issue, so I've been stumbling through it. While I've come across some similar posts, I haven't had any success in making them work. My goal is to utilize the div id to retrieve a spe ...

The anchor tag's href attribute isn't working properly when using a single slash, but it works

I'm encountering an issue where my absolute URL is not clickable in an anchor tag as shown below: <a href="/account/login"><Login</a> However, it works with double slashes: <a href="//account/login"><Login</a> B ...

Why is it not possible for me to change the font color to white?

To modify the font of the accordion heading to white, please check out the screenshot provided below. While the code functions properly in "fiddle", it seems that when I inserted the CSS snippet into a SharePoint 2013 page, the font color does not display ...

Exploring the Contents of an ASP ListView

I am attempting to align the odd-numbered items to the left using float:left, and the even-numbered items to the right using float:right. However, it seems like every item is being considered as odd AND element 1 regardless of the actual order in the list. ...

Importing Data from Wordpress JSON Feed to Create a Table

Struggling with organizing my data into a table, as each row is currently generating a new table. Utilizing the JSON Content Importer with the following code: ...

When using JavaScript, an error may occur that says "Cannot read property 'style' of null," but strangely, this issue disappears when the code is copied and

I seem to be encountering an error in my codepen and I can't figure out what I'm doing wrong. It appears that I may have misplaced something, but I would greatly appreciate any help or explanation of my mistake. function on() { document ...

Having trouble parsing JSON with Ajax in Pusher using PHP?

I am facing an issue while sending multiple parameters using the Pusher AJAX PHP library. This is the error I encounter: SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data Here is my PHP and JS code: <script src="https: ...

Endlessly triggering document.execCommand, the JavaScript selectionchange-EventListener seems to have a mind of

I recently implemented an event listener for selectionchange in the following manner: document.addEventListener("selectionchange", function() { highlight(); console.log("selectionchange-triggered"); }, false); After that, I included the code bel ...

IE8 not displaying background-image in Zen Subtheme of Drupal 7

I've been struggling to get my website to display properly across all browsers. Everything looks great except for IE8 and below. I'm not sure if it's a problem with Drupal, Zen, IE, or CSS (although the CSS code seems correct). Here is the C ...

Tips for applying a jQuery class when the page is both scrolled and clicked

As I work on building a HTML website, I encountered an interesting challenge. I want to create a dynamic feature where, as users scroll through the page, certain sections are highlighted in the navigation menu based on their view. While I have managed to a ...

`The dilemma of z-index in a dropdown menu nested within an animated card`

Having an issue that I have attempted to simplify in this StackBlitz example (the actual project is created with Angular components and Bootstrap, etc.): https://stackblitz.com/edit/angular-bootstrap-4-starter-njixcw When incorporating a dropdown within ...

The div just won't stay the size I want, no matter what

Here is a snippet of HTML code: <div class="pointwrap"> <div class="imgwrap"> <img src="howtoplay1.jpg" height="101" width="177"/> </div> <div class="textwrap"> Tap Anywhere in the Drop zone to release a ball. ...

What is the correct way to properly wrap the div component in this code snippet?

I am currently working on implementing a Javascript component that displays pinned locations and related information on a map. I have made some progress with the implementation, but unfortunately, it does not appear correctly in the actual site. There is a ...

Revamp selected bootstrap attribute

Imagine a scenario where there is: @media (min-width: 576px) { #projects-edit-middle-column .equal-columns-wrapper .equal-columns { width: 50%; padding-right: 25px; } } This is being utilized within a container: <div class="equal-columns ...

Utilizing the Colon in URL parameter when making a jQuery AJAX request

Hey there, I'm currently working on fetching data from Envato using jQuery.ajax(). However, I'm encountering a problem because the URL parameter contains a colon, which the system does not accept. $('button').click(function(){ var ...

Arranging a variety of sophisticated charts side by side using Bokeh

Is there a way to display two or more Bar charts together in a single html file using Bokeh? I have found examples of laying out figures like triangle, square from bokeh.plotting together on this page: However, I have not been able to find any examples s ...

Unable to use jQuery to delete class from an element

Here is the markup I am working with: <div class="row"> <img src="image.png" class="download-image regular-image" /> <div class="options"> <p>download</p> </div> <img src="image.png" class="download-image r ...