javascriptjquerylayoutgridjquery-masonry

Always align left in Masonry


I'm using the Masonry jQuery plugin to align a grid of items.

Here's what I have:

result

Expected behavior:

expected behavior

How can I achieve this effect? I've tried various options but nothing works.

Here's my HTML for each box:

<div class="sa-visual-grid-item" id="sa-visual-grid-item-<?=$id?>">
    <div class="sa-selected-box"></div>
    <input type="hidden" name="selected[<?=$id?>]" value="1" />
    <img class="sa-img" src="<?=$img_url?>" />
    <div class="sa-desc">
        <div class="sa-name"><?=$name?></div>
        <div class="sa-price"><?=$price?></div>
    </div>
</div>

And CSS:

.sa-visual-grid {
    height: auto;
    margin: 0 auto;
    text-align: center;
}
.sa-visual-grid-item {
    background: white;
    float: left;
    width: 250px;
    padding: 15px;
    margin: 10px 15px;
    border: 1px solid #bbb;
    box-shadow: 0px 5px 15px 0px #efefef;
    cursor:pointer;
    text-align: center;
}
.sa-selected-box {
    display: none;
    position:absolute;
    z-index:100;
    border: 8px solid #00aa00;
    width:254px;
    padding:15px;
    margin-top:-25px;
    margin-left:-25px;
}

Solution

  • Old post, I know, but this might be helpful for others:

    $('.grid').masonry({
        horizontalOrder: true
    });
    

    After adding this to my JS file, the grid-items were all left justified.

    Example:

    $('.grid').masonry({
      itemSelector: '.grid-item',
      columnWidth: 160,
      horizontalOrder: true
    });
    * { box-sizing: border-box; }
    
    body { font-family: sans-serif; }
    
    /* ---- grid ---- */
    
    .grid {
      background: #EEE;
      max-width: 1200px;
      counter-reset: grid-item;
    }
    
    /* clearfix */
    .grid:after {
      content: '';
      display: block;
      clear: both;
    }
    
    /* ---- grid-item ---- */
    
    .grid-item {
      width: 160px;
      height: 120px;
      float: left;
      background: #D26;
      border: 2px solid #333;
      border-color: hsla(0, 0%, 0%, 0.5);
      border-radius: 5px;
    }
    
    .grid-item--width2 { width: 320px; }
    .grid-item--width3 { width: 480px; }
    .grid-item--width4 { width: 720px; }
    
    .grid-item--height2 { height: 200px; }
    .grid-item--height3 { height: 260px; }
    .grid-item--height4 { height: 360px; }
    
    .grid-item:before {
      counter-increment: grid-item;
      content: counter(grid-item);
      display: block;
      color: white;
      padding-top: 0.2em;
      text-align: center;
      font-size: 1.4rem;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://unpkg.com/masonry-layout@4/dist/masonry.pkgd.js"></script>
    
    <h1>Masonry - horizontalOrder</h1>
    
    <div class="grid">
      <div class="grid-item grid-item--height2"></div>
      <div class="grid-item grid-item--height3"></div>
      <div class="grid-item"></div>
      <div class="grid-item grid-item--height2"></div>
      <div class="grid-item"></div>
      <div class="grid-item grid-item--height3"></div>
      <div class="grid-item grid-item--height2"></div>
      <div class="grid-item"></div>
      <div class="grid-item grid-item--height2"></div>
      <div class="grid-item grid-item--height3"></div>
      <div class="grid-item"></div>
      <div class="grid-item"></div>
      <div class="grid-item grid-item--height2"></div>
      <div class="grid-item"></div>
      <div class="grid-item"></div>
      <div class="grid-item grid-item--height2"></div>
      <div class="grid-item"></div>
      <div class="grid-item"></div>
      <div class="grid-item grid-item--height2"></div>
      <div class="grid-item"></div>
      <div class="grid-item"></div>
    </div>