<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Snehil on Medium]]></title>
        <description><![CDATA[Stories by Snehil on Medium]]></description>
        <link>https://medium.com/@SnehilCodes?source=rss-3dbdc87455ed------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*hiI0s6mpQUAvOdDNOgPsXg.png</url>
            <title>Stories by Snehil on Medium</title>
            <link>https://medium.com/@SnehilCodes?source=rss-3dbdc87455ed------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 12:42:00 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@SnehilCodes/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Creating Responsive Grids of Flexible Square Cards without CSS Media Queries]]></title>
            <link>https://medium.com/@SnehilCodes/creating-responsive-grids-of-flexible-square-cards-without-css-media-queries-3ee104f0e597?source=rss-3dbdc87455ed------2</link>
            <guid isPermaLink="false">https://medium.com/p/3ee104f0e597</guid>
            <category><![CDATA[grid-layout]]></category>
            <category><![CDATA[css]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Snehil]]></dc:creator>
            <pubDate>Sun, 02 Aug 2020 17:23:30 GMT</pubDate>
            <atom:updated>2020-08-02T19:03:44.997Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kVUZn_vwdbFHfPwEm9EMUw.png" /><figcaption>Screenshot from my project (WIP)</figcaption></figure><h3>Creating a Responsive Card Grid with fixed Aspect-Ratio without CSS Media Queries</h3><p>In material design, placing elements on a grid is key to creating beautiful layouts. But creating a highly responsive grid with CSS and flexbox isn’t an easy task. I always found it hard to position the child elements properly (usually due to margin and padding issues). Luckily CSS comes with its own layout for <strong>grids</strong> packed with a lot of handy functions and properties to make work easy. You can start using it by using the <strong>display: grid;</strong> property. <br>If you want to learn about grids more, you can watch <a href="https://www.youtube.com/watch?v=uuOXPWCh-6o"><strong><em>this Fireship video</em></strong></a> where I learnt it from. Also, check out <a href="https://cssgridgarden.com/"><strong><em>Grid Garden</em></strong></a> for a more practical explanation.</p><p>In this post, we’ll learn how to create a grid of cards where all the card elements are squares of the same dimension. Locking the aspect-ratio of &lt;div&gt;s while allowing them to be resizable has always been a challenge. Luckily there’s a CSS trick that exploits the fact that padding percentages are calculated based on an element&#39;s width (even for top and bottom padding). <strong>More on that later in the article.</strong></p><p><strong><em>NOTE:</em></strong><em> The same method can also be used to generate cards of any aspect ratio.<br>I made it for a ReactJs project that I’m working on but the same CSS can be used in any other framework and even with pure HTML+CSS which is shown in this article.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CsbSMar9luFNstq_UfZ2yg.gif" /><figcaption>Working example with complex content</figcaption></figure><h4>Getting Started</h4><p>First, we need a basic structure for the elements. In this example, each of the cards is made of 2 <strong>&lt;div&gt;</strong> elements containing a Unicode symbol and its name wrapped inside a <strong>&lt;div&gt;</strong>. All the cards are then wrapped inside a container.</p><pre>&lt;div class=&quot;container&quot;&gt;<br>  &lt;div class=&quot;card&quot;&gt;&lt;/div&gt;<br>  &lt;div class=&quot;card&quot;&gt;&lt;/div&gt;<br>  &lt;div class=&quot;card&quot;&gt;&lt;/div&gt;<br>  &lt;div class=&quot;card&quot;&gt;&lt;/div&gt;<br>  &lt;div class=&quot;card&quot;&gt;&lt;/div&gt;<br>  &lt;div class=&quot;card&quot;&gt;&lt;/div&gt;<br>  &lt;div class=&quot;card&quot;&gt;&lt;/div&gt;<br>  &lt;div class=&quot;card&quot;&gt;&lt;/div&gt;<br>  &lt;div class=&quot;card&quot;&gt;&lt;/div&gt;<br>  &lt;div class=&quot;card&quot;&gt;&lt;/div&gt;<br>  &lt;div class=&quot;card&quot;&gt;&lt;/div&gt;<br>  &lt;div class=&quot;card&quot;&gt;&lt;/div&gt;<br>&lt;/div&gt;</pre><h4>Converting to grid layout</h4><p>Now that the basic structure is done we can add <strong>display: grid;</strong> to convert it into a <strong>grid</strong>. For the container and card classes, add the following CSS:</p><pre>.container {<br>  display: grid;<br>  grid-template-columns: repeat(<em>auto-fit</em>, minmax(128px, 1fr));<br>  grid-gap: 0.75rem; /* To define the gap between rows/columns */<br>  gap: 0.75rem;<br>  padding: 0.75rem;<br>  background-color: #222629; /* For contrast */<br>  color: #fff;<br>}</pre><pre>.card {<br>  <em>/* This will come in handy later to center the contents */<br>  </em>position: relative;<br>}</pre><p>You can see that I used repeat(auto-fit, minmax(128px, 1fr)) for <strong>column generation</strong>. This is one of the many handy features of CSS grid that tries to fit as many card elements in a row as possible. The width of these cards is governed by the second parameter. minmax(128px, 1fr) is saying that the cards must occupy <strong>at least 128px</strong> and <strong>1fr</strong> tell the grid to fill the remaining width by dividing it among all the cards. <strong>That&#39;s it!</strong> Responsive layout without breakpoints!<br>At this point, you should be able to see the grid layout working and the cards reflowing to the next line to stay ≥128px width. Time to lock their aspect-ratio.</p><h4>Locking the aspect-ratio</h4><p>Okay hear me out on this one, CSS has a bug. Yes, even I jumped out of my chair. In short, the percentage-based padding for any element is calculated only by its width even for vertical padding. I don’t know the reason for this but if you do, I’d love to hear about it in the comments. We’re going to use this to set the height of the element based on its width. Add this to your CSS:</p><pre><em>.card:after</em> {<br>  content: &quot;&quot;;<br>  display: block;<br>  padding-bottom: 100%;<br>}</pre><p>This adds an empty element to the end of every card &lt;div&gt; and sets its bottom-padding to be 100% of the card width. This ensures the <strong>aspect-ratio is always locked</strong> at 1:1. The padding value can be changed based on the required aspect ratio. We can use this formula to calculate the padding: Height*100/Width. Here are the padding values for some standard aspect ratios:</p><pre>16:9 =&gt; padding-bottom: 56.25%;<br>16:10 =&gt; padding-bottom: 62.5%;<br>4:3 =&gt; padding-bottom: 75%;<br>1:1 =&gt; padding-bottom: 100%;</pre><h4>Almost there…</h4><p>Great, we got what we wanted and the cards are now flexible and are holding their shape. Time to add some content to the cards. If you tried adding content already you might’ve noticed that the content size is also added to the height. In order to stop that let&#39;s change the content <strong>position:</strong> to <strong>absolute;</strong><br>You need to wrap the content inside a <strong>&lt;div&gt;</strong> to do that so now our card structure and CSS looks like:</p><pre>----HTML-----------------------------------<br>&lt;div class=&quot;card&quot;&gt;<br>  &lt;div class=&quot;content&quot;&gt;<br>    &lt;div class=&quot;center&quot;&gt;<br>      Example Content<br>    &lt;/div&gt;<br>  &lt;/div&gt;<br>&lt;/div&gt;</pre><pre>----CSS------------------------------------<br>.card <em>.content</em> {<br>position: absolute;<br>left: 0;<br>right: 0;<br>top: 0;<br>bottom: 0;<br>}</pre><p>Now time for the classic roadblock of CSS, centring the content. 😫<br>Luckily converting the content to a flexbox and using align-item and justify-content does the trick. I also added some border-radius to make it look nice. The CSS for that looks like:</p><pre>.center {<br>  width: 100%;<br>  height: 100%;<br>  display: flex;<br>  flex-direction: row;<br>  align-items: center;<br>  justify-content: center;<br>  text-align: center;<br>  border-radius: 0.75rem;<br>  background-color: #474b4f;<br>}</pre><h4>And we’re done! 🍻🍻</h4><p>Wow, that took a while and a lot of CSS magic! Pats on your back everyone. Go take a break and when you’re back, do give it a clap (or 10 ❤) if you liked this article. Correct me if I was wrong somewhere, this was my first article.</p><p>I’d love to hear what awesome things you make with this grid layout. You can find a <strong>working demo</strong> at this pen: <a href="https://codepen.io/SneakySensei/pen/PoZMMqx">https://codepen.io/SneakySensei/pen/PoZMMqx</a></p><p>The complete HTML and CSS for this should look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/03fd441ddc7255460caf45fd7bfb9f99/href">https://medium.com/media/03fd441ddc7255460caf45fd7bfb9f99/href</a></iframe><p>You can connect to me on <a href="https://www.linkedin.com/in/snehilcodes/"><strong><em>LinkedIn</em></strong></a><strong><em>, </em></strong><a href="https://github.com/sneakysensei"><strong><em>GitHub</em></strong></a><strong><em>, and </em></strong><a href="https://www.instagram.com/sneakysensei/"><strong><em>Instagram</em></strong></a>. If you want to read more such posts, you can <strong>follow me on Medium</strong> and other handles. Until then, keep coding and have a nice day!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3ee104f0e597" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>