Setting up a single-width column system (Simple)

3 min read

(For more resources related to this topic, see here.)

Getting ready

To perform the steps listed in this article, we will need a text editor, a browser, and a copy of the Masonry plugin. Any text editor will do, but my browser of choice is Google Chrome, as the V8 JavaScript engine that ships with it generally performs better and supports CSS3 transitions, and as a result we see smoother animations when resizing the browser window. We need to make sure we have a copy of the most recent version of Masonry, which was Version 2.1.08 at the time of writing this article. This version is compatible with the most recent version of jQuery, which is Version 1.9.1. A production copy of Masonry can be found on the GitHub repository at the following address:

https://github.com/desandro/masonry/blob/master/jquery.masonry.min.js

For jQuery, we will be using a content delivery network (CDN) for ease of development. Open the basic single-column HTML file to follow along. You can download this file from the following location:

http://www.packtpub.com/sites/default/files/downloads/1-single-column.zip

How to do it…

  1. Set up the styling for the masonry-item class with the proper width, padding, and margins. We want our items to have a total width of 200 pixels, including the padding and margins.

    <style>
    .masonry-item {
    background: #FFA500;
    float: left;
    margin: 5px;
    padding: 5px;
    width: 180px;
    }
    </style>

  2. Set up the HTML structure on which you are going to use Masonry. At a minimum, we need a tagged Masonry container with the elements inside tagged as Masonry items.

    <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. All Masonry options need not be included, but it is recommended (by David DeSandro, the creator of Masonry) to set itemSelector for single-column usage. We will be setting this every time we use Masonry.

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

How it works…

Using jQuery, we select our Masonry container and use the itemSelector option to select the elements that will be affected by Masonry. The size of the columns will be determined by the CSS code.

Using the box model, we set our Masonry items to a width of 90 px (80-px wide, with a 5-px padding all around the item). The margin is our gutter between elements, which is also 5-px wide. With this setup, we can con firm that we have built the basic single-column grid system, with each column being 100-px wide. The end result should look like the following screenshot:

Summary

This article showed you how to set up the very basic Masonry single-width column system around which Masonry revolves.

Resources for Article :


Further resources on this subject:


Packt

Share
Published by
Packt

Recent Posts

Top life hacks for prepping for your IT certification exam

I remember deciding to pursue my first IT certification, the CompTIA A+. I had signed…

3 years ago

Learn Transformers for Natural Language Processing with Denis Rothman

Key takeaways The transformer architecture has proved to be revolutionary in outperforming the classical RNN…

3 years ago

Learning Essential Linux Commands for Navigating the Shell Effectively

Once we learn how to deploy an Ubuntu server, how to manage users, and how…

3 years ago

Clean Coding in Python with Mariano Anaya

Key-takeaways:   Clean code isn’t just a nice thing to have or a luxury in software projects; it's a necessity. If we…

3 years ago

Exploring Forms in Angular – types, benefits and differences   

While developing a web application, or setting dynamic pages and meta tags we need to deal with…

3 years ago

Gain Practical Expertise with the Latest Edition of Software Architecture with C# 9 and .NET 5

Software architecture is one of the most discussed topics in the software industry today, and…

3 years ago