{"id":4711,"date":"2018-01-04T16:39:24","date_gmt":"2018-01-04T16:39:24","guid":{"rendered":"https:\/\/www.bronco.co.uk\/our-ideas\/?p=4711"},"modified":"2020-07-02T12:17:48","modified_gmt":"2020-07-02T11:17:48","slug":"arranging-elements-from-top-to-bottom-using-grid-layout","status":"publish","type":"post","link":"https:\/\/www.bronco.co.uk\/our-ideas\/arranging-elements-from-top-to-bottom-using-grid-layout\/","title":{"rendered":"Arranging elements from Top to Bottom using Grid Layout"},"content":{"rendered":"\n<p>I hoped being set the challenge of organising a group of list items so that they\u2019re arranged top to bottom, left to right was going to be fairly simple with CSS Grid Layout; but it proved a little tougher than I expected.<\/p>\n\n\n\n<p>With <code>grid-auto-flow:column<\/code> I expected that I could easily use this, define some columns and it would automagically sort out the number of rows required. However in this instance it appears you must also specify a number of rows for the grid. If you don&#8217;t, the grid items appear to overflow the container in order to remain on a single row.<\/p>\n\n\n\n<p>For many, having to set the number of rows wouldn&#8217;t be an issue, but with multiple lists of varying length, which could grow in the future, it\u2019s impractical to be so specific with the number of rows in this instance.<\/p>\n\n\n\n<p>After some tinkering, and a few failed attempts, I finally came up with a solution\u2026<\/p>\n\n\n\n<figure>\n<div style=\"width: 100%;\">\n<p class=\"codepen\" data-height=\"407\" data-theme-id=\"0\" data-slug-hash=\"wprMOo\" data-default-tab=\"css,result\" data-user=\"keanr\" data-embed-version=\"2\" data-pen-title=\"CSS Grid Top to Bottom\">See the Pen <a href=\"https:\/\/codepen.io\/keanr\/pen\/wprMOo\/\">CSS Grid Top to Bottom<\/a> by Kean Richmond (<a href=\"https:\/\/codepen.io\/keanr\">@keanr<\/a>) on <a href=\"https:\/\/codepen.io\">CodePen<\/a>.<script async=\"\" src=\"https:\/\/production-assets.codepen.io\/assets\/embed\/ei.js\"><\/script><\/p>\n<\/div>\n<figcaption>click &#8220;Edit on Codepen&#8221; to play with screen width and view more columns<\/figcaption>\n<\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s going on here?<\/h2>\n\n\n\n<p>To get the number of rows I first need to know the number of items in each list and deliver that to my CSS. I do this through the use of Custom Properties (a.k.a CSS variables) and including this as inline CSS on each <code>&lt;ul&gt;<\/code>. As I have a different number of columns at different breakpoints I also specify the number of rows needed when the layout changes to a different number of columns (in the example I have between 1 and 4 columns).<\/p>\n\n\n\n<p>Though not in the example above, my list is generated from a database so I have some simple PHP code that generates the number of rows for my inline CSS.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>$num = mysqli_num_rows($query);\n$breakpoints = &#39; style=&quot;--grid-rows:&#39; . $num . &#39;;--grid-rows-2:&#39; . ceil($num\/2) . &#39;;--grid-rows-3:&#39; . ceil($num\/3) . &#39;;--grid-rows-4:&#39; . ceil($num\/4) . &#39;;&quot;&#39;;<\/code><\/pre><\/div>\n\n\n\n<p>Moving to the CSS file. I use the custom properties to define the number of rows in <code>grid-template-rows<\/code> at each <code>@media<\/code> defined breakpoint. In addition to this, to save repetition of the <code>grid-template-rows<\/code> property I instead redefine the value of the custom property at each breakpoint by changing what I have as my default custom property to the value of one of the others I&#8217;ve set depending on the number of columns I have displaying at the time. Unfortunately this does require the use of !important to do this.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Isn\u2019t this a bit hacky?<\/h2>\n\n\n\n<p>To be honest it\u2019s not the first solution I aimed for. I&#8217;d much prefer to have specified only the number of items in the list and done all the calculations for the number of rows required per column within the CSS file using <code>calc()<\/code>; but all options I attempted failed.<\/p>\n\n\n\n<p>With CSS Grid Layout and Custom Properties both emerging technologies I&#8217;ll admit I&#8217;m still learning and that may have worked against me. However the issues I experienced mostly appeared to be due to being unable to use <code>calc()<\/code> in conjunction with <code>grid-template-rows<\/code> to define the number of rows required. For example, none of the below would work for me&#8230;<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-css\" data-lang=\"CSS\"><code>.grid-ttb {grid-template-rows:repeat(calc(16\/2),1fr);\n\n.grid-ttb {grid-template-rows:repeat(attr(data-gridcol),1fr);\n\n:root{\n--grid-col: calc(16\/2);\n}\n.grid-ttb {grid-template-rows:repeat(var(--grid-col),1fr);<\/code><\/pre><\/div>\n\n\n\n<p>I&#8217;m hopeful a simpler solution is available and that Google is just playing hide and seek with the answer, but for now this works for modern browsers that support CSS Grid and Custom Properties and may help others attempting to do the same.<\/p>\n\n\n\n<p>If you find or know of a better solution, please add a link in the comments.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I hoped being set the challenge of organising a group of list items so that they\u2019re arranged top to bottom, left to right was going to be fairly simple with CSS Grid Layout; but it proved a little tougher than I expected. With grid-auto-flow:column I expected that I could easily use this, define some columns [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":4722,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"class_list":["post-4711","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-and-ux"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/posts\/4711","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/comments?post=4711"}],"version-history":[{"count":0,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/posts\/4711\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/media\/4722"}],"wp:attachment":[{"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/media?parent=4711"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/categories?post=4711"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}