Masonry is not compatible with Bootstrap 3

Can someone help me figure out how to make Masonry work with my Bootstrap grid columns?

Step 1 - First, I included the Masonry script in my <head> like this :

<script src="http://cdnjs.cloudflare.com/ajax/libs/masonry/3.1.5/masonry.pkgd.min.js"></script>

Step 2 - Then, I structured my bootstrap html that I want to apply Masonry on :

<div class="row" id="container"> <!-- container starts -->

            <div class="col-lg-3 col-md-6  item"> <!-- 1st column begins -->
                     
                      <div class="well clearfix">

                <section>        
                             <img src="http://placehold.it/410x130" class="img-responsive visible-lg" alt="" style=" margin-bottom: 15px;">              
                              
                             <h2 id="post">This is my wordpress post title</h2>
                       
                        <div class="post-label">
                             <span class="label label-default">Aymen</span>
                             <span class="label label-default">Web Design</span>
                             <span class="label label-default">12-10-2014</span>
                             <span class="label label-default">Comments 24</span>
                        </div>
                    <hr>
                              <p id="post">
                        The Bootstrap 3 grid system has four tiers of classes: xs (phones), sm (tablets), md (desktops), and lg (larger desktops). You can use nearly any combination of these classes to create more dynamic and flexible layouts.
                              </p>
                    <hr>
                             <a href="#"><div class="more">Read More</div></a>
                             <a href="#"><div class="share">Share me</div></a>
                </section>
                      </div> 
            </div> <!-- 1st column ends -->


            <div class="col-lg-3 col-md-6  item"> <!-- 2nd column begins -->
                     
                      <div class="well clearfix">

                <section>        
                             <img src="http://placehold.it/410x130" class="img-responsive visible-lg" alt="" style=" margin-bottom: 15px;">              
                              
                             <h2 id="post">This is my wordpress post title</h2>
                       
                        <div class="post-label">
                             <span class="label label-default">Aymen</span>
                             <span class="label label-default">Web Design</span>
                             <span class="label label-default">12-10-2014</span>
                             <span class="label label-default">Comments 24</span>
                        </div>
                    <hr>
                              <p id="post">
                        The Bootstrap 3 grid system has four tiers of classes: xs (phones), sm (tablets), md (desktops), and lg (larger desktops). You can use nearly any combination of these classes to create more dynamic and flexible layouts.
                              </p>
                    <hr>
                             <a href="#"><div class="more">Read More</div></a>
                             <a href="#"><div class="share">Share me</div></a>
                </section>
                      </div> 
            </div> <!-- 2nd column ends -->

                        <div class="col-lg-3 col-md-6  item"> <!-- 3rd column begins -->
                     
                      <div class="well clearfix">

                <section>        
                             <img src="http://placehold.it/410x130" class="img-responsive visible-lg" alt="" style=" margin-bottom: 15px;">              
                              
                             <h2 id="post">This is my wordpress post title</h2>
                       
                        <div class="post-label">
                             <span class="label label-default">Aymen</span>
                             <span class="label label-default">Web Design</span>
                             <span class="label label-default">12-10-2014</span>
                             <span class="label label-default">Comments 24</span>
                        </div>
                    <hr>
                              <p id="post">
                        The Bootstrap 3 grid system has four tiers of classes: xs (phones)...

Step 3 - Next, I inserted the following javascript code before </body> :

var container = document.querySelector('#container');
var msnry = new Masonry( container, {
  // options
  columnWidth: 200,
  itemSelector: '.item'
});

Step 4 - Finally, I added the .item class to col-lg-3 col-md-6 as shown in Step 2

However, the columns are not aligning properly as expected. Any advice on what might be causing this issue?

Answer №1

While your current setup is functional, I suggest a more dynamic approach especially when utilizing Bootstrap. Instead of assigning fixed widths to columns, consider the following adjustment:

var container = document.querySelector('#container');
var msnry = new Masonry( container, {
  // options
  columnWidth: '.item',
  itemSelector: '.item'

This code snippet will utilize Bootstrap's responsive percentages for column widths (e.g. 33.33333% for col-md-4) instead of specifying exact pixel values.

To ensure clarity and proper functionality, your HTML structure should resemble the following:

<div id="container">
  <div class="row">
    <div class="col-md-3 item">
      Some content
    </div>
    <!--- Add additional columns as needed --->
  </div>
</div>

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

Using the length of an array as an iterator within a nested ngFor loop in Angular 9

I am looping through an array of objects where each object contains another array of objects with attributes like "name" and "id". The length of this array of objects (noticias) varies. I am struggling to display these values and have only managed to acce ...

Transforming this JavaScript code to be less intrusive by implementing an event listener for an unidentified ID

As I work on enhancing the functionality of this user interface, I've encountered a challenge with two tabs that require a proper event listener to ensure my JavaScript functions smoothly. This obstacle has been hindering my progress, but as I delve i ...

What is the process for updating the Vue template during runtime?

Currently, I am working on a CMS-based Vue page. Within this page, there is a root container that contains two child containers structured as follows: <div id="app"> <div class="above-the-fold">...</div> <di ...

Tips for incorporating animation when opening a Jquery SimpleModal

The SimpleModal website features an animation-enabled example with the following sequence: 1. Display modal only 2. Fade in the overlay The code snippet for this animation is as follows: $("#the-div").modal({ onOpen: function (dialog) { dialog.overl ...

How to disable annoying browser ad extensions using Javascript

Is there a way to prevent browser add-ons from injecting HTML code? My website built in angularjs is experiencing routing issues due to certain browser add-ons. The following HTML snippet is causing errors in my angularjs: <script async="" src="http:/ ...

Ways to determine if the keys of an object are present in an array, filtered by the array key

Working on an Angular 2 Ionic application and I'm wondering if there's a straightforward way to filter individuals by age in a specific array and then verify if any key in another object matches the name of a person in the array, returning a bool ...

Attempting to store a specific h1 text.event as a variable, allowing it to be saved upon input of the initial letter

After typing the second key, you can continue to input more characters as desired. It is possible to customize the text displayed in the h1 using your own name or any other text of your preference without needing a textbox. $(document).keypress(functio ...

Fundamental JavaScript associative object

I am working with an array filled with various items. let items = {'A', 'A', 'A', 'B', 'B', 'C'} My goal is to generate a new array that shows the count of each item present in the original arra ...

Utilizing Reactjs to efficiently transmit name and value to Material UI autocomplete

I am trying to customize the Material UI Autocomplete component. I need to pass name, value and onchange similarly to how it is done for TextField. Can someone please help me achieve this? My current code is not functioning as expected. < ...

Angular Material Popup - Interactive Map from AGM

I am in the process of developing a material dialog to collect user location information using AGM (angular google maps). I have successfully implemented a map on my main page, but when the dialog is opened, it only shows a blank space instead of the map. ...

Retrieving information from Node.js Serialport

I am interested in reading the data received after sending an ascii command to my lock controller. Here is the code that sends the command to the lock controller: var express = require('express'); var router = express.Router(); var SerialPort = ...

Setting values to an array based on the index of a specific name in JavaScript - a guide

How can I set values to an array if it has an index in the name field? Here is the sample data: [ { "column": "created_at[0]", "name": "created_at[0]", "type": "range", ...

Issue with close button in bootstrap laravel alert-dismissible not working properly

Why isn't the notification closing when I click the close button? Am I overlooking something? (Referring to the 'x' symbol at the top-right corner of the alert) https://i.sstatic.net/QUJ9Y.png Below is the specific code for this alert. I ...

Can you explain the purpose of $winstonLoggerConfig<T>() and $winstonLogger<T>() in winston v3?

I'm currently using the winston logger and I want to implement flow typing for it. However, I am unsure of what exactly I should pass to it in order to achieve this. Below is my current logger setup: const logger = createLogger({ ... }); Missing typ ...

Refreshing a Node.js server page upon receiving a JSON update

My web application serves as a monitoring interface for tracking changes in "objects" processed by the computer, specifically when they exceed a certain threshold. The Node Js server is running on the same machine and is responsible for displaying data in ...

Tips for enabling or disabling elements within an array using React JS

I am looking to develop a feature where I can toggle individual boxes on and off by clicking on them. Currently, only one box at a time can be activated (displayed in green), but I want the ability to control each box independently without affecting the ot ...

Developing pagination functionality in ReactJS

I came across this piece of code in a forum (how to implement Pagination in reactJs): constructor() { super(); this.state = { todos: ['a','b','c','d','e','f','g','h ...

External vendor scripts such as JavaScript and jQuery are not functioning properly after being rendered in a ReactJS environment

I am currently developing a frontend web app using ReactJS. I obtained a template from W3layout that is built with HTML5, CSS3, and custom JS files, along with various third-party/vendor plugins like nice-select, bootstrap, slik, owl-carousel, etc. Despit ...

Transferring information from a file to a displayed page in Sails.js

I have a challenge in my application where I need to read a large dataset and send it to the client for manipulation with D3.js. The issue is that loading such big datasets can be time-consuming. My solution is to implement streaming, but I'm not sure ...

Component experiencing issues with service or @Input functionality

I have been struggling with importing a service inside a component and encountering an issue where the Input from the service does not render anything in the template. Let's take a look at my entity: export interface PageState { step: string; } e ...