Open In App

How to Add JavaScript Code in Website to Handle Masonary Issue ?

Last Updated : 19 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

On many websites, we have seen image galleries or portfolios aligned with different-sized images that have different heights & widths with equal spacing between them. This is made so with the help of different sized grid-layout which help to create an interactive website with a better user experience. This can be achieved with the help of the Masonry layout concept in web development. Masonry is a layout design pattern used in web design to create a grid layout with columns of varying heights. When using Masonry with JS scripts, it can result in layout misalignment or overlapping elements. Unlike in traditional grid layout design where every row & column are arranged with a fixed height and width, the Masonry layout facilitates positioning of the elements depending upon the size and available space. This, in turn, makes a more dynamic and visually interesting layout that adapts to different screen sizes and device types. However, most of the time, these elements need to be customized & implementing a masonry layout(based on different layouts for the different screen sizes and device types, in order to adapt it) on a website can be challenging, particularly when dealing with elements of varying heights. For this, we need to use the Javascript concepts that help to manipulate & handle it accordingly.

In this article, we will see how to include the Javascript that handles the masonry layout issues, along with understanding it through the basic example.

There are different Fields where the Masonry layout can be implemented. Some of the use cases are described below:

  • Portfolio or gallery websites: Websites that display visual content such as art galleries, photography portfolios, and design portfolios require a visually appealing and organized Masonry layout to showcase their work. Using a Masonry library or CSS flexbox/grid can help achieve this layout.
  • E-commerce websites: Online stores benefit from displaying product images or videos in a Masonry layout that is visually pleasing and easy to browse. A Masonry layout can also help to display products of varying sizes and dimensions in a neat and organized way.
  • News or blog websites: Websites that publish articles or blog posts can benefit from a Masonry layout to display featured images or thumbnails of the articles in a grid-like fashion. This layout helps readers scan through the articles and choose which ones to read.
  • Event websites: Websites that promote events, such as concerts or festivals, often require a Masonry layout to display event posters or images in a visually appealing and organized way. This layout can help attract visitors and increase ticket sales.

 

Different Challenges while Implementing & Handling the masonry issue:

  • One of the most significant challenges of implementing a masonry layout is aligning the elements correctly. When elements of different heights are arranged in a grid, there is no natural alignment, which can result in an uneven layout. This issue can be particularly problematic when using images of varying sizes, as some images may appear much larger than others.
  • Another issue with masonry layouts is that they can be challenging to implement in a responsive design. When the layout needs to adapt to different screen sizes, the alignment of the elements can become even more challenging to manage.
  • To handle these issues, several strategies can be employed when implementing a masonry layout on a website. One approach is to use a pre-built library that handles the layout for you. Libraries like Masonry.js and Isotope.js can take care of the alignment and resizing of the elements, making it much easier to implement a masonry layout on your website.
  • Another approach is to use CSS flexbox or grid to create the masonry layout. By setting the container’s display property to flex or grid, you can align the elements in a row or column and set their height based on their content. Using this approach can give you more control over the layout, but it may require more work to implement.
  • When dealing with images in a masonry layout, it is essential to use images with fixed dimensions. This way, you can set the height of each element to be the same, and the images will automatically scale to fit. Additionally, setting a maximum height for each element can also help to ensure that the layout remains balanced and aligned.
  • Finally, it is essential to consider the performance of your website when implementing a masonry layout. Large images can slow down your website, so you may need to use lazy loading to only load images when the user scrolls to them.
  • Use a Masonry library: There are several Masonry libraries available that can help you easily implement the layout pattern without having to worry about the underlying JS code. Some popular libraries include Masonry.js, Isotope, etc.

Approach: The following approach will be used to handle the masonry issue:

  • Include the Masonry library in your HTML file using the <script> tag:

<script src=
“https://cdnjs.cloudflare.com/ajax/libs/masonry/4.2.2/masonry.pkgd.min.js”></script>

  • Create a container element for your grid layout:
<div class="grid">
    <div class="grid-item">Item 1</div>
    <div class="grid-item">Item 2</div>
    <div class="grid-item">Item 3</div>
    ...
</div>
  • Initialize the Masonry layout using JavaScript:
<script>
    var grid = document.querySelector('.grid');
    var masonry = new Masonry(grid, {
        // Options go here
    });
</script>

Example: This example describes adding the JavaScript that handles the masonry layout issues by creating the Masonry constructor that is used to initialize the layout. 

HTML




<!DOCTYPE html>
<html>
  
<head>
    <title>
        Adding the Javascript that handles
        the masonry layout issues
    </title>
    <style>
        h1 {
            color: green;
        }
  
        .grid {
            display: grid;
            grid-gap: 20px;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
        }
  
        .grid-item {
            background-color: #eee;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.2);
        }
    </style>
    <script src=
      </script>
</head>
  
<body>
    <h1>GeeksforGeeks</h1>
    <h3>
        Adding the Javascript that handles
        the masonry layout issues
    </h3>
    <div class="grid">
        <div class="grid-item">Item 1</div>
        <div class="grid-item">Item 2</div>
        <div class="grid-item">Item 3</div>
        <div class="grid-item">Item 4</div>
        <div class="grid-item">Item 5</div>
        <div class="grid-item">Item 6</div>
        <div class="grid-item">Item 7</div>
        <div class="grid-item">Item 8</div>
        <div class="grid-item">Item 9</div>
        <div class="grid-item">Item 10</div>
        <div class="grid-item">Item 11</div>
        <div class="grid-item">Item 12</div>
    </div>
    <script>
        
        // Initialize Masonry layout
        var grid = document.querySelector('.grid');
        var masonry = new Masonry(grid, {
            itemSelector: '.grid-item',
            columnWidth: '.grid-item',
            gutter: 20
        });
    </script>
</body>
  
</html>


Output:

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads