Code Monkey home page Code Monkey logo

masonry-css-js's Introduction

problem image

CSS column-count for masonry-style layouts is dead easy (couple lines of CSS and very minimal markup to get it going).

The Problem

The problem (sometimes) is the way the content flows (see the diagram above). Typically, I want a left-to-right scan reading experience, i.e. cards of blog post summaries with the most recent posts to appear at the top of the view. I also don't want to use JS for the layout. Pure CSS... all the way.

The Solution

The only "help" that JS provides is re-ordering the array before rendering to the view so that we can use CSS column-count for the layout. Again, for example, this array could be a feed of recent news posts.

Check the demo: here

Check out the code: react component

Post on Medium: Easy CSS Masonry Layout w/ Left-To-Right Content Flow

Special thanks to my Klue teammate @nickb for figuring out the array re-order (and in a way even I could understand).


This project was bootstrapped with Create React App.

Here's the guide here.

masonry-css-js's People

Contributors

dev7ch avatar jessekorzan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

masonry-css-js's Issues

Unit Tests

Hey! This is really neat-- I ran into the same issue and used the snippet of your code to layout the items in my CSS Grid properly.

I also wrote some unit tests, if you'd like to use them.

describe('reorder', function () {
    it('should reorder successfully when rows are divisible', function () {
      const initialArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
      const endArray = [0, 3, 6, 9, 1, 4, 7, 10, 2, 5, 8, 11];
      expect(reorder(initialArray, 3)).to.have.ordered.members(endArray);
    });
    it('should reorder successfully when rows are not evenly divisible', function () {
      const initialArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
      const endArray = [0, 3, 6, 9, 1, 4, 7, 10, 2, 5, 8];
      expect(reorder(initialArray, 3)).to.have.ordered.members(endArray);
    });
    it('should return the same array if num columns are less than array length', function () {
      const initialArray = [0, 1, 2, 3, 4];
      const endArray = [0, 1, 2, 3, 4];
      expect(reorder(initialArray, 5)).to.have.ordered.members(endArray);
    });
  });

Improve the ordering code

The order will break if more items added to array.
You could add this functionality, it's very convenient for lazyload (load more).

Adding additional items to the layout ruins the order

When using, for example, a "show more" button at the bottom of the masonry layout, and then AJAX loading additional items in, the original items all get shuffled out of place.

Perhaps this is a limitation of using the multicolumn layout? A possible work-around might be to reorder all images together, then remove and re-add all items? That seems very inefficient and could cause paint/resource issues though. Any thoughts?

Version, no react

Hello, you have a version that is not react, in addition to only having 1 js file, 1 css, 1 html, with that it would be easier to understand, I await your prompt response.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.