{"componentChunkName":"component---src-templates-tag-js","path":"/tags/responsive/","result":{"data":{"site":{"siteMetadata":{"title":"LoginRadius Blog"}},"allMarkdownRemark":{"totalCount":2,"edges":[{"node":{"fields":{"slug":"/engineering/css3-responsive-icons/"},"html":"<p>What’s up, my neighbor? This is your boy, Darryl Tec. I am here to teach you how to create a responsive and awesomely scalable icon using CSS sprites. On top of that, we will also add a fallback for browsers that do not support SVG files.</p>\n<h3 id=\"quick-faq\" style=\"position:relative;\"><a href=\"#quick-faq\" aria-label=\"quick faq permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Quick FAQ.</h3>\n<p>SVG - Stands for Scalable Vector Graphics. In short it scales perfectly and you don’t need to worry about having a pixelated image anymore.</p>\n<p>Sprite - Google said it’s a fairy. This is not a fairy. It’s just a bunch of different icons in 1 image. Use it as background image and shift the positioning to show 1 icon. This saves you a lot of loading time. Because instead of loading, say 10 images, you’re loading just 1.</p>\n<p>CSS - Cascading Style Sheet. If you don’t know what this is, I have bad news for ya.</p>\n<h3 id=\"lets-begin\" style=\"position:relative;\"><a href=\"#lets-begin\" aria-label=\"lets begin permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Let's Begin</h3>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 51px; \"\n    >\n      <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 288.2352941176471%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAA6CAIAAAA/Te4cAAAACXBIWXMAAAsTAAALEwEAmpwYAAABbElEQVRIx+WX23LDIAxE8/8f68Z317fkTHZKKfXEEs4k6VQPNpZYI9AKxOlyQE56LcsyDMOnTb7B67ryGsexKIoPg5RluQHGcDbIU8ByMgfMZ9d1mJqmiU1WMKa+7xP9Ppi2QoK1rmvfyLTxFj2/SKZtXTBNmPjngDElq+UAs9rJhH1x/q18f3oeBefPmX5t27pJQhtW02B7SehtAqMhyIxMtN1ZRSeGnaYpJ6sgJg6j941MA4fJJ1Kiqioabre1E/MEn7OTKEiZifF3uZ0D3twGrHGGGBBzE+8AS+ljGNwAL3rSIbbug/kEyS/mecYF+sAzK5hkUhEAHoZTN4Q92AoeboLGAUZwsryJvA0+W+P877j9sBMjSZJ9MD34DBsgVA3nxj5YDIEY0BMrnLGCxe24uhVJrWCVQkHiysS0YLiqHsmJYxoZjdIjpw6La+x3ZpiCceeCEA6wjQuKioD2S+J2rPxxQXnA1egF4Csc/w4l/URccwAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"icons\"\n        title=\"icons\"\n        src=\"/static/9dcf447eeec21d2bbbedb5047cb2bd9d/7cda0/icons.png\"\n        srcset=\"/static/9dcf447eeec21d2bbbedb5047cb2bd9d/7cda0/icons.png 51w\"\n        sizes=\"(max-width: 51px) 100vw, 51px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n    </span></p>\n<p>First things first, we need a sprite image that consists four icons – or more, it’s up to you. Luckily, I have one in my digital pocket.  </p>\n<p>We’re just going to use a normal PNG file at first and change it later. Make sure it has a transparent background.</p>\n<p>Now, let’s make their placeholder. The icons you see above is using a grid of 32px by 32px and it’s lining up vertically. I made it like that so it’s easier to find anything, you’ll see why later.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"js\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">&lt;/</span><span class=\"mtk12\">pre</span><span class=\"mtk1\">&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk17\">&lt;</span><span class=\"mtk4\">div</span><span class=\"mtk1\"> </span><span class=\"mtk12\">class</span><span class=\"mtk1\">=</span><span class=\"mtk8\">&quot;frame&quot;</span><span class=\"mtk17\">&gt;&lt;/</span><span class=\"mtk4\">div</span><span class=\"mtk17\">&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk17\">&lt;</span><span class=\"mtk4\">pre</span><span class=\"mtk17\">&gt;</span></span></code></pre>\n<p>Now we need to style them.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"css\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk6\">.icon</span><span class=\"mtk1\"> {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">width</span><span class=\"mtk1\">: </span><span class=\"mtk7\">32px</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">height</span><span class=\"mtk1\">: </span><span class=\"mtk7\">32px</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">background</span><span class=\"mtk1\">: </span><span class=\"mtk8\">#eee</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">float</span><span class=\"mtk1\">: </span><span class=\"mtk8\">left</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">background-image</span><span class=\"mtk1\">: </span><span class=\"mtk11\">url</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;[ put your image here ]&quot;</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">background-size</span><span class=\"mtk1\">: </span><span class=\"mtk7\">100%</span><span class=\"mtk1\"> </span><span class=\"mtk8\">auto</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk3\">/*extra fluff*/</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">margin</span><span class=\"mtk1\">: </span><span class=\"mtk7\">10px</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">border-radius</span><span class=\"mtk1\">: </span><span class=\"mtk7\">5px</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}</span></span></code></pre>\n<p><strong>Explanation</strong></p>\n<p>Looks simple right? But the one thing that’s very important here is the background-size, which makes our sprite responsive. When you put 100% as the value, it will stretch the image depending on what width and height you have. So, be very careful when putting a value on both X and Y axis, because this will distort the image. To stop it from distorting, you should only put a value on one axis and put the other on ‘auto’ so the image scales proportionally.</p>\n<p>If you look at the image I provided, there’s only one icon on X-axis. That means we can just say 100% on X and auto on Y.</p>\n<p>So, we get background-size: 100% auto.</p>\n<p>Cool, right? No crazy percentage. Your icon should look like this:</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 636px; \"\n    >\n      <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 21.540880503144656%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAECAIAAAABPYjBAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAW0lEQVQI14WNQQrAIAwE/f9PmwgKmhC10i4KpbUH5xAyYZe460trrdbae8c8B29dwm7xlFKM0cxCCCKSc34U+6aMnKriP5o2wALFsZSyKU9UlJmJ2HtPdPxrkxtQxes+qKbsSAAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"facebook-icon\"\n        title=\"facebook-icon\"\n        src=\"/static/285fb8a2043639c416e2a33aa44eecc9/9be90/facebook-icon.png\"\n        srcset=\"/static/285fb8a2043639c416e2a33aa44eecc9/9be90/facebook-icon.png 636w\"\n        sizes=\"(max-width: 636px) 100vw, 636px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n    </span></p>\n<p>It’s time to map those icons properly. Facebook is not a problem because it’s first on the list. But for defaults sake, let’s add a code for it.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"css\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\"> </span><span class=\"mtk6\">.icon.facebook</span><span class=\"mtk1\"> {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">   </span><span class=\"mtk12\">background-position</span><span class=\"mtk1\">: </span><span class=\"mtk7\">0</span><span class=\"mtk1\"> </span><span class=\"mtk7\">0</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">   </span><span class=\"mtk12\">background-color</span><span class=\"mtk1\">: </span><span class=\"mtk8\">#3b5998</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\"> }</span></span></code></pre>\n<p>The positioning of 0 0 will map it to the top left of the element.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 202px; \"\n    >\n      <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 81.1881188118812%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAQCAIAAACZeshMAAAACXBIWXMAAAsTAAALEwEAmpwYAAAA8ElEQVQoz6WSWwtFQBzEff8PJN6VojwqSoQ8yP22HJdzJlvanGXPZWpr/fMzs2Ol5x+SsLZtO03phBBS1/U8z49d769JLEB1PMZx7Lpu27ZlWRZFMU0TB4bDsiwHTydJkoBZ17Vpmr7vEWEcxzOMVLqu+76fpimsgiAIw1CW5WEY6OcA3zkriqKqqqZptm2bppnnuWEYURR9BDuOY1mW53nYIDAmyEk3EOCu6/ixsTBFmWztbLGAq6qCOd+ZZeierR1tIzPOIoC5AoxTwJ8P3wsY+N/hLMsuYwthNI/C+W0L4csbJoTxF8kueoW/g2/0AmPSqSE3ibfjAAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"here-icon\"\n        title=\"here-icon\"\n        src=\"/static/c1cfab56ff8d6707145ed83661f038d6/2e687/here-icon.png\"\n        srcset=\"/static/c1cfab56ff8d6707145ed83661f038d6/2e687/here-icon.png 202w\"\n        sizes=\"(max-width: 202px) 100vw, 202px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n    </span></p>\n<p>For the background-color, you can get almost all of the official social icon colors in hex <a href=\"/social-media-colors-hex/\">here</a>.</p>\n<p>How about the rest? Well, there’s a simple formula for that.</p>\n<h3 id=\"the-raw-css-way\" style=\"position:relative;\"><a href=\"#the-raw-css-way\" aria-label=\"the raw css way permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>The raw CSS way</h3>\n<p>Since we’re making this responsive, we need to use percentage. We need to get the height of the image via percentage. Remember what I said earlier about x and y and scaling proportionally? Since the icons are lined up vertically, we need to get the Y-axis, which is the height. The easy way to do that is to just use 100% and never touch x axis again.</p>\n<p>Now, we have to get the position of the icons. Let’s start by counting the icons starting with 0 (zero).</p>\n<p>0 = Facebook</p>\n<p>1 = Google</p>\n<p>2 = LinkedIn</p>\n<p>3 = Twitter</p>\n<p>Facebook isn’t really a problem because we already know where it is. So, let’s exclude it from the count. That gives us 3 icons. We can now use those numbers to get the icon position.</p>\n<p>imageHeight = 100%<br>\niconCount = 3<br>\niconPosition = [1,  2 or 3]<br>\nWe need to go downwards on the sprite. That means it’s negative.</p>\n<p>-imageHeight / iconCount * iconPosition<br>\n-100% / 3 / 1 = 33.33% is Google’s position<br>\n-100% / 3 / 2 = 66.66% is LinkedIn’s position<br>\n-100% / 3 / 3 = 99.99% is Twitter’s position<br>\nThat gives us this code</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"css\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk6\">.icon.twitter</span><span class=\"mtk1\"> {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">background-position</span><span class=\"mtk1\">: </span><span class=\"mtk7\">0</span><span class=\"mtk1\"> </span><span class=\"mtk7\">-33.33%</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">background-color</span><span class=\"mtk1\">: </span><span class=\"mtk8\">#55acee</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk6\">.icon.linkedin</span><span class=\"mtk1\"> {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">background-position</span><span class=\"mtk1\">: </span><span class=\"mtk7\">0</span><span class=\"mtk1\"> </span><span class=\"mtk7\">-66.66%</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">background-color</span><span class=\"mtk1\">: </span><span class=\"mtk8\">#007bb6</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk6\">.icon.google</span><span class=\"mtk1\"> {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">background-position</span><span class=\"mtk1\">: </span><span class=\"mtk7\">0</span><span class=\"mtk1\"> </span><span class=\"mtk7\">-99.99%</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">background-color</span><span class=\"mtk1\">: </span><span class=\"mtk8\">#F90101</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}</span></span></code></pre>\n<h2 id=\"the-stylus-way\" style=\"position:relative;\"><a href=\"#the-stylus-way\" aria-label=\"the stylus way permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>The Stylus way</h2>\n<p>If you use stylus you can just do this:<br>\nCreate the mixin</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"javascript\" data-index=\"4\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk11\">getPos</span><span class=\"mtk1\">(</span><span class=\"mtk12\">imageHeight</span><span class=\"mtk1\">, </span><span class=\"mtk12\">iconCount</span><span class=\"mtk1\">, </span><span class=\"mtk12\">iconPosition</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">imageHeight</span><span class=\"mtk1\"> / </span><span class=\"mtk12\">iconCount</span><span class=\"mtk1\"> * </span><span class=\"mtk12\">iconPosition</span></span></code></pre>\n<p>Then call it.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"javascript\" data-index=\"5\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">.</span><span class=\"mtk12\">icon</span><span class=\"mtk1\">.</span><span class=\"mtk12\">twitter</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">background</span><span class=\"mtk1\">-</span><span class=\"mtk12\">position</span><span class=\"mtk1\"> </span><span class=\"mtk7\">0</span><span class=\"mtk1\"> </span><span class=\"mtk11\">getPos</span><span class=\"mtk1\">(-</span><span class=\"mtk7\">100</span><span class=\"mtk1\">%, </span><span class=\"mtk7\">3</span><span class=\"mtk1\">, </span><span class=\"mtk7\">1</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">background</span><span class=\"mtk1\">-</span><span class=\"mtk12\">color</span><span class=\"mtk1\"> #55</span><span class=\"mtk12\">acee</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\"> </span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">.</span><span class=\"mtk12\">icon</span><span class=\"mtk1\">.</span><span class=\"mtk12\">linkedin</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">background</span><span class=\"mtk1\">-</span><span class=\"mtk12\">position</span><span class=\"mtk1\"> </span><span class=\"mtk7\">0</span><span class=\"mtk1\"> </span><span class=\"mtk11\">getPos</span><span class=\"mtk1\">(-</span><span class=\"mtk7\">100</span><span class=\"mtk1\">%, </span><span class=\"mtk7\">3</span><span class=\"mtk1\">, </span><span class=\"mtk7\">2</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">background</span><span class=\"mtk1\">-</span><span class=\"mtk12\">color</span><span class=\"mtk1\"> #007</span><span class=\"mtk12\">bb6</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\"> </span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">.</span><span class=\"mtk12\">icon</span><span class=\"mtk1\">.</span><span class=\"mtk12\">google</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">background</span><span class=\"mtk1\">-</span><span class=\"mtk12\">position</span><span class=\"mtk1\"> </span><span class=\"mtk7\">0</span><span class=\"mtk1\"> </span><span class=\"mtk11\">getPos</span><span class=\"mtk1\">(-</span><span class=\"mtk7\">100</span><span class=\"mtk1\">%, </span><span class=\"mtk7\">3</span><span class=\"mtk1\">, </span><span class=\"mtk7\">3</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">background</span><span class=\"mtk1\">-</span><span class=\"mtk12\">color</span><span class=\"mtk1\"> #</span><span class=\"mtk12\">F90101</span></span></code></pre>\n<p>By now your icon should look like this.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 644px; \"\n    >\n      <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 16.304347826086957%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAADCAIAAAAcOLh5AAAACXBIWXMAAAsTAAALEwEAmpwYAAAAjElEQVQI12P4jwT+/v03e82pWSuOXXr6bemlL+effZ+x73rX/ntfTh//1FL689wJiCK4egYI9e8fiPz1609U+crIwsUbL7+v2/dhzeWPSbMPBS68+GLp/FdWil/WLAKp/PMbXTMc3Hrw+vrdlx+//7326te7b3+vP3t/6dmnP+/e/rpw6u+7Nwh7wAAA4+GfO6qUrmcAAAAASUVORK5CYII='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"color-icons\"\n        title=\"color-icons\"\n        src=\"/static/dbde29abe640e8c397f80b3324234398/78274/color-icons.png\"\n        srcset=\"/static/dbde29abe640e8c397f80b3324234398/78274/color-icons.png 644w\"\n        sizes=\"(max-width: 644px) 100vw, 644px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n    </span></p>\n<p>It’s time to change that PNG background to an SVG background.</p>\n<p>and link it to your background image</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"css\" data-index=\"6\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk6\">.icon</span><span class=\"mtk1\"> {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">width</span><span class=\"mtk1\">: </span><span class=\"mtk7\">32px</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">height</span><span class=\"mtk1\">: </span><span class=\"mtk7\">32px</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">background</span><span class=\"mtk1\">: </span><span class=\"mtk8\">#eee</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">float</span><span class=\"mtk1\">: </span><span class=\"mtk8\">left</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">background-image</span><span class=\"mtk1\">: </span><span class=\"mtk11\">url</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;</span><span class=\"mtk14\">[ put your PNG image here ]”);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">background-image:</span><span class=\"mtk1\"> </span><span class=\"mtk12\">linear-gradient(transparent,</span><span class=\"mtk1\"> </span><span class=\"mtk12\">transparent</span><span class=\"mtk1\">), </span><span class=\"mtk11\">url</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;</span><span class=\"mtk14\">[ &lt;b&gt;put your SVG image here&lt;/b&gt; ]”);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">background-size:</span><span class=\"mtk1\"> </span><span class=\"mtk12\">100%</span><span class=\"mtk1\"> </span><span class=\"mtk12\">auto;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\"> </span></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">/*extra</span><span class=\"mtk1\"> </span><span class=\"mtk12\">fluff*/</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">margin:</span><span class=\"mtk1\"> </span><span class=\"mtk12\">10px;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">border-radius:</span><span class=\"mtk1\"> </span><span class=\"mtk12\">5px;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">}</span></span></code></pre>\n<p>If you’re wondering why we’re adding 2 background images is because not all browsers support SVG file. The linear-gradient(transparent, transparent) tricks the CSS into falling back to the PNG image if it doesn’t support it.</p>\n<p>Now, Rescale your width and height and see the magic of responsive icons.</p>\n<p>Aaaaaand then we’re done. That’s how you create a responsive icon.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n  .dark-default-dark .mtk1 { color: #D4D4D4; }\n  .dark-default-dark .mtk12 { color: #9CDCFE; }\n  .dark-default-dark .mtk17 { color: #808080; }\n  .dark-default-dark .mtk4 { color: #569CD6; }\n  .dark-default-dark .mtk8 { color: #CE9178; }\n  .dark-default-dark .mtk6 { color: #D7BA7D; }\n  .dark-default-dark .mtk7 { color: #B5CEA8; }\n  .dark-default-dark .mtk11 { color: #DCDCAA; }\n  .dark-default-dark .mtk3 { color: #6A9955; }\n  .dark-default-dark .mtk14 { color: #F44747; }\n</style>","frontmatter":{"date":"July 21, 2015","updated_date":null,"title":"CSS3 Responsive Icons","tags":["CSS","Responsive","UI"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1,"src":"/static/35808f86ae877ca92a333571b6b36e6f/630fb/css3-responsive-icons-300x300.png","srcSet":"/static/35808f86ae877ca92a333571b6b36e6f/69585/css3-responsive-icons-300x300.png 200w,\n/static/35808f86ae877ca92a333571b6b36e6f/630fb/css3-responsive-icons-300x300.png 300w","sizes":"(max-width: 300px) 100vw, 300px"}}},"author":{"id":"Team LoginRadius","github":"LoginRadius","avatar":null}}}},{"node":{"fields":{"slug":"/engineering/css-responsive-grid/"},"html":"<p>I know what you're thinking. Why am I writing this blog? There's like a bunch of apps out there that do this thing automatically. See, that's the problem, \"automatically\". That means you have no control over it when it messes things up. Even if you do, you might mess things up for other stuff too.</p>\n<p>This is why controlling your own code is better than letting other apps do it. Just like what my mentor said, \"Don't let the apps write your own code, they screw it up\".</p>\n<p><strong>Let's get started</strong></p>\n<p>Let's make a responsive grid without using any crazy percentages or complicated margins. Sounds cool? Good.</p>\n<p>By now you know how complicated it is to calculate all the width and margins to make sure they correspond with each other. Not to mention that you need to use percentages on both of them which sometimes doesn't work, especially with our friend IE8.</p>\n<p>What if we create a grid that doesn't need margins for spacing? Or using percentages to separate objects? Sounds awesome right? No more calculating for the right percentages and getting crazy numbers.</p>\n<p>Let's start with 4 grids first. So 100 / 4 = 25 then add a percentage (%) sign and we get 25% for our 4 grid system. No crazy numbers there, but we need spacing. To add spaces between those elements just add padding on left and right. Pixels or percent, it's up to you. Yes, yes, padding adds width but, if you add \"box-sizing: border box\" on our lovely grid, the padding will start respecting your decision to add a width of 25% and start pushing everything inside.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 768px; \"\n    >\n      <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 16.615384615384617%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAADCAYAAACTWi8uAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAi0lEQVQI12WMOw7CMBBEfbcUHAIXOQc9R0FGUQDlHvTIjT+y/L2EmUGJZIliPTtvZyy891fn3Ka1fhlj1hDCklK6xxgVVFFx26y1D/qD1VpVzlntuRv0WUq5CBw+AB3HTgXsYB2Bn6e21v4YPTsjQ/ct8JxgZswZIxGSCEvuCMqDcUbGzMDYnfHX9AUPcsjc7sEjBgAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"grid\"\n        title=\"grid\"\n        src=\"/static/7cf3e7c8de8647c6f451ada302ed5451/e5715/grid.png\"\n        srcset=\"/static/7cf3e7c8de8647c6f451ada302ed5451/a6d36/grid.png 650w,\n/static/7cf3e7c8de8647c6f451ada302ed5451/e5715/grid.png 768w,\n/static/7cf3e7c8de8647c6f451ada302ed5451/d9199/grid.png 960w\"\n        sizes=\"(max-width: 768px) 100vw, 768px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n    </span></p>\n<p>There you have it, Spacing. By now your CSS code will look like this.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"css\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk6\">div</span><span class=\"mtk1\"> {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">width</span><span class=\"mtk1\">: </span><span class=\"mtk7\">25%</span><span class=\"mtk1\">;</span><span class=\"mtk3\">/*4 grid system*/</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">padding</span><span class=\"mtk1\">: </span><span class=\"mtk7\">0</span><span class=\"mtk1\"> </span><span class=\"mtk7\">5px</span><span class=\"mtk1\">;</span><span class=\"mtk3\">/*spacing*/</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">box-sizing</span><span class=\"mtk1\">: </span><span class=\"mtk8\">border-box</span><span class=\"mtk1\">;</span><span class=\"mtk3\">/*removes the added with from padding*/</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">float</span><span class=\"mtk1\">: </span><span class=\"mtk8\">left</span><span class=\"mtk1\">; </span><span class=\"mtk3\">/*put them side by side*/</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}</span></span></code></pre>\n<p><strong>Problem</strong></p>\n<p>Of course, there's no perfect code. Since we're using padding on both left and right. you'll see some spacing too on the left and right side of your website. For example, I used 5px padding. That means I will have a 5px offset on the left and right side of my screen. It doesn't align with my site anymore.</p>\n<p><strong>Solution</strong></p>\n<p>To tackle this problem we need to add a margin on the parent of our grid. Since my padding is 5px, I need to add a -5px margin on left and right to stretch it back to it's intended size. So our code will look like this now.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"css\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">parent {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">margin</span><span class=\"mtk1\">: </span><span class=\"mtk7\">0</span><span class=\"mtk1\"> </span><span class=\"mtk7\">-5px</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">parent </span><span class=\"mtk6\">div</span><span class=\"mtk1\"> {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">width</span><span class=\"mtk1\">: </span><span class=\"mtk7\">25%</span><span class=\"mtk1\">;</span><span class=\"mtk3\">/*4 grid system*/</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">padding</span><span class=\"mtk1\">: </span><span class=\"mtk7\">0</span><span class=\"mtk1\"> </span><span class=\"mtk7\">5px</span><span class=\"mtk1\">;</span><span class=\"mtk3\">/*spacing*/</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">box-sizing</span><span class=\"mtk1\">: </span><span class=\"mtk8\">border-box</span><span class=\"mtk1\">;</span><span class=\"mtk3\">/*removes the added with from padding*/</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">float</span><span class=\"mtk1\">: </span><span class=\"mtk8\">left</span><span class=\"mtk1\">; </span><span class=\"mtk3\">/*put them side by side*/</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}</span></span></code></pre>\n<p> I'd like to also point out that if your parent has a width (which it doesn't really need because our grid css will add a width to the parent), the negative margins won't work. So, be careful.</p>\n<p>Another thing to watch out for, because we're using floats, we will also need to use a clearfix hack on our parent.</p>\n<p>Using the simple code we learned today will create all the grids you want.</p>\n<p>1 = 100%<br>\n2 = 50%<br>\n3 = 33.33%<br>\n4 = 25%<br>\n5 = 20%<br>\n6 = 16.66%<br>\n7 = 14.28%<br>\n8 = 12.5%<br>\n9 = 11.11%<br>\n10 = 10%</p>\n<p>and so on.</p>\n<p>See it on <a href=\"https://codepen.io/notdarryltec/pen/emMpQB\">codepen</a>.</p>\n<p>And this is where it ends. I hope this will help you easily create a grid in the near future. If you have any other tips, leave it in the comments and I'll be happy to try it out. Cheers.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n  .dark-default-dark .mtk6 { color: #D7BA7D; }\n  .dark-default-dark .mtk1 { color: #D4D4D4; }\n  .dark-default-dark .mtk12 { color: #9CDCFE; }\n  .dark-default-dark .mtk7 { color: #B5CEA8; }\n  .dark-default-dark .mtk3 { color: #6A9955; }\n  .dark-default-dark .mtk8 { color: #CE9178; }\n</style>","frontmatter":{"date":"March 16, 2015","updated_date":null,"title":"CSS Responsive Grid, Re-imagined","tags":["CSS","Grid","Responsive"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1,"src":"/static/61bc9402fec623803bf04d21e948e84a/2a4de/desdev.png","srcSet":"/static/61bc9402fec623803bf04d21e948e84a/69585/desdev.png 200w,\n/static/61bc9402fec623803bf04d21e948e84a/497c6/desdev.png 400w,\n/static/61bc9402fec623803bf04d21e948e84a/2a4de/desdev.png 600w","sizes":"(max-width: 600px) 100vw, 600px"}}},"author":{"id":"Team LoginRadius","github":"LoginRadius","avatar":null}}}}]}},"pageContext":{"tag":"Responsive"}},"staticQueryHashes":["1171199041","1384082988","2100481360","23180105","528864852"]}