Book Image

Instant jQuery Masonry How-to

By : Kyle David Taylor
Book Image

Instant jQuery Masonry How-to

By: Kyle David Taylor

Overview of this book

jQuery Masonry is a revolutionary new plugin that enables dynamic layouts on any website. This powerful tool shuffles and rearranges elements to fit any screen size which will ensure your website keeps up with one of the biggest trends of the year, Responsive Web Design.Instant jQuery Masonry How-to will allow you to start building fully responsive layouts for portfolios, galleries, blog pages, and more. Gain a complete understanding of the workings of Masonry, which will allow you to integrate this unique tool into your existing and future projects.The hands-on guide will take you from the basics of jQuery Masonry to utilizing its power within Wordpress and Drupal. You will learn the basics such as implementing single and multi-column layouts, before developing more complex solutions for various media types, fluid layouts, and animating transitions. With the widespread adoption of responsive web design, this book will bring your blog, portfolio, or any project you are working on up to speed.
Table of Contents (7 chapters)

Aligning items from right to left (Advanced)


For some Middle Eastern languages such as Hebrew and Arabic, script is primarily written from right to left. The W3C have written a standard that defines the text direction through attributes and CSS properties, but it is hard to make sense of these when the text direction is formatted from left to right. Masonry has a built-in option for such situations.

Getting ready

In the code bundle for this book, open the 5-rtl.html file in your preferred text editor to follow along.

How to do it...

  1. We will start off with the existing CSS properties that we have already defined for the Masonry elements. In this recipe, we have the choice of using either the dir="rtl" attribute on the Masonry container, or of defining the text direction in CSS. I think CSS is easier, so let us define it here.

    <style>
       .masonry-item {
          background: #FFA500;
          direction: rtl;
          float: left;
          margin: 5px;
          padding: 5px;
          width: 130px;
       }
    </style>
  2. Build the standard Masonry HTML structure and add as many Masonry items as necessary.

    <div>
       <button id="add-html">Add HTML</button>
    </div>
    <div id='masonry-container'>
       <div class='masonry-item '>
          Maecenas faucibus mollis interdum.
       </div>
       <div class='masonry-item '>
          Maecenas faucibus mollis interdum. Donec sed odio dui. Nullam quis risus eget urna mollis ornare vel eu leo. Vestibulum id ligula porta felis euismod semper.
       </div>
       <div class='masonry-item '>
          Nullam quis risus eget urna mollis ornare vel eu leo. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum.
       </div>
    </div>
  3. Using the standard Masonry methods, insert the following script; make sure to set the isRTL option to true:

    <script>
       $(function(){
    
          $('#masonry-container').masonry({
             itemSelector : '.masonry-item',
             isRTL: true
          });
    
       });
    </script>

How it works...

This is a fairly simple process. By default, when Masonry is setting the positions of items in the layout, it always starts from the left. For example, the inline style will look like this: style="position: absolute; top: 0px; left: 300px;". When isRTL is set to true, the positioning starts from the right instead. This is a very simple technique, yet very effective. The result should look like the following screenshot: