<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet type="text/css" href="https://www.aaron-gustafson.com/c/feed.min.css" ?><feed xmlns="http://www.w3.org/2005/Atom"
      xmlns:amg="https://www.aaron-gustafson.com.com/amg-dtd/"><title>Aaron Gustafson: Content tagged SVG</title><subtitle>The latest 20 posts and links tagged SVG.</subtitle><id>https://www.aaron-gustafson.com</id><link href="https://www.aaron-gustafson.com/feeds/svg.xml" rel="self"/><link href="https://www.aaron-gustafson.com"/><author><name>Aaron Gustafson</name><uri>https://www.aaron-gustafson.com</uri></author><updated>2023-08-21T21:11:09Z</updated><entry><id>https://www.aaron-gustafson.com/notebook/please-size-your-inline-svgs/</id><title type="html"><![CDATA[✍🏻 Please size your inline SVGs]]></title><link href="https://www.aaron-gustafson.com/notebook/please-size-your-inline-svgs/" rel="alternate" type="text/html" /><published>2023-08-21T21:11:09Z</published><content type="html" xml:base="https://www.aaron-gustafson.com"><![CDATA[<p>While it is a bit of an edge case, every now and then I’ll hit a site—yes, even a high profile one—and the CSS will fail to load for some reason. When this happens, inevitably every inline SVG resource on the page will grow to fill the entire width of my viewport, making for a <em>really</em> awkward experience.</p><h2 id="what%E2%80%99s-the-issue%3F" tabindex="-1"><a class="header-anchor" href="#what%E2%80%99s-the-issue%3F" aria-hidden="true">#</a> What’s the issue?</h2><p>Not to pick on anyone in particular, but consider <a href="https://presentations.aaron-gustafson.com/uygzjR/progressive-enhancement-where-do-i-begin#saVRzdC">this example from a recent talk I gave</a>:</p><figure id="2023-08-21-01"><p><img src="https://www.aaron-gustafson.com/i/posts/2023-08-21/01.png" alt="" width="696" height="481"></p><figcaption>The U.S. Transportation Safety Administration’s TSA PreCheck® landing page, with CSS applied.</figcaption></figure><p>When CSS fails to load, however, check out what happens:</p><figure id="2023-08-21-02"><p><img src="https://www.aaron-gustafson.com/i/posts/2023-08-21/02.png" alt="" width="696" height="481"></p><figcaption>The U.S. Transportation Safety Administration’s TSA PreCheck® landing page, without CSS applied. Note the <strong>huge</strong> SVG.</figcaption></figure><p>Yeah, that’s an inline SVG. You see, without any explicit dimensions set, the SVG will naturally grow to become as large as possible. Chances are you’re constraining that growth in CSS somewhere, but when your CSS fails to apply for any reason, every inline SVG on your site will swell like <a href="https://roalddahl.fandom.com/wiki/Violet_Beauregarde">Violet Beauregarde after eating Willy Wonka’s “three-course dinner” chewing gum</a>.</p><h2 id="how-do-we-solve-this%3F" tabindex="-1"><a class="header-anchor" href="#how-do-we-solve-this%3F" aria-hidden="true">#</a> How do we solve this?</h2><p>Thankfully, this is a pretty easy situation to avoid: just set an explicit <code>width</code> and <code>height</code>. To use an example from this site, instead of saying</p><pre class="language-html" tabindex="0"><code class="language-html"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>svg</span><span class="token attr-name">viewBox</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">“</span>0 0 38 48<span class="token punctuation">”</span></span><span class="token attr-name">version</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">“</span>1.1<span class="token punctuation">”</span></span><span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">“</span><a href="http://www.w3.org/2000/svg">http://www.w3.org/2000/svg</a><span class="token punctuation">”</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>svg</span><span class="token punctuation">&gt;</span></span></code></pre><p>You can explicitly set the <code>width</code> and <code>height</code> in the <code>svg</code> element like this:</p><pre class="language-html" tabindex="0"><code class="language-html"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>svg</span><span class="token attr-name">width</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">“</span>38<span class="token punctuation">”</span></span><span class="token attr-name">height</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">“</span>48<span class="token punctuation">”</span></span><span class="token attr-name">viewBox</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">“</span>0 0 38 48<span class="token punctuation">”</span></span><span class="token attr-name">version</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">“</span>1.1<span class="token punctuation">”</span></span><span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">“</span><a href="http://www.w3.org/2000/svg">http://www.w3.org/2000/svg</a><span class="token punctuation">”</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>svg</span><span class="token punctuation">&gt;</span></span></code></pre><p>What you set these values to will likely vary depending on how the icon is being used. In a pinch, you could also pull the values directly from the <code>viewbox</code> value. And using that value, you could even make the inline values dynamic within your template, reading in the <code>viewbox</code> values and tweaking them to a ratio specific to the context.</p><p>Setting the SVG’s dimensions inline like this doesn’t restrict their flexibility either. You can still use CSS to override these inline values and set the SVG to whatever size you wish:</p><pre class="language-css" tabindex="0"><code class="language-css"><span class="token selector">svg</span><span class="token punctuation">{</span><span class="token property">inline-size</span><span class="token punctuation">:</span> 200px<span class="token punctuation">;</span><span class="token property">block-size</span><span class="token punctuation">:</span> 200px<span class="token punctuation">;</span><span class="token punctuation">}</span></code></pre><p>I’ve thrown together a quick comparison over on CodePen so you can see the three different states:</p><figure id="2023-08-21-03"><iframe class="codepen" height="500" style="width:100%;" scrolling="no" title="CodePen Embed" src="https://codepen.io/anon/embed/JjwoLME?height=500&theme-id=dark&default-tab=result" frameborder="0" loading="lazy" allowtransparency="true" allowfullscreen="true"><p><a href="https://codepen.io/aarongustafson/pen/JjwoLME" target="_blank" rel="noopener">See the Pen</a></p></iframe></figure><p>And now that you know, please, please, <em>please</em> take a few minutes to make this small, simple change to your websites. While not a catastrophic issue, taking care to control how your sites render in the worst of circumstances goes a long way to demonstrating thoughtful consideration of your users.</p><hr><p><strong>Update:</strong> For more in-depth info on this topic (and scenarios where CSS isn’t applied to your SVGs), be sure to check out <a href="https://www.sarasoueidan.com/blog/svg-style-inheritance-and-fousvg/">this piece from Sara Soueidan</a>.</p>]]></content><amg:twitter><![CDATA[#ProTip: If you don’t explicitly set the dimensions of your inline SVGs, they’ll render full width when your CSS isn’t applied properly.]]></amg:twitter><amg:summary><![CDATA[<p>While it is a bit of an edge case, every now and then I’ll hit a site—yes, even a high profile one—and the CSS will fail to load for some reason. When this happens, inevitably every inline SVG resource on the page will grow to fill the entire width of my viewport, making for a <em>really</em> awkward experience.</p>]]></amg:summary><summary type="html"><![CDATA[<p>While it is a bit of an edge case, every now and then I’ll hit a site—yes, even a high profile one—and the CSS will fail to load for some reason. When this happens, inevitably every inline SVG resource on the page will grow to fill the entire width of my viewport, making for a <em>really</em> awkward experience.</p>]]></summary><category term="CSS" /><category term="design" /><category term="HTML" /><category term="SVG" /><category term="web design" /><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://www.aaron-gustafson.com/i/posts/2023-08-21/hero.jpg" /></entry><entry><id>https://www.aaron-gustafson.com/notebook/links/svg-can-do-that/</id><title type="html"><![CDATA[🔗 SVG can do THAT?!]]></title><link href="https://www.aaron-gustafson.com/notebook/links/svg-can-do-that/" rel="alternate" type="text/html" /><link href="https://slides.com/sdrasner/svg-can-do-that" rel="related" type="text/html" /><published>2017-08-25T19:03:11Z</published><content type="html" xml:base="https://www.aaron-gustafson.com"><![CDATA[<p>This is an incredible presentation by <a href="https://sarahdrasnerdesign.com/">Sarah Drasner</a> on many of the very cool things you can do with SVG. So much inspiration!</p>]]></content><amg:twitter><![CDATA[Great presentation by @sarah_edo: SVG can do THAT?!]]></amg:twitter><category term="SVG" /><category term="animation" /><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://media.slid.es/thumbnails/cff6dcef98b71a676eb9d5e4d2198773/thumb.jpg?373511886" /></entry></feed>