Open In App

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

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:

 



Different Challenges while Implementing & Handling the masonry issue:

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

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

<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>
<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. 




<!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:

 


Article Tags :