0

Im using Masanory to set an image grid. All my images have equal height but different widths. I need it to look like this : http://www.dreamstime.com/new-stock-photos-images . In there all the images have the same height and different widths but the two sides are in a straight line. How can I do this?

Here's my html :

<div id="photos">
  <img class="item" src="../photo-164660.jpg" alt="Thumb 67556492 photo 164660">
  <img class="item" src="../photo-165523.jpg" alt="Thumb 82982186 photo 165523">
  <img class="item" src="../photo-65322.jpg" alt="Thumb 37340524 photo 65322">
  <img class="item" src="../photo-141502.jpg" alt="Thumb 85132138 photo 141502">
</div>

The javascript :

  $(document).ready(function() {

      var container = document.querySelector('#photos');
      var msnry;
      // initialize Masonry after all images have loaded
      imagesLoaded(container, function() {
          msnry = new Masonry(container, {
              // options
              columnWidth: 10,
              resizable: true,
              itemSelector: '.item',
              layoutMode: 'fitRows'


          });
      });
  });

Here's what I get : enter image description here

3 Answers 3

2

This may not have existed when you asked your question, but I just achieved very nice results using https://github.com/naturalatlas/image-layout and its "Fixed Partition" solution. It works a lot like Masonry, gives you x, y, width, height values for each element in your grid. I used the calculation library layouts/fixed-partition.js and then set the position of my elements myself using the returned data.

1
  • 1
    Should be selected as the answer; this library is still exactly what I need even after almost 8 years.
    – katerlouis
    Dec 27, 2022 at 2:53
-1

You need to set image width, masonry doesn't resize your photos, it just reorganise them inside the container.

For example you can use percents, here's working pen

.item {
  width: 33.3%;
}

You can check other examples on masonry website

2
  • That made the images load in a vertical line one below the other
    – THpubs
    May 10, 2015 at 12:15
  • my pen works well with this css, so if you don't attache yours i can't help you May 10, 2015 at 15:00
-1

I would better use Bootstrap to solve this kind of problem. But i think here is a solution without it.

display: -webkit-flex; /* Safari */
-webkit-justify-content: space-around; /* Safari 6.1+ */
display: flex;
justify-content: space-around;
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.