{"id":6346,"date":"2024-01-11T16:40:00","date_gmt":"2024-01-11T16:40:00","guid":{"rendered":"https:\/\/codehim.com\/?p=6346"},"modified":"2024-01-22T14:44:16","modified_gmt":"2024-01-22T09:44:16","slug":"javascript-masonry-grid-layout","status":"publish","type":"post","link":"https:\/\/codehim.com\/layout\/javascript-masonry-grid-layout\/","title":{"rendered":"JavaScript Masonry Grid Layout"},"content":{"rendered":"<p>This JavaScript code snippet helps you to create a responsive masonry grid layout. It counts items and renders rows and columns dynamically. You can easily integrate this grid layout for image galleries, posts, or content cards.<\/p>\n<h2>How to Create JavaScript Masonry Grid Layout<\/h2>\n<p>1. First of all, create a div element with a class name &#8220;wrapper&#8221; and place div elements with the class name &#8220;item&#8221; inside it. You can specify a different height for each item that will be automatically adjusted in the masonry layout. Inside the item, you can place any content that you want to display as a masonry grid layout.<\/p>\n<pre class=\"prettyprint linenums lang-html\">&lt;div class=\"wrapper\"&gt;\r\n  &lt;div class=\"item\" style=\"height: 90px;\"&gt;1&lt;\/div&gt;\r\n  &lt;div class=\"item\" style=\"height: 200px;\"&gt;2&lt;\/div&gt;\r\n  &lt;div class=\"item\" style=\"height: 80px;\"&gt;3&lt;\/div&gt;\r\n  &lt;div class=\"item\" style=\"height: 120px;\"&gt;4&lt;\/div&gt;\r\n  &lt;div class=\"item\" style=\"height: 290px;\"&gt;5&lt;\/div&gt;\r\n  &lt;div class=\"item\" style=\"height: 200px;\"&gt;6&lt;\/div&gt;\r\n  &lt;div class=\"item\" style=\"height: 250px;\"&gt;7&lt;\/div&gt;\r\n  &lt;div class=\"item\" style=\"height: 70px;\"&gt;8&lt;\/div&gt;\r\n  &lt;div class=\"item\" style=\"height: 70px;\"&gt;9&lt;\/div&gt;\r\n  &lt;div class=\"item\" style=\"height: 50px;\"&gt;10&lt;\/div&gt;\r\n  &lt;div class=\"item\" style=\"height: 120px;\"&gt;11&lt;\/div&gt;\r\n  &lt;div class=\"item\" style=\"height: 260px;\"&gt;12&lt;\/div&gt;\r\n&lt;\/div&gt;\r\n<\/pre>\n<p>2. After that, define the basic CSS styles for the masonry layout as follows:<\/p>\n<pre class=\"prettyprint linenums lang-css\">.wrapper {\r\n  width: 960px;\r\n  margin: 0 auto;\r\n  overflow: hidden;\r\n  padding: 0px 0px 10px 10px;\r\n}\r\n\r\n.wrapper .col { \r\n\tfloat: left;\r\n  width: 25%;\r\n}\r\n\r\n.wrapper .col .item {\r\n   background: #ccc;\r\n   display: block;\r\n   margin: 10px 10px 0px 0px;\r\n   padding: 10px;\r\n}<\/pre>\n<p>3. Finally, add the following JavaScript code and done.<\/p>\n<pre class=\"prettyprint linenums lang-js\">var totalRows = 4,\r\n    itemCol = 0;\r\nfor(var rowCount = 0; rowCount &lt; totalRows; rowCount++){\r\n  newCol = document.createElement('div');\r\n  newCol.className = 'col';\r\n  document.getElementsByClassName('wrapper')[0].appendChild(newCol);\r\n}\r\n\r\nfor(var itemCount = 0; itemCount &lt; document.getElementsByClassName('item').length; itemCount++){\r\n  document.getElementsByClassName('col')[itemCol].appendChild(document.getElementsByClassName('item')[0]);\r\n  if(itemCol &lt; totalRows - 1){\r\n    itemCol++; \r\n  } else {\r\n    itemCol = 0;\r\n  }\r\n}<\/pre>\n<p>That&#8217;s all! hopefully, you have successfully integrated this masonry grid layout code snippet into your project. If you have any questions or facing any issues, feel free to comment below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This JavaScript code snippet helps you to create a responsive masonry grid layout. It counts items and renders rows and&#8230;<\/p>\n","protected":false},"author":1,"featured_media":6358,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[109],"tags":[],"class_list":["post-6346","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-layout"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>JavaScript Masonry Grid Layout &#8212; CodeHim<\/title>\n<meta name=\"description\" content=\"Here is a lightweight JavaScript code snippet to create masonry grid layout. You can view demo and download source code.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/codehim.com\/layout\/javascript-masonry-grid-layout\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript Masonry Grid Layout &#8212; CodeHim\" \/>\n<meta property=\"og:description\" content=\"Here is a lightweight JavaScript code snippet to create masonry grid layout. You can view demo and download source code.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/codehim.com\/layout\/javascript-masonry-grid-layout\/\" \/>\n<meta property=\"og:site_name\" content=\"CodeHim\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/codehimofficial\" \/>\n<meta property=\"article:published_time\" content=\"2024-01-11T16:40:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-01-22T09:44:16+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/codehim.com\/wp-content\/uploads\/2022\/04\/www.codehim.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"960\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Asif Mughal\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@CodeHimOfficial\" \/>\n<meta name=\"twitter:site\" content=\"@CodeHimOfficial\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Asif Mughal\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/codehim.com\/layout\/javascript-masonry-grid-layout\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/codehim.com\/layout\/javascript-masonry-grid-layout\/\"},\"author\":{\"name\":\"Asif Mughal\",\"@id\":\"https:\/\/codehim.com\/#\/schema\/person\/cc48f1dbe072a89a62a98171b7db43ed\"},\"headline\":\"JavaScript Masonry Grid Layout\",\"datePublished\":\"2024-01-11T16:40:00+00:00\",\"dateModified\":\"2024-01-22T09:44:16+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/codehim.com\/layout\/javascript-masonry-grid-layout\/\"},\"wordCount\":158,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/codehim.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/codehim.com\/layout\/javascript-masonry-grid-layout\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/codehim.com\/wp-content\/uploads\/2022\/04\/www.codehim.png\",\"articleSection\":[\"Layout\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/codehim.com\/layout\/javascript-masonry-grid-layout\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/codehim.com\/layout\/javascript-masonry-grid-layout\/\",\"url\":\"https:\/\/codehim.com\/layout\/javascript-masonry-grid-layout\/\",\"name\":\"JavaScript Masonry Grid Layout &#8212; CodeHim\",\"isPartOf\":{\"@id\":\"https:\/\/codehim.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/codehim.com\/layout\/javascript-masonry-grid-layout\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/codehim.com\/layout\/javascript-masonry-grid-layout\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/codehim.com\/wp-content\/uploads\/2022\/04\/www.codehim.png\",\"datePublished\":\"2024-01-11T16:40:00+00:00\",\"dateModified\":\"2024-01-22T09:44:16+00:00\",\"description\":\"Here is a lightweight JavaScript code snippet to create masonry grid layout. You can view demo and download source code.\",\"breadcrumb\":{\"@id\":\"https:\/\/codehim.com\/layout\/javascript-masonry-grid-layout\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/codehim.com\/layout\/javascript-masonry-grid-layout\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/codehim.com\/layout\/javascript-masonry-grid-layout\/#primaryimage\",\"url\":\"https:\/\/codehim.com\/wp-content\/uploads\/2022\/04\/www.codehim.png\",\"contentUrl\":\"https:\/\/codehim.com\/wp-content\/uploads\/2022\/04\/www.codehim.png\",\"width\":1280,\"height\":960,\"caption\":\"JavaScript Masonry Grid Layout\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/codehim.com\/layout\/javascript-masonry-grid-layout\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/codehim.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Layout\",\"item\":\"https:\/\/codehim.com\/category\/layout\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"JavaScript Masonry Grid Layout\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/codehim.com\/#website\",\"url\":\"https:\/\/codehim.com\/\",\"name\":\"CodeHim\",\"description\":\"Web Design Code Snippets\",\"publisher\":{\"@id\":\"https:\/\/codehim.com\/#organization\"},\"alternateName\":\"Web Design Codes\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/codehim.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/codehim.com\/#organization\",\"name\":\"CodeHim - Web Design Code & Scripts\",\"url\":\"https:\/\/codehim.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/codehim.com\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/codehim.com\/wp-content\/uploads\/2023\/06\/Codehim-short-logo.jpg\",\"contentUrl\":\"http:\/\/codehim.com\/wp-content\/uploads\/2023\/06\/Codehim-short-logo.jpg\",\"width\":280,\"height\":280,\"caption\":\"CodeHim - Web Design Code & Scripts\"},\"image\":{\"@id\":\"https:\/\/codehim.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/codehimofficial\",\"https:\/\/x.com\/CodeHimOfficial\",\"https:\/\/www.instagram.com\/codehim\/\",\"https:\/\/www.linkedin.com\/company\/codehim\",\"https:\/\/co.pinterest.com\/codehim\/\",\"https:\/\/www.youtube.com\/@codehim\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/codehim.com\/#\/schema\/person\/cc48f1dbe072a89a62a98171b7db43ed\",\"name\":\"Asif Mughal\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/codehim.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/b25bfcd7d4e341c2c6f785a88d8ad2a4?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/b25bfcd7d4e341c2c6f785a88d8ad2a4?s=96&d=mm&r=g\",\"caption\":\"Asif Mughal\"},\"description\":\"I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences. I truly enjoy what I'm doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.\",\"sameAs\":[\"https:\/\/codehim.com\"],\"url\":\"https:\/\/codehim.com\/author\/asif-mughal\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JavaScript Masonry Grid Layout &#8212; CodeHim","description":"Here is a lightweight JavaScript code snippet to create masonry grid layout. You can view demo and download source code.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/codehim.com\/layout\/javascript-masonry-grid-layout\/","og_locale":"en_US","og_type":"article","og_title":"JavaScript Masonry Grid Layout &#8212; CodeHim","og_description":"Here is a lightweight JavaScript code snippet to create masonry grid layout. You can view demo and download source code.","og_url":"https:\/\/codehim.com\/layout\/javascript-masonry-grid-layout\/","og_site_name":"CodeHim","article_publisher":"https:\/\/www.facebook.com\/codehimofficial","article_published_time":"2024-01-11T16:40:00+00:00","article_modified_time":"2024-01-22T09:44:16+00:00","og_image":[{"width":1280,"height":960,"url":"https:\/\/codehim.com\/wp-content\/uploads\/2022\/04\/www.codehim.png","type":"image\/png"}],"author":"Asif Mughal","twitter_card":"summary_large_image","twitter_creator":"@CodeHimOfficial","twitter_site":"@CodeHimOfficial","twitter_misc":{"Written by":"Asif Mughal","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/codehim.com\/layout\/javascript-masonry-grid-layout\/#article","isPartOf":{"@id":"https:\/\/codehim.com\/layout\/javascript-masonry-grid-layout\/"},"author":{"name":"Asif Mughal","@id":"https:\/\/codehim.com\/#\/schema\/person\/cc48f1dbe072a89a62a98171b7db43ed"},"headline":"JavaScript Masonry Grid Layout","datePublished":"2024-01-11T16:40:00+00:00","dateModified":"2024-01-22T09:44:16+00:00","mainEntityOfPage":{"@id":"https:\/\/codehim.com\/layout\/javascript-masonry-grid-layout\/"},"wordCount":158,"commentCount":0,"publisher":{"@id":"https:\/\/codehim.com\/#organization"},"image":{"@id":"https:\/\/codehim.com\/layout\/javascript-masonry-grid-layout\/#primaryimage"},"thumbnailUrl":"https:\/\/codehim.com\/wp-content\/uploads\/2022\/04\/www.codehim.png","articleSection":["Layout"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/codehim.com\/layout\/javascript-masonry-grid-layout\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/codehim.com\/layout\/javascript-masonry-grid-layout\/","url":"https:\/\/codehim.com\/layout\/javascript-masonry-grid-layout\/","name":"JavaScript Masonry Grid Layout &#8212; CodeHim","isPartOf":{"@id":"https:\/\/codehim.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/codehim.com\/layout\/javascript-masonry-grid-layout\/#primaryimage"},"image":{"@id":"https:\/\/codehim.com\/layout\/javascript-masonry-grid-layout\/#primaryimage"},"thumbnailUrl":"https:\/\/codehim.com\/wp-content\/uploads\/2022\/04\/www.codehim.png","datePublished":"2024-01-11T16:40:00+00:00","dateModified":"2024-01-22T09:44:16+00:00","description":"Here is a lightweight JavaScript code snippet to create masonry grid layout. You can view demo and download source code.","breadcrumb":{"@id":"https:\/\/codehim.com\/layout\/javascript-masonry-grid-layout\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/codehim.com\/layout\/javascript-masonry-grid-layout\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codehim.com\/layout\/javascript-masonry-grid-layout\/#primaryimage","url":"https:\/\/codehim.com\/wp-content\/uploads\/2022\/04\/www.codehim.png","contentUrl":"https:\/\/codehim.com\/wp-content\/uploads\/2022\/04\/www.codehim.png","width":1280,"height":960,"caption":"JavaScript Masonry Grid Layout"},{"@type":"BreadcrumbList","@id":"https:\/\/codehim.com\/layout\/javascript-masonry-grid-layout\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/codehim.com\/"},{"@type":"ListItem","position":2,"name":"Layout","item":"https:\/\/codehim.com\/category\/layout\/"},{"@type":"ListItem","position":3,"name":"JavaScript Masonry Grid Layout"}]},{"@type":"WebSite","@id":"https:\/\/codehim.com\/#website","url":"https:\/\/codehim.com\/","name":"CodeHim","description":"Web Design Code Snippets","publisher":{"@id":"https:\/\/codehim.com\/#organization"},"alternateName":"Web Design Codes","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/codehim.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/codehim.com\/#organization","name":"CodeHim - Web Design Code & Scripts","url":"https:\/\/codehim.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codehim.com\/#\/schema\/logo\/image\/","url":"http:\/\/codehim.com\/wp-content\/uploads\/2023\/06\/Codehim-short-logo.jpg","contentUrl":"http:\/\/codehim.com\/wp-content\/uploads\/2023\/06\/Codehim-short-logo.jpg","width":280,"height":280,"caption":"CodeHim - Web Design Code & Scripts"},"image":{"@id":"https:\/\/codehim.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/codehimofficial","https:\/\/x.com\/CodeHimOfficial","https:\/\/www.instagram.com\/codehim\/","https:\/\/www.linkedin.com\/company\/codehim","https:\/\/co.pinterest.com\/codehim\/","https:\/\/www.youtube.com\/@codehim"]},{"@type":"Person","@id":"https:\/\/codehim.com\/#\/schema\/person\/cc48f1dbe072a89a62a98171b7db43ed","name":"Asif Mughal","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codehim.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/b25bfcd7d4e341c2c6f785a88d8ad2a4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b25bfcd7d4e341c2c6f785a88d8ad2a4?s=96&d=mm&r=g","caption":"Asif Mughal"},"description":"I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences. I truly enjoy what I'm doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.","sameAs":["https:\/\/codehim.com"],"url":"https:\/\/codehim.com\/author\/asif-mughal\/"}]}},"views":5118,"_links":{"self":[{"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/posts\/6346","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/comments?post=6346"}],"version-history":[{"count":0,"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/posts\/6346\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/media\/6358"}],"wp:attachment":[{"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/media?parent=6346"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/categories?post=6346"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/tags?post=6346"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}