css flex grid masonry layout

Want to create a flexbox based masonry grid layout using CSS? Solution: See this CSS Flex Grid Masonry Layout With Dynamic Image Add/Remove Feature.

Previously I have shared a masonry image grid, but this is with dynamic image add and remove features. Basically, Masonry is a grid layout based on columns. Unlike other grid layouts, it doesn’t have a fixed height of rows. Masonry layout optimizes the use of space inside the web page by reducing any unnecessary gaps. Without this type of layout, certain restrictions are required to maintain the structure of the layout. I am sure that you have seen this type of layout on many websites.

Today you will learn to create a dynamic image’s layout with add/remove image features. Basically, there is 6 images grid at first, Each column has 2 images one small and one big in height. At the bottom, there are 2 buttons for add and remove columns. When you will click on the add button then image columns will insert dynamically and if you click on the remove button then the grid column will disappear one by one.

So, Today I am sharing CSS Flex Grid Masonry Layout With Dynamic Image Add/Remove Feature. The grid layout completely based on CSS flexbox but the image insert and delete feature is based on JS. There is no library or frameworks like jQuery or anything else. You can use this on any image sharing website or on the portfolio section.

If you are thinking now how this flexbox image grids actually are, then see the preview given below.

Preview Of Dynamic Masonry Image Grids

See this video preview to getting an idea of how this design looks like.

Live Demo

Now you can see this visually, also you can see it live by pressing the button given above. If you like this, then get the source code of its.

You May Also Like:

CSS Flex Grid Masonry Layout Source Code

Before sharing source code, let’s talk about it. First I have the main div and put another div, 6 images, and 2 buttons inside the div. At first, I have manually placed 6 images using HTML <img src=""> tag, later JavaScript will add more images dynamically on the action. And Inside the button, I have placed simple plus and minus text.

Now using CSS I have placed all the elements in the right place, as you can see in the preview. I gave 100% width and height to the main div and used CSS display: flex; command to creating the grids. Using CSS I have done many things like margin, padding, position, border, etc. Also using CSS :nth-child() property I gave heights to all the images randomly.

JavaScript is here only to remove and adding images. All the images are powered by unsplash.com and they provide random images in their service. Random images come with no like 1,2,3, etc after the slash, we have to put a number with an increment for getting more new random images. And there I have created a function for adding image on click and remove the image on the minus button click.

Left all other things you will understand after getting the codes, I can’t explain all in writing. For creating this program you have to create 3 files. First file for HTML, second for CSS, and the third file for JavaScript. Follow the steps to creating this without any error.

index.html

Create an HTML file named ‘index.html‘ and put these codes given below.

style.css

Now create a CSS file named ‘style.css‘ and put these codes given here.

function.js

The final step, create a JavaScript file named ‘function.js‘ and put the codes.

That’s It. Now you have successfully created CSS Flex Grid Masonry Layout With Dynamic Image Add/Remove Feature. If you have any doubt or question comment down below.

Thanks For Visiting, Keep Visiting.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here