<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Damon Muma</title>
    <description>The latest articles on Forem by Damon Muma (@thedamon).</description>
    <link>https://forem.com/thedamon</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F45023%2F4304f8bd-d8dd-45c6-b694-cfa32d063b35.jpeg</url>
      <title>Forem: Damon Muma</title>
      <link>https://forem.com/thedamon</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/thedamon"/>
    <language>en</language>
    <item>
      <title>My favourite line of CSS</title>
      <dc:creator>Damon Muma</dc:creator>
      <pubDate>Sat, 23 May 2020 04:51:00 +0000</pubDate>
      <link>https://forem.com/thedamon/my-favourite-line-of-css-4klp</link>
      <guid>https://forem.com/thedamon/my-favourite-line-of-css-4klp</guid>
      <description>&lt;p&gt;Hey folks.&lt;/p&gt;

&lt;h2&gt;
  
  
  This is my favourite line of CSS:
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  grid-template-columns: repeat( auto-fill, minmax(min(calc(180px + 12vmin), 100%), 1fr));
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Here's what it does:
&lt;/h2&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/thedamon/embed/oNjVbVR?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Not a big deal, eh? just a bunch of squares. But try resizing the codepen window like a weirdo and see how they all just fit! Oooooh.&lt;/p&gt;

&lt;h2&gt;
  
  
  Here's why I like it:
&lt;/h2&gt;

&lt;p&gt;It (along with a lil &lt;code&gt;display: grid&lt;/code&gt;) is a fully responsive grid written with no media queries that keeps a good amount of content on screen for everyone no matter their screen size. &lt;/p&gt;

&lt;p&gt;The design goal solved here is basically: "I want all these things to be about yay big, but I want them to tile evenly into a row, and I want them to get a little smaller as there's less room on the screen".&lt;/p&gt;

&lt;p&gt;It's to my brain a simple need but the mathy reality of it is pretty complicated. I really like that I can express it with one line of CSS.&lt;/p&gt;

&lt;p&gt;There's a lot of cool stuff in this one line, so let's take it apart to let it shine.&lt;/p&gt;

&lt;h3&gt;
  
  
  Vmin?
&lt;/h3&gt;

&lt;p&gt;The black sheep/vermin of the css unit family, but, &lt;code&gt;vmin&lt;/code&gt; is, in fact, the bomb, diggity.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;1% of the smaller of the height or width of the viewport&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Because here's the problem with sizing everything based on the &lt;em&gt;width&lt;/em&gt; of the viewport:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---6IOhanb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/tzfxu4a3x09lllvsj511.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---6IOhanb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/tzfxu4a3x09lllvsj511.png" alt="A large horizontal rectangle with barely any of a large square fitting inside of it. The rest falls off the bottom."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Most people do it this way, and it's why the landscape view of a mobile website is usually a bit garbage. Don't forget to be responsive when you're being responsive!&lt;/p&gt;

&lt;p&gt;I'm not saying always use &lt;code&gt;vmin&lt;/code&gt;, but it does elegantly allow you to consider tall narrow windows &lt;em&gt;and&lt;/em&gt; short wide windows as similarly restricted space-wise, which we usually forget to do on the internet.&lt;/p&gt;

&lt;p&gt;Another cool thing you try at home: a &lt;code&gt;vmean&lt;/code&gt; unit: &lt;code&gt;calc({n/2}vh + {n/2}vw)&lt;/code&gt;. This gives you 1% of the &lt;em&gt;average&lt;/em&gt; of the viewport height + width — the overall amount of screen space available (This is actually cooler than vmin, but I &lt;em&gt;mainly&lt;/em&gt; want to rant about height queries so I'm just tucking it in here.)&lt;/p&gt;

&lt;h3&gt;
  
  
  calc(vsomething + pixies)?
&lt;/h3&gt;

&lt;p&gt;The above assumes you're doing some 'responsive scaling' of an object in order to make best use of the screen real estate. Combining a base &lt;code&gt;px&lt;/code&gt; or &lt;code&gt;em&lt;/code&gt; size with a percentage of the viewport allows you to make things smoothly scale up as the window gets bigger (and just got super snazzy with &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/clamp"&gt;&lt;code&gt;clamp()&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This is often used with &lt;code&gt;font-size&lt;/code&gt; to quickly achieve slightly bigger text on bigger screens that have room for it. &lt;a href="https://css-tricks.com/simplified-fluid-typography/"&gt;Here's a great overview from a couple days ago&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;How do you choose how many of each unit to use? The higher the &lt;code&gt;v*&lt;/code&gt;, the more it will scale with the screen. Other than that I do some real sloppy head math as a starting point, then try different values and drag the corner of my screen around all over the place like a weirdo until it looks right. Do you have a better way? &lt;/p&gt;

&lt;p&gt;&lt;em&gt;(🔥 hot tip: compared to copy, give headings more of the v-units so they scale more dramatically as the screen size changes)&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  min() ?
&lt;/h3&gt;

&lt;p&gt;Min (along with his friend Max Clamp) is new but very handy. (I also edited this article to add it when I realized that it &lt;em&gt;does&lt;/em&gt; work inside minmax, and the trick is just &lt;em&gt;not to have syntax errors&lt;/em&gt; 🤦‍♂️).&lt;/p&gt;

&lt;p&gt;The trick with minmax is that it favors the minimum, so if your value is wider than the screen (say if you want to base it on 400px, but your container is 300px wide) it will stretch outside its bounds.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Y-nTMUss--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/p2kenfj9xftnw851b4r6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Y-nTMUss--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/p2kenfj9xftnw851b4r6.png" alt="The same image of squares from before, but rotated so that the inner square is escaping out the side of the outer square"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is a weird decision to me—I never want something to stretch outside its container—but you would have to choose one of min or max to win in a fight and The CSS Lobby chose minimum.&lt;/p&gt;

&lt;p&gt;the &lt;code&gt;min()&lt;/code&gt; function allows us to request the smallest of two values. Inside minmax feels redundant, but it will let you make sure the element never gets too big for its britches, hence &lt;code&gt;minmax( min(calc(180px + 12vmin), 100%), 1fr)&lt;/code&gt; (the result of the calc but not more than the size of the container).&lt;/p&gt;

&lt;p&gt;Great writeup on this &lt;a href="https://css-tricks.com/intrinsically-responsive-css-grid-with-minmax-and-min/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This part is honestly pretty weird and needing it almost makes this &lt;em&gt;not&lt;/em&gt; my favourite line of CSS anymore (you don't need it if your base size is small enough. &lt;em&gt;Technically&lt;/em&gt; we don't need it here, but it's an important feature to know if you wanna try this at home).&lt;/p&gt;

&lt;h3&gt;
  
  
  repeat ( auto-fill ( minmax ( ?
&lt;/h3&gt;

&lt;p&gt;There are probably a bunch of blog posts about this one already. To me it was kinda the most exciting part of grid: not needing to mess around with media queries to just put em in a row and let em wrap when they ought to.&lt;/p&gt;

&lt;p&gt;Usually they tell you:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;repeat(auto-fill, minmax(300px, 1fr))
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It makes the thing be &lt;em&gt;at least&lt;/em&gt; 300px but stretches it out to fill the rest of the row.&lt;/p&gt;

&lt;p&gt;Which is pretty cool. But cmon that responsive scaling just dopens it up that extra bit doesn't it? Bigger screens want bigger stuff!&lt;/p&gt;

&lt;h2&gt;
  
  
  The old ways
&lt;/h2&gt;

&lt;p&gt;(a.k.a. if you still need to support IE11 😟)&lt;/p&gt;

&lt;p&gt;So you could also still do&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.tile&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;100%&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;@media&lt;/span&gt; &lt;span class="n"&gt;screen&lt;/span&gt; &lt;span class="n"&gt;and&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;min-width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;620px&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;50%&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;@media&lt;/span&gt; &lt;span class="n"&gt;screen&lt;/span&gt; &lt;span class="n"&gt;and&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;min-width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;920px&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;calc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;100%&lt;/span&gt; &lt;span class="p"&gt;/&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;@media&lt;/span&gt; &lt;span class="n"&gt;screen&lt;/span&gt; &lt;span class="n"&gt;and&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;min-width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1150px&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;calc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;25%&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;(continued up to 4 more times depending on what sizes we're dealing with)&lt;/p&gt;

&lt;p&gt;Or you could do&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"container"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"row"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"tile col-sm-6 col-md-4 col-lg-3"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      ...
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But they have their own problems&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You have to define potentially arbitrary breakpoints, or arbitrarily use your already existing ones.&lt;/li&gt;
&lt;li&gt;You would have to buy into the idea of a 12 column grid system even if you don't want to. (Which also means you can only have a number of items per row that is divisible by 12) &lt;/li&gt;
&lt;li&gt;Basically, you have to fill your brain with a lot of stuff that isn't "I want em to fit, and be about this size".&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;My fav line of css is a bit intense (4 nested functions??) but look at it again:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  grid-template-columns: repeat( auto-fill, minmax(min(calc(180px + 12vmin), 100%), 1fr));
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;A bit of a mouthful, but it represents the actual task it's solving a lot more directly than the other methods. I know we take grids for granted and many of us live and breathe dividing things into 12, but the fact that you go "this is 6" when you want to have 2 things per row has always felt unnecessarily circuitous to me.&lt;/p&gt;

&lt;h2&gt;
  
  
  In the wild
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.radioslipstream.com/covers/"&gt;Peruse my real-world usecase&lt;/a&gt; if you want.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's missing?
&lt;/h2&gt;

&lt;p&gt;This is great, but I still lament some things about laying things out in CSS Grid that I couldn't figure out a way to do here:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;I want to have grid-lines between items instead of just gap, and you kinda just can't do that (you'll see this not really working on the codepen).&lt;/li&gt;
&lt;li&gt;I want to be able for the last line's items to be centered instead of left or right aligned, and you kinda just can't do that.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let me know if there's a way to do those things and thanks for dropping by!&lt;/p&gt;

&lt;p&gt;And since we can nest declarations 4 functions deep in CSS now, is it time to introduce a &lt;a href="https://www.arthurconandoyle.com/sherlockholmes.html"&gt;pipe operator&lt;/a&gt;? Sorry wrong link try again. &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Pipeline_operator"&gt;Pipe operator&lt;/a&gt;&lt;/p&gt;

</description>
      <category>css</category>
      <category>cssgrid</category>
      <category>grid</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Maintaining an alternating pattern across a responsive grid</title>
      <dc:creator>Damon Muma</dc:creator>
      <pubDate>Sun, 29 Sep 2019 19:09:17 +0000</pubDate>
      <link>https://forem.com/thedamon/maintaining-a-pattern-across-a-responsive-grid-4j2b</link>
      <guid>https://forem.com/thedamon/maintaining-a-pattern-across-a-responsive-grid-4j2b</guid>
      <description>&lt;p&gt;I get dangerously intrigued by problems that seem potentially impossible where CSS is concerned, but completely reasonable from a visual design point of view.&lt;/p&gt;

&lt;p&gt;Here's one that cropped up yesterday: a responsive gallery where the pictures alternate dark-themed and light-themed (could also be B&amp;amp;W/color, red-themed/green-themed, photo/drawing or any alternating stylistic property). &lt;/p&gt;

&lt;p&gt;In the markup the images alternate. It even looks totally great on its own when there are an odd number of pictures per row. But when there are an even number of &lt;code&gt;itemsPerRow&lt;/code&gt;, we get vertical bands because each row starts with a 'light' picture. &lt;/p&gt;

&lt;p&gt;What we want, though, is for the diagonal tiling effect to be maintained across every breakpoint. Something like (3 mb gif alert 🚨):&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fdhdwzotheeohcankk847.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fdhdwzotheeohcankk847.gif" alt="Zooming in on a grid that has checkerboard pattern no matter number of items per row"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In a nutshell 🥜&lt;/strong&gt;: The last item in row 1 needs to stylistically match the first item in row 2, but only when there are an even number of items per row. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;TLDR for the designers&lt;/em&gt;: If your front-end dev said this is impossible, they were wrong 😱! (&lt;strong&gt;if&lt;/strong&gt; you don't need to support IE 😉)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When I first ran across the problem I was stumped. Could I achieve a patterned grid with differing numbers of items per row and the same HTML without using javascript? 🤔&lt;/p&gt;

&lt;p&gt;Conceptually what I want here without complicated math, is for the direction of the grid to weave back and forth as I move down the page. Indeed it would be awesome if I could just declare &lt;code&gt;grid-auto-flow: snakes!;&lt;/code&gt; and move on to the next problem. Unfortunately no such property exists.&lt;/p&gt;

&lt;p&gt;But we &lt;em&gt;can&lt;/em&gt; still make it happen in just CSS with some diligence and 3 somewhat advanced but very useful selectors and grid properties:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;nth-child selectors&lt;/li&gt;
&lt;li&gt;grid-column&lt;/li&gt;
&lt;li&gt;grid-auto-flow&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I'll explain each of these and then illustrate how to apply them to our situation. Or just jump to the 'solution' and read the code directly.&lt;/p&gt;
&lt;h1&gt;
  
  
  :nth-child()&lt;a&gt;&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;The &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-child" rel="noopener noreferrer"&gt;nth-child selector&lt;/a&gt; is an important one. It's very powerful, but sometimes a bit difficult to figure out when using it beyond its basic functionality of &lt;code&gt;odd&lt;/code&gt;/&lt;code&gt;even&lt;/code&gt;/&lt;code&gt;4&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;:nth-child()&lt;/code&gt; also accepts an equation, which will be very useful for manipulating a series of data of an unknown length, which is exactly what we need to do here. If you already know how this one works, feel free to skip this section!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;img&lt;/span&gt;&lt;span class="nd"&gt;:nth-child&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;4&lt;/span&gt;&lt;span class="nt"&gt;n&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="err"&gt;5&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="c"&gt;/*  */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That would result in selecting the following children:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;(4*0)+5&lt;/code&gt; = 5th&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;(4*1)+5&lt;/code&gt; = 9th&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;(4*2)+5&lt;/code&gt; = 13th&lt;/li&gt;
&lt;li&gt;etc&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In case your linear algebra is rusty, there's a pretty straightforward way to apply this.&lt;/p&gt;

&lt;p&gt;Given our specific problem, we need to select every &lt;code&gt;x&lt;/code&gt;th item, starting with the &lt;code&gt;y&lt;/code&gt;th. More englishy, we want to select "the first item of alternating rows of 4". Diving a little deeper, we want to ignore the first row, and start with the 5th item overall (&lt;code&gt;y&lt;/code&gt;/&lt;code&gt;firstItem&lt;/code&gt;). After that we want to select every 8th item after that (to continue selecting the first column of every other row—(&lt;code&gt;x&lt;/code&gt;/&lt;code&gt;itemsPerRow * 2&lt;/code&gt;) ). That looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// This is not real javascript! 😉&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;

&lt;span class="nx"&gt;equation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt; 
&lt;span class="nx"&gt;selector&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`8n + 5`&lt;/span&gt;

&lt;span class="c1"&gt;// or&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;itemsPerRow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;firstItem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;itemsPerRow&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="c1"&gt;// 5&lt;/span&gt;

&lt;span class="nx"&gt;equation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;itemsPerRow&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;firstItem&lt;/span&gt;
&lt;span class="nx"&gt;selector&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`8n + 5`&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That wasn't real javascript, just an attempt to illustrate the maths going on here. That same equation will allow us to modify any grid, as we'll see later.&lt;/p&gt;

&lt;h1&gt;
  
  
  Grid-column &lt;a&gt;&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/grid-column" rel="noopener noreferrer"&gt;Grid-column&lt;/a&gt; is a property that can be assigned to a specific grid-item in order to move it to a different place than where it normally would fall. So if I have something like &lt;code&gt;grid-template-columns: repeat(4, auto)&lt;/code&gt; (which places my grid items in rows of 4), I can use &lt;code&gt;grid-column&lt;/code&gt; to &lt;strong&gt;move&lt;/strong&gt; a particular item (chosen by nth-child) to a different position.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.grid-item&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

&lt;span class="c"&gt;/* put the item in the second column */&lt;/span&gt;
  &lt;span class="nl"&gt;grid-column&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c"&gt;/* put the item in the last column */&lt;/span&gt;
  &lt;span class="nl"&gt;grid-column&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;-1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c"&gt;/* make the item span between column 2 and column 4 */&lt;/span&gt;
  &lt;span class="nl"&gt;grid-column&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;/&lt;/span&gt;&lt;span class="m"&gt;4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cool!&lt;/p&gt;

&lt;h1&gt;
  
  
  Grid-auto-flow &lt;a&gt;&lt;/a&gt;
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.grid&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="py"&gt;grid-auto-flow&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;dense&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the one that makes this even possible. Without it all would be lost (at least until &lt;code&gt;grid-auto-flow: 🐍&lt;/code&gt; is supported 😉).&lt;/p&gt;

&lt;p&gt;Normally, when you reposition a grid item, it repositions every following item with it, leaving a gap. If you want to change the order of &lt;strong&gt;just&lt;/strong&gt; the one particular item when using &lt;code&gt;grid-column&lt;/code&gt;, the &lt;code&gt;dense&lt;/code&gt; keyword takes the other items and moves them up to fill the spaces. It's a little like &lt;code&gt;position: absolute&lt;/code&gt; or a float, except the targeted items still take up space. Whoa.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/grid-auto-flow" rel="noopener noreferrer"&gt;Grid-auto-flow&lt;/a&gt; does other stuff, too, but &lt;code&gt;dense&lt;/code&gt; is what we came here for.&lt;/p&gt;

&lt;h1&gt;
  
  
  Putting it all together
&lt;/h1&gt;

&lt;p&gt;So putting these rules together you get something that looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.grid&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="py"&gt;grid-template-columns&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;repeat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;auto&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="py"&gt;grid-auto-flow&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;dense&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;/* 4 itemsPerRow, every other row(4*2), starting with the 5th: 8n+5 */&lt;/span&gt;
&lt;span class="nt"&gt;img&lt;/span&gt;&lt;span class="nd"&gt;:nth-child&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;8&lt;/span&gt;&lt;span class="nt"&gt;n&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="err"&gt;5&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;grid-column&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F5g104hthy50ckndg7wqk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F5g104hthy50ckndg7wqk.png" alt="repositioning the first image in every other row"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  The finished pattern &lt;a&gt;&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;To apply this in the real world, we have to set up the correct repositioning rules for just the breakpoints where they're necessary. Here's some example codepens that use a data attribute instead of a media query to make playing with the results a little nicer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example for alternating pattern
&lt;/h2&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/thedamon/embed/wvwbXvm?height=600&amp;amp;default-tab=css,result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Example for cycle of 3
&lt;/h2&gt;

&lt;p&gt;You can also use the same strategy to make a repeating pattern of 3 style types always look aesthetically pleasing no matter the number of items per row.&lt;/p&gt;

&lt;p&gt;It gets a little more complicated, but it also provides an example of needing to reposition 2 items per row (row of 6 and a cycle of 3 styles).&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/thedamon/embed/eYOaEvL?height=600&amp;amp;default-tab=css,result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  CSS + Media queries
&lt;/h2&gt;

&lt;p&gt;For it to work like we actually want it to, via media queries, you would need something like this (example below is for a cycle of 2 and a grid up to 4 per row:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.photo-grid&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;grid&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="py"&gt;grid-auto-flow&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;dense&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.photo-grid&lt;/span&gt; &lt;span class="nt"&gt;img&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;100%&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;/* I don't like min AND max queries (mobile first!), but it makes perfect sense here because we're doing some specific things to the image that we don't want to apply to the 900px breakpoint. */&lt;/span&gt;
&lt;span class="k"&gt;@media&lt;/span&gt; &lt;span class="n"&gt;screen&lt;/span&gt; &lt;span class="n"&gt;and&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;min-width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;600px&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;and&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max-width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;899px&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nc"&gt;.photo-grid&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="py"&gt;grid-template-columns&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;repeat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;auto&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nc"&gt;.photo-grid&lt;/span&gt; &lt;span class="nt"&gt;img&lt;/span&gt;&lt;span class="nd"&gt;:nth-child&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;4&lt;/span&gt;&lt;span class="nt"&gt;n&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="err"&gt;3&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;grid-column&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;@media&lt;/span&gt; &lt;span class="n"&gt;screen&lt;/span&gt; &lt;span class="n"&gt;and&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;min-width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;900px&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nc"&gt;.photo-grid&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="py"&gt;grid-template-columns&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;repeat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;auto&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;@media&lt;/span&gt; &lt;span class="n"&gt;screen&lt;/span&gt; &lt;span class="n"&gt;and&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;min-width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1200px&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nc"&gt;.photo-grid&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="py"&gt;grid-template-columns&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;repeat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;auto&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nc"&gt;.photo-grid&lt;/span&gt; &lt;span class="nt"&gt;img&lt;/span&gt;&lt;span class="nd"&gt;:nth-child&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;8&lt;/span&gt;&lt;span class="nt"&gt;n&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="err"&gt;5&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;grid-column&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Is it accessible?
&lt;/h1&gt;

&lt;p&gt;This is a question you should always be asking when stumbling across a wacky CSS or JS technique on the internet, and I am thrilled if you're looking at all this thinking "While this is theoretically useful in very specific and uncommon situations, I worry that it isn't fully accessible!"&lt;/p&gt;

&lt;p&gt;Indeed! It's important for screen-readers to be able to make sense of a site, and messing with the order of elements visually vs in source can be problematic — &lt;a href="https://www.w3.org/WAI/WCAG21/quickref/?versions=2.0#qr-navigation-mechanisms-focus-order" rel="noopener noreferrer"&gt;2.4.3: focus order&lt;/a&gt; and &lt;a href="https://www.w3.org/WAI/WCAG21/quickref/?versions=2.0#qr-content-structure-separation-sequence" rel="noopener noreferrer"&gt;1.3.2: meaningful sequence&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you are ordering your content visually, the order probably isn't inherently important to reading the content of the page, so this should not be an issue! But do keep in mind, if source order is important (i.e. you are &lt;em&gt;explicitly&lt;/em&gt; sorting content in a specific order (alphabetically, new→old), or intentionally presenting it in a narrative order) then the visual order and the mark-up order need to be maintained.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion.
&lt;/h1&gt;

&lt;p&gt;I would love to be able to do this in a breakpoint-free way using, say, &lt;code&gt;repeat(auto-fill, minmax(300px, 1fr) )&lt;/code&gt; and a mythical &lt;code&gt;grid-auto-flow: snake/alternating-rows&lt;/code&gt; to essentially eliminate all the weird math. But in the meantime this was the best I could come up with 😅!&lt;/p&gt;

&lt;p&gt;Let me know in the comments if you have a cleaner approach to this or a similar problem!&lt;/p&gt;

</description>
      <category>cssgrid</category>
      <category>css</category>
      <category>design</category>
      <category>artdirection</category>
    </item>
  </channel>
</rss>
