<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

 <title></title>
 <link href="https://sarasoueidan.com/rss.xml" rel="self"/>
 <link href="https://sarasoueidan.com/"/>
 <id>https://sarasoueidan.com</id>

 
 <entry>
   <title>Building a fully-accessible help tooltip</title>
   <link href="http://sarasoueidan.com//blog/accessible-tooltips/"/>
   <id>https://sarasoueidan.com/blog/accessible-tooltips</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;
	Today is one of those days that started out with a Google search for yet another accessibility question/concern. I’m working on a new project for my client &lt;a href=&quot;https://sarasoueidan.com/case-studies/provata-health/&quot;&gt;Provata&lt;/a&gt; and part of that project is to build a sweet and seemingly simple help tooltip which explains to the reader/user what the &lt;a href=&quot;https://www.cvdriskchecksecure.com/framinghamriskscore.aspx&quot;&gt;Framingham calculator&lt;/a&gt; is.
&lt;/p&gt;

&lt;p&gt;The tooltip is triggered by a small help icon like the one shown in the top right corner of this screenshot:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/provata-tooltip.png&quot; alt=&quot;The tooltip trigger as provided in the project mockup&quot; /&gt;&lt;/p&gt;

&lt;p&gt;As with every project, starting out by thinking about what HTML element to use to mark up a component was the first thing I did. But it turned out there is no HTML element to mark up a tooltip like this. And when we have no semantic elements to mark up our components, we are faced with the challenge to make sure that assistive technologies (ATs)—which are usually capable of understanding and conveying meaning via semantics—are able to understand what our elements really are and what they do.&lt;/p&gt;

&lt;h2 id=&quot;aria-attributes-to-the-rescue&quot;&gt;ARIA attributes to the rescue?&lt;/h2&gt;

&lt;p&gt;Making elements more readable by ATs when HTML isn’t enough is possible using ARIA attributes. For example, you can get by creating a progress bar without actually using the not-too-well-supported HTML &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;progess&amp;gt;&lt;/code&gt; element. Making that bar look and behave like a bar is straightforward using some CSS, but if you’re using &lt;code class=&quot;highlighter-rouge&quot;&gt;div&lt;/code&gt;s and &lt;code class=&quot;highlighter-rouge&quot;&gt;span&lt;/code&gt;s to build it, you need to give ATs something more to make something of those non-semantic elements. This is where ARIA attributes like &lt;code class=&quot;highlighter-rouge&quot;&gt;role=&quot;progressbar&quot;&lt;/code&gt; and its companions, the helpful &lt;code class=&quot;highlighter-rouge&quot;&gt;aria-valuenow&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;aria-valuemin&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;aria-valuemax&lt;/code&gt; (among others), come in handy. Sprinkling in some simple JavaScript, you upate the value inside &lt;code class=&quot;highlighter-rouge&quot;&gt;aria-valuenow&lt;/code&gt; as the user progresses through whatever steps you provided them with and you’re generally good to go. You can read more about this &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_progressbar_role&quot;&gt;on MDN&lt;/a&gt; if you’re curious or unfamilir with this.&lt;/p&gt;

&lt;p&gt;Great.&lt;/p&gt;

&lt;p&gt;We have an ARIA attribute value to help us indicate that a certain element is a tooltip: &lt;code class=&quot;highlighter-rouge&quot;&gt;tooltip&lt;/code&gt;. Using &lt;code class=&quot;highlighter-rouge&quot;&gt;role=&quot;tooltip&quot;&lt;/code&gt; ATs know that this element is indeed a tooltip.&lt;/p&gt;

&lt;p&gt;But tooltips are usually triggered by an action performed on another element.&lt;/p&gt;

&lt;p&gt;Let’s first get back to the basics. According to Wikipedia:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;The tooltip or infotip or a hint is a common graphical user interface element. It is used in conjunction with a cursor, usually a pointer. The user hovers the pointer over an item, without clicking it, and a tooltip may appear—a small “hover box” with information about the item being hovered over. Tooltips do not usually appear on mobile operating systems, because there is no cursor (though tooltips may be displayed when using a mouse).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I have a problem with the “Tooltips do not usually appear on mobile operating systems, because there is no cursor” part because mobile users have just as much right to get information on a page as non-mobile users. Whether you’re using a mouse or your finger, you should be able to understand what the Framingham score is, so this tooltip should be accessible in all contexts. And this is exactly where the fun starts.&lt;/p&gt;

&lt;h2 id=&quot;triggering-the-openingclosing-of-the-tooltip&quot;&gt;Triggering the opening/closing of the tooltip&lt;/h2&gt;

&lt;p&gt;I knew I couldn’t rely on hover alone (even though my client only requested hover) because, unless you have a touch screen with a fancy digital pen which allows for hover to be triggered, you won’t be able to see the hint inside the tooltip, and that is unacceptable.&lt;/p&gt;

&lt;p&gt;I set out to make the tooltip show when the help icon is both hovered and clicked.&lt;/p&gt;

&lt;p&gt;It is worth noting at this point that sometimes tooltips work on both touch and pointer screens without having to do any extra work, such as those that are shown as help labels on &lt;code class=&quot;highlighter-rouge&quot;&gt;input&lt;/code&gt; fields, like &lt;a href=&quot;http://heydonworks.com/practical_aria_examples/#input-tooltip&quot;&gt;this example&lt;/a&gt; by Heydon Pickering. In scenarios like this one, &lt;code class=&quot;highlighter-rouge&quot;&gt;role=&quot;tooltip&quot;&lt;/code&gt; in combination with &lt;code class=&quot;highlighter-rouge&quot;&gt;aria-describedby&lt;/code&gt; are enough to indicate to ATs that this piece of text is indeed a tooltip which describes the content or functionality of the input field. Marking the tooltip up in this case is very easy because it’s already clear enough what it is. All that remains is for you to show/hide the text when the &lt;code class=&quot;highlighter-rouge&quot;&gt;input&lt;/code&gt; field is focused, and this can be done using a couple of lines of CSS alone. The experience is great on mouse and touch screens, and everyone gets to experience it the same way.&lt;/p&gt;

&lt;p&gt;However, things aren’t so straightforward when you have an example like the one in my project. And apparently I’m not the only one who’s been here and has been just as confused as to how to approach this.&lt;/p&gt;

&lt;p&gt;Here are a few of the ideas that crossed my mind as I was thinking about the implementation of this:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Use a &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;button&amp;gt;&lt;/code&gt; to trigger the opening and closing of the tooltip. Tooltip would initially be hidden so that ATs won’t read it out loud as the user moves down the page. The tooltip interrupts the flow of the content in our project’s case and so should only be shown on demand.&lt;/li&gt;
  &lt;li&gt;“on demand” means when the user hovers/clicks/taps/focuses the trigger.&lt;/li&gt;
  &lt;li&gt;The tooltip would initially be hidden using &lt;code class=&quot;highlighter-rouge&quot;&gt;display: none&lt;/code&gt; and only shown on demand.&lt;/li&gt;
  &lt;li&gt;Since semantics and “how machines would read this” is how I start all of my HTML code, I thought:
    &lt;ul&gt;
      &lt;li&gt;if I use &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;button&amp;gt;&lt;/code&gt; I’ll have to use &lt;code class=&quot;highlighter-rouge&quot;&gt;aria-controls&lt;/code&gt; to indicate what that button controls and that it will show/hide some element when it is clicked. All good.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Speaking of semantics, I could also use a link (&lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;a&amp;gt;&lt;/code&gt;) that links to a specific section on the page, which in my case is the element containing the hint.&lt;/li&gt;
  &lt;li&gt;When using a link, the user will &lt;em&gt;jump&lt;/em&gt; to the section on the page to which they’re being taken. I hate page jumps. I avoid them like the plague unless the decision to jump is intentional. (For example, a “Back to top” link on a very long page.)&lt;/li&gt;
  &lt;li&gt;In order to avoid the jump when using &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;a&amp;gt;&lt;/code&gt;, &lt;em&gt;and&lt;/em&gt; in order to make the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;button&amp;gt;&lt;/code&gt; open &lt;em&gt;and&lt;/em&gt; close the tooltip, I need JavaScript.&lt;/li&gt;
  &lt;li&gt;For some holistic reason, I wanna avoid JavaScript and use it only when necessary. What if the user is a senior using some very old machine that has JS disabled?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Usually, when I’m torn between two solutions and when the JS-no-JS thoughts start floating in my mind, I look for second opinions. My first source of a second opinion is Google. I thought: “someone else must have built one of these before, and so must have somebody else too, so let’s see how they approached it and solved these issues”.&lt;/p&gt;

&lt;p&gt;At this point I was still on the “I want to do this without JS if possible” boat, but believe it or not I love JS and was completely open to using it if the JS-based solution is better than all of the others.&lt;/p&gt;

&lt;p&gt;Googling got me to land on a question that was exactly the same as mine. I found a lot of similar questions but all of them were more like the tooltip-on-input example. I found &lt;a href=&quot;http://webaim.org/discussion/mail_thread?thread=5041&quot;&gt;a very old thread&lt;/a&gt; started by &lt;a href=&quot;http://twitter.com/zomigi&quot;&gt;Zoe Gillenwater&lt;/a&gt; who had the same question back in 2011 that I had today. I am aware that some technical details in there would/could be invalid today but the general principles are still valid. I highly recommend you read through that thread before continuing to read this article because everything else in here is based on some of the insights I got from that thread. Main points mentioned in the thread included:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Avoid using &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;button&amp;gt;&lt;/code&gt; and stick to &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;a&amp;gt;&lt;/code&gt; because links can be used to practically anything whereas buttons should be left for using in forms. I have to say &lt;strong&gt;I disagree here&lt;/strong&gt;. If you know a very good reason to avoid using &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;button&amp;gt;&lt;/code&gt;, please &lt;a href=&quot;https://twitter.com/SaraSoueidan&quot;&gt;let me know&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Using &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;a&amp;gt;&lt;/code&gt; you have to consider/remember:
    &lt;ul&gt;
      &lt;li&gt;If you use &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;a&amp;gt;&lt;/code&gt; and have the hint be included inside of it:
        &lt;ul&gt;
          &lt;li&gt;The code looks like this:
            &lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;      &amp;lt;a href=&quot;#&quot; aria-describedby=&quot;#tip&quot;&amp;gt;
          &amp;lt;!-- your icon here, img or svg --&amp;gt;
          &amp;lt;span id=&quot;tip&quot;&amp;gt; Your hint text here &amp;lt;/span&amp;gt;
      &amp;lt;/a&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
            &lt;/div&gt;
          &lt;/li&gt;
          &lt;li&gt;The text will be readable with the flow (which is &lt;em&gt;not&lt;/em&gt; favorable in my case) because the content of the hint interrupts the flow of the information and data displayed in the main content.&lt;/li&gt;
          &lt;li&gt;Using CSS only, you can hide and show the text with &lt;code class=&quot;highlighter-rouge&quot;&gt;display: none&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;display: block&lt;/code&gt; respectively when the link is hovered. This is good for sighted users using a mouse. However,…&lt;/li&gt;
          &lt;li&gt;If you hide the text using &lt;code class=&quot;highlighter-rouge&quot;&gt;display: none&lt;/code&gt; inside the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;a&amp;gt;&lt;/code&gt; and show it on focus, ATs won’t be able to read the shown text because the contents of the link are announced on initial &lt;code class=&quot;highlighter-rouge&quot;&gt;focus&lt;/code&gt; only and won’t be re-announced when the text is displayed inside the link with &lt;code class=&quot;highlighter-rouge&quot;&gt;display: block&lt;/code&gt;.&lt;/li&gt;
          &lt;li&gt;Testing this out I noticed that since the link doesn’t really link to anywhere, the user will jump to the top of the page when they tap the link on a touch screen. This is particularly horrible in our case because we have a long page and jumping up will only confuse the users. &lt;strong&gt;This can be solved using JavaScript to &lt;code class=&quot;highlighter-rouge&quot;&gt;preventDefault&lt;/code&gt; on click.&lt;/strong&gt;&lt;/li&gt;
          &lt;li&gt;&lt;em&gt;(My opinion: I really dislike the fact that the link doesn’t really link anywhere. It’s a self-containing link, if that makes any sense, which negates the role of the link to begin with, in my opinion. So even though I tested it out of curiosity, I knew I didn’t want to use this.)&lt;/em&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;If you use &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;a&amp;gt;&lt;/code&gt; and have it link to a separate piece of text (not a descendant of the link itself):
        &lt;ul&gt;
          &lt;li&gt;The code would be along the lines of: &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;a href=&quot;#tip&quot;&amp;gt;&amp;lt;!-- icon here --&amp;gt;&amp;lt;/a&amp;gt;&amp;lt;div id=&quot;tip&quot;&amp;gt; &amp;lt;!-- tooltip text here --&amp;gt; &amp;lt;/div&amp;gt;&lt;/code&gt;&lt;/li&gt;
          &lt;li&gt;The link still causes a page jump unless using JavaScript.&lt;/li&gt;
          &lt;li&gt;On touch screens, tapping the link focuses it which in turn opens the tooltip, but the link image remains the same and there is no way to close the tooltip unless the user clicks anywhere outside it to remove its focus, which is not intuitive because there’s a lot of cognitive effort and guesswork and “how do I close this” guesswork and frustration involved, especially since the tooltip covers the content when open.&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Every single no-JS solution came with a very bad downside that negatively affected the user experience. JavaScript is the way to make this work properly for every user in every context. It solves every one of those problems mentioned, except for the “this link doesn’t link anywhere” problem which can only be solved by, well, not using a link that links nowhere. :D (Some may disagree that a link not linking anywhere is not particularly wrong or invalid, but it’s not something I’d personally do.) I ended up choosing a solution that works, using JavaScript, and has a not-too-bad fallback as well. This pretty much covers the main thoughts and considerations I had throughout this. &lt;strong&gt;I will post the markup and functionality I ended up using in this project’s case study.&lt;/strong&gt;&lt;/p&gt;

&lt;h2 id=&quot;conclusions&quot;&gt;Conclusion(s)?&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;JavaScript is imperative to make fully-accessible interactive components.&lt;/strong&gt; Sure, you can get away without using it in some cases, but a lot of accessibility requires JavaScript. Some ARIA roles and attributes are absolutely necessary to make components accessible, and many of those will simply not behave as they need to unless you make them work with JavaScript. Since my client is a health company, it is more likely that their users have one form of disability or the other than it is that they have JS disabled, so it’s safe to worry about not implementing JavaScript than about it not running.&lt;/p&gt;

&lt;p&gt;If you’re interested in learning more about which attributes require what, I highly recommend checking out the &lt;a href=&quot;http://whatsock.com/training/matrices/&quot;&gt;ARIA Role Matrices&lt;/a&gt; from WhatSock. It provides and easy-to-scan and read overview.&lt;/p&gt;

&lt;p&gt;Paul J Adam has also created &lt;a href=&quot;http://pauljadam.com/demos/aria-role-tooltip.html&quot;&gt;a demo&lt;/a&gt; to show the different ways to show tooltips on hover. His examples still use JavaScript to make the components more accessible by toggling ARIA attributes as the tooltips open/close. This one is also definitely worth checking out.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Did I miss something? &lt;a href=&quot;https://twitter.com/SaraSoueidan&quot;&gt;Let me know on in a tweet&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;


    
  </content>
 </entry>
 
 <entry>
   <title>Four Tips for Getting More Work Done While Traveling</title>
   <link href="http://sarasoueidan.com//blog/knomo-travel-productivity-tips/"/>
   <id>https://sarasoueidan.com/blog/knomo-travel-productivity-tips</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;
	Staying productive during traveling can sometimes be a challenge. There are some things you can do to make the most out of your transit times and get more work done. Knomo—a UK based company that makes bags for digital nomads and techies like us, asked me to share some tips for staying productive while traveling. I shared four of the most important ones. I hope you find them useful! I will be sharing &lt;em&gt;many&lt;/em&gt; more in another blog post too, so stay tuned!
&lt;/p&gt;

    

     <div>
        <p>
          <em>This article is <a href="https://www.knomobags.com/uk/blog/knomad-diaries-four-tips-for-getting-more-work-done-while-travelling">published @ <strong>Knomo London</strong>.</a></em>
        </p>
    </div>
    
  </content>
 </entry>
 
 <entry>
   <title>Mimic Relative Positioning Inside an SVG with Nested SVGs</title>
   <link href="http://sarasoueidan.com//blog/mimic-relative-positioning-in-svg/"/>
   <id>https://sarasoueidan.com/blog/mimic-relative-positioning-in-svg</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;Positioning elements inside an SVG image is very similar—if not identical—to positioning elements absolutely in HTML. Every element in SVG is positioned &quot;absolutely&quot; relative to the SVG viewport, and the position inside the viewport is governed by the &lt;i&gt;current coordinate system in use&lt;/i&gt;. But this similarity in positioning elements should not conceal the fact that there is a fundamental difference between SVG elements and HTML elements: &lt;strong&gt;SVG elements do not have a box model like HTML elements do in CSS&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Before we move on, let’s quickly review what a box model is in CSS and how it affects positioning things.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;box-model&quot;&gt;Quick Review of The Box Model in CSS&lt;/h2&gt;

&lt;p&gt;Every HTML element has a box model in CSS that is composed of four boxes: the &lt;em&gt;content box&lt;/em&gt;, the &lt;em&gt;padding box&lt;/em&gt;, the &lt;em&gt;border box&lt;/em&gt;, and the &lt;em&gt;margin box&lt;/em&gt;.&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;/images/box-model.png&quot; alt=&quot;A visual representation of the CSS Box Model&quot; /&gt;
	&lt;figcaption&gt;
		The box model of an element in CSS—includes the content, padding, border, and margin areas. Image borrowed from the &lt;a href=&quot;http://tympanus.net/codrops/css_reference/box-sizing/&quot;&gt;&lt;code&gt;box-sizing&lt;/code&gt; entry&lt;/a&gt; in the &lt;a href=&quot;http://tympanus.net/codrops/css_reference/&quot;&gt;Codrops CSS Reference&lt;/a&gt;.
	&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Normally, when an element’s size is set, the width and height properties determine the width and height of the element’s content box. Any padding added to the element will increase the total computed width and/or height of the element—this is how the default box model works in regards to sizing the element. The &lt;code class=&quot;highlighter-rouge&quot;&gt;box-sizing&lt;/code&gt; property allows you to control how the sizing of an element’s dimensions works. More specifically, using the box-sizing property, you can tell the browser to include the padding width and/or border width in the width of the element, without increasing that width. This is useful for many use cases, but mostly so for when you’re building grid systems in CSS, for example. You can learn all about this property and its values in &lt;a href=&quot;http://tympanus.net/codrops/css_reference/box-sizing/&quot;&gt;this entry&lt;/a&gt; over on Codrops.&lt;/p&gt;

&lt;p&gt;An element’s box model is also &lt;strong&gt;used to create a positioning context for the contents of the element&lt;/strong&gt;, where applicable, or for the element itself.&lt;/p&gt;

&lt;p&gt;When the value of an element’s &lt;code class=&quot;highlighter-rouge&quot;&gt;position&lt;/code&gt; changes from the default &lt;code class=&quot;highlighter-rouge&quot;&gt;static&lt;/code&gt; value, it either creates a positioning context for its descendants or for itself. Whenever the default position changes, a positioning context is needed to specify where and how an element is going to be positioned outside the default page’s content flow. (You can learn more about this subject &lt;a href=&quot;http://tympanus.net/codrops/css_reference/position/&quot;&gt;here&lt;/a&gt;.)&lt;/p&gt;

&lt;p&gt;If you want to remove an element from the page’s content flow, you can do that by positioning it absolutely. Positioning an element absolutely means it will be positioned relative to one of its ascendants, using that ascendant’s box as a positioning context.&lt;/p&gt;

&lt;p&gt;Each positioning context, however, requires a coordinate system. The cooridnate system is established by the dimensions (width and height) of the element’s box model. Any descendant of the element will then be positioned inside and relative to the element using this coordinate system.&lt;/p&gt;

&lt;p class=&quot;size-2x&quot;&gt;In SVG, however, there is only one coordinate system by default used to position elements inside the viewport: the &lt;i&gt;current coordinate system in use&lt;/i&gt;, established by the SVG &lt;code&gt;viewBox&lt;/code&gt;. And so when an element needs to be positioned inside an SVG, it is positioned relative to the entire SVG viewport. &lt;/p&gt;

&lt;p class=&quot;note&quot;&gt;Technically, there are &lt;strong&gt;two&lt;/strong&gt; default coordinate systems in an SVG. But only one of those is relevant when dealing with positioning SVGs unless you explicitly change the values of both. If you're not familiar with SVG coordinate systems and how they're established and used, I highly recommend reading &lt;a href=&quot;https://sarasoueidan.com/blog/svg-coordinate-systems&quot;&gt;this article&lt;/a&gt; before continuing through this one. In this article, we'll be dealing with the ‘normal’ case where we only need to deal with one.&lt;/p&gt;

&lt;p&gt;Individual elements don’t have a box model and therefore don’t have their own coordinate systems that can be used as positioning contexts for other elements. So, what if you do want to position an SVG element relative to another SVG element or group of elements?&lt;/p&gt;

&lt;p&gt;The answer is: nested &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt;s.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;nesting-svgs&quot;&gt;Nesting SVGs&lt;/h2&gt;

&lt;p&gt;One of my favourite things about SVG is that it’s an image defined by markup. And that markup is what gives us a lot of power over the contents of that image and how they are displayed.&lt;/p&gt;

&lt;p&gt;You can nest &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt;s. That is, you can put an &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; inside another &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt;. And then you can put another &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; inside that &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt;. And then you can put yet another &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt; inside that &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt;. And you can go on and on.&lt;/p&gt;

&lt;p&gt;You can nest SVGs as deeply as you want. How many levels deep you want to go depends on what you want to do and whether or not you need to, of course. I’ve personally never needed to nest SVGs more than two levels deep.&lt;/p&gt;

&lt;pre class=&quot;brush:html;&quot;&gt;
&amp;lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot;&amp;gt;
    &amp;lt;!-- some SVG content --&amp;gt;
    &amp;lt;svg&amp;gt;
        &amp;lt;!-- some inner SVG content --&amp;gt;
    &amp;lt;/svg&amp;gt;

    &amp;lt;svg&amp;gt;
        &amp;lt;!-- other inner SVG content --&amp;gt;
    &amp;lt;/svg&amp;gt;
&amp;lt;svg&amp;gt;
&lt;/pre&gt;

&lt;h3&gt;Some notes about nested &lt;code&gt;&amp;lt;svg&amp;gt;s&lt;/code&gt;&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;The inner &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; element does not require specifying a namespace (&lt;code class=&quot;highlighter-rouge&quot;&gt;xmlns&lt;/code&gt;) on it because it is assumed to be the same namespace as the outer &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt;’s namespace. Even the outer (root) &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; does not require a namespace if it is embedded inline in an HTML5 document.&lt;/li&gt;
  &lt;li&gt;You can use a nested SVG to group elements together and then position them inside the parent SVG. Of course, you can group elements inside an SVG using the group tag &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;g&amp;gt;&lt;/code&gt;, but using an &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; instead has a few advantages, such as being able to specify the group’s &lt;code class=&quot;highlighter-rouge&quot;&gt;width&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;height&lt;/code&gt;, and positioning it using absolute values &lt;code class=&quot;highlighter-rouge&quot;&gt;x&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;y&lt;/code&gt; instead of having to use transforms (for &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;g&amp;gt;&lt;/code&gt;). By specifying a width and height to the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt;, you restrict the content inside it to the bounds of the viewport that is defined by the &lt;code class=&quot;highlighter-rouge&quot;&gt;width&lt;/code&gt;, and &lt;code class=&quot;highlighter-rouge&quot;&gt;height&lt;/code&gt; attributes. Any content that lies beyond these bounds will be clipped.&lt;/li&gt;
  &lt;li&gt;Percentage values specified for elements inside an inner &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; will be calculated relative to that &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt;, not relative to the root &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt;. Percentage values specified on the inner &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; itself will be calculated relative to the root &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;So, Why Nest &lt;code&gt;&amp;lt;svg&amp;gt;s&lt;/code&gt;?&lt;/h2&gt;

&lt;p&gt;One use case for nesting SVGs is creating interesting responsive effects where the contents of the SVG would hide or reveal other portions of content at different viewport sizes.&lt;/p&gt;

&lt;p&gt;Such an example is the following SVG illustration of a small bird inside an egg:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/svg-nesting-example-1.png&quot; style=&quot;max-width: 400px; display: block; margin: 0 auto;&quot; alt=&quot;The SVG image we will be using contains a bird covered by an egg made of two shells.&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Normally, if the &lt;a href=&quot;http://tympanus.net/codrops/2014/08/19/making-svgs-responsive-with-css/&quot;&gt;SVG is responsive&lt;/a&gt;, resizing the screen would make the entire SVG smaller while maintaining the positions of the content inside of it and the spatial relationships between them:&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;/images/svg-nesting-example-1-resized.png&quot; alt=&quot;The SVG as it looks when the viewport shrinks. It looks the same, but smaller.&quot; /&gt;
	&lt;figcaption&gt;Resizing the responsive SVG in the browser makes the SVG shrink in size, without affecting the position and spatial relationships of the content inside of it.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;By nesting &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt; elements, we can create separate “layers” inside the root &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; that we can then control so that the contents of these layers would change position inside the root &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt; as the viewport size changes. By doing that, we can show and hide different portions of content inside the SVG as desired.&lt;/p&gt;

&lt;blockquote class=&quot;pull-quote&quot;&gt;
	By nesting &lt;code&gt;svg&lt;/code&gt; elements, we can create separate “layers” inside the main &lt;code&gt;&amp;lt;svg&amp;gt;&lt;/code&gt;
&lt;/blockquote&gt;

&lt;p&gt;For example, we can separate the above illustration into 3 layers that would reveal the small bird on smaller sizes:&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;/images/svg-nesting-example-1-2.png&quot; alt=&quot;The bird illustration with the two egg shells changing position on smaller sizes, revealing the bird underneath.&quot; /&gt;
&lt;/figure&gt;

&lt;p&gt;This effect is achieved by using different &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectRatio&lt;/code&gt; values on each of the inner &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt;s. This ensures that the contents of each &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt;—i.e. the contents of each ‘layer’, ‘sticks’ to either edge of the root SVG, thus revealing the content in between.&lt;/p&gt;

&lt;p&gt;I’ve written a detailed article about how to achieve this; so, if you’re interested, do &lt;a href=&quot;https://sarasoueidan.com/blog/nesting-svgs/&quot;&gt;check it out&lt;/a&gt;.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;relative-position-in-svg&quot;&gt;‘Relative’ Positioning in SVG Using a Nested &lt;code&gt;svg&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;The fact that contents of an inner &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt; are positioned relative to that &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt; itself gets us one step closer to positioning elements relative to other elements as opposed to being relative to the root &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But how exactly does a nested &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt; enable us to position one element &lt;em&gt;relative to another non-&lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt; element&lt;/em&gt;?&lt;/p&gt;

&lt;!-- The answer is: by combining it with an element’s __bounding box__. --&gt;

&lt;p&gt;Before we answer that question, we need to understand what an SVG element’s &lt;strong&gt;Bounding Box&lt;/strong&gt; is.&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;svg-bounding-box&quot;&gt;What is a Bounding Box?&lt;/h3&gt;

&lt;p&gt;Not all SVG elements are created equal. The powerful thing about SVG is that its basic shapes allow us to create all kinds of non-rectangular shapes: from arbitrary paths, to open or closed polylines and polygons, to circles and ellipses.&lt;/p&gt;

&lt;p&gt;Because of the nature of these elements and their lack of a CSS box model, the SVG specification compensates for the lack of a box model by &lt;a href=&quot;https://www.w3.org/TR/SVGTiny12/coords.html#BoundingBox&quot;&gt;introducing the concept of a &lt;strong&gt;bounding box&lt;/strong&gt;&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
The bounding box (or &quot;bbox&quot;) of an element is the tightest fitting rectangle aligned with the axes of that element's user coordinate system that entirely encloses it and its descendants.
&lt;/blockquote&gt;

&lt;p&gt;In simpler words, a bounding box is the smallest rectangle that you can draw around an element, that encloses the entire element—all its points and edges.&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;/images/bounding-box-ai.png&quot; alt=&quot;The bounding box of a spiral object drawn in Adobe Illustrator.&quot; /&gt;
	&lt;figcaption&gt;
		The light blue rectangle represents the smallest rectangle around the spiral shape, and thus is a visual representation of the shape’s bounding box. By selecting an element in the graphics editor, you can retrieve the properties of the element’s bounding box. In the above screenshot, these properties are available in Adobe Illstrator’s &lt;i&gt;Transform&lt;/i&gt; panel.
	&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Three kinds of bounding boxes can be computed for an element:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;The object bounding box&lt;/strong&gt; is the bounding box that contains only an element’s geometric shape.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;The stroke bounding box&lt;/strong&gt; is the bounding box that contains an element’s geometric shape and its stroke shape.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;The decorated bounding box&lt;/strong&gt; is the bounding box that contains an element’s geometric shape, its stroke shape and its markers.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;An element’s bounding box is characterized by properties that can be retrieved using the &lt;code class=&quot;highlighter-rouge&quot;&gt;getBBox()&lt;/code&gt; method—the SVG equivalent of &lt;code class=&quot;highlighter-rouge&quot;&gt;getBoundingClientRect()&lt;/code&gt;: &lt;code class=&quot;highlighter-rouge&quot;&gt;x&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;y&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;width&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;height&lt;/code&gt;.&lt;/p&gt;

&lt;pre class=&quot;brush:js&quot;&gt;
var svgElement = document.getElementById('el');
bbox = svgElement.getBBox();

console.log( bbox.x ) ;
console.log( bbox.y ) ;
console.log( bbox.width ) ;  
console.log( bbox.height ) ;
&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Using the element’s bounding box, we can fake the presence of a coordinate system around that element, which we can then use to position other elements.&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote class=&quot;pull-quote&quot;&gt;
Using the element’s bounding box, we can fake the creation of a coordinate system around that element, which we can then use to position other elements.
&lt;/blockquote&gt;

&lt;p&gt;More specifically, we will be creating and using an inner &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; to establish a new cooridnate system around an element. The properties of the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; will be defined by the properties of the element’s bounding box: the x, y, width, and height properties.&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;element-coordinate-system&quot;&gt;Creating a new coordinate system around an SVG element&lt;/h3&gt;

&lt;p&gt;Suppose we have the following SVG image (&lt;a href=&quot;http://www.vecteezy.com/vector-art/82694-birds-in-nest-vector&quot;&gt;courtesy of Vecteezy&lt;/a&gt;) with the bird and the nest:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/bird-nest.svg&quot; style=&quot;border: 2px solid #eee;&quot; alt=&quot;An SVG containing a bird and a nest, with the bird positioned far from the nest.&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Let’s have some fun. The bird in the above image is trying to get back to its nest. (My idea of fun is, admittedly, not than fun.)&lt;/p&gt;

&lt;p&gt;Normally, we are able to position the bird above the nest by specifying its position inside the SVG using the entire SVG canvas’s coordinate system.&lt;/p&gt;

&lt;p&gt;We can certainly do that.&lt;/p&gt;

&lt;p&gt;But, ideally, we’d be able to position it by using percentage values that would be calculated relative to the nest’s “box”. We can mimic that by creating a coordinate system around the nest using our new &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; element. The &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; element has its own coordinate system established by its width and height. We will use &lt;em&gt;that&lt;/em&gt; coordinate system to make up for the missing coordinate system on the nest.&lt;/p&gt;

&lt;p&gt;Then, we move the bird (the actual bird content) into that &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; tag. By being contained by the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt;, the bird‘s position will be calculated relative to the coordinate system established on that &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But to create the relative connection between the bird and the nest elements, we need the positioning context of the bird—which is our &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt;—to resemble a coordinate system around the nest.&lt;/p&gt;

&lt;p&gt;In order to do that, we will position the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; on top of the nest, &lt;em&gt;visually&lt;/em&gt;. It’s important to note here that the inner SVG does not actually wrap the nest—the nest elements are not contained inside the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; tag. We’re only positioning the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; &lt;em&gt;on top&lt;/em&gt; of the nest, visually, so that it seems as though &lt;strong&gt;the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; is the &lt;em&gt;visual representation&lt;/em&gt; of the nest’s coordinate system&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In order to determine the exat position of the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; (its &lt;code class=&quot;highlighter-rouge&quot;&gt;x&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;y&lt;/code&gt; position inside the root &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt;) and its dimensions, we will be using the nest’s bounding box properties.&lt;/p&gt;

&lt;p&gt;The position of the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt;—the &lt;code class=&quot;highlighter-rouge&quot;&gt;x&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;y&lt;/code&gt; values—will be equal to the &lt;code class=&quot;highlighter-rouge&quot;&gt;x&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;y&lt;/code&gt; values of the nest’s bounding box. That is, the bounding box of the group of elements forming the nest. (Groups can have bounding boxes, just like single elements can.) The inner &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt; will also have explicit height and width values which are equal to the height and width of the nest’s bounding box.&lt;/p&gt;

&lt;p&gt;Here is what it looks like visually:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/nest-bbox.png&quot; style=&quot;border: 2px solid #eee;&quot; alt=&quot;A visual respresentation of the nested svg element positioned on top of (or around) the nest looks like the bounding box of the nest itself.&quot; /&gt;&lt;/p&gt;

&lt;p&gt;What the above image is missing is the fact that the bird is now contained inside of it. So this is what it really looks like:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/bird-nest-svg.png&quot; style=&quot;border: 2px solid #eee;&quot; alt=&quot;A visual respresentation of the nested svg element positioned on top of (or around) the nest, with the bird positioned inside of it.&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The grey border is the border representing the bounding box, and also the new coordinate system around the nest established by the &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It’s important to note here that the bird is now positioned relative to the coordinate system of the inner &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt;. Notice how it is offset by some amount of pixels from both the top and left edges of the inner &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt;, just like it was positioned relative to the root &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt;. That is fine for now. We will need to get rid of that space to get a finer control over the position of the bird. But we’ll get to that shortly.&lt;/p&gt;

&lt;p&gt;Another thing to note is that since the inner &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt; has an explicit height and width which are equal to the height and width of the nest’s bounding box, the bird’s feet get cut off at the bottom due to the way it is positioned. If you have other, more or different elements in your own projects, those might get cut off too. You definitely don’t want that. So to work around that, you need to explicitly set the &lt;code class=&quot;highlighter-rouge&quot;&gt;overflow&lt;/code&gt; value to &lt;code class=&quot;highlighter-rouge&quot;&gt;visible&lt;/code&gt; on the inner &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt;. This will ensure that the inner &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt; behaves only like a positioning context, not like a container that restricts its contents to a specific area visually.&lt;/p&gt;

&lt;p&gt;Here is what the code looks like:&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;svg id=&quot;birds&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;100%&quot; viewBox=&quot;0 0 3945.8 2400&quot;&amp;gt;
    &amp;lt;title&amp;gt;Bird &amp;amp; Nest&amp;lt;/title&amp;gt;
    &amp;lt;g id=&quot;nest&quot;&amp;gt;
      &amp;lt;path ...&amp;gt;
      &amp;lt;!-- ... --&amp;gt;
    &amp;lt;/g&amp;gt;
    &amp;lt;!-- The ID I'm giving this SVG is just for demonstration purposes --&amp;gt;
    &amp;lt;svg x=&quot;698&quot; y=&quot;1219&quot; width=&quot;1055&quot; height=&quot;641&quot; viewBox=&quot;0 0 1055 641&quot; style=&quot;overflow: visible;&quot; id=&quot;coord-sys&quot;&amp;gt;
	    &amp;lt;g id=&quot;bird&quot;&amp;gt;
	      &amp;lt;path ...&amp;gt;
	      &amp;lt;!-- ... --&amp;gt;
	    &amp;lt;/g&amp;gt;
    &amp;lt;/svg&amp;gt;
&amp;lt;/svg&amp;gt;
&lt;/pre&gt;

&lt;p&gt;Just like with the root &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt;, the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; value of the inner SVG &lt;code class=&quot;highlighter-rouge&quot;&gt;svg#coord-sys&lt;/code&gt; is determined by its dimensions.&lt;/p&gt;

&lt;p&gt;Next up, we need to position the bird inside the new coordinate system. I won’t refer to the inner &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt; as “inner svg” anymore—I’ll be referring to it as &lt;code class=&quot;highlighter-rouge&quot;&gt;svg#coord-sys&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Since we will be positioning the bird inside the &lt;code class=&quot;highlighter-rouge&quot;&gt;svg#coord-sys&lt;/code&gt;, we need to be able to specify a position for &lt;em&gt;the group of elements forming this bird&lt;/em&gt;. After all, the bird is not made up of one element only—it is a group of shapes. And so we need to position &lt;em&gt;a group of elements&lt;/em&gt;, not just one element. The group of elements forming the bird is wrapped in a group (&lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;g&amp;gt;&lt;/code&gt;) element.&lt;/p&gt;

&lt;p&gt;But the problem is: the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;g&amp;gt;&lt;/code&gt; element does not have &lt;code class=&quot;highlighter-rouge&quot;&gt;x&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;y&lt;/code&gt; attributes. So we can’t simply move it to a specific position like so:&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;g id=&quot;bird&quot; x=&quot;50%&quot; y=&quot;50%&quot;&amp;gt;
&lt;/pre&gt;

&lt;p&gt;Usually, to move a group of elements around inside an SVG, we use SVG or CSS transform functions (translation transformation, most of the time). You can use transforms to move the group around, sure. But that would negate the whole idea we’re trying to achieve and would make the new coordinate system useless. After all, we could have used transforms to position the bird close to the nest inside the root &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt; without having to create a new coordinate system.&lt;/p&gt;

&lt;p&gt;What we want is to mimic the way elements are positioned in CSS, &lt;em&gt;relative to each other&lt;/em&gt;. So to say “move this group of elements to the position (x, y) inside this particular positioning context”.&lt;/p&gt;

&lt;p&gt;Since &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;g&amp;gt;&lt;/code&gt; does not have &lt;code class=&quot;highlighter-rouge&quot;&gt;x&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;y&lt;/code&gt; attributes, we’re going to substitute it with &lt;i&gt;another&lt;/i&gt; &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;svg id=&quot;birds&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;100%&quot; viewBox=&quot;0 0 3945.8 2400&quot;&amp;gt;
    &amp;lt;title&amp;gt;Bird &amp;amp; Nest&amp;lt;/title&amp;gt;
    &amp;lt;!-- ... --&amp;gt;
    &amp;lt;svg x=&quot;698&quot; y=&quot;1219&quot; width=&quot;1055&quot; height=&quot;641&quot; viewBox=&quot;0 0 1055 641&quot; style=&quot;overflow: visible;&quot;&amp;gt;
	    &amp;lt;svg id=&quot;bird&quot;&amp;gt;
	      &amp;lt;!-- ... --&amp;gt;
	    &amp;lt;/svg&amp;gt;
    &amp;lt;/svg&amp;gt;
&amp;lt;/svg&amp;gt;
&lt;/pre&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt; wrapping the bird has an ID &lt;code class=&quot;highlighter-rouge&quot;&gt;bird&lt;/code&gt;. This SVG, as opposed to its ancestor, will only serve as a container and, even though it does create a new coordinate system, we won’t be using that system. Using this &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt;, we can now move the bird around inside the new coordinate system established around (on top of) the nest.&lt;/p&gt;

&lt;p&gt;At this point, it is best to get rid of the white offset space around the bird. The innermost &lt;code class=&quot;highlighter-rouge&quot;&gt;svg#bird&lt;/code&gt; has the same dimensions and &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; as its wrapping &lt;code class=&quot;highlighter-rouge&quot;&gt;svg#coord-sys&lt;/code&gt;; which means that in order to move the bird around, we need to take this white space into account. So if we want to move the bird to position it at the top left corner of the system, we won’t be able to simply say set x and y to zero—we will need to use a negative offset in both directions to achieve it. That’s not practical. We would also need to take this offset into account wherever and however we want to position the bird later.&lt;/p&gt;

&lt;p&gt;&lt;small&gt;At this point, you need to be not only familiar but also comfortable with how the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; works. I’m going to assume you are. If you’re not, pause here and go read &lt;a href=&quot;https://sarasoueidan.com/blog/svg-coordinate-systems&quot;&gt;this article&lt;/a&gt; first.&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;We will change the value of the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; of &lt;code class=&quot;highlighter-rouge&quot;&gt;svg#bird&lt;/code&gt; to crop the white space out. (So we &lt;em&gt;are&lt;/em&gt; going to use its coordinate system, but only a little bit.)&lt;/p&gt;

&lt;p&gt;By default, a nested &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt; will occupy 100% the width and height of its container, unless you tell it otherwise.&lt;/p&gt;

&lt;p&gt;So &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;#bird&lt;/code&gt; now has the exact same dimensions as that of the &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;#coord-sys&lt;/code&gt;. It’s the one with the pink border in the following image:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/bird-nest-viewbox-shift.png&quot; style=&quot;border: 2px solid #eee;&quot; alt=&quot;A visual representation of the innermost svg showing its dimensions compared to its containing svg.&quot; /&gt;&lt;/p&gt;

&lt;p&gt;We don’t need the dimensions to be different in this example so we will leave them as they are.&lt;/p&gt;

&lt;p&gt;The image above also shows the amount of white space by which the bird is shifted inside that &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt;. So in order to “unshift” it, we will change the value of the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; of the &lt;code class=&quot;highlighter-rouge&quot;&gt;svg#bird&lt;/code&gt; to crop that white space out.&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;svg id=&quot;birds&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;100%&quot; viewBox=&quot;0 0 3945.8 2400&quot;&amp;gt;
    &amp;lt;!-- ... --&amp;gt;
    &amp;lt;svg x=&quot;698&quot; y=&quot;1219&quot; width=&quot;1055&quot; height=&quot;641&quot; style=&quot;overflow: visible;&quot;&amp;gt;
	    &amp;lt;svg id=&quot;bird&quot; viewBox=&quot;150 230 1055 641&quot;&amp;gt;
	      &amp;lt;path ...&amp;gt;
	      &amp;lt;!-- ... --&amp;gt;
	    &amp;lt;/svg&amp;gt;
    &amp;lt;/svg&amp;gt;
&amp;lt;/svg&amp;gt;
&lt;/pre&gt;

&lt;p&gt;That will shift the bird so that it is positioned at the top left of the coordinate system. I’m unfocusing &lt;code class=&quot;highlighter-rouge&quot;&gt;svg#bird&lt;/code&gt; in the following image, so only the nest’s coordinate system is still shown, and the new position of the bird inside of it:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/bird-nest-cropped.png&quot; style=&quot;border: 2px solid #eee;&quot; alt=&quot;The bird is now positioned at the top left corner of the newly established coordinate system.&quot; /&gt;&lt;/p&gt;

&lt;p&gt;So now that the bird is positioned at the top left of its wrapper, we can move it around and get the expected result every time. For example, if we were to move the bird by 50% in both directions:&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;svg id=&quot;bird&quot; style=&quot;overflow: visible&quot; viewBox=&quot;150 230 1055 641&quot; x=&quot;50%&quot; y=&quot;50%&quot;&amp;gt;
&lt;/pre&gt;

&lt;p&gt;We would get the following result:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/bird-nest-position.png&quot; style=&quot;border: 2px solid #eee;&quot; alt=&quot;The bird positioned at 50% by 50% across the newly created cooridnate system.&quot; /&gt;&lt;/p&gt;

&lt;p&gt;With this setup, we can now move the bird around inside the nest’s coordinate system just like we would move an HTML element inside another one in CSS. Both relative and absolute position values work here too.&lt;/p&gt;

&lt;p&gt;Pretty nice, huh? This is possibly the closest we can (currently) get to relative positioning in SVG today.&lt;/p&gt;

&lt;p&gt;Granted, to get here is not the simplest process, but once you’ve got a good grasp of how SVG coordinate systems and the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; work, it’s hopefully not so complicated.&lt;/p&gt;

&lt;p&gt;Here is a live demo of the above bird and nest, with the position of the bird set so that it stands at the edge of its nest:&lt;/p&gt;

&lt;p data-height=&quot;300&quot; data-theme-id=&quot;3617&quot; data-slug-hash=&quot;6f00a9b23653395afdd1c009d8ad6961&quot; data-default-tab=&quot;html,result&quot; data-user=&quot;SaraSoueidan&quot; data-embed-version=&quot;2&quot; class=&quot;codepen&quot;&gt;See the Pen &lt;a href=&quot;http://codepen.io/SaraSoueidan/pen/6f00a9b23653395afdd1c009d8ad6961/&quot;&gt;[Article Demo] Relative Positioning in SVG&lt;/a&gt; by Sara Soueidan (&lt;a href=&quot;http://codepen.io/SaraSoueidan&quot;&gt;@SaraSoueidan&lt;/a&gt;) on &lt;a href=&quot;http://codepen.io&quot;&gt;CodePen&lt;/a&gt;.&lt;/p&gt;
&lt;script async=&quot;&quot; src=&quot;//assets.codepen.io/assets/embed/ei.js&quot;&gt;&lt;/script&gt;

&lt;h3&gt; Final Words &lt;/h3&gt;

&lt;p&gt;The example used in this article is a very specific example and is, admittedly, not the most practical use case of all times. Your use case(s) are likely to be entirely different. You might be working with a very different SVG where you may not even need to do any viewBox cropping at all. If you create your SVG yourself, you can position your element (e.g. the bird in our case) at the top left of the SVG canvas, so that when you wrap it in another &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt;, it would also be positioned at the top left, and you wouldn’t have to do any cropping at all. I left this example slightly more complex just so we can cover more scenarios. (And because I was a little lazy to edit the SVG in Illustrator after having written half of this article. But I keep wanting to deny that.)&lt;/p&gt;

&lt;p&gt;But the takeaway is &lt;em&gt;how&lt;/em&gt; to mimic relative positioning using nested &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt;s. Whether you use one level, two levels, or more, the concepts are the same.&lt;/p&gt;

&lt;p&gt;You might find this technique useful for positioning SVG UI elements relative to each other. Or maybe relative positioning in dynamically created SVGs. Your imagination is the limit.&lt;/p&gt;

&lt;p&gt;I hope you found this article useful. Thank you for reading!&lt;/p&gt;

    
  </content>
 </entry>
 
 <entry>
   <title>Making the Switch Away from Icon Fonts to SVG: Converting Font Icons to SVG</title>
   <link href="http://sarasoueidan.com//blog/icon-fonts-to-svg/"/>
   <id>https://sarasoueidan.com/blog/icon-fonts-to-svg</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;
	If you’re reading this article, then I can probably assume you’ve already decided to switch from using fonts for icons to an SVG icon system. Or maybe you're pondering the idea and want to get an overview of how that would be done and whether or not it's worth it. Either way, this post is here to help you with that.
&lt;/p&gt;

&lt;p&gt;If you’re not already convinced as to why SVG is a better icon system, then I highly recommend reading &lt;a href=&quot;https://css-tricks.com/icon-fonts-vs-svg/&quot;&gt;this article&lt;/a&gt;—a cagematch-style comparison between icon fonts and inline SVG for icon systems. Many companies and organizations including Github have already made the switch to SVG, and have written great articles explaining why they found SVG to be a better alternative. I’ve listed some articles at the end of this post for further reading.&lt;/p&gt;

&lt;h2 id=&quot;making-the-switch&quot;&gt;Making the Switch&lt;/h2&gt;

&lt;h3 id=&quot;1-grab-your-icon-fonts-files&quot;&gt;1. Grab your icon fonts files.&lt;/h3&gt;

&lt;p&gt;Font icons are font glyphs. They’re part of a web font and are thus defined in web font files and formats.&lt;/p&gt;

&lt;p&gt;I don’t usually use icon fonts, so for the sake of demonstration, I headed to &lt;a href=&quot;http://fontello.com/&quot;&gt;Fontello.com&lt;/a&gt;—an online icon font generator—to create an icon font that I can work with for this blog post. I picked a few icons and then generated an icon font and downloaded it.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/fontello-icons.gif&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Fontello generates a folder containing a demo page showing you how to display the icons on your own page and what class names to use.&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;/images/fontello-files.png&quot; /&gt;
	&lt;figcaption&gt;The files generated by Fontello.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Among the generated files is the actual font used to define the icons. The font files are available inside a &lt;code class=&quot;highlighter-rouge&quot;&gt;font&lt;/code&gt; folder.&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;/images/fontello-fonts.png&quot; /&gt;
	&lt;figcaption&gt;The font file formats generated by Fontello.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;These are the files you need to proceed. We’re going to be using these files to “extract” the icons and convert them to SVG.&lt;/p&gt;

&lt;h3 id=&quot;2-choose-your-tool&quot;&gt;2. Choose your tool.&lt;/h3&gt;

&lt;p&gt;To convert the icons to SVG, we can use one of the following tools:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;&lt;a href=&quot;https://github.com/bpierre/fontello-svg&quot;&gt;fontello-svg&lt;/a&gt;&lt;/strong&gt;: “a command-line tool to generate the SVG versions of a Fontello icon set, with a corresponding CSS file.”&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;&lt;a href=&quot;https://www.npmjs.com/package/font-blast&quot;&gt;font-blast&lt;/a&gt;&lt;/strong&gt;: “You can use font-blast to extract icons from any icon font - Font Awesome, Foundation, anything from Fontello etc.”&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;&lt;a href=&quot;https://icomoon.io/app/&quot;&gt;Icomoon app&lt;/a&gt;&lt;/strong&gt;: a web app for generating and creating icon sets in both SVG and icon font formats.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I’m sure there might be more tools to do this, but these are the ones I know about.&lt;/p&gt;

&lt;p&gt;I’m going to be using &lt;strong&gt;Icomoon&lt;/strong&gt; and &lt;strong&gt;font-blast&lt;/strong&gt; in this article because they’re general tools that can be used with any font and are not restricted to just one. Both &lt;strong&gt;fontello-svg&lt;/strong&gt; and &lt;strong&gt;font-blast&lt;/strong&gt; are used pretty much the same way, and you can find extra information about &lt;strong&gt;fontello-svg&lt;/strong&gt; in the Github repository’s Readme.&lt;/p&gt;

&lt;h3 id=&quot;3-convert-the-font-icons-to-svg-icons&quot;&gt;3. Convert the font icons to SVG icons.&lt;/h3&gt;

&lt;h4 id=&quot;31-using-icomoon&quot;&gt;3.1. Using Icomoon&lt;/h4&gt;

&lt;p&gt;To convert the font icons to SVG icons using Icomoon, we first need to upload them.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/icomoon-upload.gif&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Your font icons will be available as an icon set in the app. The next steps are the same steps you would take if you were choosing from the set of already-available icons on the page:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Select the icons you want to download as SVG.&lt;/li&gt;
  &lt;li&gt;Click the &lt;strong&gt;&lt;em&gt;Generate SVG &amp;amp; More&lt;/em&gt;&lt;/strong&gt; button.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img src=&quot;/images/icomoon-download.gif&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Like Fontello, Icomoon generates a folder containing the icons you generated, along with a demo page showing you how they can be used on your own pages.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/icomoon-files.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The icons you’ve converted to SVG are available in the &lt;strong&gt;&lt;em&gt;SVG&lt;/em&gt;&lt;/strong&gt; folder.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/icomoon-svg-icons.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;There is one SVG file for every icon. The set is ready to be embedded in your page.&lt;/p&gt;

&lt;p&gt;But before embedding the icons, you might want to sprite them. That is, create one SVG sprite that contains all of the icons, and then use that sprite to display each icon at a time, wherever needed on the page. Icomoon conveniently generates an SVG sprite (&lt;em&gt;&lt;strong&gt;symbol-defs.svg&lt;/strong&gt;&lt;/em&gt;) for you along with a polyfill (&lt;em&gt;&lt;strong&gt;svgxuse.js&lt;/strong&gt;&lt;/em&gt;) for older browsers that don’t support external sprite references.&lt;/p&gt;

&lt;h4 id=&quot;32-using-font-blast&quot;&gt;3.2. Using font-blast&lt;/h4&gt;

&lt;p&gt;To convert the font icons to SVG icons using &lt;strong&gt;font-blast&lt;/strong&gt; you need to first install font-blast using npm via your terminal.&lt;/p&gt;

&lt;pre class=&quot;brush:js&quot;&gt;
$ npm install font-blast -g
&lt;/pre&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;-g&lt;/code&gt; flag (short for &lt;code class=&quot;highlighter-rouge&quot;&gt;global&lt;/code&gt;) ensures that you can run the script anywhere on your computer, regardless of the installation root.&lt;/p&gt;

&lt;p&gt;As mentioned in the font-blast documentation, “You can generate icons from the command line by called the script with two parameters: the SVG file of the font, and the directory where inidivual icons should be placed”:&lt;/p&gt;

&lt;pre class=&quot;brush:js&quot;&gt;
$ font-blast [svg-font-file] [destination-dir]
&lt;/pre&gt;

&lt;p&gt;My command looked like this:&lt;/p&gt;

&lt;pre class=&quot;brush:js&quot;&gt;
font-blast /Users/Sara/Downloads/fontello-08cdd41f/font/fontello.svg  /Users/Sara/Downloads/fontello-08cdd41f/svg-icons
&lt;/pre&gt;

&lt;p&gt;&lt;small&gt;Tip: You can drag your folder into the terminal, which will sort of drop the path to that folder into the terminal, so you don’t have to manually write it or grab it and then copy-paste it.&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;Running the above command, font-blast retrieves the icons from the font files and creates an SVG icon for each one, and saves the result to the folder you specify in the command line. My terminal then looks like this:&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;/images/font-blast-command-result.png&quot; /&gt;
	&lt;figcaption&gt;The result of running the font-blast command in the terminal, letting us know how many icons were found and converted to SVG.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;The &lt;strong&gt;&lt;em&gt;svg-icons&lt;/em&gt;&lt;/strong&gt; folder I chose for the generated files looks like this:&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;/images/font-blast-files.png&quot; /&gt;
	&lt;figcaption&gt;The files generated by font-blast show the source font file used to extract the icons and include a folder containing the generated SVG icons.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;As you have guessed, the &lt;strong&gt;&lt;em&gt;SVG&lt;/em&gt;&lt;/strong&gt; folder contains the generated SVG icons:&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;/images/font-blast-svg-icons.png&quot; /&gt;
	&lt;figcaption&gt;The font-blast-generated SVG icons.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;The icons are then ready to be embedded on your page.&lt;/p&gt;

&lt;h3 id=&quot;4-sprite-embed-style-animate-have-fun&quot;&gt;4. Sprite, Embed, Style, Animate, Have fun!&lt;/h3&gt;

&lt;p&gt;There are &lt;strong&gt;three&lt;/strong&gt; main ways to create and use SVG sprites. I wrote an overview of these techniques &lt;a href=&quot;https://24ways.org/2014/an-overview-of-svg-sprite-creation-techniques/&quot;&gt;here&lt;/a&gt;. We will explore each of these techniques in more detail in an upcoming article (or a series of articles), show the pros and cons of each one, and how to create and use the SVG sprites, so stay tuned.&lt;/p&gt;

&lt;p&gt;&lt;small&gt;You can &lt;a href=&quot;https://sarasoueidan.com/rss.xml&quot;&gt;subscribe to the RSS feed&lt;/a&gt; of my blog to stay up-to-date with new articles. An e-mail newsletter is in the works too!&lt;/small&gt;&lt;/p&gt;

&lt;h3 id=&quot;recommended-reading&quot;&gt;Recommended Reading:&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://css-tricks.com/icon-fonts-vs-svg/&quot;&gt;Inline SVG vs Icon Fonts [CAGEMATCH]&lt;/a&gt; — a &lt;strong&gt;must-read&lt;/strong&gt; that clearly highlights the advantages of SVG and why it’s a superior icon system format.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://blog.cloudfour.com/seriously-dont-use-icon-fonts/&quot;&gt;Seriously, Don’t Use Icon Fonts&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://benfrain.com/seriously-use-icon-fonts/&quot;&gt;Seriously, Use Icon Fonts&lt;/a&gt; — I still recommend using SVG for icon systems, but it’s always good to read a different point of view.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/blog/2112-delivering-octicons-with-svg&quot;&gt;Delivering Octicons with SVG&lt;/a&gt; on the Github blog&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://ianfeather.co.uk/ten-reasons-we-switched-from-an-icon-font-to-svg/&quot;&gt;Ten reasons we switched from an icon font to SVG&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

    
  </content>
 </entry>
 
 <entry>
   <title>SVG Style Inheritance and the ‘Flash Of Unstyled SVG’</title>
   <link href="http://sarasoueidan.com//blog/svg-style-inheritance-and-FOUSVG/"/>
   <id>https://sarasoueidan.com/blog/svg-style-inheritance-and-FOUSVG</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;There are too few things not to like about SVG, and I don’t mean the things that browsers cause by incomplete or lack of certain features or buggy implementations. Yet you might sometimes get some unpredictable results that might frustrate you when working with SVG, if you don’t know the details of &lt;em&gt;how&lt;/em&gt; certain features &lt;em&gt;should&lt;/em&gt; behave and what to expect from them, as per the specifications. SVG presentation attributes come with a bit of their own behavior which might sometimes surprise you.&lt;/p&gt;

&lt;p&gt;I’ve written and spoken quite a bit about styling and animating SVGs with CSS, and have included a list of resources to dive into the details at the end of the article. I’ve also touched on the subject of style inheritance and the CSS Cascade in SVG in both speaking and writing. However, the topic is worth its own blog post.&lt;/p&gt;

&lt;p&gt;I’ve been meaning to write this article for a while now but have been kept preoccupied. But what prompted me to finally write it today is this tweet I saw yesterday:&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;Issue: Oversized SVG icons when the page’s CSS fails to load &lt;a href=&quot;https://t.co/FwkaBAzrAT&quot;&gt;https://t.co/FwkaBAzrAT&lt;/a&gt;&lt;/p&gt;&amp;mdash; Web Platform Daily (@openwebdaily) &lt;a href=&quot;https://twitter.com/openwebdaily/status/704380922892820481&quot;&gt;February 29, 2016&lt;/a&gt;
&lt;/blockquote&gt;

&lt;p&gt;And I remembered that I have already mentioned the cause and solution to this issue in one of my talks as well as in the Smashing Book 5 chapter on SVG, but never in one of my articles, not even the article focused on making SVGs responsive with CSS. (See links at the bottom of the article.)&lt;/p&gt;

&lt;h2 id=&quot;so-whats-the-problem-again&quot;&gt;So, what’s the problem again?&lt;/h2&gt;

&lt;p&gt;When CSS is disabled or fails to load for any reason—such as when you’re on lo-fi, most SVG images, especially inline ones, will scale and take up the entire viewport width, thus making the unstyled page look even more ‘broken’ than it already does.&lt;/p&gt;

&lt;p&gt;I’ve come across this scaling issue quite a few times in the past when I visited &lt;a href=&quot;http://codepen.io&quot;&gt;Codepen&lt;/a&gt; on my fairly slow connection (which sometimes gets more than just ‘fairly’ slow). The responsive Codepen logo would take up the entire viewport area, thus blocking the content underneath it and forcing you to scroll down to read whatever comes after it.&lt;/p&gt;

&lt;p class=&quot;size-2x&quot;&gt;This—allow me to call it—&lt;em&gt;Flash of Unstyled SVGs (FOUSVG)&lt;/em&gt; is caused by the lack of &lt;code&gt;width&lt;/code&gt; and &lt;code&gt;height&lt;/code&gt; attributes on the &lt;code&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; element. &lt;/p&gt;

&lt;h2 id=&quot;but-the-svgs-are-meant-to-be-responsive&quot;&gt;But the SVGs are meant to be responsive…&lt;/h2&gt;

&lt;p&gt;So why would you want to set explicit dimensions on your &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt;when what you’re really trying to do is make the SVG fluid, right?&lt;/p&gt;

&lt;p&gt;Right.&lt;/p&gt;

&lt;p&gt;Ideally, we should make our SVGs responsive while also catering for any worst case scenarios where our styles are ignored or simply not applied for any reason.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;By taking advantage of the SVG style inheritance tree&lt;/strong&gt;, we &lt;em&gt;can&lt;/em&gt; make our SVGs responsive while simultaneously planning for worst case scenarios, and providing a decent, less broken, fallback.&lt;/p&gt;

&lt;h2 id=&quot;the-how&quot;&gt;The How&lt;/h2&gt;

&lt;p&gt;In order to avoid the no-CSS scaling issue, all you need to do is &lt;em&gt;not&lt;/em&gt; remove the &lt;code class=&quot;highlighter-rouge&quot;&gt;width&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;height&lt;/code&gt; attributes from the SVG.&lt;/p&gt;

&lt;h3 id=&quot;1-keep-the-width-and-height-attributes&quot;&gt;1. Keep the &lt;code class=&quot;highlighter-rouge&quot;&gt;width&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;height&lt;/code&gt; attributes&lt;/h3&gt;

&lt;p&gt;This means that, if you’re creating the SVG yourself in, say, Adobe Illustrator, you should &lt;strong&gt;avoid checking the ‘Responsive’ option in the Export panel(s)&lt;/strong&gt;.&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;/images/ai-new-export-panel.jpg&quot; /&gt;
	&lt;figcaption&gt;
		The ‘Responsive’ option in the new Illustrator SVG Export Options panel.
	&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;figure&gt;
	&lt;img src=&quot;/images/export-options-responsive.jpg&quot; /&gt;
	&lt;figcaption&gt;
		The ‘Responsive’ option in the older Illustrator SVG Export Options panel.
	&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;It’s very tempting to check that option, because you do want your SVGs to be responsive, after all, but you shouldn’t check it unless what you need is for the SVG to simply occupy the entire width on the screen—like when it’s &lt;em&gt;supposed&lt;/em&gt; to be viewport-width, for example. &lt;small&gt;(I’ve embedded an SVG in my current client project and I &lt;em&gt;wanted it&lt;/em&gt; to occupy the full page width, so I safely removed the attributes.)&lt;/small&gt;&lt;/p&gt;

&lt;p class=&quot;size-2x&quot;&gt;Unchecking the ‘Responsive’ option will ensure that Illustrator will export the SVG and preserve the dimensions it has.&lt;/p&gt;

&lt;h3 id=&quot;2-specify-your-desired-width-and-height-values-in-the-css&quot;&gt;2. Specify your desired &lt;code class=&quot;highlighter-rouge&quot;&gt;width&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;height&lt;/code&gt; values in the CSS&lt;/h3&gt;

&lt;p&gt;You want your SVG to scale—be fluid—and fill out its container’s width?&lt;/p&gt;

&lt;p&gt;No problem. Tell the browser you want that to happen by specifying the dimensions specified in the attributes above, using CSS properties:&lt;/p&gt;

&lt;pre class=&quot;brush: css&quot;&gt;
svg {
	width: 100%;
	height: 100%;
}
&lt;/pre&gt;

&lt;p&gt;or maybe something like&lt;/p&gt;

&lt;pre class=&quot;brush:css&quot;&gt;
svg {
	width: 1em;
	height: 1em;
	/* maybe even... */
	color: currentColor;
}
&lt;/pre&gt;

&lt;p&gt;which will restrict the dimensions of an SVG icon, for example, and &lt;strong&gt;scale it in proportion to the text&lt;/strong&gt; it is inline with.&lt;/p&gt;

&lt;p&gt;Specifying your desired width and height in the CSS will make sure the width and height attributes no longer restrict the dimensions of the SVG when the CSS is successfully loaded and applied.&lt;/p&gt;

&lt;p class=&quot;size-2x&quot;&gt;If the CSS &lt;em&gt;does&lt;/em&gt; fail to load, the browser will use the values provided in the attributes as fallback, thus preventing the excessive scaling of the SVG.&lt;/p&gt;

&lt;h2 id=&quot;the-why-why-does-the-above-technique-work&quot;&gt;The Why: Why does the above technique work?&lt;/h2&gt;

&lt;p&gt;SVG presentation attributes are special style properties—a shorthand for setting a CSS property on an SVG node. For this reason, it only makes sense that SVG presentation attributes would contribute to the style cascade.&lt;/p&gt;

&lt;p&gt;When I got started with SVG, I expected presentation attributes to be more specific than any other style declaration. It made sense to me that the “closer” a style is to a node, the more specific it is. For example, inline &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;style&amp;gt;&lt;/code&gt;s are more specific than external styles, an inline &lt;code class=&quot;highlighter-rouge&quot;&gt;style&lt;/code&gt; attribute is more specific than &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;style&amp;gt;&lt;/code&gt; ‘islands’, so I thought it made sense for presentation attributes to be the most specific form of styles. But I was wrong.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://twitter.com/tabatkins/status/704392579895263233&quot;&gt;Just like HTML presentational attributes&lt;/a&gt;, SVG attributes count as low-level author style sheets and are overridden by any other style definitions: external style sheets, document style sheets and inline styles.&lt;/p&gt;

&lt;blockquote class=&quot;pull-quote&quot;&gt;
	SVG attributes count as low-level “author style sheets” and are overridden by any other style definitions: external style sheets, document style sheets and inline styles.
&lt;/blockquote&gt;

&lt;p&gt;This makes it possible to provide a fallback for when the CSS styles are not available, so the SVGs still look good in their ‘worst’ case.&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;/images/svg-preso-attrs-cascade.jpg&quot; alt=&quot;Table showing the position of presentation attributes in the CSS Cascade&quot; /&gt;
	&lt;figcaption&gt;
		The order of styles in the cascade. Styles lower in the diagram override those above them. Presentation attribute styles are overridden by all other styles except for those specific to the user agent.
	&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;h3 id=&quot;tip-you-can-use-presentation-attributes-to-provide-fallback-for-more-than-svg-scaling&quot;&gt;Tip: You can use presentation attributes to provide fallback for more than SVG scaling&lt;/h3&gt;

&lt;p&gt;If you have an SVG you’re styling and animating using CSS properties, you may start by ditching the presentation attributes altogether—we do have an option to do just that in Illustrator’s Export panel, by choosing CSS Properties over Presentation Attributes. That will lead to all attributes being exported as CSS properties, if they &lt;em&gt;can&lt;/em&gt; be set as CSS properties.&lt;/p&gt;

&lt;p&gt;&lt;small&gt;You see, only a subset of all CSS properties may be set by SVG attributes, and vice versa. The SVG specification lists the &lt;a href=&quot;https://www.w3.org/TR/SVG/propidx.html&quot;&gt;SVG attributes that may be set as CSS properties&lt;/a&gt;. Some of these attributes are shared with CSS, such as &lt;code class=&quot;highlighter-rouge&quot;&gt;opacity&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;transform&lt;/code&gt;, among others, while some are not, such as &lt;code class=&quot;highlighter-rouge&quot;&gt;fill&lt;/code&gt;, stroke and &lt;code class=&quot;highlighter-rouge&quot;&gt;stroke-width&lt;/code&gt;, among others.&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;In SVG 2, this list will include &lt;code class=&quot;highlighter-rouge&quot;&gt;x&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;y&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;width&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;height&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;cx&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;cy&lt;/code&gt; and a few other presentation attributes that were not possible to set via CSS in SVG 1.1. The new list of attributes &lt;a href=&quot;https://www.w3.org/TR/SVG2/styling.html#SVGStylingProperties&quot;&gt;can be found in the SVG 2 specification&lt;/a&gt;.&amp;lt;/small&amp;gt;&lt;/p&gt;

&lt;p&gt;&lt;small&gt;Some of the ‘new’ CSS properties for SVG are already implemented and available in Chrome today!&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;There is a benefit to keeping the presentation attributes inside the SVG as well, not just on the root element. These benefits are highlighted the most when you attempt to style the contents of &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;use&amp;gt;&lt;/code&gt;d elements in SVG.&lt;/p&gt;

&lt;p&gt;When you re-&lt;code class=&quot;highlighter-rouge&quot;&gt;use&lt;/code&gt; an element, the contents of that element are copied, as is, into wherever you place the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;use&amp;gt;&lt;/code&gt; in the page. But you may want to re-use an element mutliple times and style each occurance differently. You can do that by leveraging CSS Custom Properties (a.k.a. CSS Variables).&lt;/p&gt;

&lt;p&gt;When ‘theming’ multiple &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;use&amp;gt;&lt;/code&gt; elements with CSS custom properties, it is recommended to always keep the presentation attributes that provide the default styles for the reused SVG, so that the image is styled both when the CSS fails to load and/or when the SVG is viewed in browsers that don’t yet support custom properties. Without the presentation attributes, most styles—such as &lt;code class=&quot;highlighter-rouge&quot;&gt;fill&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;stroke&lt;/code&gt;—will default to black, which is probably not what you want.&lt;/p&gt;

&lt;h2 id=&quot;weird-svg-scaling-gotchas-to-be-aware-of&quot;&gt;Weird SVG scaling gotchas to be aware of&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Depending on how you embed your SVG&lt;/strong&gt;, browsers will generally default to a 300px by 150px size, which is &lt;a href=&quot;https://www.w3.org/TR/CSS2/visudet.html#inline-replaced-width&quot;&gt;the default size for replaced elements in CSS&lt;/a&gt;. This is the default size you get for &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;iframe&amp;gt;&lt;/code&gt;s as well, for example.&lt;/p&gt;

&lt;p&gt;If the dimensions of the SVG are not specified, or if they are explicitly set to &lt;code class=&quot;highlighter-rouge&quot;&gt;auto&lt;/code&gt;, the browsers will default to the 300 by 150 pixels dimension. If either dimension is set to &lt;code class=&quot;highlighter-rouge&quot;&gt;auto&lt;/code&gt; (instead of 100% as mentioned earlier), the browser will cancel out the value set in the presentation attributes and will default to one of the default height and width values.&lt;/p&gt;

&lt;p&gt;Well, kind of..&lt;/p&gt;

&lt;p&gt;Again, It Depends™.&lt;/p&gt;

&lt;p&gt;You see, the browser behavior depends on how you embed your SVG. If you embed the SVG using &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;iframe&amp;gt;&lt;/code&gt;, you get the 300x150 as a default. If you embed the SVG inline in the document (using &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt;), most browsers will scale it to the width of its container and will scale the height proportionally to preserve the SVG’s aspect ratio, while Internet Explorer will scale the width &lt;strong&gt;but not the height&lt;/strong&gt;, so you need to explicitly tell it to scale the height as well.&lt;/p&gt;

&lt;p&gt;&lt;small&gt;Fun fact: IE will scale the height of the SVG if you give it an explicit &lt;em&gt;width&lt;/em&gt; value of 100%. Crazy, but true.&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;I’ve written more about the rendering of different SVG embed techniques in the ‘Making SVGs Responsive with CSS’ article linked at the bottom of the article, along with ways to ensure the SVG is ‘responsified’ across all browsers.&lt;/p&gt;

&lt;p&gt;Amelia Bellamy-Royds has also gotten into more detail about the browser scaling techniques in her article ‘How to Scale SVG’ on CSS-Tricks.&lt;/p&gt;

&lt;h2 id=&quot;scaling-tip-never-drop-the-viewbox&quot;&gt;Scaling Tip: Never drop the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;Ever.&lt;/p&gt;

&lt;p&gt;This is possibly the most important thing you need to be aware of when attempting to scale SVG: you &lt;strong&gt;&lt;em&gt;need&lt;/em&gt;&lt;/strong&gt; the SVG &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; to properly scale SVG images.&lt;/p&gt;

&lt;blockquote class=&quot;pull-quote&quot;&gt;
	you &lt;em&gt;&lt;strong&gt;need&lt;/strong&gt;&lt;/em&gt; the SVG &lt;code&gt;viewBox&lt;/code&gt; to properly scale SVG images. 
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Do &lt;em&gt;not&lt;/em&gt; remove it.&lt;/strong&gt;&lt;/p&gt;

&lt;p class=&quot;note&quot;&gt;
	After reading this artilce, developer David Bushell shared his own experience highlighting the importance of the &lt;code&gt;viewBox&lt;/code&gt; to make sure SVG images are rendered as expected. You can check his post out &lt;a href=&quot;http://dbushell.com/2016/03/01/be-careful-with-your-viewbox/&quot;&gt;here&lt;/a&gt;.
&lt;/p&gt;

&lt;p&gt;&lt;small&gt;&lt;/small&gt;&lt;/p&gt;

&lt;h2 id=&quot;wrapping-up&quot;&gt;Wrapping Up&lt;/h2&gt;

&lt;p&gt;Having explicit, non-percentage &lt;code class=&quot;highlighter-rouge&quot;&gt;width&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;height&lt;/code&gt; values set on an SVG not only helps with fixing FOUSVG issues, but it also helps with other scaling problems, especially when the SVG is used as a background image in CSS. Internet Explorer sometimes refuses to scale the image properly if it doesn’t have its aspect ratio specified with the &lt;code class=&quot;highlighter-rouge&quot;&gt;width&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;height&lt;/code&gt; attributes. I’ve had this happen even with non-background images recently as well. Quite honestly, I don’t know the details of why or when exactly this happens, but I do know that we can avoid it by having these attributes available anyway.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; is even more important than the width and height, so always make sure you keep it there. You can learn all there is to know about how the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; works in &lt;a href=&quot;https://sarasoueidan.com/blog/svg-coordinate-systems/&quot;&gt;this article&lt;/a&gt;. It is a very powerful attribute that is definitely worth taking the time to master.&lt;/p&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further Reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;[Slides] &lt;a href=&quot;http://slides.com/sarasoueidan/styling-animating-svgs-with-css#/&quot;&gt;Styling and Animating SVGs with CSS&lt;/a&gt; – my talk from 2014, given at CSSConf and CSSConf EU.&lt;/li&gt;
  &lt;li&gt;[Article] &lt;a href=&quot;https://www.smashingmagazine.com/2014/11/styling-and-animating-svgs-with-css/&quot;&gt;Styling and Animating SVGs with CSS&lt;/a&gt; — sort of a talk transcript, published on Smashing Magazine&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://tympanus.net/codrops/2014/08/19/making-svgs-responsive-with-css/&quot;&gt;Making SVGs Responsive with CSS&lt;/a&gt; — article published on Codrops&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://css-tricks.com/scale-svg/&quot;&gt;How to Scale SVG&lt;/a&gt; — by Amelia Bellamy-Royds&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/CSS/Scaling_of_SVG_backgrounds&quot;&gt;Scaling of SVG Backgrounds&lt;/a&gt; on MDN&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://tympanus.net/codrops/2015/07/16/styling-svg-use-content-css/&quot;&gt;Styling the Contents of SVG &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;use&amp;gt;&lt;/code&gt; with CSS&lt;/a&gt; on Codrops&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://sarasoueidan.com/blog/svg-coordinate-systems/&quot;&gt;Understanding SVG Coordinate Systems and Transformations (Part 1): The viewport, &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectRatio&lt;/code&gt; Attributes&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;p&gt;I hope you found this article useful. Thank you for reading!&lt;/p&gt;

    
  </content>
 </entry>
 
 <entry>
   <title>2015 In Review</title>
   <link href="http://sarasoueidan.com//blog/2015-in-review/"/>
   <id>https://sarasoueidan.com/blog/2015-in-review</id>
   <content type="html">
    &lt;p&gt;&lt;i&gt;Happy new year!&lt;/i&gt; 🌟&lt;br /&gt; OK, I felt awkward starting a blog post that way—I confess. ☺️ But this is also an unusual post—not an article about CSS or SVG, and not a lengthy tutorial about web development. It is also the quickest post I ever wrote.&lt;/p&gt;

&lt;p&gt;This is my first time writing a year in review kind of post. A lot of great stuff has happened in 2015 that I thought are worth documenting and have a look back at a few years from now. I’m grateful and thank God for every one of these things and everything else that has happened. And since a photo is worth a thousand words, I’ll start with a preview of the highlights in a few pictures, followed by sort of an alt text for each one, and then a little more:&lt;/p&gt;

&lt;style&gt;
    img {
        width: 100%;
        display: block;
        float: left;
        -webkit-filter: grayscale(1);
        filter: grayscale(1);
    }
    img.nofilter {
        -webkit-filter: none;
        filter: none;
    }
    img.half {
        width: 50%;
    }
    img.third{
        width: 33.33%;
    }
&lt;/style&gt;

&lt;figure class=&quot;wider&quot;&gt;
&lt;img src=&quot;/images/2015-02.jpg&quot; class=&quot;half&quot; /&gt;
&lt;img src=&quot;/images/2015-08.jpg&quot; class=&quot;half nofilter&quot; /&gt;
&lt;img src=&quot;/images/2015-01.jpg&quot; class=&quot;third nofilter&quot; /&gt;
&lt;img src=&quot;/images/2015-04.jpg&quot; class=&quot;third&quot; /&gt;
&lt;img src=&quot;/images/2015-05.jpg&quot; class=&quot;third&quot; /&gt;
&lt;img src=&quot;/images/2015-07.png&quot; class=&quot;half nofilter&quot; /&gt;
&lt;img src=&quot;/images/2015-06.jpg&quot; class=&quot;half&quot; /&gt;

&lt;figcaption style=&quot;text-align: left; clear: both; padding: 2em 1em;&quot;&gt;
    Some of my favourite work-related highlights of 2015. From the top left:
    &lt;ol&gt;
        &lt;li&gt;Delivering the opening keynote at CSSDevConf 2015. Photo by Chris Casciano.&lt;/li&gt;
        &lt;li&gt;My 2015 speaker badges for the conferences: CSSConf AU, Smashing Conf LA, btconf Duesseldorf, Microsoft Edge Summit, Generate London, Velocity NY, CSSDevConf, View Source conf, ffconf.&lt;/li&gt;
        &lt;li&gt;Me accepting the Developer of the Year award at the &lt;i&gt;net&lt;/i&gt; awards ceremony. Photo by and copyright of &lt;i&gt;net&lt;/i&gt; magazine. (I think I look  sleep deprived in that photo, but it was already two hours past my bed time, so, there’s that.)&lt;/li&gt;
        &lt;li&gt;Photo of the net award. (These things are very difficult to take proper photos of! ☝)&lt;/li&gt;
        &lt;li&gt;Photo of the O’Reilly Web Platform Award I received in April. (I took like ten photos till I finally got a decent one. 😅)&lt;/li&gt;
        &lt;li&gt;Photo of the first page of the “Mastering SVG for Responsive Web Design” chapter in the Smashing Book 5.&lt;/li&gt;
        &lt;li&gt;Me giving a workshop at Smashing conf LA. Photo by Marc Thiele.&lt;/li&gt;
    &lt;/ol&gt;
&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;ol&gt;
  &lt;li&gt;In 2014, Manoela Ilic asked me to write &lt;strong&gt;a CSS Reference for Codrops&lt;/strong&gt;. In February 2015, we finally &lt;a href=&quot;https://sarasoueidan.com/blog/codrops-css-reference&quot;&gt;released the 8–months worth of work &amp;amp; writing&lt;/a&gt; to the world.&lt;/li&gt;
  &lt;li&gt;This year, I also &lt;strong&gt;got published for the first time in a real book&lt;/strong&gt;—&lt;a href=&quot;http://www.smashingmagazine.com/2015/03/31/real-life-responsive-web-design-smashing-book-5/&quot;&gt;the Smashing Book 5&lt;/a&gt;, which I contributed to with a very extensive article about using SVG in a RWD workflow.&lt;/li&gt;
  &lt;li&gt;Back in April, I &lt;strong&gt;won an O’Reilly Web Platform award&lt;/strong&gt;. I had never heard of the award before I was informed about winning it, and so I had no clue that I was even nominated. That &lt;a href=&quot;https://sarasoueidan.com/blog/oreilly-web-platform-award/&quot;&gt;came as such a beautiful surprise&lt;/a&gt;!&lt;/li&gt;
  &lt;li&gt;Back in June, I was &lt;strong&gt;nominated and shortlisted for two &lt;em&gt;net&lt;/em&gt; awards&lt;/strong&gt;: &lt;i&gt;Developer of the Year&lt;/i&gt; and &lt;i&gt;Outstanding Contribution&lt;/i&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
In September, I __won the *Developer of the Year* *net* award__. This was one of the most awesome things that have happened since I started doing what I do on the web, so thank you &lt;em&gt;so much&lt;/em&gt; for each and every one of you who nominated me and voted for me. I am humbled and extremely proud! *You are awesome!* 💜
&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;I published 23 articles. That’s an average of 2 articles per month, which I think is good. I like to take my time with the articles I write, as opposed to try to publish as much as possible during a specific period of time.&lt;/li&gt;
  &lt;li&gt;I &lt;strong&gt;spoke at 9 conferences worldwide&lt;/strong&gt;—in the US, UK, EU and AU, including the first &lt;a href=&quot;https://channel9.msdn.com/events/WebPlatformSummit/2015&quot;&gt;Microsoft Edge Summit&lt;/a&gt;, and &lt;strong&gt;gave my first workshop&lt;/strong&gt; about SVG at Smashing Conf in LA.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;(A lot of people ask me for videos of these talks sometimes. You can find a list of them—in chronological order— on my [speaking page](https://sarasoueidan.com/speaking).)&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;I &lt;strong&gt;gave two conference keynotes&lt;/strong&gt;, one of which was the first day opening keynote—at &lt;em&gt;CSSDevConf&lt;/em&gt; on board the Queen Mary (a boat!!) in California.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These seven points pretty much sum up the biggest highlights of the year, and the ones like to remember it with.&lt;/p&gt;

&lt;p&gt;Of course, many things happened on a personal life level, but those don’t belong here, so I will spare you the list of them.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;This one is on the weirder kind of highlights side, but definitely one of the things I remember 2015 with: I finally got a British visa! Those of you who have been following me in the last couple of years are already familiar with my visa &lt;a href=&quot;http://beyondtellerrand.com/blog/why-you-never-should-give-up&quot;&gt;stories&lt;/a&gt;. I apply for visas a lot, given the passport I hold (Lebanese) and then the places I speak in. I need to apply for a visa for most conferences I want to speak at, which sucks but is unfortunately necessary.&lt;/p&gt;

&lt;p&gt;But one visa application that was particularly painful for me was the British visa, given how unfair the result was, simple because the person responsible for granting the visas was “not satisfied with my intentions in the UK”. 😒🙄
I decided to give it another shot last year for Generate London and ffconf, as well as to attend the &lt;em&gt;net&lt;/em&gt; awards ceremony. After having showed evidence of my 10 previous speaking appearances at that time, the UK Visa &amp;amp; Immigration was &lt;i&gt;finally&lt;/i&gt; convinced, and they granted me the visa. Woo.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Of course, 2015 wasn’t all rainbows and unicorns. I failed to do some of the things I was hoping to do, too.&lt;/p&gt;

&lt;p&gt;Possibly the number one thing I regret to say that I didn’t do was to keep maintaining and updating the Codrops CSS Reference with new entries as I intended to.&lt;/p&gt;

&lt;p&gt;With client development projects taking priority over all other work, and then the many speaking and writing commitments I had, it was very hard to find enough time to do this. Maintaining the reference requires way more time than I could possibly devote for it.&lt;/p&gt;

&lt;p&gt;As such, I think it’s an appropriate time to officially announce that I am no longer the maintainer of the reference. (More news and updates about this will be shared on and by Codrops some time in the near future, so no elaboration is necessary at this point.) The reference is one those things I’m most proud of, and I know it will be well taken care of.&lt;/p&gt;

&lt;p&gt;In 2015, I also managed to strain my wrist which is still not healed today. I can’t exercise like I used to, I don’t have enough strength in my left hand to do things that I would normally do with ease, and, well, it’s just frustrating. The pain comes and goes but it’s &lt;i&gt;just not healed yet&lt;/i&gt;, which means I still have to see a doctor (again) and keep away from any form of physical activity that adds to the strain of it.&lt;/p&gt;

&lt;p&gt;On the bright side: this means that I take longer breaks from work, which is something to add to the list of things I failed to do. I overworked and pushed myself to the limits, and reached the boundaries of a burnout mutliple times last year. This is one of those things I am currently working on and making sure it doesn’t happen again. Saying ‘No’ to stuff helps a lot, by the way. &lt;em&gt;#protip&lt;/em&gt;&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;To keep this post short, &lt;strong&gt;I will skip the 2016 resolutions list&lt;/strong&gt;. Partly because it’s not finalized yet, mainly because my goals aren’t really year-specific, so I’m not sure they count as “2016 resolutions”, and additionally because &lt;strong&gt;I’ve got a couple more announcements and updates coming&lt;/strong&gt; that deserve their own blog posts. So, stay tuned!&lt;/p&gt;

&lt;p&gt;I want my web site to be home for most (not all) of my articles and tutorials in the future, so make sure to subscribe to the RSS feed to stay updated with what comes next.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;So, that was my look back at 2015. I’m really excited to see what 2016 will bring. I don’t expect it to be all good, because I’m a realistic person, but I do always keep a positive eye on the future.. or at least I try to.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;What was &lt;i&gt;your&lt;/i&gt; 2015 like? What have you accomplished and what are you most proud of? And what have you got planned for 2016?&lt;/em&gt;&lt;/p&gt;

    
  </content>
 </entry>
 
 <entry>
   <title>Animated SVG vs GIF [CAGEMATCH]</title>
   <link href="http://sarasoueidan.com//blog/svg-vs-gif/"/>
   <id>https://sarasoueidan.com/blog/svg-vs-gif</id>
   <content type="html">
    &lt;p&gt;SVG can do much more than display static images. Its animation capabilities are one of its most powerful features, giving it a distinctive advantage over all other image formats. They are one of many reasons that make SVG images better than raster images, including GIFs. But this, of course, only applies to images that are good candidates for SVG, such as:&lt;/p&gt;

&lt;ul&gt;
	&lt;li&gt;Logos,&lt;/li&gt;
	&lt;li&gt;non-complex, vector-based illustrations,&lt;/li&gt;
	&lt;li&gt;user interface controls, &lt;/li&gt;
	&lt;li&gt;infographics,&lt;/li&gt;
	&lt;li&gt;and icons.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Of course, if you have an image that is better suited for the raster format—such as a photograph or a very complex vector illustration (that would normally have a very big size as an SVG), then you should use a raster image format instead. Not only should the image be a good candidate for SVG, but SVG should also be a good candidate for the image. If the image size is much less as a PNG, for example, then you should use PNG, and serve different versions/resolutions of that image using &lt;code class=&quot;highlighter-rouge&quot;&gt;srcset&lt;/code&gt;, or &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;picture&amp;gt;&lt;/code&gt;, depending on what you’re working on and trying to achieve.&lt;/p&gt;

&lt;blockquote class=&quot;pull-quote&quot;&gt;
	Not only should the image be a good candidate for SVG, but SVG should also be a good candidate for the image.
&lt;/blockquote&gt;

&lt;p&gt;Generally speaking, the images listed above are usually perfect candidates for SVG. And if you’re going to animate any of those, creating your animations by animating the SVG code is the sensible way to go.&lt;/p&gt;

&lt;p&gt;However, last week, a link popped up in my Twitter timeline that linked to a set of icons that are animated as GIFs.&lt;/p&gt;

&lt;p&gt;The first thing that crossed my mind when I saw them was that they were perfect candidates for SVG and should be created as SVG images, not GIFs.&lt;/p&gt;

&lt;p&gt;SVGs can indeed replace GIFs in many places, just like they can replace other raster image formats for candidates like those mentioned above. The ability to animate SVG images is what gives it this advantage and ability. And this applies to more than just animated icons.&lt;/p&gt;

&lt;p&gt;So, here is why I think you should use SVG instead of GIFs whenever you can.&lt;/p&gt;

&lt;h2 id=&quot;image-quality&quot;&gt;Image Quality&lt;/h2&gt;

&lt;p&gt;The first advantage to using SVG over GIFs—or any image format, for that matter—is, unsurprisingly, SVG’s number one feature: resolution-independence. An SVG image will look super crisp on any screen resolution, no matter how much you scale it up. Whereas GIFs—a raster image format—do not. Try zooming in a page that contains a GIF image and watch the GIF become pixelated and its contents blurred.&lt;/p&gt;

&lt;p&gt;For example, the following GIF recording of an SVG animation looks fine at this small size:&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/svg-vs-gif--animation-example.gif&quot; alt=&quot;&quot; /&gt;
	&lt;figcaption&gt;
		A GIF recording of the &lt;a href=&quot;http://codepen.io/chrisgannon/pen/myZzJv&quot;&gt;SVG Motion Trails demo&lt;/a&gt; by Chris Gannon.
	&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Zooming into the page a few times will cause the image to be pixelated and the edges and curves of the elements inside to become jagged, as you can see in the image below:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://sarasoueidan.com/images/svg-vs-gif--animation-example-zoomed-in.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Whereas if you &lt;a href=&quot;http://codepen.io/chrisgannon/pen/myZzJv&quot;&gt;check the SVG demo out&lt;/a&gt; and zoom into the page, the SVG content will remain crisp and clear no matter how much you zoom in.&lt;/p&gt;

&lt;p&gt;To provide crisp images for high-resolution displays when you’re using a bitmap image format like GIF, you need to use &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;picture&amp;gt;&lt;/code&gt; or &lt;code class=&quot;highlighter-rouge&quot;&gt;srcset&lt;/code&gt; and switch the images up for different contexts.&lt;/p&gt;

&lt;p&gt;Of course, the higher the image resolution, the bigger the file size will be. With GIFs, the file size will end up ridiculously large; but we’ll get to that in a minute. Also, using a high-resolution GIF and serving it at a smaller size for mobiles is bad for performance. &lt;strong&gt;Don’t do it.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When you create GIF-animated icons or images, their dimensions are fixed. Change the dimensions or zoom in and out of the page, and they’ll get pixelated. With SVG, size is free, and clarity is a constant. You can create a small SVG and have it scale up as much as needed without sacrificing image clarity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;:&lt;/p&gt;

&lt;table class=&quot;cagematch&quot;&gt;
	&lt;tr class=&quot;th&quot;&gt;
		&lt;td&gt;GIF&lt;/td&gt; &lt;td&gt;Animated SVG&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td class=&quot;lose&quot;&gt;GIF, just like other image formats, are not resolution-independent, and will therefore look pixelated when scaled up or viewed on higher resolutions.&lt;/td&gt;
		&lt;td class=&quot;win&quot;&gt;
			SVG is scalable and resolution-independent, and will look crisp clear on any screen resolution.
		&lt;/td&gt;
	&lt;/tr&gt;
&lt;/table&gt;

&lt;h2 id=&quot;colors-and-transparency&quot;&gt;Colors and Transparency&lt;/h2&gt;

&lt;p&gt;Perhaps the number one deal-breaker with GIFs is the way transparency is handled, especially when the image is displayed on a background other than a white background.&lt;/p&gt;

&lt;p&gt;This is an issue that is most likely to emerge when using GIF icons (whether animated or not), since icons are usually created with transparent backgrounds.&lt;/p&gt;

&lt;p&gt;For example, take the following circle with a stroke, created as both an SVG image (left) and a GIF with a transparent background (right). The problem is evident as soon as you look at the two images: the GIF circle has grey fringes around its stroke.&lt;/p&gt;

&lt;figure class=&quot;double&quot; style=&quot;background-color: #003366;&quot;&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/svg-vs-gif--circle-on-transparent-background.svg&quot; alt=&quot;&quot; width=&quot;350px&quot; /&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/svg-vs-gif--circle-on-transparent-background.gif&quot; alt=&quot;&quot; width=&quot;350px&quot; /&gt;
&lt;/figure&gt;

&lt;p&gt;If you’re not reading this in the browser, the effect might not be visible to you because the figure styles might not be applied. Here is a screenshot showing the problem (on the right):&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://sarasoueidan.com/images/svg-vs-gif--artefact.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This happens because transparency in GIF images is binary. This means that each pixel is either &lt;em&gt;on&lt;/em&gt; or &lt;em&gt;off&lt;/em&gt;; a pixel is either transparent or fully opaque. This, in turn, means that the transition between the foreground color and the background color is not as smooth, and results in artefacts caused by inadequate sampling frequency, commonly known as &lt;em&gt;aliasing&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;When a line is not completely straight, it causes some pixels (around the edges) to be partially transparent and partially opaque, so the software needs to figure out what color to use for those pixels. The halo effect “is caused by all the pixels which would have been &amp;gt; 50% opaque being fully opaque and carrying the bg color against which they were rasterized” (&lt;a href=&quot;http://twitter.com/svgeesus/&quot;&gt;Chris Lilley&lt;/a&gt;). So this effect is usually a result of pixel contamination from the color of the background against which the image was composited against upon creation/saving in a graphics editor.&lt;/p&gt;

&lt;p&gt;Aliasing is usually countered with &lt;em&gt;anti-aliasing&lt;/em&gt;, but that is not as simple when transparency is binary:&lt;/p&gt;

&lt;blockquote&gt;
	&lt;p&gt;There is a &lt;strong&gt;severe interaction between anti-aliasing and binary transparency&lt;/strong&gt;. Because the background colour of the image is mixed in with the foreground colours, simply replacing a single background colour with another is not enough to simulate transparency. There will be a whole host of shades which are &lt;em&gt;mixtures&lt;/em&gt; of background and foreground colours [...]. The effect in this case is a white halo around objects, because the original image was anti-aliased to a white background colour.&lt;/p&gt;
	&lt;cite&gt;— &lt;a href=&quot;http://twitter.com/svgeesus/&quot;&gt;Chris Lilley&lt;/a&gt; (&lt;a href=&quot;http://www.w3.org/Conferences/WWW4/Papers/53/gq-trans.html&quot;&gt;Source&lt;/a&gt;)&lt;/cite&gt;
&lt;/blockquote&gt;

&lt;p&gt;The solution to this problem is variable transparency, commonly known as the alpha channel, which allows for varying degrees of transparency and hence a smoother transition between the foreground and background color, which is not what is available in GIF; thus, the halo effect problem. Images with the halo effect usually look best when used with white backgrounds; any other high-contrast background color will make the artefact visible.&lt;/p&gt;

&lt;p&gt;I’m not quite sure if there is a way to work around this issue, but I’ve not yet come across a GIF with a transparent background and curved edges that did not have this problem. I’ve even seen rectangular shapes suffer from it as well.&lt;/p&gt;

&lt;p&gt;If you want to use your image/icon on a non-white background—say, on a dark footer background, this alone could be a deal-breaker. But there are other reasons SVG is better than GIFs too, that we’ll cover in the next sections.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; if you’re reading this article in a browser but still don’t see the fringes in the first image on a smaller screen, try zooming the page in to see the effect.&lt;/p&gt;

&lt;p&gt;Why might you not be able to see the fringes on smaller sizes? The answer is: the browser smoothes the edges as a part of the image resize process.
Does this mean that you can utilize this to get rid of the fringes and still use a GIF? Yes, you can. But to do that, you have to use a GIF that is much bigger than the size you want to render it at, and then resize it. This also means that you will be serving your users images that are much bigger than they need, therefore taking up more of their bandwidth on mobile, as well as hurting the overall page size and performance. Please don’t do that.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;:&lt;/p&gt;

&lt;table class=&quot;cagematch&quot;&gt;
	&lt;tr class=&quot;th&quot;&gt;
		&lt;td&gt;GIF&lt;/td&gt; &lt;td&gt;Animated SVG&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td class=&quot;lose&quot;&gt;GIF images are capable of only binary transparency. This causes artefacts, known as the &lt;em&gt;halo effect&lt;/em&gt; to show up whenever the image or icon is used on a non-white background. The higher the background color contrast with the image, the more visible the halo effect, which makes the icons practically unusable.&lt;/td&gt;
		&lt;td class=&quot;win&quot;&gt;
			SVG images come with an alpha channel and do not suffer from any problems when they are used on different background colors.
		&lt;/td&gt;
	&lt;/tr&gt;
&lt;/table&gt;

&lt;h2 id=&quot;animation-techniques--animation-performance&quot;&gt;Animation Techniques &amp;amp; Animation Performance&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;You can animate SVGs using CSS, javaScript or SMIL&lt;/strong&gt;, and each of them gives you a different level of control that you can take advantage of to create all kinds of animations on SVG elements.&lt;/p&gt;

&lt;p&gt;There are no “techniques” to animate GIF images. They are animated by showing a series of images—one for each frame—sequentially, in a fixed manner, at a fixed pace. You know, the way GIFs just work. Granted, you can get creative with your icons before you turn them into GIFs and then “record” the animation and convert it into a GIF, but how good will it look? And how much control over the animation timing will you get afterwards? None.&lt;/p&gt;

&lt;p&gt;Unless you make sure you have at least 60 frames—that is, 60 images—&lt;em&gt;&lt;strong&gt;per second&lt;/strong&gt;&lt;/em&gt; to create your GIF, the animation will not look smooth. Whereas with SVG, achieving smooth animations is much easier and simpler by taking advantage of browser optimizations.&lt;/p&gt;

&lt;p&gt;A GIF has a bigger file size than PNG or JPEGs, and the longer the animation duration, the bigger the size will be. Now, what if your animation plays for at least 5 ot 6 seconds? What if it plays for much longer?&lt;/p&gt;

&lt;p&gt;You get the picture.&lt;/p&gt;

&lt;p&gt;Let’s look at a more specific yet minimal example. Below are two images: an animated SVG on the left, and an animated GIF on the right. The rectangle in both images changes color over the course of six seconds.&lt;/p&gt;

&lt;figure class=&quot;double&quot;&gt;
	&lt;svg width=&quot;300&quot; height=&quot;150&quot; viewBox=&quot;0 0 300 150&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot;&gt;&lt;style&gt;svg {width: 48%;}path { animation: loop 6s linear infinite; } @keyframes loop { to { fill: #009966; } }&lt;/style&gt;&lt;path fill=&quot;#ff1493&quot; d=&quot;M0 0h300v150h-300z&quot; /&gt;&lt;/svg&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/svg-vs-gif--rectangle-animation.gif&quot; alt=&quot;&quot; width=&quot;300px&quot; height=&quot;150px&quot; /&gt;
	&lt;figcaption&gt;The SVG image on the left and the GIF on the right.
	&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;There are a couple of things to note here:&lt;/p&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
	&lt;li&gt;The GIF animation looks smooth but if you look closely you will notice that the SVG rectangle is going through a wider range of colors as it transitions from the initial to the final color. &lt;strong&gt;The number of colors the GIF goes through is limited by its number of frames.&lt;/strong&gt; In the above image, the GIF goes through 60 frames, i.e. 60 colors, whereas the SVG goes through the entire spectrum between the shade of pink used and the final green color.&lt;/li&gt;
	&lt;li&gt;For looping animations like this one, it is generally best to avoid the color jump shown in the above animation, and create the animation so that it reverses once it reaches the green color; that way, it will transition smoothly back to pink and then start the second round of animation from there too, avoiding that unsightly color jump.
	&lt;p&gt; With CSS, you can reverse the animation using the &lt;code&gt;alternate&lt;/code&gt; animation direction value. But with GIF, you will need to work on your number of frames and probably end up doubling it to make this happen; this will, of course, also increase the size of the image as well.
	&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The sizes of the two images shown above are:&lt;/p&gt;

&lt;ul&gt;
	&lt;li&gt;GIF image size: &lt;strong&gt;21.23KB&lt;/strong&gt; &lt;/li&gt;
	&lt;li&gt;SVG image size: &lt;strong&gt;0.355KB&lt;/strong&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is no trivial difference. But we all know we can optimize our images. So let’s do that.&lt;/p&gt;

&lt;p&gt;Optimizing the SVG using the SVGO Drag-and-Drop GUI brings the SVG’s file size down to &lt;strong&gt;0.249KB&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;To optimize the GIF, you can use one of the several GIF optimization tools online. I used &lt;a href=&quot;http://ezgif.com/&quot;&gt;ezgif.com&lt;/a&gt; to optimize the above image. (Other tools also exist; &lt;a href=&quot;http://www.lcdf.org/gifsicle/&quot;&gt;gifsicle&lt;/a&gt; is one of them.) The file size dropped down to &lt;strong&gt;19.91KB&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;There are many options you can choose from when optimizing GIF images. I optimized the above image so that the number of frames remains intact, using Lossy GIF compression, which &lt;q&gt;can reduce animated GIF file size by 30%—50% at a cost of some dithering/noise.&lt;/q&gt;&lt;/p&gt;

&lt;p&gt;You can also optimize it by removing every nth frame; that can reduce the file size even further, but at the cost of the animation not being smooth anymore. And in the case of an animation like the case at hand, removing frames will make the change in color be “jumpy” and noticeable.&lt;/p&gt;

&lt;p&gt;Other optimization options are also available such as color reduction (which wouldn’t be suitable for our color-dependent animation here) and transparency reduction. You can learn more about these options on the &lt;a href=&quot;http://ezgif.com/&quot;&gt;ezgif.com&lt;/a&gt; optimization page.&lt;/p&gt;

&lt;p&gt;To recap: If you want your GIF animation to be smooth, you’re going to need more frames per second, and that will consequently increase the file size by a lot. Whereas with SVG, you’re likely to maintain a much smaller file size. The above example is minimal, and I’m sure there are better ones out there, but I wanted the most minimal example to show the difference between the two formats.&lt;/p&gt;

&lt;p&gt;Even if you were animating the above rectangle using JavaScript or even a JavaScript framework—since animations on SVG don’t work in IE, for example, the file size of that framework combined with that of the SVG is still likely to be smaller or at least equal to the size of the GIF image size. For example, using &lt;a href=&quot;http://greensock.com&quot;&gt;GreenSock&lt;/a&gt;’s TweenLite, the size of the SVG with the library combined would be less than 13KB (which is still less than the size of the GIF), since TweenLite is 12KB minified. If you do end up with a size equal to that of the GIF, the other benefits of SVG will tip the scale and you will be getting more out of it.&lt;/p&gt;

&lt;p&gt;Some other JavaScript libraries exist that focus on certain animation tasks at a time, and come in impressivly small file sizes (&amp;lt;5KB), such as &lt;a href=&quot;https://github.com/lmgonzalves/segment/blob/gh-pages/dist/segment.min.js&quot;&gt;Segment&lt;/a&gt; which is used to animate SVG paths to create line drawing effects. Segment is 2.72KB minified. That’s not too shabby, is it?&lt;/p&gt;

&lt;p&gt;There can be exceptions, so you should always test. But given the nature of GIFs and how they work, you will likely find that SVG is a better option in most cases.&lt;/p&gt;

&lt;p class=&quot;note&quot;&gt;Note: SVG Performance is not at its absolute best today, but this will hopefully change in the future. IE/MS Edge offer the best SVG rendering performance among all browsers today. Despite that, SVG animations will still look better than GIF animations, especially when you're tackling long animations, because the file size of the GIF—assuming it’s recorded at 60fps—will have a negative impact on the overall page performance. Libraries like GreenSock also offer impressive performance as well. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;:&lt;/p&gt;

&lt;table class=&quot;cagematch&quot;&gt;
	&lt;tr class=&quot;th&quot;&gt;
		&lt;td&gt;GIF&lt;/td&gt; &lt;td&gt;Animated SVG&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td class=&quot;lose&quot;&gt;
			&lt;ol&gt;
				&lt;li&gt;GIF images are generally larger than SVG images. The more complex and longer the animation, the more frames are required to create it and therefore the bigger the file size and the more the negative impact on performance.&lt;/li&gt;
				&lt;li&gt;Unless GIF animation plays at 60fps, the animation is going to be jagged and not smooth. Also, the more the number of frames per second, the bigger the file size, especially for longer animations.&lt;/li&gt;
			&lt;/ol&gt;
			&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; There will be a compromise that needs to be made. Either the GIF animation is smooth and the overall file and page size and performance is negatively affected, or the GIF animation will suffer with less frames. One form of performance is risked in both scenarios.&lt;/p&gt;
		&lt;/td&gt;
		&lt;td class=&quot;average&quot;&gt;
			&lt;p&gt;SVG images take advantage of the browser optimizations when animating elements. Even though browser performance on SVG elements is still not at its best, animation will still perform better without having to make page performance compromises.&lt;/p&gt;
			&lt;p&gt;
				SVG file size is still very reasonable, if not very small, compared to GIFs, even when certain animation libraries might be required to create cross-browser animations.
			&lt;/p&gt;
		&lt;/td&gt;
	&lt;/tr&gt;
&lt;/table&gt;

&lt;h3 id=&quot;maintaining--modifying-animations&quot;&gt;Maintaining &amp;amp; Modifying Animations&lt;/h3&gt;

&lt;p&gt;..is a pain if you are using GIFs. You will need to use a graphics editor such as Photoshop or Illustrator or After Effects, to name a few. And if you’re like me, then graphics editors are not where your skills shine, and you feel more at home when you make modifications in code, not in graphics editors.&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/svg-vs-gif--photoshop-frames.png&quot; alt=&quot;&quot; /&gt;
	&lt;figcaption&gt;Screenshot of the animation timeline as created in Photoshop. The lower part shows a fraction of the frames created for the animation. Fore more complex animations, more frames are required. Also notice the layers panel.
		&lt;p&gt;&lt;small&gt;Thanks to my designer friend &lt;a href=&quot;http://twitter.com/WalterStephanie&quot;&gt;Stephanie Walter&lt;/a&gt; for the PS animation tips.&lt;/small&gt;&lt;/p&gt;&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;What happens if you want to change your animation timing? or if you want to change the timing functions for one or multiple elements inside your image? or if you want to change the direction in which an element moves? What if you want to change the entire effect and have the elements in your image do something completely different?&lt;/p&gt;

&lt;p&gt;You will need to recreate the image or icon all over again. Any change requires you to jump into the graphics editor and work with frames and a frame-based UI. That would be like torture to developers, and a Mission Impossible for those of us who don’t know our way around those editors enough to make these changes.&lt;/p&gt;

&lt;p&gt;With SVG, making any kind of change to the animation(s) is only a few lines of code away.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion (developer’s perspective)&lt;/strong&gt;:&lt;/p&gt;

&lt;table class=&quot;cagematch&quot;&gt;
	&lt;tr class=&quot;th&quot;&gt;
		&lt;td&gt;GIF&lt;/td&gt; &lt;td&gt;Animated SVG&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td class=&quot;lose&quot;&gt;Maintaining and modifying GIF animations requires re-creating the image or resorting to a frame-based graphics editor’s UI to do so, which is a problem for design-challenged developers.&lt;/td&gt;
		&lt;td class=&quot;win&quot;&gt;
			SVG animations can be changed and controlled right inside the SVG code—or anywhere the animations are defined, usually using a few lines of code.
		&lt;/td&gt;
	&lt;/tr&gt;
&lt;/table&gt;

&lt;h2 id=&quot;file-size-page-load-time--performance&quot;&gt;File Size, Page Load Time &amp;amp; Performance&lt;/h2&gt;

&lt;p&gt;In the previous section, we focused on the performance of the animation itself. In this one, I want to shed some light on the page performance as a whole and how it is affected by the image format choice you make.&lt;/p&gt;

&lt;p&gt;Fact: The bigger the file size, the more the negative impact on page load time and performance. With that in mind, let’s see how using SVG instead of GIFs can help improve the overall page load time by looking at a more practical, real-world example.&lt;/p&gt;

&lt;p&gt;At my first SVG talk, 18 months ago, I mentioned how SVG can be used to replace animated GIFs and result in overall better page performance. In that talk, I provided a real-world example of a real-world web page that took advantage of what SVG has to offer and reaped the benefits: the &lt;a href=&quot;http://sprout.is/&quot;&gt;Sprout&lt;/a&gt; homepage.&lt;/p&gt;

&lt;p&gt;The Sprout homepage has two animated images that were initially created and displayed as GIFs. Two years ago, &lt;a href=&quot;https://twitter.com/mfortress&quot;&gt;Mike Fortress&lt;/a&gt; wrote &lt;a href=&quot;http://oak.is/thinking/animated-svgs/&quot;&gt;an article on the Oak blog&lt;/a&gt;, in which he explains how they recreated the animated GIFs, particularly the chart GIF (see image below) as an animated SVG image.&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/svg-vs-gif--sprout-chart.svg&quot; alt=&quot;&quot; /&gt;
	&lt;figcaption&gt;
		The SVG version of the chart used on the Sprout homepage and written about on the Oak article. &lt;small&gt;(All rights reserved by their owners.)&lt;/small&gt;
		&lt;p&gt;Note that the animation is created using SMIL so it will not be animating if you’re viewing it in Internet Explorer.&lt;/p&gt;
	&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;In his article, Mike shares some interesting insights on their new page performance as a result of making the switch to SVG:&lt;/p&gt;

&lt;blockquote&gt;
	&lt;p&gt;This chart, and one other animation on Sprout, were initially GIFs. By using animated SVGs instead of GIFs we were able to reduce our page size &lt;strong&gt;from 1.6 mb to 389 kb&lt;/strong&gt;, and reduce our page load time &lt;strong&gt;from 8.75 s to 412 ms&lt;/strong&gt;. That’s a huge difference.&lt;/p&gt;
	&lt;cite&gt;—Mike Fortress, &lt;a href=&quot;http://oak.is/thinking/animated-svgs/&quot;&gt;“Animated SVGs: Custom Easing and Timing”&lt;/a&gt;&lt;/cite&gt;
&lt;/blockquote&gt;

&lt;p&gt;A huge difference indeed.&lt;/p&gt;

&lt;p&gt;The Sprout chart is a perfect candidate for SVG. There is no reason to animate it by converting the animation into a GIF recording, when SVG can bring so much more benefits.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://jakearchibald.com/&quot;&gt;Jake Archibald&lt;/a&gt; realizes the power of SVG animations too, and uses them to create and animate interactive illustrations to complement his articles. His &lt;a href=&quot;https://jakearchibald.com/2014/offline-cookbook/&quot;&gt;Offline Cookbook&lt;/a&gt; article is an excellent example (and an excellent article, by the way). Could he have used GIFs to do that? Of course. But given the number of images he used, the GIFs could have easily increased the overall page size to a couple or few megabytes, with each GIF being at least hundreds of kilobytes in size; whereas &lt;strong&gt;the entire web page currently weighs at 128KB only with &lt;em&gt;all&lt;/em&gt; the SVG images embedded inline&lt;/strong&gt;, because &lt;a href=&quot;https://sarasoueidan.com/blog/structuring-grouping-referencing-in-svg&quot;&gt;you can reuse elements in SVG&lt;/a&gt;, so any repetitive elements will not only cause the entire page to &lt;a href=&quot;http://calendar.perfplanet.com/2014/tips-for-optimising-svg-delivery-for-the-web/&quot;&gt;gzip much, much better&lt;/a&gt;, but for each page, the overall size of the SVGs becomes smaller.&lt;/p&gt;

&lt;p&gt;Now &lt;em&gt;that&lt;/em&gt; is impressive.&lt;/p&gt;

&lt;p&gt;I will rest my case about page load and performance here. But it is still important to note that there &lt;em&gt;can&lt;/em&gt; be exceptions. Again, in most cases, you’re likely going to find that SVG is better than a GIF, but you’ll always need to test anyway.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;:&lt;/p&gt;

&lt;table class=&quot;cagematch&quot;&gt;
	&lt;tr class=&quot;th&quot;&gt;
		&lt;td&gt;GIF&lt;/td&gt; &lt;td&gt;Animated SVG&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td class=&quot;lose&quot;&gt;GIF images are generally bigger in size than SVG images are with animations added to them. This negatively affects the overall page size, load times and performance.&lt;/td&gt;
		&lt;td class=&quot;win&quot;&gt;
			SVG images can be used and reused, as well as gzipped better, making their file sizes generally smaller than those of GIFs, thus improving page load times and performance.
		&lt;/td&gt;
	&lt;/tr&gt;
&lt;/table&gt;

&lt;h2 id=&quot;browser-support&quot;&gt;Browser Support&lt;/h2&gt;

&lt;p&gt;Probably the only absolute advantage to GIFs over SVGs is browser support. GIFs work pretty much everywhere, while SVG support is less global. Even though we have many &lt;a href=&quot;https://css-tricks.com/a-complete-guide-to-svg-fallbacks/&quot;&gt;ways to provide fallback for non-supporting browsers&lt;/a&gt;—and current browser support should not be hindering anyone from using SVG, the fallback images, if provided as PNG or JPG, are going to be static, animation-less.&lt;/p&gt;

&lt;p&gt;Of course, you can always provide a GIF as a fallback to SVG, but the previously-mentioned considerations and disadvantages should be kept in mind.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;:&lt;/p&gt;

&lt;table class=&quot;cagematch&quot;&gt;
	&lt;tr class=&quot;th&quot;&gt;
		&lt;td&gt;GIF&lt;/td&gt; &lt;td&gt;Animated SVG&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td class=&quot;win&quot;&gt;GIF images work pretty much everywhere.&lt;/td&gt;
		&lt;td class=&quot;average&quot;&gt;
			SVG images have less global browser support, but they come with a lot of ways to provide fallback for non-supporting browsers.
		&lt;/td&gt;
	&lt;/tr&gt;
&lt;/table&gt;

&lt;h2 id=&quot;accessibility-concerns-a11y&quot;&gt;Accessibility Concerns (#a11y)&lt;/h2&gt;

&lt;p&gt;Move something on a page, or anywhere, for that matter, and you’ve instantly added a distraction—something that is sure to grab a user’s attention as soon as it starts moving. This is simply how the human brain works. This is also one of the reasons ad banners are so focused on—and built with—a strong focus on animation. This is also why animated ad banners are &lt;strong&gt;extremely annoying&lt;/strong&gt;. They are distracting, especially when you’re trying to perform a task on a page that requires your entire attention, such as reading an article.&lt;/p&gt;

&lt;p&gt;Now imagine a page with a set of animated icons (or images) that just won’t stop animating no matter what you do. We’re no longer talking about one or two images on a homepage or within an article here; we’re talking about UI elements and controls, and smaller icons that are likely to be present in multiple places on a page, and on multiple pages. Unless your icon is &lt;em&gt;supposed&lt;/em&gt; to be inifnitely animation—for example, if it is a spinner animating during a user-inactive waiting phase, then it is likely to introduce a problem, and become more of an annoyance, than a “nice thing”.&lt;/p&gt;

&lt;p&gt;As a matter of fact, for some people it can become more of an annoyance, as continuous motion can literally make some people feel ill.&lt;/p&gt;

&lt;p&gt;In her article &lt;a href=&quot;http://alistapart.com/article/designing-safer-web-animation-for-motion-sensitivity&quot;&gt;“Designing Safer Web Animation For Motion Sensitivity”&lt;/a&gt;, designer and web animation consultant Val Head discusses the effects of overused animation on the web on people with visually-triggered vestibular disorders (emphasis mine):&lt;/p&gt;

&lt;blockquote&gt;
	&lt;p&gt;It’s no secret that a lot of people consider scrolljacking and parallax effects annoying and overused. But what if motion does more than just annoy you? What if it also makes you ill?&lt;/p&gt;
	&lt;p&gt;That’s a reality that people with visually-triggered vestibular disorders have to deal with. As animated interfaces increasingly become the norm, more people have begun to notice that &lt;strong&gt;large-scale motion on screen can cause them dizziness, nausea, headaches, or worse. For some, the symptoms can last long after the animation is over.&lt;/strong&gt; Yikes.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now imagine if the animation does &lt;em&gt;not&lt;/em&gt; end… Double Yikes.&lt;/p&gt;

&lt;p&gt;Val’s article explains the problem in more detail, as she gathers feedback from two people who actually have these problems and share their experience with animation in different examples.&lt;/p&gt;

&lt;p&gt;One of the solutions that can help avoid these problems is to &lt;a href=&quot;http://alistapart.com/article/designing-safer-web-animation-for-motion-sensitivity#section10&quot;&gt;provide the user with the ability to control the animation&lt;/a&gt; so that they can stop it when it gets disturbing.&lt;/p&gt;

&lt;p&gt;With SVG, you can do that. You can fully control the animation and play it once or twice on page load—if you really need to have it play as soon as the user “enters” your page, and then fire it on hover for subsequent tweens, using nothing but a few lines of CSS or JavaScript. &lt;strong&gt;You do not need hundreds or thousands of lines of CSS or JavaScript to create an icon animation&lt;/strong&gt;, unless your icon is a really complex scene with a lot of components that are animated inside of it. But I think that in that case, it does not count as an “icon” anymore, but more of a regular image.&lt;/p&gt;

&lt;p&gt;You can go even as far as control playback, speed for each consequent tween, and much more, assuming, of course, you are using JavaScript to gain this level of control.&lt;/p&gt;

&lt;p&gt;Or you can add a toggle to give the user the ability to stop an infinitely playing animation. You can’t do that with GIFs… unless you opt for replacing the GIF with a static image upon a certain toggle action.&lt;/p&gt;

&lt;p&gt;Some might even argue that you could display a static version of the image—as a PNG for example, and then provide the GIF version on hover. But this comes with a few problems of its own:&lt;/p&gt;

&lt;ul&gt;
	&lt;li&gt;If the images are inline, you’ll need to replace these images using JavaScript. That action does not require any JavaScript if you are using SVG.&lt;/li&gt;
	&lt;li&gt;If the images are foreground images (embedded in the HTML using &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt;), and you need to replace these images, you will end up with double the amount of HTTP requests for every image. And if they are background images inlined in the style sheet (which is not recommended), the images (especially the GIFs) will add to the size of the style sheet and therefore to the overall render-blocking time of the page.&lt;/li&gt;
	&lt;li&gt;If/when you switch image sources on hover, there is a noticable flash between the first and the second image on slower connections. My connection is slow; sometimes 3G-slow, and I have yet to remember a time when an image was replaced with another one on hover, viewport resize, or whatever, and not have seen that flash. This situation gets even worse when the second image (GIF loaded on hover) is fairly big in size—there will be a flash, followed by a slow, janky animation of the GIF while it loads completely. That’s never attractive.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, yes, you can switch image sources to control if or when the GIF animation plays, but you’re losing the finer control over the GIF and affecting the user’s experience with the UI.&lt;/p&gt;

&lt;p&gt;You are also able to control how many times the animation plays in the GIF—which is pretty cool, but that means that the animation will play only &lt;strong&gt;&lt;em&gt;n&lt;/em&gt;&lt;/strong&gt; number of times. And then to re-fire the animation upon a user interaction, you will need to resort to the above technique with multiple images.&lt;/p&gt;

&lt;p&gt;Multiple images to maintain, multiple HTTP requests, and an overall hacky, non-optimal solution to what could have been easily achieved with one SVG image.&lt;/p&gt;

&lt;ul&gt;
	&lt;li&gt;Embed the &lt;strong&gt;one&lt;/strong&gt; SVG image on the page.&lt;/li&gt;
	&lt;li&gt;Create the animation any way you want/need. (Or create the animation before you embed the image.)&lt;/li&gt;
	&lt;li&gt;Play, pause, control the animation; and give the user the ability to control it as well.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;No extra HTTP requests for every image, no complicated animation timeline maintenance in graphics editors, and no accessibility concerns or woes that cannot be avoided with a few lines of code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;:&lt;/p&gt;

&lt;table class=&quot;cagematch&quot;&gt;
	&lt;tr class=&quot;th&quot;&gt;
		&lt;td&gt;GIF&lt;/td&gt; &lt;td&gt;Animated SVG&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td class=&quot;average&quot;&gt;GIF images cannot be stopped by the user without requiring extra images and extra HTTP requests. Even then, the control is not full.&lt;/td&gt;
		&lt;td class=&quot;win&quot;&gt;
			SVG animations can be fully customized so that they are enabled, disabled and controlled by the user without requiring any hacky approaches.
		&lt;/td&gt;
	&lt;/tr&gt;
&lt;/table&gt;

&lt;h3 id=&quot;content-accessibility&quot;&gt;Content Accessibility&lt;/h3&gt;

&lt;table class=&quot;cagematch&quot;&gt;
	&lt;tr class=&quot;th&quot;&gt;
		&lt;td&gt;GIF&lt;/td&gt; &lt;td&gt;Animated SVG&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td class=&quot;average&quot;&gt;GIF images are only as accessible as PNG and JPEG images are—using an appropriate &lt;code&gt;alt&lt;/code&gt; attribue value to describe them.
			&lt;p&gt;The content inside the image cannot be discerned or made directly accessible to screen readers beyond what the overall image description does.&lt;/p&gt;&lt;/td&gt;
		&lt;td class=&quot;win&quot;&gt;
			SVG images are accessible as well as semantic. The content inside the image that is being animated can also be described and made accessible to screen readers using SVG’s built-in accessibility elements, and enhanced using ARIA roles and attributes as well. (You can read all about making SVGs accessible &lt;a href=&quot;http://www.sitepoint.com/tips-accessible-svg/&quot;&gt;here&lt;/a&gt;.)
		&lt;/td&gt;
	&lt;/tr&gt;
&lt;/table&gt;

&lt;h2 id=&quot;interactivity&quot;&gt;Interactivity&lt;/h2&gt;

&lt;p&gt;There’s not much to add here but the fact that you can interact with individual elements inside an SVG, during, before or after an animation, but that is not possible with a GIF. So, if you use a GIF, you will lose the ability to do anything beyond triggering or stopping the animation, and even those are not really done inside the SVG, as we’ve seen, but are achieved by swapping the GIF out with a static replacement. Even changing the colors of elements inside the GIF would require additional images to do so. That is yet another advantage to using SVG over GIFs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;:&lt;/p&gt;

&lt;table class=&quot;cagematch&quot;&gt;
	&lt;tr class=&quot;th&quot;&gt;
		&lt;td&gt;GIF&lt;/td&gt; &lt;td&gt;Animated SVG&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td class=&quot;lose&quot;&gt;Animations defined in GIF images cannot be interactive. You cannot interact with individual elements inside a GIF element, nor create links out of individual elements either.&lt;/td&gt;
		&lt;td class=&quot;win&quot;&gt;
			SVG content is fully interactive. You can create hover and click interactions (and more) to which individual elements inside the SVG image can respond.
		&lt;/td&gt;
	&lt;/tr&gt;
&lt;/table&gt;

&lt;h2 id=&quot;responsive--adaptive-animations&quot;&gt;Responsive &amp;amp; Adaptive Animations&lt;/h2&gt;

&lt;p&gt;The ability to animate SVG directly from the code, as well as manipulate its many, many attributes, results in and adds yet another advantage over GIF-based animations: the ability to create responsive, adaptive and performant animations, without adding any extra HTTP requests, using a few lines of code, and with quite smaller file sizes.&lt;/p&gt;

&lt;p&gt;Sarah Drasner wrote &lt;a href=&quot;http://www.smashingmagazine.com/2015/03/different-ways-to-use-svg-sprites-in-animation/&quot;&gt;an article on Smashing Magazine&lt;/a&gt; showing different ways to animate SVG sprites. One of these ways is having multiple “scenes” inside an SVG, animating them with CSS, and then changing the SVG “view”—by changing the value of &lt;a href=&quot;https://sarasoueidan.com/blog/svg-coordinate-systems&quot;&gt;the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; attribute&lt;/a&gt;—to show one scene at a time, depending on the current viewport size and available screen estate.&lt;/p&gt;

&lt;p&gt;If you wanted to create the same animation using GIF images, you would lose the animation control capabilities as well as require multiple images which are probably going to be bigger (in file size) than the one SVG image.&lt;/p&gt;

&lt;p&gt;But if you don’t want to go with animating SVG code, you could always create an SVG sprite and animate it the way you would animate any other image format—using &lt;code class=&quot;highlighter-rouge&quot;&gt;steps()&lt;/code&gt; and a few lines of CSS. Sarah also talks about this technique in her article. Animating SVG images does not need to be complicated, and is generally performant.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;:&lt;/p&gt;

&lt;table class=&quot;cagematch&quot;&gt;
	&lt;tr class=&quot;th&quot;&gt;
		&lt;td&gt;GIF&lt;/td&gt; &lt;td&gt;Animated SVG&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td class=&quot;average&quot;&gt;Given that content inside a GIF cannot be controlled with code, it is not possible to make the animations adapt or respond to viewport or context changes without resorting to seperate images.&lt;/td&gt;
		&lt;td class=&quot;win&quot;&gt;
			Given that SVG content is directly animatable using code, the content as well as its animations can be modified so that they respond and/or adapt to different viewport sizes and contexts, without having to resort to any additional assets.
		&lt;/td&gt;
	&lt;/tr&gt;
&lt;/table&gt;

&lt;h2 id=&quot;final-words&quot;&gt;Final Words&lt;/h2&gt;

&lt;p&gt;GIFs have pretty good browser support, yes, but the advantages of SVGs outweigh theirs in almost every aspect. There might be exceptions, and in those cases do, by all means, use GIFs or any other image format that does a better job than SVG would. You might even use Video or HTML5 Canvas or whatever.&lt;/p&gt;

&lt;p&gt;SVG can bring a lot of performance benefits to the table when compared to other image formats, especially GIFs.&lt;/p&gt;

&lt;p&gt;Thus, given all of the above, I recommend that anywhere SVG could be used for animation, GIFs should be avoided. You’re free to ignore my recommendation, of course, but you’d be giving up on the many benefits that SVG animations offer.&lt;/p&gt;

&lt;p&gt;Unless GIFs show a lot of advantages over SVGs that go beyond browser support for IE8 and below, then I believe SVGs should be the way to go.&lt;/p&gt;

&lt;p&gt;A few resources to help you get started with SVG animations:&lt;/p&gt;

&lt;ul&gt;
	&lt;li&gt;&lt;a href=&quot;http://blogs.adobe.com/dreamweaver/2015/06/the-state-of-svg-animation.html&quot;&gt;The State of SVG Animation&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;http://www.smashingmagazine.com/2015/03/different-ways-to-use-svg-sprites-in-animation/&quot;&gt;A Few Different Ways to Use SVG Sprites in Animation&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;http://www.smashingmagazine.com/2015/09/creating-cel-animations-with-svg/&quot;&gt;Creating Cel Animations with SVG&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;http://greensock.com&quot;&gt;GreenSock&lt;/a&gt; has a bunch of very useful articles on animating SVGs&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;http://snapsvg.io/start/&quot;&gt;Snap.svg&lt;/a&gt;, also known as “The jQuery of SVG”&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;https://davidwalsh.name/svg-animations-snap&quot;&gt;SVG Animations Using CSS and Snap.SVG&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;http://www.smashingmagazine.com/2014/11/styling-and-animating-svgs-with-css/&quot;&gt;Styling and Animating SVGs with CSS&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;https://jakearchibald.com/2013/animated-line-drawing-svg/&quot;&gt;Animated Line Drawing in SVG&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;hr class=&quot;line&quot; /&gt;

&lt;p&gt;I hope you found this article useful.&lt;/p&gt;

&lt;p&gt;Thank you for reading.&lt;/p&gt;

&lt;p class=&quot;note&quot;&gt;Many thanks to Jake Archibald for reviewing and giving feedback to the article, and to Chris Lilley for his feedback re transparency in GIF images. It wouldn’t have been so comprehensive (read: ridiculously long) without their feedback. ^^&lt;/p&gt;

    
  </content>
 </entry>
 
 <entry>
   <title>Tips for Creating and Exporting Better SVGs for the Web</title>
   <link href="http://sarasoueidan.com//blog/svg-tips-for-designers/"/>
   <id>https://sarasoueidan.com/blog/svg-tips-for-designers</id>
   <content type="html">
    &lt;p&gt;Working with SVG in a RWD workflow usually involves a design phase and a development phase. The design phase is usually handled by designers who may or may not know how to code. And because of the nature of SVG as both an image format &lt;em&gt;and&lt;/em&gt; a document format, every step taken in the graphics editor in the process of creating the SVG directly affects the resulting code and hence the work of the developer in charge of embedding, scripting or animating the SVG. In my day-to-day work, I am usually the developer whom designers hand the design assets over to, and SVG images are part of those assets.&lt;/p&gt;

&lt;p&gt;Most of the assets I’ve been handed in my past projects needed a do-over and/or a second round of editing in the graphics editor before I could script them, because the resulting SVG code was not optimized enough for the kind of work—especially animation—that I was hired to do. The reason for that is that many of the designers I’ve worked with knew very little—if anything—about SVG &lt;em&gt;code&lt;/em&gt;. They create vector graphics and UI assets all the time, but, for them, SVG is no more than an image format and they don’t know much about the code generated when their assets are exported as SVG documents.&lt;/p&gt;

&lt;p&gt;There are some steps that designers can take or avoid—a set of “dos and don’ts”—that can help make the generated code cleaner. In this article, I want to share some of these. If you know any more, please do share them in the comments at the end of the article.&lt;/p&gt;

&lt;p&gt;The tips we’re going to go over are applicable in Adobe Illustrator (Ai)—my graphics editor of choice—as well as other graphics editors. But since I personally use Ai, it is what I will be focusing on throughout this article.&lt;/p&gt;

&lt;p&gt;We’re also going to go over the &lt;strong&gt;current&lt;/strong&gt; SVG export options available in Ai and which ones to choose and why. But note that &lt;strong&gt;these options will change in the future&lt;/strong&gt;, and this article will then be updated to reflect those changes.&lt;/p&gt;

&lt;p class=&quot;note&quot;&gt;
	That this article is based on my talk “SVG For Web Designers (and Developers)”—a talk I gave at CSSDevConf 2015 last month.
&lt;/p&gt;

&lt;p&gt;So, let’s start.&lt;/p&gt;

&lt;p class=&quot;note update&quot;&gt;
	If you’re using Sketch to create SVGs, there are some things you can do to export cleaner code as well. Sean Kesterson has shared some tips in &lt;a href=&quot;https://medium.com/sketch-app-sources/exploring-ways-to-export-clean-svg-icons-with-sketch-the-correct-way-752e73ec4694#.gbtebz7ex&quot;&gt;this article&lt;/a&gt;.
&lt;/p&gt;

&lt;h2 id=&quot;1&quot; class=&quot;deeplink&quot;&gt;1. Create Simple Shapes Using Simple Shape Elements, Not &lt;code&gt;&amp;lt;path&amp;gt;&lt;/code&gt;s.&lt;/h2&gt;

&lt;p&gt;There is a reason we have different basic shapes in SVG for creating, well, basic shapes. One could create practically any shape using a &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;path&amp;gt;&lt;/code&gt; element, right?&lt;/p&gt;

&lt;p&gt;Simple shape elements (&lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;line&amp;gt;&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;circle&amp;gt;&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;rect&amp;gt;&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;ellipse&amp;gt;&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;polygon&amp;gt;&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;polyline&amp;gt;&lt;/code&gt;) are there for many reasons, and one of these reasons is that they are more readable and more maintainable and editable by hand than their &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;path&amp;gt;&lt;/code&gt; alternatives.&lt;/p&gt;

&lt;p&gt;Basic shapes come with a set of attributes that allow you to control the shape features, such as position (&lt;code class=&quot;highlighter-rouge&quot;&gt;x&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;y&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;cx&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;cy&lt;/code&gt;) and dimensions (&lt;code class=&quot;highlighter-rouge&quot;&gt;width&lt;/code&gt; &amp;amp; &lt;code class=&quot;highlighter-rouge&quot;&gt;height&lt;/code&gt;), while paths don’t come with these attributes.&lt;/p&gt;

&lt;p&gt;For example, the following snippet shows the difference between a circle created and exported as a simple shape, versus one created and exported as a &lt;code class=&quot;highlighter-rouge&quot;&gt;path&lt;/code&gt;:&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;circle fill=&quot;#FFFFFF&quot;
        stroke=&quot;#000&quot;
        cx=&quot;28.1&quot;
        cy=&quot;28.1&quot;
        r=&quot;27.6&quot;/&amp;gt;

&amp;lt;!-- versus --&amp;gt;

&amp;lt;path fill=&quot;#FFFFFF&quot;
      stroke=&quot;#000&quot;
      d=&quot;M55.7,28.1
         c0,15.2-12.4,27.6-27.6,27.6
         S0.5,43.3,0.5,28.1
	     S12.9,0.5,28.1,0.5
	     S55.7,12.9,55.7,28.1z&quot;/&amp;gt;
&lt;/pre&gt;

&lt;p&gt;If you want to animate your shape by, say, moving the position of the circle or making it bigger, you can do that by animating the position of the center via the x and y coordinates (&lt;code class=&quot;highlighter-rouge&quot;&gt;cx&lt;/code&gt; &amp;amp; &lt;code class=&quot;highlighter-rouge&quot;&gt;cy&lt;/code&gt;) and the radius of the circle (&lt;code class=&quot;highlighter-rouge&quot;&gt;r&lt;/code&gt;). Whereas if you are working with a circle generated as a path, you will have to resort to CSS/SVG transformations (translation and scaling) to do that. And then suppose you want to animate that path and the animation requires you to apply further transformations to it? It can easily become a transformation mess.&lt;/p&gt;

&lt;p&gt;Another advantage to using simple shapes is that, in the majority of cases, the code required to create a shape using simple shape elements is less than that required to create the same shape using a &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;path&amp;gt;&lt;/code&gt; element (see above snippet for a comparison), so using simple shapes will also result in a smaller file size, which is always better.&lt;/p&gt;

&lt;h2 id=&quot;2&quot; class=&quot;deeplink&quot;&gt; 2. Convert Text to Outlines.. Or Don’t.&lt;/h2&gt;

&lt;p&gt;To convert text to outlines:&lt;/p&gt;

&lt;ol&gt;
	&lt;li&gt;Select the text you want to convert.&lt;/li&gt;
	&lt;li&gt;Choose &lt;strong&gt;Type → Create Outlines&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;img src=&quot;https://sarasoueidan.com/images/convert-to-outlines.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;dl&gt;
	&lt;dt&gt;Pros:&lt;/dt&gt;
	&lt;dd&gt;
		&lt;ul&gt;
			&lt;li&gt;Text converted to outlines will preserve the font face used, without having to use a web font to display it. This means you save a few extra HTTP requests and don’t risk displaying your text with a fallback font that generally doesn't look good enough to substitute the beautiful font of your choice.
				&lt;p&gt;
					Outlining the text and preserving the font face used is good for preserving a brand’s identity when the latter is defined by the font face used, for example: in a logo. I almost always turn logo text to outlines. Outlining is also good for preserving the font face of certain scripts when used for headlines.
				&lt;/p&gt;
			&lt;/li&gt;
		&lt;/ul&gt;
	&lt;/dd&gt;
	&lt;dt&gt;Cons:&lt;/dt&gt;
	&lt;dd&gt;
		&lt;ul&gt;
			&lt;li&gt;Text converted to outlines is not real text: it is a set of paths that form the outline (shape) of the text. Consequently, the text becomes unreal and inaccessible, not searchable and not selectable.
				&lt;p&gt;
					In the case of a script headline or even a logo where the text is outlined, using an &lt;code&gt;alt&lt;/code&gt; text (if the logo is embedded as an image) or SVG’s accessibility elements (&lt;code&gt;&amp;lt;title&amp;gt;&lt;/code&gt; &amp;amp; &lt;code&gt;&amp;amp;&amp;gt;&lt;/code&gt;) is a good idea for providing alternative text for screen readers.
				&lt;/p&gt;
				&lt;p&gt;I highly recommend reading all about making SVGs accessible in these two articles by Léonie Watson:&lt;/p&gt;
				&lt;ul&gt;
					&lt;li&gt;&lt;a href=&quot;http://www.sitepoint.com/tips-accessible-svg/&quot;&gt;Tips for Creating Accessible SVG&lt;/a&gt;&lt;/li&gt;
					&lt;li&gt;&lt;a href=&quot;https://www.paciellogroup.com/blog/2013/12/using-aria-enhance-svg-accessibility/&quot;&gt;Using ARIA to Enhance SVG Accessibility&lt;/a&gt;&lt;/li&gt;
				&lt;/ul&gt;&lt;/li&gt;
			&lt;li&gt;Converting text to outlines can significantly increase your SVG file size, depending on the complexity of the font face used. The image below shows the difference in the size (and readability) of an SVG with text converted to outlines (left) and text exported as SVG &lt;code&gt;&amp;lt;text&amp;gt;&lt;/code&gt; (right).&lt;/li&gt;
			&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/text-output.jpg&quot; alt=&quot;&quot; /&gt;
	&lt;figcaption&gt;&lt;/figcaption&gt;
&lt;/figure&gt;
			&lt;li&gt;Paths are not easily controlled and animated as &lt;code&gt;&amp;lt;text&amp;gt;&lt;/code&gt; elements (including &lt;code&gt;&amp;lt;tspan&amp;gt;&lt;/code&gt;s) are. The latter have a set of attributes that give you more control over your animations, while path data is limited in that sense.&lt;/li&gt;
		&lt;/ul&gt;
	&lt;/dd&gt;
&lt;/dl&gt;

&lt;h2 id=&quot;3&quot; class=&quot;deeplink&quot;&gt; 3. Simplify Paths.&lt;/h2&gt;

&lt;p&gt;A path is defined by a set of points which in turn are defined by a couple of coordinates each.&lt;/p&gt;

&lt;p&gt;The less the number of points, the less the path data (&lt;code class=&quot;highlighter-rouge&quot;&gt;d&lt;/code&gt; attribute), and, consequently, the less the overall SVG file size. This is always a good step to take because a smaller file size is better for performance.&lt;/p&gt;

&lt;p&gt;To simplify a path:&lt;/p&gt;

&lt;ol&gt;
	&lt;li&gt;Select the path&lt;/li&gt;
	&lt;li&gt;Go to &lt;strong&gt;Object → Path → Simplify&lt;/strong&gt;&lt;/li&gt;
	&lt;li&gt;Tweak the number of points. Make sure you have the &lt;strong&gt;Preview&lt;/strong&gt; checked so can see how the path changes as you change the number of points. Tweak the number to get to the minimum number of points while preserving (or sacrificing) as much of the path’s visual appearance as you need.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;img src=&quot;https://sarasoueidan.com/images/simplify-paths.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://sarasoueidan.com/images/path-points.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;There is a video tutorial created by Adobe to explain the process; so if you are more into videos, you can check it out &lt;a href=&quot;http://tv.adobe.com/watch/companion-videos-for-inspire/svg-for-the-web-using-the-simplify-panel-in-illustrator-cc/&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You can also simplify paths using the &lt;strong&gt;Warp Tool&lt;/strong&gt;. I’m not a designer and I usually use Ai’s Simplify algorithm to simplify my paths, so, if you’re a seasoned designer, you probably already know much more about the Warp tool than I do. There is &lt;a href=&quot;http://www.smashingmagazine.com/2011/07/examples-and-tips-for-using-illustrator-s-warp-tools/&quot;&gt;an article over at Smashing Magazine&lt;/a&gt; all about this tool, in case you want to check it out.&lt;/p&gt;

&lt;h2 id=&quot;4&quot; class=&quot;deeplink&quot;&gt; 4. Avoid Merging Paths Unless You Don’t Need Control Over Individual Paths.&lt;/h2&gt;

&lt;p&gt;Many designers tend to combine or merge paths whenever possible. To merge paths:&lt;/p&gt;

&lt;ol&gt;
	&lt;li&gt;Select the paths you want to merge.&lt;/li&gt;
	&lt;li&gt;Go to &lt;strong&gt;Window → Pathfinder&lt;/strong&gt;&lt;/li&gt;
	&lt;li&gt;Click the &lt;strong&gt;Merge&lt;/strong&gt; option among the list of options at the bottom of the panel (third icon from the left, shown in the screenshot below).&lt;/li&gt;
&lt;/ol&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/merge-paths.png&quot; alt=&quot;&quot; /&gt;
	&lt;figcaption&gt;&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Combining paths may have its benefits, but avoid it when you or the developer needs to control and/or animate paths separately. Some animations are designed so that multiple elements are animated seperately, or sometimes you just want to style the paths using different fill colors. If you combine the paths, that will no longer be possible.&lt;/p&gt;

&lt;p&gt;You need to make sure you know what the developer (or yourself, if you’ll be handling the development phase as well) needs and wants to do with the shapes you’re working on, and make the decision to merge or not to merge accordingly. This will save both of you a lot of time and friction.&lt;/p&gt;

&lt;h2 id=&quot;5&quot; class=&quot;deeplink&quot;&gt; 5. Create Filters Using &lt;em&gt;SVG Filters&lt;/em&gt;, Not &lt;em&gt;Photoshop Effects&lt;/em&gt;.&lt;/h2&gt;

&lt;p&gt;If you use the filters in the &lt;strong&gt;Photoshop Effects&lt;/strong&gt; section under the &lt;strong&gt;Effect&lt;/strong&gt; option, Illustrator is going to export the effects you create as raster images. For example, if you create a drop shadow using the &lt;strong&gt;Blur&lt;/strong&gt; Photoshop effect, the drop shadow generated will be a raster image embedded inside the SVG either inline or externally, using &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;image&amp;gt;&lt;/code&gt;. You definitely don’t want that when you work with SVG.&lt;/p&gt;

&lt;p&gt;To generate your effects as SVG code, you need to use the SVG Filters available:&lt;/p&gt;

&lt;ol&gt;
	&lt;li&gt;Go to &lt;strong&gt;Effect → SVG Filters&lt;/strong&gt;&lt;/li&gt;
	&lt;li&gt;Choose and use one of the filters available in that panel.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;img src=&quot;https://sarasoueidan.com/images/svg-filters-in-ai.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;6&quot; class=&quot;deeplink&quot;&gt; 6. Fit Artboard to Drawing.&lt;/h2&gt;

&lt;p&gt;Have you ever embedded an SVG on a page, gave it a specific height and width and then found that it was being displayed at a size smaller than what you specified?&lt;/p&gt;

&lt;p&gt;In &lt;em&gt;most&lt;/em&gt; cases, this is caused by an amount of white space &lt;em&gt;inside&lt;/em&gt; &lt;a href=&quot;https://sarasoueidan.com/blog/svg-coordinate-systems&quot;&gt;the SVG viewport&lt;/a&gt;. The viewport is displayed at the size you are specifying in your style sheet, but the extra space inside of it—around the graphic—causes your image to “shrink”, because that white space is taking an amount of, well, space, inside the viewport.&lt;/p&gt;

&lt;p&gt;To avoid this, you need to make sure your artboard is &lt;em&gt;just big enough&lt;/em&gt; to fit the drawing inside of it, but not any bigger.&lt;/p&gt;

&lt;p&gt;The artboard dimensions are the dimensions of the exported SVG viewport, and any white space in the artboard will be generated as white space inside the viewport.&lt;/p&gt;

&lt;p&gt;To fit your artboard to your drawing:&lt;/p&gt;

&lt;ol&gt;
	&lt;li&gt;Select the entire graphic. (I use &lt;strong&gt;cmd/ctrl + A&lt;/strong&gt;.)&lt;/li&gt;
	&lt;li&gt;Go to &lt;strong&gt;Object → Artboards&lt;/strong&gt; and choose the &lt;strong&gt;Fit to Artwork Bounds&lt;/strong&gt; option.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;img src=&quot;https://sarasoueidan.com/images/fit-artboard.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;7&quot; class=&quot;deeplink&quot;&gt; 7. Use Good Naming, Grouping and Layering Conventions.&lt;/h2&gt;

&lt;p&gt;I know this sounds like a no-brainer, but it needs to be emphasized for a few reasons:&lt;/p&gt;

&lt;ul&gt;
	&lt;li&gt;&lt;strong&gt;The IDs and class names you use in the graphics editor are going to be translated to IDs and class names in the generated code.&lt;/strong&gt; The more these names make sense and the clearer they label their respective elements, the less friction there will be when the developer works with the code.
	&lt;p&gt;Now, I'm not saying you have to think up the perfect names—I'm sure we all have different ways of naming things and naming can be one of the hardest tasks, but labelling groups appropriately goes a long way. For example, if you are drawing a car, then using an ID of &lt;code&gt;wheel&lt;/code&gt; to name the layer or group wrapping the shapes making up the wheel would be appropriate. If you are grouping all wheels in one group, you might give it an ID &lt;code&gt;wheels&lt;/code&gt;. Simple names to tell elements and groups apart go a long way and save a lot of time, especially if the developer will be editing and manipulating the code by hand.&lt;/p&gt;
		&lt;p&gt;Illustrator does not do the best job at naming things, so specifying names helps reduce the amount of junk it produces. Granted, there will be some extra editing required to get rid of the annoying underscores that Ai insists on generating, but using proper names helps make this process a bit easier.&lt;/p&gt;
		&lt;p class=&quot;note&quot;&gt;As mentioned before, the next verison of Illustrator will show a big improvement in the way SVGs are generated, including generated IDs.&lt;/p&gt;&lt;/li&gt;

	&lt;li&gt;&lt;strong&gt;Use layers to group related elements.&lt;/strong&gt; Layers are translated into groups in code, so name these well too. Create layers/groups to wrap related elements together, especially those that might be animated as a whole. A lot of time could be spent reordering and grouping elements by hand by the developer if this is not already done in the design phase. To save time, make sure you group appropriately. Talking to the developer in the design phase and designing how the animation will be executed together is a big time saver.&lt;/li&gt;

	&lt;li&gt;If the images you're creating are going to be used to create an SVG sprite, &lt;strong&gt;the names you use can and will be used by most automation tools to generate new files.&lt;/strong&gt; So using clear and proper names will result in cleaner file names.
	 &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;8&quot; class=&quot;deeplink&quot;&gt; 8. Choose The Best Suitable Export Options for the Web.&lt;/h2&gt;

&lt;p class=&quot;note update&quot;&gt;
	Starting with Illustrator CC 2015.2 released in November 2015, a new SVG Export workflow (File &amp;gt; Export &amp;gt; SVG) is available to export web-optimized SVG files for your web and screen design workflows. You can also choose to export individual objects versus the entire artboard. Refer to &lt;a href=&quot;https://helpx.adobe.com/illustrator/how-to/export-svg.html&quot;&gt;this article&lt;/a&gt; for details.
&lt;/p&gt;

&lt;p&gt;At the time of writing of this article, Illustrator comes with a bunch of export options that allow you to generate a generally better SVG code.&lt;/p&gt;

&lt;p&gt;To export your SVG:&lt;/p&gt;

&lt;ol&gt;
	&lt;li&gt;Choose &lt;strong&gt;File → Save As&lt;/strong&gt;
		&lt;img src=&quot;https://sarasoueidan.com/images/file-save.png&quot; alt=&quot;&quot; /&gt;&lt;/li&gt;
	&lt;li&gt;Choose &lt;strong&gt;SVG&lt;/strong&gt; from the dropdown menu
		&lt;img src=&quot;https://sarasoueidan.com/images/save-as.png&quot; alt=&quot;&quot; /&gt;&lt;/li&gt;
	&lt;li&gt;Click &lt;strong&gt;Save&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Once you click save, a dialog will show up that contains a set of options that you can customize, and that will affect the generated SVG code:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://sarasoueidan.com/images/export-options.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The options shown in the image above are the ones recommended for generating SVG for the web.&lt;/p&gt;

&lt;p&gt;Of course, you can choose to Outline text if you don’t want to use a web font; Illustrator provides you with an option to do it upon export, as you can see, as well.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;Image Location&lt;/strong&gt; option specifies whether any raster images will be embedded inline in your SVG or will be external with a link inside the SVG. This, again, depends on what you need. Inlining images inside the SVG can increase its file size dramatically. Last time a designer sent me an SVG with an image inlined in it, the file size was more than 1MB! After removing that image (which was caused by the Photoshop Effects used, that we mentioned earlier), the file size dropped to less than 100KB! So, choose wisely.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;CSS Properties&lt;/strong&gt; option gives you the option to choose how you want the styles inside the SVG to be created: using presentation attributes, inline styles, or in a &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;style&amp;gt;&lt;/code&gt; tag. This is also a matter of preference and depends on how you intend to manipulate the SVG once you’ve embedded it. If you’re not the one who’s going to do that, make sure you consult with the developer to choose the option that suits their needs best.&lt;/p&gt;

&lt;p&gt;The less the number of &lt;strong&gt;Decimal Places&lt;/strong&gt;, the less the file size of the SVG. One decimal place should generally be enough, so I’d go with that.&lt;/p&gt;

&lt;p&gt;Note that if you choose 3 or 4 decimal places, for example, and then use an optimization tool to optimize the SVG and bring that number down back to 1, the SVG might end up visually broken; so it is best to choose this option early on.&lt;/p&gt;

&lt;p&gt;There is more to the options panel than what I have covered. Adobe’s Michaël Chaize has written an excellent article about the export panel that explains what each option does exactly. I highly recommend checking his article out:&lt;/p&gt;

&lt;ul&gt;
	&lt;li&gt;&lt;a href=&quot;http://creativedroplets.com/export-svg-for-the-web-with-illustrator-cc/&quot;&gt;Export SVG for the web with Illustrator CC&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, at the time of writing of this article, Illustrator will still generate unnecessary code such as editor metadata, empty groups, among others, so you will need to optimize the SVG further after you’ve exported it, be it by hand, or using a standalone SVG optimization tool.&lt;/p&gt;

&lt;p&gt;But before we jump into the Optimization section, I want to note that you may or may not want to check an extra option as you save the SVG: the “&lt;strong&gt;Use Artboards&lt;/strong&gt;” option, in the Save panel:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://sarasoueidan.com/images/use-artboards.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This option is useful for when you are working with multiple SVG images (for example: icons) and you are using an artboard for every icon.&lt;/p&gt;

&lt;p&gt;Exporting multiple artboards will generate multiple &lt;code&gt;.svg&lt;/code&gt; files, one for each artboard (one for each icon).&lt;/p&gt;

&lt;p&gt;If you are working with only one artboard, this option will be disabled by default.&lt;/p&gt;

&lt;p&gt;Choosing to export one or multiple SVG files depends on how the SVG is going to be embedded.&lt;/p&gt;

&lt;p&gt;For example, if you are going to create an SVG sprite for an SVG icon system, there are several ways you can create and use the sprite, and each one would require a different approach: one technique requires the icons to be separate at first, and another requires them to be part of one image.&lt;/p&gt;

&lt;p&gt;I will be writing a separate post about spriting SVGs and the artboard options, but until then, you can get an overview of the different spriting techniques in the following article I wrote for 24Ways.org:&lt;/p&gt;

&lt;ul&gt;
	&lt;li&gt;&lt;a href=&quot;https://24ways.org/2014/an-overview-of-svg-sprite-creation-techniques/&quot;&gt;An Overview of SVG Sprite Creation Techniques&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;optimize&quot; class=&quot;deeplink&quot;&gt; To Optimize or Not to Optimize...&lt;/h3&gt;

&lt;p&gt;It is usually recommended to optimize the SVG after exporting it from a graphics editor using a standalone optimization tool. The current most popular optimization tool is the NodeJS-based tool called SVGO. But it may not always be a good idea to optimize your SVG, especially if you intend to animate it.&lt;/p&gt;

&lt;p&gt;If you intend to script and/or animate the SVG, you’re likely to set up a certain document structure—wrapper groups, ID names that you are not using/referencing inside the SVG but intend to use in your JavaScript, etc. This structure is going to change if you optimize your SVG using SVGO (or any other optimization tool).&lt;/p&gt;

&lt;p&gt;Optimization tools usually remove any unused groupd and IDs, as well as apply many changes to the SVG to make sure it is optimized well.&lt;/p&gt;

&lt;p&gt;I’ve once optimized an SVG after applying an animation to it using &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;animate&amp;gt;&lt;/code&gt;. The SVG was broken and so was the animation inside of it, because the entire structure was changed. So that is something to keep in mind before optimizing SVGs.&lt;/p&gt;

&lt;p&gt;If you’ve manually edited and/or generated an SVG with a certain structure that you need, avoid optimizing using an optimization tool, and optimize by hand as much as possible. Some editor junk at the beginning and end of the SVG can be easily removed by hand. Other junk, such as metadata and classes generated by editors like Sketch—which has no SVG optmization options, can be harder to optimize by hand.&lt;/p&gt;

&lt;p&gt;I generally never use Sketch to generate complex SVGs. I use Illustrator or Inkscape; the latter comes with a default export panel which gives you a lot of options to optimize your SVG before exporting it (see image below). Inkscape generates the cleanest SVG code at the time of writing of this article—that is, if you choose the &lt;strong&gt;Optimized SVG&lt;/strong&gt; option, but the blurriness of the UI on a retina screen as well as its dependency on X11 on OS X make it a pain to use, so I am currently sticking with Illustrator.&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/inkscape-export.png&quot; alt=&quot;&quot; /&gt;
	&lt;figcaption&gt;&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;If you do need/want to optimize your SVG, SVGO is the tool I would recommend.&lt;/p&gt;

&lt;p&gt;SVGO comes with a bunch of plugins that you can fit into practically any kind of workflow. You can find more information about those tools in the following article I wrote a few months ago:&lt;/p&gt;

&lt;ul&gt;
	&lt;li&gt;&lt;a href=&quot;https://sarasoueidan.com/blog/svgo-tools&quot;&gt;Useful SVGO[ptimization] Tools&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;9&quot; class=&quot;deeplink&quot;&gt; 9. Communicate. Communicate early.&lt;/h2&gt;

&lt;p&gt;Possibly the most important tip I can give is to communicate, and to do it early in the design process.&lt;/p&gt;

&lt;p&gt;I’m now assuming that you—the designer creating the SVG—are not the same person responsible for developing the SVG (scripting, animating, embedding, etc.).&lt;/p&gt;

&lt;p&gt;Almost every one of the above tips requires knowledge of the development phase and what the developer intends to do with the SVG—how they intend to embed, script, style and animate it. So, unless you’re the same person making decisions for both phases, and unless you want to waste a lot of time reiterating and editing the SVGs, you need to make sure you know what the developer needs to do with the SVG and what approach(es) they will be taking. If you’re working on a project that has a tight deadline, you probably can’t afford to waste a big amount of time making changes and revisions to image assets, when you can avoid that by communicating early.&lt;/p&gt;

&lt;p&gt;Designers and developers can be each other’s best friends. The very nature of SVG requires both design and development phases to be open to one another, and this, in turn, requires the designer(s) and developer(s) to talk, &lt;em&gt;before&lt;/em&gt; the design process begins, and throughout the process as well.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;I hope this article helps save you some time in the future. I’m not a designer so I might have missed a few clever design tips. If you know any that would help generate better SVGs, please feel free to share them in the comments below.&lt;/p&gt;

&lt;p&gt;Thank you for reading.&lt;/p&gt;

    
  </content>
 </entry>
 
 <entry>
   <title>Developer of the Year 2015 net Award</title>
   <link href="http://sarasoueidan.com//blog/developer-of-the-year-2-15-net-award/"/>
   <id>https://sarasoueidan.com/blog/developer-of-the-year-2-15-net-award</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;
	Last Friday, I gave my first keynote in the UK at Generate Conf. That same day, the &lt;a href=&quot;http://thenetawards.com&quot;&gt;&lt;em&gt;net&lt;/em&gt; awards&lt;/a&gt; were announced. On Saturday, I came back home with an award: the &lt;strong&gt;Developer of the Year&lt;/strong&gt; award. This is a short thank you post to everyone involved.
&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/netaward-me.png&quot; alt=&quot;Developer of the Year net award&quot; /&gt;
	&lt;figcaption&gt;
		Photo by &lt;em&gt;net&lt;/em&gt; magazine. All rights reserved. (&lt;a href=&quot;https://www.flickr.com/photos/netmag/21647815869/in/faves-136619150@N04/&quot;&gt;Original Photo&lt;/a&gt;)
	&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;A few months ago, many fellow designers and developers nominated me for two &lt;em&gt;net&lt;/em&gt; awards: Outstanding Contribution and Developer of the Year.&lt;/p&gt;

&lt;p&gt;Last Friday, the winners of the different categories were announced, and I’m thrilled to have won the Developer of the Year award.&lt;/p&gt;

&lt;p&gt;I can’t even begin to explain how happy and humbled that makes me. To be recognized and appreciated by your fellow colleagues is a great privilege, and a badge I wear with pride.&lt;/p&gt;

&lt;p&gt;I haven’t gotten a chance to say thanks to &lt;a href=&quot;https://storify.com/SaraSoueidan/netawards-2015-developer-of-the-year&quot;&gt;each and every one who congratulated me over Twitter&lt;/a&gt; after the award announcements—the feedback was so overwhelming (and fast!) that I just couldn’t keep up, so I thought I’d wait till I’m back home to write a thank you post instead.&lt;/p&gt;

&lt;p&gt;So: &lt;strong&gt;thank you&lt;/strong&gt;! Thank you to every one who nominated me and deemed me worthy of this title, and thank you to every one who voted and supported me. Two and a half years ago, when I started working in this field, I didn’t expect to be here today. I had different plans, most of which were unclear, and I never thought when I wrote &lt;a href=&quot;http://sarasoueidan.com/blog/windows8-animations&quot;&gt;my first blog post&lt;/a&gt; (on a really ugly blog design :P) that I’d end up shifting my overall path and doing all the writing and speaking that I am doing today. I am thankful to God and grateful for each and every step and opportunity I got along the way, and for all your wonderful support.&lt;/p&gt;

&lt;p&gt;And now, since I’m really bad at coming up with proper ‘speeches’, I’ll keep it short and end with a huge Thank You one more time. You are awesome! ^_^&lt;/p&gt;

&lt;p&gt;–S&lt;/p&gt;


    
  </content>
 </entry>
 
 <entry>
   <title>CSS vs. SVG: The Final Round(up)</title>
   <link href="http://sarasoueidan.com//blog/css-vs-svg-the-final-roundup/"/>
   <id>https://sarasoueidan.com/blog/css-vs-svg-the-final-roundup</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;
	This is the last article in the series of article comparing common CSS techniques to their SVG counterparts. This article is a roundup of several CSS and SVG solutions, as opposed to being an article comparing one solution that can be achieved using either CSS and SVG. There are already a bunch of excellent articles out there that cover the details for each of these solutions, so we will get an overview of each solution and link to those articles for each section as we go. I highly recommend you check them all out.
&lt;/p&gt;

    

     <div>
        <p>
          <em>This article is <a href="http://blogs.adobe.com/dreamweaver/2015/09/css-vs-svg-the-final-roundup.html">published @ <strong>Adobe Dreamweaver Team Blog</strong>.</a></em>
        </p>
    </div>
    
  </content>
 </entry>
 
 <entry>
   <title>CSS vs. SVG: Shapes and Arbitrarily-Shaped UI Components</title>
   <link href="http://sarasoueidan.com//blog/css-vs-svg-arbitrarily-shaped-ui-components/"/>
   <id>https://sarasoueidan.com/blog/css-vs-svg-arbitrarily-shaped-ui-components</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;
	This post is the third in the series of posts exploring techniques and examples that can be achieved using both CSS and SVG, and compares them both.In this article, we are going to go over techniques for creating arbitrarily-shaped UI components using CSS properties and SVG’s capabilities, and a mix of both! Specifically, we will be talking about how to create circular menus, as these are the perfect example of usable non-rectangular UI elements.
&lt;/p&gt;

    

     <div>
        <p>
          <em>This article is <a href="http://blogs.adobe.com/dreamweaver/2015/09/css-vs-svg-shapes-and-arbitrarily-shaped-ui-components.html">published @ <strong>Adobe Dreamweaver Team Blog</strong>.</a></em>
        </p>
    </div>
    
  </content>
 </entry>
 
 <entry>
   <title>CSS vs SVG: Styling Checkboxes and Radio Buttons</title>
   <link href="http://sarasoueidan.com//blog/css-vs-svg-styling-checkboxes-and-radio-buttons/"/>
   <id>https://sarasoueidan.com/blog/css-vs-svg-styling-checkboxes-and-radio-buttons</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;
	This post is the second in the series of posts exploring techniques and examples that can be achieved using both CSS and SVG, and compares them both. 
	In this article, we’re going to look into ways to style checkboxes and radio buttons using both CSS and SVG. You will learn how you can create animated checkboxes using SVG's line drawing capabilities. 
&lt;/p&gt;

    

     <div>
        <p>
          <em>This article is <a href="http://blogs.adobe.com/dreamweaver/2015/08/css-vs-svg-styling-checkboxes-and-radio-buttons.html">published @ <strong>Adobe Dreamweaver Team Blog</strong>.</a></em>
        </p>
    </div>
    
  </content>
 </entry>
 
 <entry>
   <title>Building A Circular Navigation with CSS Clip Paths</title>
   <link href="http://sarasoueidan.com//blog/building-a-circular-navigation-with-css-clip-paths/"/>
   <id>https://sarasoueidan.com/blog/building-a-circular-navigation-with-css-clip-paths</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;
	The CSS &lt;code&gt;clip-path&lt;/code&gt; property is one of the most underused and yet most interesting properties in CSS. It can be used in conjunction with CSS Shapes to create interesting layouts, and can be taken to the extreme to create some incredibly impressive layouts and animations like the Species in Pieces project. While exploring the creation of arbitrarily-shaped UI components using CSS and SVG, it occurred to me that the clip-path property, when combined with SVG paths, can be used to create circular menus fairly easily, especially considering the (expected) browser behaviour when handling pointer events on clipped regions as per the specification. In this article, we'll explore this idea further and learn how it's done.
&lt;/p&gt;

    

     <div>
        <p>
          <em>This article is <a href="https://css-tricks.com/building-a-circular-navigation-with-css-clip-paths/">published @ <strong>CSS-Tricks</strong>.</a></em>
        </p>
    </div>
    
  </content>
 </entry>
 
 <entry>
   <title>CSS vs. SVG: Graphical Text Effects</title>
   <link href="http://sarasoueidan.com//blog/css-vs-svg-graphical-text/"/>
   <id>https://sarasoueidan.com/blog/css-vs-svg-graphical-text</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;
	This post is the first in a series of posts exploring techniques and examples that can be achieved using both CSS and SVG, and compares them both. Since I am biased to SVG, this series is really intended to prove that SVG — because of its nature as both an image and a document format — is simply better than CSS when it comes to solving certain design problems on the web. But to keep an objective point of view, we will be weighing the pros and cons of each technique and find out where and when CSS or SVG can serve as a better tool to the design goals at hand.
	In this article, we’re going to look into the CSS &lt;code&gt;background-clip&lt;/code&gt; and &lt;code&gt;mask-image&lt;/code&gt; properties, as well as SVG pattern fills and masks.
&lt;/p&gt;

    

     <div>
        <p>
          <em>This article is <a href="http://blogs.adobe.com/dreamweaver/2015/07/css-vs-svg-graphical-text.html">published @ <strong>Adobe Dreamweaver Team Blog</strong>.</a></em>
        </p>
    </div>
    
  </content>
 </entry>
 
 <entry>
   <title>Chapter 4, Smashing Book 5</title>
   <link href="http://sarasoueidan.com//blog/smashing-book-5/"/>
   <id>https://sarasoueidan.com/blog/smashing-book-5</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;
The new Smashing Book is out! It’s packed with a &lt;em&gt;lot&lt;/em&gt; of time-saving, practical techniques for crafting fast, maintainable and scalable responsive websites. I wrote a chapter in the book—&lt;strong&gt;Chapter 4: Mastering SVG For Responsive Web Design&lt;/strong&gt;. Here is an overview of what that chapter covers, and why I think you should buy the book.
&lt;/p&gt;

&lt;figure style=&quot;position: static; width: 100%;&quot;&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/smashing-book-5-1.jpg&quot; alt=&quot;Image of the Smashing Book 5.&quot; /&gt;
	&lt;figcaption&gt;Photo credit: &lt;a href=&quot;https://twitter.com/MatCompagnucci/status/622441177795723264/photo/1&quot;&gt;Mattia Compagnucci&lt;/a&gt;&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;I always get questions from my followers about a good place to start learning SVG. I &lt;a href=&quot;https://sarasoueidan.com/tags/svg/index.html&quot;&gt;write about SVG a lot&lt;/a&gt;, but I realize that it’s always better to have one place where you can jump start whatever you’re learning and then take it from there. I believe the SVG chapter in the book is a great place to do just that.&lt;/p&gt;

&lt;p&gt;Chapter four of the Smashing Book 5 is &lt;strong&gt;80 pages&lt;/strong&gt; of SVG, covering everything you need to know to start implementing SVG in your responsive web designs today.&lt;/p&gt;

&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;blockquote class=&quot;testimonial testimonial--right grey&quot;&gt;
	There is a chapter on SVG for RWD by Sara Soueidan that kind of made me feel stupid, not because of how it was written, that was great, but because it introduced me to so many new things about SVG. What the heck have I been learning all of these years?
	&lt;cite&gt;—&lt;a href=&quot;https://www.makerscabin.com/mag/smashing-book-5-a-review/&quot;&gt;Paul Scrivens, “Smashing Book #5, A Review”&lt;/a&gt;&lt;/cite&gt;
&lt;/blockquote&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;chapter-contents&quot;&gt;So, What Can You Expect To Learn About?&lt;/h2&gt;

&lt;figure style=&quot;position: static; width: 100%;&quot;&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/smashing-book-5-2.jpg&quot; alt=&quot;Image of the Smashing Book 5, chapter 4.&quot; /&gt;
	&lt;figcaption&gt;Photo credit (and thanks to): &lt;a href=&quot;https://twitter.com/MatCompagnucci&quot;&gt;Mattia Compagnucci&lt;/a&gt;&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;The answer to this question in my head is always a list of what is &lt;em&gt;not&lt;/em&gt; covered in the chapter, because the topics covered include so much! Here is an overview of the contents:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;What is SVG?&lt;/strong&gt;: a brief overview of the image format and its history.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Vector vs. Raster&lt;/strong&gt;: a comparison between vector and raster image formats such as JPEG, PNG, etc., characteristics of each, and what makes the SVG format stand out.
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;Advantages of SVG&lt;/strong&gt;: the many, many features that makes SVG a better image format for responsive web design, including but not exclusive to: scalability and resolution-independence, performance, accessibility, animation, and more.&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Vector vs Raster: Which is the Better Format?&lt;/strong&gt;: a set of guideliness and things to consider when choosing the image format for your graphic, including why and when SVG might or might not be a good choice.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Quick Overview of SVG Syntax and Code&lt;/strong&gt; including..
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;Understanding the SVG Viewport and viewBox&lt;/strong&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Creating and Exporting SVGs in Vector Authoring Tools&lt;/strong&gt;: tips, dos and don’ts for the right workflow when designing SVG images that ensure the resulting code is as optimal as possible. This section includes:
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;Exporting SVG for the Web From Adobe Illustrator&lt;/strong&gt;: overview of the export options and which ones to pick to make sure you end up with better code.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Optimizing SVGs Using Standalone Optimization Tools&lt;/strong&gt;: there are a lot of tools, each can fit into a different workflow. We go over a list of some of the best tools.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Embedding SVGs&lt;/strong&gt;: the different ways to embed SVGs on a page, and the pros and cons of each, tips on improving performance when choosing some of them.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Using SVGs as an Icon Font Replacement
(or, How to Create SVG Sprites and Use Them as an Icon System)&lt;/strong&gt;: what the tite says. Sections covered in this master section include:
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;SVGs vs Icon Fonts&lt;/strong&gt;: SVGs are a far more superior icon system, and this section covers all the reasons why, as in intriduction to..&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Spriting SVGs&lt;/strong&gt;: introduction to the concept, followed up:
        &lt;ul&gt;
          &lt;li&gt;&lt;strong&gt;SVG icon system with icons as background images&lt;/strong&gt;: the how-to of creating an SVG sprite for icons used as background images on the page, including tools to simplify the process.
            &lt;ul&gt;
              &lt;li&gt;Covered is also a section about SVG data URIs, including any performance considerations.&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
          &lt;li&gt;&lt;strong&gt;SVG Icon System with Icons as Foreground Images&lt;/strong&gt;: how-to and workflow automation tools.
            &lt;ul&gt;
              &lt;li&gt;&lt;strong&gt;Styling the icons and applying transitions to them&lt;/strong&gt;: including styling the contents of &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;use&amp;gt;&lt;/code&gt;.&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Providing and Automating Fallback for HTML-Inline SVGs&lt;/strong&gt;: some known and some lesser known techniques for:
        &lt;ul&gt;
          &lt;li&gt;&lt;strong&gt;SVG fallback as foreground images&lt;/strong&gt;: how to provide fallback for SVG images that are embedded as foreground images. There are several options, all covered.&lt;/li&gt;
          &lt;li&gt;&lt;strong&gt;SVG fallback as background images&lt;/strong&gt;: also a few options to choose from leveraging different CSS technqiues.&lt;/li&gt;
          &lt;li&gt;&lt;strong&gt;SVG Fallback Using SVG Conditional Processing (SVG Fallback as Background Image)&lt;/strong&gt;: SVG’s built-in fallback mechanism.&lt;/li&gt;
          &lt;li&gt;&lt;strong&gt;Other Fallback Techniques&lt;/strong&gt; for different embedding techniques.&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Better SVG Spriting Using Native Fragment Identifiers&lt;/strong&gt;: this is my personally-favourite SVG spriting tchnique and one of the lesser used ones, and the one closest to the way PNG image spriting works.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Making SVG’s Cross-Browser Responsive with CSS&lt;/strong&gt; including:
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;Making SVGs Fluid with CSS&lt;/strong&gt;: everything including browser bugs and workarounds.&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Making SVGs Adaptive with CSS&lt;/strong&gt; using CSS Media Queries to show and hide (or even animate) parts of the SVG depending on viewport size.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Making SVGs Accessible&lt;/strong&gt;: the least you can do to provide even the most basic accessibility to your SVG images today.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Using SVGS to Optimize the Delivery of Other Image Formats&lt;/strong&gt;: use cases that go beyond simply displaying or animating vector images. It includes:
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;Using SVG Masks to Optimize Raster Graphics&lt;/strong&gt;&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Using SVG As a Container for Serving Responsive Images&lt;/strong&gt; a.k.a the Clown Car Technique.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Where To Go From Here&lt;/strong&gt;: pointers to more ideas and resources to look into.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As you can see the chapter covers &lt;em&gt;a lot&lt;/em&gt;. The topics it does not cover (otherwise it would have turned into a book) are: graphical effects (such as filters) and animation. I’m pretty sure there is even more about SVG to get into that the chapter does not cover, but hey, it’s only a chapter of a book!&lt;/p&gt;

&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;blockquote class=&quot;testimonial testimonial--right grey&quot;&gt;
	Reading the new Smashing book, which is timely as I'm spritifying some SVGs, and it's easier than flying @SaraSoueidan over to do it for me.
	&lt;cite&gt;—&lt;a href=&quot;https://twitter.com/brucel/status/623169965189918720&quot;&gt;Bruce Lawson&lt;/a&gt;&lt;/cite&gt;
&lt;/blockquote&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;the-reviewers&quot;&gt;The Reviewers&lt;/h2&gt;

&lt;p&gt;The chapter was reviewed by &lt;a href=&quot;http://twitter.com/DmitryBaranovsk&quot;&gt;Dmitry Baranovskiy&lt;/a&gt; (creator of the &lt;a href=&quot;http://raphaeljs.com/&quot;&gt;Raphael&lt;/a&gt; library and its modern &lt;a href=&quot;http://snapsvg.io/&quot;&gt;Snap.svg&lt;/a&gt; alternative) and &lt;a href=&quot;http://twitter.com/jaffathecake&quot;&gt;Jake Archibald&lt;/a&gt; (Google), both experienced SVG developers.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;designers-complete-reference&quot;&gt;A Designer’s Complete Reference&lt;/h2&gt;

&lt;p&gt;The chapters of the book are complimentary to each other, so the entire book is a collection of topics that go extremely well together, giving you a set of diverse topics that are sure to come in handy for any responsive design project you work on.&lt;/p&gt;

&lt;p&gt;While writing the SVG chapter, I mentioned the problems that non-vector images might bring up, and how the new &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;picture&amp;gt;&lt;/code&gt; element emerged as the almost-perfect solution for that problem. (SVG still has its benefits over these formats, of course, but raster images still have their place and importance where SVGs just can’t fill in.) And since my chapter was focused on SVG, I couldn’t get into the details of how to serve responsive raster images. Yoav Weiss’ chapter fills in that spot and covers everything you need to know about using responsive images. Not only that, but Yoav’s chapter also includes tips that you can apply to SVG images as well, making these two chapters the ultimate reference for images used in responsive design.&lt;/p&gt;

&lt;p&gt;You can’t expect an SVG chapter to not mention SVG fonts, too. But then again, since that topic is outside the scope of what could be covered in the chapter, Bram Stein’s chapter on web font performance fills in that spot in his chapter that covers everything you need to know about web font formats, performance, fallback, and more.&lt;/p&gt;

&lt;p&gt;The book was written by some of the brightest people in our industry, all experts in their fields, smarter than me, and known for their expertise in specific topics—all of which they covered in lengthy, very informative articles. You will find topics ranging from design workflows, patterns, to content choreography, images, advanced layout with Flexbox, responsive process, performance optimization, all the way to optimizing for offline experiences.&lt;/p&gt;

&lt;p&gt;I hope you find the chapter on SVG useful, and I’m confident you will find the remaining chapters insightful as well. I promise you will not regret it.&lt;/p&gt;

&lt;p&gt;So, what are you waiting for? &lt;a href=&quot;http://www.smashingmagazine.com/2015/03/31/real-life-responsive-web-design-smashing-book-5/&quot;&gt;Go grab your copy&lt;/a&gt; and feel free to &lt;a href=&quot;http://twitter.com/SaraSoueidan&quot;&gt;tweet at us&lt;/a&gt; with any feedback or comments (or even pretty pictures)!&lt;/p&gt;

&lt;div class=&quot;button-wrapper&quot;&gt;&lt;a href=&quot;http://www.smashingmagazine.com/2015/03/31/real-life-responsive-web-design-smashing-book-5/&quot; class=&quot;button ghost&quot;&gt;Buy the Book&lt;/a&gt;&lt;/div&gt;


    
  </content>
 </entry>
 
 <entry>
   <title>Styling the Contents of SVG &lt;use&gt; with CSS</title>
   <link href="http://sarasoueidan.com//blog/styling-svg-use-with-css/"/>
   <id>https://sarasoueidan.com/blog/styling-svg-use-with-css</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;
	An in-depth article on how to style the contents of the SVG &amp;lt;use&amp;gt; element and overcome some challenges it brings. We get into where the contents are cloned (the shadow DOM!), what limitations that brings up and how to work around them by taking advantage of the CSS cascade and using CSS Variables to get full control over the content while providing fallback for non-supporting browsers.
&lt;/p&gt;

    

     <div>
        <p>
          <em>This article is <a href="http://tympanus.net/codrops/2015/07/16/styling-svg-use-content-css/">published @ <strong>Codrops</strong>.</a></em>
        </p>
    </div>
    
  </content>
 </entry>
 
 <entry>
   <title>Art-Directing an SVG Embedded Using &lt;object&gt;</title>
   <link href="http://sarasoueidan.com//blog/art-directing-svg-object/"/>
   <id>https://sarasoueidan.com/blog/art-directing-svg-object</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;I have recently finished a front-end development project for &lt;em&gt;Provata Health&lt;/em&gt; — a US-based health and wellness company specializing in health promotion and behavior change science. As part of their &lt;a href=&quot;http://provatahealth.com&quot;&gt;marketing website&lt;/a&gt;, I worked on an infographic that showcases the three major health results one can achieve by following their health program. The graphic is a perfect candidate for SVG and you’ll see why throughout this article. But even though almost all of the graphics on the site were vector (which made the entire project exciting to me), I want to focus on this particular graphic and how and why I made the development choices I made while embedding it.&lt;/p&gt;

&lt;p&gt;I had to make a few “unusual” decisions to make sure I provide the best performance and accessibility possible, including inlining a piece of JavaScript in the middle of the page. So I wanted to share why I made those decisions, and at the same time shed the light on yet another case where SVG could be improved to adapt to our development needs.&lt;/p&gt;

&lt;p&gt;Also, after &lt;a href=&quot;https://twitter.com/SaraSoueidan/status/611163768811323392&quot;&gt;tweeting&lt;/a&gt; about inlining the JavaScript in the middle of the document, I asked my go-to performance specialist—the nice Mr. &lt;a href=&quot;http://twitter.com/aerotwist&quot;&gt;Paul Lewis&lt;/a&gt;—about any performance implications, he too thought I should write about this and share why I made that decision. So here I am.&lt;/p&gt;

&lt;p&gt;First, let’s have a look at that graphic and see why it is a perfect SVG candidate, before we get into embedding, art-directing and fallback.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;theresults-svg-graphic&quot;&gt;The Results SVG Graphic&lt;/h2&gt;

&lt;p class=&quot;note&quot;&gt;
Disclaimer: I am writing this case study and sharing this image after having gotten a written approval from my client to do so. You can check the graphic out live &lt;a href=&quot;http://provatahealth.com/results.html&quot;&gt;here&lt;/a&gt;.
&lt;/p&gt;

&lt;p&gt;This is the SVG graphic I worked on in the website’s &lt;em&gt;Results&lt;/em&gt; page, and how it was supposed to look on mobile and desktop, respectively.&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/provata-results-graphic.jpg&quot; alt=&quot;The Provata Results Graphic as displayed on mobile (left) and desktop (right).&quot; /&gt;
	&lt;figcaption&gt;The Provata Results Graphic as displayed on mobile (left) and desktop (right). You can check the graphic out live &lt;a href=&quot;http://provatahealth.com/results.html&quot;&gt;here&lt;/a&gt;.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;The text inside of the graphic is just as important as the rest of the HTML text on the page, with the addition of the positioning that illustrates what category each result belongs to in a nice visual manner. When you have text like this, SVG should be the first thing to think about. This kind of graphic (similar to an infographic) is a perfect candidate for SVG and one that makes SVG’s accessibility and visual features shine.&lt;/p&gt;

&lt;p&gt;Imagine having to absolute-position each and every piece of text and image inside that graphic in an HTML document, taking into account and document flow (or lack thereof); doing this using SVG not only makes more sense but also comes with the advantage of being able to use a graphics editor to achieve all of the positioning visually, instead of hand-coding the positions using pixels or &lt;code class=&quot;highlighter-rouge&quot;&gt;em&lt;/code&gt;s (or whatever unit you prefer).&lt;/p&gt;

&lt;p&gt;Not only that, but you also get the fluidity and responsiveness of SVG images and text by default, so you won’t have to worry about adapting any text and image sizes to different viewport sizes. Not to mention text accessibility inside of the image. Win win win!&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;choosing-the-embedding-technique&quot;&gt;Choosing The Embedding Technique&lt;/h2&gt;

&lt;p&gt;Since the contents of the SVG are not animated (they were at first, but the UX team decided to drop the animations later for better UX), then the first logical embedding technique that comes to my mind is &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;img&amp;gt;&lt;/code&gt;, or its cool cousin &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;picture&amp;gt;&lt;/code&gt;; either way, it would be embedded as-an-image—as a foreground image, to be specific, because 1) there is no reason to embed it as a background image and 2) because &lt;a href=&quot;http://www.stevesouders.com/blog/2015/05/12/hero-image-custom-metrics/&quot;&gt;foreground images have better performance metrics than background images&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;However, there is one important reason why embedding it as-an-image is not an option: not only does the text inside of the image need to be as “real” (read: readable, selectable and searchable) as any other piece of text on the page, but &lt;strong&gt;we also need to be able to provide this same text as a raw text fallback&lt;/strong&gt; in case the SVG fails to load for any reason.&lt;/p&gt;

&lt;p&gt;So, to recap:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;The text inside of the image needs to be accessible to screen readers.&lt;/li&gt;
  &lt;li&gt;The text inside of the image needs to be selectable and searchable by the reader.&lt;/li&gt;
  &lt;li&gt;The text inside of the image &lt;strong&gt;should be the fallback provided for non-supporting browsers&lt;/strong&gt;. That is, if the image fails to load for any reason, the text content of the image should be the content to replace it, &lt;em&gt;not&lt;/em&gt; a PNG or JPEG version of the graphic. This decision was one of high importance to me because the entire section of that page depended on that text to convey a message, so should the user not be able to read the SVG text, I wanted them to be able to simply &lt;em&gt;read&lt;/em&gt; the text content.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Given all of the above, &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;object&amp;gt;&lt;/code&gt; was the perfect candidate and the element I eventually used to embed the graphic.&lt;/p&gt;

&lt;p&gt;To make sure the text inside the SVG is accessible, searchable and selectable, make sure you don’t turn it into outlines. Keep it as default SVG &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;text&amp;gt;&lt;/code&gt;. With this, you also get the ability to apply the page’s font face to the SVG text as well, using &lt;code class=&quot;highlighter-rouge&quot;&gt;@font-face&lt;/code&gt;. So you get the text rendered and working just like you expect it to.&lt;/p&gt;

&lt;p&gt;Between the opening and closing &lt;code class=&quot;highlighter-rouge&quot;&gt;object&lt;/code&gt; tags, instead of providing a PNG version of the image for non-supporting browsers—which is what most developers would normally opt for, I provided a raw text alternative to the graphic, and the content of that text was the exact same as the content inside of the SVG graphic.&lt;/p&gt;

&lt;p&gt;This works pretty well. While testing the graphic on mobile, and before finalizing the JavaScript (see next section), the graphic failed to load in one of the tests because the URL of the image I had specified was incorrect, so I got the text content in that section of the page instead, and it looked and worked perfect for the purpose of that section, as opposed to having loaded a PNG version that could have been unreadable.&lt;/p&gt;

&lt;p&gt;Having chosen the embedding element, decided for an accessible fallback, and knowing that we need to provide two seperate images for desktop and mobile, it’s time to handle the image swapping..&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;art-direction&quot;&gt;Art Direction&lt;/h2&gt;

&lt;p&gt;I’m a huge proponent of the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; clipping technique. The attribute is extremely powerful and art-directing an image using this attribute is always the first thing that comes to my mind. If you’re not familiar with how this works, you should read my &lt;a href=&quot;https://sarasoueidan.com/blog/svg-art-direction-using-viewbox/&quot;&gt;previous blog post&lt;/a&gt; explaining exactly how that’s done.&lt;/p&gt;

&lt;p&gt;However, this is one use case where the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; clipping technique would not suffice because clipping would not be enough to hide the parts we do not need on smaller screens, because the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; can only clip to rectangular areas, so we would either have to use a custom clip path to clip out the excess content, or we could hide them by making them invisible (using &lt;code class=&quot;highlighter-rouge&quot;&gt;opacity&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;visibility&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;display&lt;/code&gt;, etc.), and the latter can be &lt;a href=&quot;http://tympanus.net/codrops/2014/08/19/making-svgs-responsive-with-css/&quot;&gt;easily achieved using CSS media queries&lt;/a&gt;. So the way it would work would be:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Hide the branched parts (text and small illustrations) on small screens using &lt;code class=&quot;highlighter-rouge&quot;&gt;opacity: 0;&lt;/code&gt; or &lt;code class=&quot;highlighter-rouge&quot;&gt;display: none&lt;/code&gt;, for example&lt;/li&gt;
  &lt;li&gt;Then clip the canvas to the remaining graphic (the three overlapping colored circles) to get rid of unwanted excess white space resulting from hiding the rest of the graphic.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Changing the viewBox using CSS is currently not possible, so that would require JavaScript. Hiding the unwanted parts on small screens is possible in CSS, though. So this makes it possible to do half of the job using JS and the other half using CSS. I don’t like the sound of this, but this is the only way it’s possible now. Ideally, we would be able to change the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; using nothing but CSS, thus croppig and hiding the content using a few lines of CSS, but that’s currently not possible. (See previous article for details.)&lt;/p&gt;

&lt;blockquote class=&quot;pull-quote&quot;&gt;
	art-directing SVG using the `viewBox` attribute is a powerful technique but one you should not pursue if the graphic you are embedding is too big to serve in full composition on mobile.
&lt;/blockquote&gt;

&lt;p&gt;So, we can use one SVG image and art-direct it using CSS and a few lines of JavaScript. Great.&lt;/p&gt;

&lt;p&gt;However, something else must be kept in mind here: &lt;strong&gt;performance&lt;/strong&gt;. In my previous article on this subject, I mentioned that art-directing SVG using the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; attribute is a powerful technique but one you should not pursue if the graphic you are embedding is too big to serve in full composition on mobile. And in the case of the Provata graphic, the hidden parts were indeed non-trivial in contributing to the overall file size, making it significantly bigger, so using the same full image and hiding parts of it on mobile was definitely not a suitable approach in this case.&lt;/p&gt;

&lt;p&gt;Which brings us to the next section..&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;art-directing-the-svg-object&quot;&gt;Art-Directing The SVG &lt;code&gt;&amp;lt;object&amp;gt;&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;Instead of serving the same graphic for desktop and mobile, we opted for two different graphics. Both of them were embedded using an &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;object&amp;gt;&lt;/code&gt; tag.&lt;/p&gt;

&lt;p&gt;Had we embedded the SVG as an image, we would have been able to easily switch the source of the image using the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;picture&amp;gt;&lt;/code&gt; element and its &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;source&amp;gt;&lt;/code&gt; descendant. I wrote all about this in a previous article as well, so you might want to &lt;a href=&quot;https://sarasoueidan.com/blog/svg-picture&quot;&gt;check it out&lt;/a&gt;. The code for that would look something like this:&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;picture&amp;gt;
    &amp;lt;source
        media=&quot;(max-width: 640px)&quot;
        srcset=&quot;results-graphic--small.svg&quot;
        type=&quot;image/svg+xml&quot;&amp;gt;
    &amp;lt;source
        srcset=&quot;results-graphic--full.svg&quot;
        type=&quot;image/svg+xml&quot;&amp;gt;

    &amp;lt;img src=&quot;results-graphic--default-fallback.jpg&quot; alt=&quot;Header description..&quot;&amp;gt;
&amp;lt;/picture&amp;gt;
&lt;/pre&gt;

&lt;p&gt;But since we need the text fallback and are using &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;object&amp;gt;&lt;/code&gt;, we need to swap out the source of the &lt;code class=&quot;highlighter-rouge&quot;&gt;object&lt;/code&gt; using JavaScript because it’s not possible to do so using markup..&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;using-js-to-switch-object-source&quot;&gt;Using JavaScript to Switch &lt;code&gt;&amp;lt;object&amp;gt;&lt;/code&gt; Source&lt;/h3&gt;

&lt;p&gt;To detect viewport size, I like using &lt;a href=&quot;http://modernizr.com&quot;&gt;Modernizr&lt;/a&gt;. So the function swapping the &lt;code class=&quot;highlighter-rouge&quot;&gt;object&lt;/code&gt; source looks something like this:&lt;/p&gt;

&lt;pre class=&quot;brush:js&quot;&gt;
// after getting a reference to the graphic...
function changeSource(){
    var url = graphic.getAttribute('data');

    if (Modernizr.mq('(max-width: 767px)')) {
        if(url == &quot;path/to/results-graphic--mobile.svg&quot;) return;
        else {
            graphic.setAttribute('data', 'path/to/results-graphic--mobile.svg');
            // maybe show/hide something else related to it here
        }
    }
    else {
        if(url == &quot;path/to/results-graphic--desktop.svg&quot;) return;
        else {
            graphic.setAttribute('data', 'path/to/results-graphic--desktop.svg');
            // maybe show/hide something else related to it here
        }
    }
}
&lt;/pre&gt;

&lt;p&gt;Of course, make you sure sure you trigger this function and re-trigger on window resize, and since the function already checks for whether or not the source is the one required at a specific screen size, it won’t keep swapping the source every time (that would otherwise cause a horrible flash making the image unusable). The rest is self-explanatory.&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;inline-or-external-js&quot;&gt;Inline or External JS? Sync or Async?&lt;/h3&gt;

&lt;p&gt;I inlined the script right after the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;/object&amp;gt;&lt;/code&gt; tag in the markup, and did &lt;em&gt;not&lt;/em&gt; make it &lt;code class=&quot;highlighter-rouge&quot;&gt;async&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;“&lt;em&gt;Why?&lt;/em&gt; 😳”&lt;/p&gt;

&lt;p&gt;I needed the browser to parse the JS and specify the &lt;code class=&quot;highlighter-rouge&quot;&gt;object&lt;/code&gt; source &lt;strong&gt;as soon as possible&lt;/strong&gt;, I did &lt;em&gt;not&lt;/em&gt; want it to parse the entire document before it displayed the graphic, because it’s the most important graphic on the page and I wanted it to show up as soon as possible. So, I did not async the inline script.&lt;/p&gt;

&lt;p&gt;(We devs can turn any word into a verb and it’s totally legit.)&lt;/p&gt;

&lt;p&gt;(Yes it is.)&lt;/p&gt;

&lt;p&gt;At first, I was relying on the JS to set the &lt;code class=&quot;highlighter-rouge&quot;&gt;object&lt;/code&gt; source on both mobile and desktop. That caused some issues on mobile—loading- and performance-wise, so I ended up setting the &lt;code class=&quot;highlighter-rouge&quot;&gt;data&lt;/code&gt; to point to the mobile version of the graphic by default, and have the script do the swapping to the desktop version when on desktop. That got rid the mobile loading issue, and, expectedly, works pretty well on desktop.&lt;/p&gt;

&lt;p&gt;Now, this works, perf is pretty good on both desktop on mobile &lt;em&gt;and&lt;/em&gt; I even got a “&lt;em&gt;looks good to me&lt;/em&gt;” from Paul, so I took that as my “good to go” sign.&lt;/p&gt;

&lt;p&gt;The takeaway here is: &lt;strong&gt;always test your pages&lt;/strong&gt;, no matter what technique you use to achieve something. It turned out that inining small pieces of JavaScript is sometimes even recommended to improve load times, &lt;a href=&quot;https://developers.google.com/speed/docs/insights/BlockingJS#InlineJS&quot;&gt;says Google&lt;/a&gt;, and that’s exactly what I achieved by inlining the JS in this case.&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;an-object-extension&quot;&gt;An &lt;code&gt;&amp;lt;object&amp;gt;&lt;/code&gt; Extension?&lt;/h3&gt;

&lt;p&gt;As I worked on this project, I &lt;a href=&quot;https://twitter.com/SaraSoueidan/status/611142858221973504&quot;&gt;tweeted&lt;/a&gt; about how nice it would be if we have a &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;picture&amp;gt;&lt;/code&gt; cousin that did the same thing as &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;picture&amp;gt;&lt;/code&gt; does for &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;img&amp;gt;&lt;/code&gt;, but something more &lt;code class=&quot;highlighter-rouge&quot;&gt;object&lt;/code&gt;-oriented. (Pun not intended.)&lt;/p&gt;

&lt;p&gt;Art-directing an SVG embedded as an &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;object&amp;gt;&lt;/code&gt; is something I’ve done more than once, and all in real-life, practical use cases as part of client projects. So this is something that is useful and would make a lot more sense if we could do it &lt;strong&gt;using markup&lt;/strong&gt; &lt;em&gt;without&lt;/em&gt; having to resort to (inline) JavaScript.&lt;/p&gt;

&lt;p&gt;After second thoughts, I think that maybe, instead of coming up with a new element to do this, &lt;em&gt;extending&lt;/em&gt; the &lt;code class=&quot;highlighter-rouge&quot;&gt;object&lt;/code&gt; element with a &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;source&amp;gt;&lt;/code&gt; or &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;data&amp;gt;&lt;/code&gt; element that is similar to &lt;code class=&quot;highlighter-rouge&quot;&gt;source&lt;/code&gt; might be quite handy.&lt;/p&gt;

&lt;p&gt;So we could do something like:&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;object type=&quot;image/svg+xml&quot;&amp;gt;
    &amp;lt;data src=&quot; &quot; media=&quot; &quot; &amp;gt;
    &amp;lt;data src=&quot; &quot; media=&quot; &quot; &amp;gt;
&amp;lt;/object&amp;gt;
&lt;/pre&gt;

&lt;p&gt;This is just an idea, and probably not even close to something implementable—it is just meant as an idea that might be shaped into something usable/implementable or maybe a similar idea that achieves the same functonality could spawn from it.&lt;/p&gt;

&lt;p&gt;This is yet another thing added to my SVG wishlist. Until we get anything like that, JavaScript is the way to go to swap &lt;code class=&quot;highlighter-rouge&quot;&gt;object&lt;/code&gt; sources.&lt;/p&gt;

&lt;p class=&quot;note&quot;&gt;
&lt;em&gt;UPDATE: After reading the article, &lt;a href=&quot;http://twitter.com/tigt_&quot;&gt;Taylor Hunt&lt;/a&gt; pointed me to &lt;a href=&quot;http://www.w3.org/TR/SVGParamPrimer/&quot;&gt;this specification&lt;/a&gt;, which defines a &lt;code&gt;&amp;lt;param&amp;gt;&lt;/code&gt; element to extend the options for some SVG elements. Maybe this element or something very similar in concept could achieve the URL-swapping without resorting to script. Just something to keep in mind.&lt;/em&gt;
&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;happy-cuser-client-dev&quot;&gt;Happy User, Happy Client, Happy Dev&lt;/h2&gt;

&lt;p&gt;In that order. The image used was important in conveying a message and was a vital part of the site’s content, so we made sure it was as accessible and good-looking as it could possibly be, using the awesome that SVG is.&lt;/p&gt;

&lt;blockquote&gt;
   &lt;p&gt; At Provata, we focus on creating dynamic, interactive, and engaging digital health experiences. So we wanted our marketing website to reflect that. Sara expertly incorporated SVG techniques to deliver a final product beyond our expectations. She is an incredibly knowledgeable and diligent developer. She captured the spirit of our company brilliantly. &lt;/p&gt;
   &lt;cite&gt;—Alex Goldberg, CEO Provata Health&lt;/cite&gt;
&lt;/blockquote&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;final-words&quot;&gt;Final Words&lt;/h2&gt;

&lt;p&gt;Working on this site has been great, and my client was one of the best I’ve worked with. Not only do I love the branding and design, but I also love the spirit behind the health program. (You can learn more about it &lt;a href=&quot;http://provatahealth.com&quot;&gt;here&lt;/a&gt;.) And my absolute favourite part was getting to work with lots of SVG animations using &lt;a href=&quot;http://greensock.com&quot;&gt;my favourite animation library&lt;/a&gt;. &lt;small&gt;(Thank you, Jack.)&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;The SVG decision-making process is also one I enjoy a lot, even though the amount of options we have can sometimes be overwhelming, and in spite of the fact that we sometimes may need to make certain compromises when choosing one option over the other, &lt;em&gt;and&lt;/em&gt; in spite of the lack of full-fledged tools and techniques we got at hand today. I love SVG and enjoy going through the entire process every time.&lt;/p&gt;

&lt;p&gt;Hopefully things will get better in the future. The more we use SVG, the more practical, real-life use cases come up with, the more features we need, the more features we should request, and hopefully the more features make it to specs and implementation.&lt;/p&gt;

&lt;p&gt;I also learned quite a few designer-y things while working on this project, and plan on sharing more of those in future articles and talks, so stay tuned!&lt;/p&gt;

&lt;p&gt;Thank you for reading.&lt;/p&gt;

    
  </content>
 </entry>
 
 <entry>
   <title>The State of SVG Animation</title>
   <link href="http://sarasoueidan.com//blog/state-of-svg-animation/"/>
   <id>https://sarasoueidan.com/blog/state-of-svg-animation</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;
	The state of SVG animation is changing. CSS, SMIL and JavaScript can be used to animate SVGs. However, SMIL is soon to be deprecated and was never supported in Internet Explorer. CSS animations on SVG elements don’t have the best browser support (yet), not to mention are quite buggy in some browsers. JavaScript is currently the best SVG animation tool. In this article, we'll go over the current and future state of SVG animation, giving you an overview of what you can and can't do, and some advice on what to choose for your specific animation needs.
&lt;/p&gt;

    

     <div>
        <p>
          <em>This article is <a href="http://blogs.adobe.com/dreamweaver/2015/06/the-state-of-svg-animation.html#.VXGQW1yqqkq">published @ <strong>Adobe Dreamweaver Team Blog</strong>.</a></em>
        </p>
    </div>
    
  </content>
 </entry>
 
 <entry>
   <title>Art-Directing SVG Images With The viewBox Attribute: How-To, Notes, Tips and Why We Need A viewBox Property in CSS</title>
   <link href="http://sarasoueidan.com//blog/svg-art-direction-using-viewbox/"/>
   <id>https://sarasoueidan.com/blog/svg-art-direction-using-viewbox</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;
	The SVG &lt;code&gt;viewBox&lt;/code&gt; attribute is easily one of SVG's most powerful features. Mastering this attribute can take your SVG skills to the next level, especially considering that a couple of the main SVG spriting techniques rely on this attribute to work. Because the &lt;code&gt;viewBox&lt;/code&gt; attribute can be used to crop and extend the SVG canvas, it can be used for art-directing SVGs—by using it to crop the SVG to the area that you want to display at a time. In this article, I want to go over how to do this, mention some tips that can save you some time doing it, and show the importance of having a &lt;code&gt;viewBox&lt;/code&gt; property in CSS, in hopes that this article would serve as a practical use case that helps push &lt;a href=&quot;https://lists.w3.org/Archives/Public/www-svg/2013Dec/0080.html&quot;&gt;this old SVGWG proposal&lt;/a&gt; forward.
&lt;/p&gt;

&lt;p&gt;Since I’ve already covered everything you need to know about the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; attribute in a &lt;a href=&quot;https://sarasoueidan.com/blog/svg-coordinate-systems&quot;&gt;previous post&lt;/a&gt;, I will assume that you have a basic understanding of how the attribute works and what each of its values stands for. I will be mentioning some of the basics along the way, but I highly recommend you head to my &lt;a href=&quot;https://sarasoueidan.com/blog/svg-coordinate-systems&quot;&gt;other article&lt;/a&gt; and scan it before you move on with this article if you’re not familiar with the attribute.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;overview-of-viewbox-parameters&quot;&gt;Quick Overview Of the &lt;code&gt;viewBox&lt;/code&gt; Parameters&lt;/h2&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; attribute is used to specify the origin and dimensions of the user coordinate system of an SVG image. All the drawing inside of the SVG is done relative to this system. And since the SVG canvas is conceptually infinite in all directions, you can even draw shapes outside the boundaries of this coordinate system; but the position of those shapes relative to the SVG viewport can also be controlled by the position of the user coordinate system.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; attribute takes four parameters that specify the position of the origin of the system and its dimensions: &lt;code class=&quot;highlighter-rouge&quot;&gt;x y width height&lt;/code&gt;. Initially, this system is identical to the initial viewport coordinate system established by the width and height of the SVG image, and its origin is at (0, 0)—the top left corner of the SVG.&lt;/p&gt;

&lt;p&gt;By changing the value of the &lt;code class=&quot;highlighter-rouge&quot;&gt;x&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;y&lt;/code&gt; parameters you change the position of the origin. By changing the &lt;code class=&quot;highlighter-rouge&quot;&gt;width&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;height&lt;/code&gt;, you change the dimensions of the system. This eventually allows you to extend and crop the SVG canvas using nothing but the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; attribute. Read along for examples.&lt;/p&gt;

&lt;p class=&quot;note&quot;&gt;IMPORTANT NOTE: Throughout this article I won't be changing the default behavior (scale and position) of the &lt;code&gt;viewBox&lt;/code&gt; inside the SVG viewport. Therefore, as per the default behavior of the attribute, the &lt;code&gt;viewBox&lt;/code&gt; will scale as much as possible while still be entirely contained inside the viewport, and positioned at its center. Using the &lt;code&gt;preserveAspectratio&lt;/code&gt; attribute, you can change the scale and position of the &lt;code&gt;viewBox&lt;/code&gt; to your liking, but that is not required for the technique in this article to work, and therefore we won't be getting into that in this article.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;cropping-svg-using-viewbox&quot;&gt;Cropping the SVG Using &lt;code&gt;viewBox&lt;/code&gt; a.k.a SVG Art Direction Using the &lt;code&gt;viewBox&lt;/code&gt; Attribute&lt;/h2&gt;

&lt;p&gt;A while back, a client of mine requested that the SVG header photo of his website change on different screen sizes, so that only one portion of the full composition is visible on small screens, a bigger portion is visible on medium screens, and the full composition be visible on large screens. The first idea that crossed my mind when he requested that was to use the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; attribute to crop the SVG and only show the portions of the image he wanted on different screen sizes.&lt;/p&gt;

&lt;p&gt;By changing the dimensions of the SVG coordinate system and the position of its origin, we can crop an SVG to only show the parts that we want inside the viewport.&lt;/p&gt;

&lt;p&gt;Let’s see how that’s done.&lt;/p&gt;

&lt;p&gt;Suppose we have the following SVG image in full composition, that we want to crop on smaller screen sizes. The image is a freebie &lt;a href=&quot;http://www.freepik.com/free-photos-vectors/house&quot;&gt;House vector designed by Freepik&lt;/a&gt; and is licensed under Creative Commons Attribution 3.0 Unported License. For the sake of simplicity, we will assume that the image is going to be cropped only once to show one portion on small–medium screens, and the full composition on larger screens, as shown in the image below.&lt;/p&gt;

&lt;figure&gt;
&lt;img src=&quot;https://sarasoueidan.com/images/house-landscape.png&quot; alt=&quot;&quot; /&gt;
&lt;figcaption&gt;The full composition we are going to crop using the &lt;code&gt;viewBox&lt;/code&gt; attribute. The image on the right shows the area of the image that we want to show on smaller screens.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Now, there are a few considerations when cropping an SVG by changing the value of the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; attribute. We’ll get to these shortly. But first, in order to change the coordinate system so that it matches the dashed rectangular area in the above image, we need to change the value from its initial &lt;code class=&quot;highlighter-rouge&quot;&gt;0 0 800 800&lt;/code&gt; parameters by translating the system’s origin and changing the width and height.&lt;/p&gt;

&lt;p&gt;But how do you know the new position and dimensions without having to go through a lot of trial and error?&lt;/p&gt;

&lt;p&gt;There are a couple of ways. Since we’re already in the graphics editor (Ai, in my example), we can use the editor’s panels to retrieve the positions and dimensions of elements.&lt;/p&gt;

&lt;p&gt;There is a reason why I drew that dashed rectangle to wrap the area I want to show on smaller screens: we can retrieve the position and dimensions of this rectangle and use them as values for the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt;. Using Ai’s Transform panel (see image below), we retrieve these values. By selecting the rectangle and then clicking the Transform link at the top right corner, we get the panel shown in the image below, with the &lt;code class=&quot;highlighter-rouge&quot;&gt;x&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;y&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;width&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;height&lt;/code&gt; values that we are going to use.&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/illustrator-transform-values.png&quot; alt=&quot;&quot; /&gt;
	&lt;figcaption&gt;The Transform panel in Ai can be used to retrieve the values of the selected rectangle's position and dimensions. &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;As you have probably noticed, the values are not rounded, so we can do that ourselves. Using the above information, we change the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; value to: &lt;code class=&quot;highlighter-rouge&quot;&gt;0 200 512 512&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Since the aspect ratio of the new &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; is the same as the aspect ratio of the SVG viewport (both are square)&lt;/strong&gt;, the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; is going to scale up and only the selected area will be visible inside of the viewport. The result of changing the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; value is:&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/new-svg-viewbox.png&quot; alt=&quot;&quot; style=&quot;max-width: 600px;&quot; /&gt;
	&lt;figcaption&gt;The new cropped SVG. Only the portion we specified using the &lt;code&gt;viewBox&lt;/code&gt; attribute is visible inside of the viewport. The blue border represents the SVG viewport.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;We need to ask a question here at this point:&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;case-of-different-aspect-ratios&quot;&gt;What if the aspect ratio of the cropped area (thus, the viewBox) is not the same as the aspect ratio of the SVG viewport?&lt;/h3&gt;

&lt;p&gt;Well, in this case, there will be visible overflow. By visible overflow I don’t mean overflow extending beyond the boundaries of the SVG viewport, but overflow relative to the  new user coordinate system defined by the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt;. The following image illustrates the problem.&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/diff-aspect-ratio-viewbox.png&quot; alt=&quot;&quot; /&gt;
	&lt;figcaption&gt;If the aspect ratio of the &lt;code&gt;viewBox&lt;/code&gt; is different from that of the viewport, and there is content inside the SVG that overflows the user coordinate system, you could end up with something like this.
		&lt;p&gt;The black border represents the new user coordinate system, and the blue border is the SVG viewport.&lt;/p&gt;&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;The black border in the above image on the right is the area defined by the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt;. As per the default behavior of the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; inside the viewport, it has been centered and scaled up as much as possible while remaining fully contained inside the viewport (blue border).&lt;/p&gt;

&lt;p&gt;Since the SVG canvas is conceptually infinite in all directions, you can draw outside the boundaries of the user coordinate system, and the content would simply overflow the system as shown in the above image.&lt;/p&gt;

&lt;p&gt;If you change the aspect ratio of the SVG viewport (the SVG width and height) so they match those of the &lt;code&gt;viewBox&lt;/code&gt;’s, you won’t see that overflow anymore, since the &lt;code&gt;viewBox&lt;/code&gt; will scale to fit the viewport as in the previous example.&lt;/p&gt;

&lt;p&gt;But, in some scenarios, you may not be able or simply not want to change the aspect ratio of the Svg. An example is if you are using an SVG sprite to display images of a set of avatars on the page. In most cases, the avatars all have a fixed aspect ratio—you won’t be changing the size of each avatar to match the content of the image inside it. Or maybe you’re embedding an icon system and want/need all icons to have the same dimensions all the time.&lt;/p&gt;

&lt;p&gt;To clip off any excess (for example, part of another icon in the sprite that is showing inside the viewport), you can use a &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;clipPath&amp;gt;&lt;/code&gt; to clip that excess out. The clip path would be a &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;rect&amp;gt;&lt;/code&gt; element that covers the entire &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; area, and is then applied to the root SVG.&lt;/p&gt;

&lt;p&gt;There is, however, one thing to keep in mind here: make sure the &lt;code class=&quot;highlighter-rouge&quot;&gt;x&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;y&lt;/code&gt; attributes of the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;rect&amp;gt;&lt;/code&gt; are identical to those of the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt;, otherwise the &lt;code class=&quot;highlighter-rouge&quot;&gt;rect&lt;/code&gt; will be positioned relative to the old/initial system’s origin and you will end up cropping and clipping an unexpected part of the SVG.&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;svg xmlns=&quot;http://w3.org/2000/svg&quot; viewBox=&quot;vx vy width height&quot; clip-path=&quot;url(#clipper)&quot; width=&quot;..&quot; height=&quot;..&quot;&amp;gt;
	&amp;lt;!-- SVG content here --&amp;gt;
	&amp;lt;clipPath id=&quot;clipper&quot;&amp;gt;
		&amp;lt;rect x=&quot;vx&quot; y=&quot;vy&quot; width=&quot;100%&quot; height=&quot;100%&quot;&amp;gt;&amp;lt;/rect&amp;gt;
	&amp;lt;/clipPath&amp;gt;
&amp;lt;/svg&amp;gt;
&lt;/pre&gt;

&lt;p&gt;Of course, clipping the excess out will mean that you’re still using different aspect ratios and are thus going to end with that extra white space on either side of the content. If the SVG is a continuous scene as in our previous example, this will be unwanted and you will need to adjust the aspect ratio of the viewport. If the SVG is a bunch of icons that you’re showing one at a time inside different viewports, this might not be an issue.&lt;/p&gt;

&lt;p&gt;The important thing here to keep in mind that the aspect ratio of the &lt;code&gt;viewBox&lt;/code&gt; is best kept the same as that of the viewport; else, you will have to apply a fix to avoid any excess unwanted space inside the SVG.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;So, the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; can be used to crop the SVG and only show parts of it when needed. But how would that be done in a practical example?&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;art-directing-svg-for-rwd&quot;&gt;Art-Directing An SVG For Responsive Web Design&lt;/h3&gt;

&lt;p&gt;Nothing new to add in this section, except the actual process with code. So, suppose you have the above SVG and you want to use it as a header image, for example, and you only want to show the cropped part on small–medium screen sizes and the full composition on bigger screens.&lt;/p&gt;

&lt;p&gt;In order to change the value of the SVG viewport’s width and height, we can use CSS. Simple. But to change the value of the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt;, we currently need to use JavaScript.&lt;/p&gt;

&lt;p&gt;Not all SVG presentation attributes are available as CSS properties; only the set of attributes that have CSS property equivalents can be set in CSS. You can see an overview of the set of attributes available as CSS properties in &lt;a href=&quot;http://slides.com/sarasoueidan/styling-animating-svgs-with-css#/10&quot;&gt;this table&lt;/a&gt;. In SVG2, more attributes (like &lt;code class=&quot;highlighter-rouge&quot;&gt;x&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;y&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;cx&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;cy&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;r&lt;/code&gt;, etc.) will be added to the list; but those are the properties we can work with today.&lt;/p&gt;

&lt;p&gt;In order to show different parts of the SVG by changing the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; value based on different media queries, you can, for example, use Modernizr, check for media query conditions, and then change the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; accordingly, in JavaScript. An example of that might look like so:&lt;/p&gt;

&lt;pre class=&quot;brush:js&quot;&gt;
// get a reference to the root &amp;lt;svg&amp;gt;
var svgRoot = ...; // depends on how you embed and retrieve your SVG
// define your viewBox value(s) to be used
var vbValue = '0 200 512 512';
// use Modernizr's media query detection to change the viewBox value
if (Modernizr.mq('(max-width: 700px)')) {
   svgRoot.setAttribute('viewBox', vbValue);
}
// else if ... etc.
&lt;/pre&gt;

&lt;p&gt;This works. But wouldn’t it be much more optimal if we could use CSS to do this?&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;css-viewbox-property&quot;&gt;Using a CSS &lt;code&gt;viewBox&lt;/code&gt; Property To Art-Direct SVGs&lt;/h3&gt;

&lt;p class=&quot;note&quot;&gt;DISCLAIMER: &lt;strong&gt;At the time of writing of this article, there is no CSS &lt;code&gt;viewBox&lt;/code&gt; property&lt;/strong&gt;. This is just an example to demonstrate why such a property would be useful and an example of how I imagine it would be used.&lt;/p&gt;

&lt;p&gt;Ideally, we would be able to do something like this:&lt;/p&gt;

&lt;pre class=&quot;brush:css&quot;&gt;
&amp;lt;style&amp;gt;

@media screen and (max-width: 700px) {
    svg {       
        viewBox: 0 200 512 512; 
    } 
}

/* etc. */ 

&amp;lt;/style&amp;gt;
&lt;/pre&gt;

&lt;p&gt;These styles would go inside (or outside) an SVG, and the SVG will then adapt its &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; according to the viewport size—be it the viewport of the page (in case of inline &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt;), or the viewport established by the dimensions of whichever element is used to reference the SVG (which would lend us something practically identical to element queries).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This is currently not possible because we don’t have a &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; property in CSS.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A while back, I asked an SVG spec editor about this, and he said that I could propose it to the SVGWG with a practical use case and example. After some discussion on Twitter, I learned that there already is a fairly old &lt;a href=&quot;https://lists.w3.org/Archives/Public/www-svg/2013Dec/0080.html&quot;&gt;SVGWG proposal thread&lt;/a&gt; that goes a few years back. The initial proposal is still there today, and my hope is that, with a practical use case like this, this proposal could be pushed forward and the property implementation specced at some point in the near future.
If you would like to see the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; property in CSS, please help make this happen by pushing this thread forward and commenting on it.&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;notes&quot;&gt;Things To Keep In Mind When Approaching SVG Art-Direction with &lt;code&gt;viewBox&lt;/code&gt;&lt;/h3&gt;

&lt;p&gt;While working on my client project, it took me literally less than a minute to art-direct the header image the way he wanted. However, we ended up going for three separate SVGs instead of using the same SVG with different viewBoxes on different screen sizes.&lt;/p&gt;

&lt;p&gt;The reason we chose to go with three SVGs is that the size of the full composition was too big to serve on mobile—reaching a whopping 100kb+ in size. The initial SVG was around 200kb, and I was able to slash the file size down to half by &lt;a href=&quot;https://sarasoueidan.com/blog/svgo-tools&quot;&gt;optimizing the SVG&lt;/a&gt;, but the resulting size was still too big to serve on mobile, so we ended up using three different images. This is something to keep in mind when art-directing SVGs: performance matters. A lot. So, if your SVG is too big, don’t art-direct it using &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now, if you choose to serve three different SVG images, you can do so in one of many possible ways—depending on the way you embed your SVG, and this also depends on what you want or don’t want to do with it.&lt;/p&gt;

&lt;p&gt;The ideal way to serve different SVG images would be to use the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;picture&amp;gt;&lt;/code&gt; element. Not only does it allow us to provide different SVGs for the browser to choose from without needing JavaScript, but it also enables us to provide &lt;strong&gt;multiple&lt;/strong&gt; optimized fallback images for non-supporting browsers (think IE8 and below) as well. &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;picture&amp;gt;&lt;/code&gt; is great when used with SVG, and you can read all about providing SVG fallback using &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;picture&amp;gt;&lt;/code&gt; in &lt;a href=&quot;https://sarasoueidan.com/blog/svg-picture&quot;&gt;this article&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;All this being said, &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;picture&amp;gt;&lt;/code&gt; will not be your best choice if you want to animate or interact with your SVG. Just like an SVG embedded using &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;img&amp;gt;&lt;/code&gt;, the SVG cannot be styled and animated unless the styles and animations are defined inside the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; file, the SVG cannot be scripted (for security reasons), and any interactions (CSS or JS) — like hover, for example — will not work.&lt;/p&gt;

&lt;p&gt;So, as I always say: SVG provides us with a lot of options to do almost everything; you need to weigh things, prioritize, sometimes maybe even make compromizes, and pick your best route based on that. But never compromise performance in favor of development convenience.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Before we finish up, and since we’re on the subject of changing the SVG canvas’ size using the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; attribute, let’s take a look at another example where we can leverage the power of this attribute to save us some time and effort when working with SVG.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;extending-svg-canvas-using-viewbox&quot;&gt;Extending the SVG Canvas using &lt;code&gt;viewBox&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;Just like the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; attribute can be used to crop an SVG, it can be used to extend the SVG canvas as well.&lt;/p&gt;

&lt;p&gt;A few weeks ago I created &lt;a href=&quot;https://sarasoueidan.com/tools/circulus/&quot;&gt;a tool that allows you to generate circular menus in SVG&lt;/a&gt;. I created a few examples to show how a generated menu could be animated using JavaScript. The demos are embedded on the app page using the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;object&amp;gt;&lt;/code&gt; element. The boundaries of the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;object&amp;gt;&lt;/code&gt; define the boundaries of the SVG viewport, and anything that lies outside these boundaries is considered overflow and will be hidden by default.&lt;/p&gt;

&lt;p class=&quot;note&quot;&gt;Note that the phrase &quot;outside these boundaries&quot; refers to content inside the SVG that is still drawn on the infinite SVG canvas, but that extends beyond the finite rectangle defined by the viewport.&lt;/p&gt;

&lt;p&gt;The menus are created such that the size of the SVG is just big enough to contain the menu, not more, to avoid any excess and unwanted white space around the menu.&lt;/p&gt;

&lt;p&gt;I applied a staggering, bouncing animation to one of the menus as an example of how the menu can be animated. The bouncing effect “stretched” the menu items, and this led to the items being cut off while they animated.&lt;/p&gt;

&lt;figure&gt;
&lt;img src=&quot;https://sarasoueidan.com/images/items-overflow-recording.gif&quot; alt=&quot;Screen recording showing how the menu items get cut off when they are animated with a bouncing animation.&quot; /&gt;
&lt;figcaption&gt;
Initially, and since the SVG viewport defined by the &lt;code&gt;&amp;lt;object&amp;gt;&lt;/code&gt; element is just as big as the menu itself, the bouncing effect on the menu items results in these items being cut off when animated.
&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;The staggering bouncing animation is applied to the items such that it will scale an item from zero (items are initially not visible, scaled down) to 100% using a bouncing timing function, the effect of which will be scaling the item &lt;em&gt;beyond&lt;/em&gt; 100% right before it is scaled back to 100%. This effect causes the item to scale up beyond the boundaries of the SVG and hence get cut off.&lt;/p&gt;

&lt;p&gt;The following image shows the result of scaling the menu item up beyond the boundaries of the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;object&amp;gt;&lt;/code&gt; used to embed it (grey border).&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&quot;https://sarasoueidan.com/images/circular-menu-overflow-illustration.png&quot; alt=&quot;Image showing the result of applying a bouncing scale animation to the menu item, resulting in the item being cut off.&quot; style=&quot;max-width: 600px;&quot; /&gt;
&lt;figcaption&gt;
Illustration showing the menu item overflowing the boundaries of the SVG viewport when it is scaled up. The grey border represents the border of the SVG viewport (the &lt;code&gt;&amp;lt;object&amp;gt;&lt;/code&gt;).
&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Setting &lt;code class=&quot;highlighter-rouge&quot;&gt;overflow: visible&lt;/code&gt; on the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;object&amp;gt;&lt;/code&gt; does &lt;em&gt;not&lt;/em&gt; fix this, because &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;object&amp;gt;&lt;/code&gt; is practically similar to an &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;iframe&amp;gt;&lt;/code&gt; in behavior. What we need to do is &lt;em&gt;extend&lt;/em&gt; the SVG canvas &lt;em&gt;inside&lt;/em&gt; the viewport created by the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;object&amp;gt;&lt;/code&gt; so that the scaled items have enough space to “bounce” without exceeding its boundaries. We can do this using the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; attribute.&lt;/p&gt;

&lt;p&gt;To extend the SVG canvas, you simply increase its dimensions. So, instead of 500px by 250px—the original dimensions of the SVG menu, we use 700px by 350px; this will increase &lt;strong&gt;the height of the canvas visible inside of the viewport&lt;/strong&gt; by 100px, and its width inside of the viewport by 200px. I chose these values based on how much the menu item is being scaled up in the bounce effect. Depending on your SVG and what you’re working on, these values might be different.&lt;/p&gt;

&lt;p&gt;Now, to make sure the menu remains centered inside of the viewport, we’re going to shift the position of the coordinate system by 100 pixels in the negative direction (upwards and to the left). Applying this shift to the origin of the coordinate system is practically the same as applying a translation transformation to the menu inside of the system. The result will be that the menu remains centered inside of the viewport.&lt;/p&gt;
&lt;figure&gt;
&lt;img src=&quot;https://sarasoueidan.com/images/circular-menu-overflow-fix-illustration.png&quot; alt=&quot;Image showing the result of extending the SVG canvas to provide more space for the menu items to be animated without being cut off.&quot; style=&quot;max-width: 700px;&quot; /&gt;
&lt;figcaption&gt;
In this illustration, the blue borders represent the border of the SVG viewport (the &lt;code&gt;&amp;lt;object&amp;gt;&lt;/code&gt;). The grey borders in this image show the initial dimensions of the user coordinate system. The blue numbers and arrows represent the extension of the coordinate system inside of the viewport.
&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;By extending the dimensions of the user coordinate system, you are increasing the area of the canvas visible inside of the viewport. The result of doing this will also be that the contents of the canvas will look slightly smaller—depending on how much you increase the canvas. In the case of the menu, the result was perfectly acceptable.&lt;/p&gt;

&lt;p&gt;The following screen recording shows the result of extending the SVG canvas and how the animation now looks inside the buondaries of the SVG.&lt;/p&gt;

&lt;figure&gt;
&lt;img src=&quot;https://sarasoueidan.com/images/items-overflow-fix-recording.gif&quot; alt=&quot;Screen recording showing the result of extending the SVG canvas using the viewBox attribute, thus preventing the menu items from being cut off once animated.&quot; /&gt;
&lt;figcaption&gt;
Once the SVG canvas has been extended and the menu items have enough space to scale up, they are no longer cut off when the bouncing effect is applied. 
&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Changing four values inside the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; attribute to extend the SVG canvas was all that was needed to troubleshoot and solve the issue of the items being cut off. Now that’s pretty powerful, isn’t it?&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;wrapping-up&quot;&gt;Wrapping Up&lt;/h2&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; attribute is awesome. It is literally SVG on steroids. By using this attribute, you can save a lot of time when working with SVG, troubleshoot SVG quickly without having to resort to a graphics editor, and, all in all, feel more comfortable editing SVG by hand.&lt;/p&gt;

&lt;p&gt;I highly recommend you learn all about this attribute if you haven’t already, &lt;a href=&quot;http://sarasoueidan.com/demos/interactive-svg-coordinate-system/index.html&quot;&gt;play with its values&lt;/a&gt;, and then leverage its power in your work. And if you do decide to use it to art-direct SVGs, don’t forget to keep performance in mind.&lt;/p&gt;

&lt;p&gt;One of the main objectives of this article was to also shed some light on the importance of having a &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; property in CSS, so if you’re convinced that this property is needed, please take the time to vote on / respond to the SVGWG thread and support the request.&lt;/p&gt;

&lt;p&gt;Thank you very much for reading! :)&lt;/p&gt;

    
  </content>
 </entry>
 
 <entry>
   <title>An Introduction To Graphical Effects in CSS</title>
   <link href="http://sarasoueidan.com//blog/css-graphics/"/>
   <id>https://sarasoueidan.com/blog/css-graphics</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;
	In this article, we will take a deep introduction into CSS’s graphical effects—specifically, CSS Filters and Compositing and Blending capabilities. We will go over the properties for each, their different values, and usage examples and some of the graphial effects that can be created using nothing but a few lines of CSS.
&lt;/p&gt;

    

     <div>
        <p>
          <em>This article is <a href="http://blogs.adobe.com/dreamweaver/2015/04/an-introduction-to-graphical-effects-in-css.html#.VUGQ3dpViko">published @ <strong>Adobe Dreamweaver Team Blog</strong>.</a></em>
        </p>
    </div>
    
  </content>
 </entry>
 
 <entry>
   <title>I Won A Web Platform Award</title>
   <link href="http://sarasoueidan.com//blog/oreilly-web-platform-award/"/>
   <id>https://sarasoueidan.com/blog/oreilly-web-platform-award</id>
   <content type="html">
    &lt;p&gt;Today, O’Reilly’s Fluent Conf is taking place in San Francisco, California. And as part of the conference, the O’Reilly Web Platform Awards were announced. Apparently, I was nominated for an award and, according to the co-chairs of the conference, I got the most amount of nominations and eventually won an award.&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/web-platform-award.jpg&quot; alt=&quot;The Web Platform award.&quot; /&gt;
&lt;/figure&gt;

&lt;blockquote&gt;
	O’Reilly Web Platform Awards recognize individual contributors who have demonstrated exceptional leadership, creativity, and collaboration in the development of JavaScript, HTML, CSS, and the supporting Web ecosystem. The nomination process is open to the entire web community and all entries will be judged by the Fluent program committee.
&lt;/blockquote&gt;

&lt;p&gt;This is my first time ever winning a web award, and I feel privileged to have won it from such a prestigious company.&lt;/p&gt;

&lt;p&gt;Simon sent me the “Congratulations, you won a web platform award!” email a couple of weeks before the awards were announced. My first reaction when I read the email was: “Okay this must have gotten into my inbox by mistake.” So I ended up responding to his message asking him if the email was really intended for &lt;em&gt;me&lt;/em&gt;. He said that it was. I couldn’t believe it for a while and it took me some time to let the idea sink in that I had actually won an award.&lt;/p&gt;

&lt;p&gt;I had &lt;em&gt;no clue&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;I love doing what I do and sharing what I know. I find great pleasure in helping others with what I know and always have—ever since I can remember. I really don’t know what to say except &lt;strong&gt;Thank You&lt;/strong&gt; to each and every one who nominated me, and to the committee who voted, and to O’Reilly Fluent for this great award. It is such a wonderful and overwhelming feeling to realize that your work has been recognized by the very community you’re part of and have been contributing to. To know that the community appreciates your work and finds it useful is one of the best feelings ever.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Thank you&lt;/strong&gt;.&lt;/p&gt;


    
  </content>
 </entry>
 
 <entry>
   <title>A Primer To Background Positioning In CSS</title>
   <link href="http://sarasoueidan.com//blog/css-background-positioning/"/>
   <id>https://sarasoueidan.com/blog/css-background-positioning</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;
	An article in which we take a deep dive into CSS’s background positioning properties with visual explanations and examples. Did you know that the CSS &lt;code&gt;background-position&lt;/code&gt; property accepts edge offset values? That is, you can position a background image relative to &lt;em&gt;any&lt;/em&gt; edge—not just top and left—and specify the offset relative to that edge using a length value. In this article, we will learn all about that, and more.
&lt;/p&gt;

    

     <div>
        <p>
          <em>This article is <a href="http://blogs.adobe.com/dreamweaver/2015/03/a-primer-to-background-positioning-in-css.html">published @ <strong>Adobe Dreamweaver Team Blog</strong>.</a></em>
        </p>
    </div>
    
  </content>
 </entry>
 
 <entry>
   <title>Building a Circular Navigation with SVG</title>
   <link href="http://sarasoueidan.com//blog/building-a-circular-navigation-with-svg/"/>
   <id>https://sarasoueidan.com/blog/building-a-circular-navigation-with-svg</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;
	Last week, I released &lt;a href=&quot;https://sarasoueidan.com/tools/circulus&quot;&gt;CIRCULUS.SVG&lt;/a&gt;—the SVG circular menu generator. In this article I want to go over why SVG is better suited for creating this kind of UI element, and give you and overview of the SVG code for creating the menu items using SVG elements and transformations. 
&lt;/p&gt;

&lt;p&gt;Note that, unlike my usual tutorials, we will not be going over a detailed how-to, but only an overview of the concepts behind this. Creating the menu in detail would require lots of maths and digging into the SVG path data syntax which deserves an article of its own; so, for the sake of brevity, I will &lt;em&gt;not&lt;/em&gt; be digging into either of these, but will go quickly over the concepts.&lt;/p&gt;

&lt;p&gt;Now, to draw the sectors, you can, of course, literally &lt;em&gt;draw&lt;/em&gt; them in a graphics editor like Illustrator, Inkscape or Sketch, and then export your graphic as SVG, make it interactive, and embed it. However, since the title says “building”, we’re going to go over how to draw these items with code.&lt;/p&gt;

&lt;p&gt;If you’re only interested in the end result—a usable circular menu, you can head to the generator and create your own menu there. Otherwise, let’s start with why SVG is better than CSS for creating circular menus.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;svg-vs-css&quot;&gt;SVG vs. CSS&lt;/h2&gt;

&lt;p&gt;Over a year ago, I wrote &lt;a href=&quot;http://tympanus.net/codrops/2013/08/09/building-a-circular-navigation-with-css-transforms/&quot;&gt;an article&lt;/a&gt; over at Codrops about using CSS Transforms to create a circular navigation. Even though the technique works, it comes with a couple of browser bugs &amp;amp; inconsistencies, not to mention that it’s practially hacky—we’re bending the rectangular box model to create the menu items by skewing and rotating the list items and cutting them off by hiding the overflow on their container. The innards of the list items need to be “un-skewed”, and everything is positioned absolutely.&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/css-circular-nav-demo.gif&quot; alt=&quot;Screen recording showing the steps needed to create a circular menu in CSS&quot; /&gt;
	&lt;figcaption&gt;
		Screen recording showing the steps needed to create a circular menu in CSS. The interactive demo can be found in the Codrops article.
	&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Placing content other than icons inside the menu can get difficult depending on the type of content. And finally, to make the menu repsonsive, you’re gonna need to use media queries and adjust the different sizes used for different viewport widths (and/or heights!).&lt;/p&gt;

&lt;p&gt;With SVG, on the other hand, it’s very different.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Shapes in SVG are marked up as semantic, fully-accessible XML elements like &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;rect&amp;gt;&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;circle&amp;gt;&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;ellipse&amp;gt;&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;path&amp;gt;&lt;/code&gt;. And with the different drawing elements available, SVG makes for the perfect candidate for drawing non-rectangular shapes and elements. And to top it off, SVG items can be styled and scripted and hence are completely interactive. That’s exactly what we need for our circular menu.&lt;/p&gt;

&lt;p&gt;Since we are creating circular slices — a.k.a &lt;strong&gt;sectors&lt;/strong&gt; (symbol: ⌔) — as menu items, we will use the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;path&amp;gt;&lt;/code&gt; element to mark each slice up, since SVG’s path commands will allow us to draw the slices in a more straightforward manner using the line and arc commands available.&lt;/p&gt;

&lt;p&gt;Drawing the menu items in SVG is much, much more straightforward. Here is an interactive demonstration showing how the items are drawn and positioned inside the menu. Play the animation to see the demonstration.&lt;/p&gt;

&lt;p data-height=&quot;500&quot; data-theme-id=&quot;3617&quot; data-slug-hash=&quot;2e56afeaa278c90141853ff805da1a06&quot; data-default-tab=&quot;result&quot; data-user=&quot;SaraSoueidan&quot; class=&quot;codepen&quot;&gt;See the Pen &lt;a href=&quot;http://codepen.io/SaraSoueidan/pen/2e56afeaa278c90141853ff805da1a06/&quot;&gt;Building A Circular Menu With SVG #2&lt;/a&gt; by Sara Soueidan (&lt;a href=&quot;http://codepen.io/SaraSoueidan&quot;&gt;@SaraSoueidan&lt;/a&gt;) on &lt;a href=&quot;http://codepen.io&quot;&gt;CodePen&lt;/a&gt;.&lt;/p&gt;
&lt;script async=&quot;&quot; src=&quot;//assets.codepen.io/assets/embed/ei.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;Now &lt;em&gt;that&lt;/em&gt; is definitely better than the steps taken in CSS, isn’t it? The SVG &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;path&amp;gt;&lt;/code&gt; comes with a bunch of commands for drawing lines and arcs. Let’s take a closer look at the commands and parameters used to draw our menu items.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;drawing-items-using-svg-path&quot;&gt;Drawing A Menu Item Using SVG &lt;code&gt;&amp;lt;path&amp;gt;&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;We’re going to need some data to draw our sectors. We will then pass this data to the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;path&amp;gt;&lt;/code&gt; commands as parameters that define the shapes we’re drawing.&lt;/p&gt;

&lt;p&gt;A sector is defined by three points, a radius, and an angle. In order to draw a sector using the SVG &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;path&amp;gt;&lt;/code&gt; element, you need to know the coordinates for the three points. Then, using the path commands, we are going to &lt;strong&gt;move to&lt;/strong&gt; the center of the circle (the first point), draw a &lt;strong&gt;line to&lt;/strong&gt; the circle’s circumference (second point), then draw an &lt;strong&gt;elliptical arc&lt;/strong&gt; from the second point to the position of the third point, and then &lt;strong&gt;close the path&lt;/strong&gt; by drawing a line back to the center.&lt;/p&gt;

&lt;p&gt;The following is an interactive illustration showing how the path will be drawn. Click the button to start the demonstration.&lt;/p&gt;

&lt;p data-height=&quot;700&quot; data-theme-id=&quot;3617&quot; data-slug-hash=&quot;24de844274fb139d7eb1702783c9076d&quot; data-default-tab=&quot;result&quot; data-user=&quot;SaraSoueidan&quot; class=&quot;codepen&quot;&gt;See the Pen &lt;a href=&quot;http://codepen.io/SaraSoueidan/pen/24de844274fb139d7eb1702783c9076d/&quot;&gt;24de844274fb139d7eb1702783c9076d&lt;/a&gt; by Sara Soueidan (&lt;a href=&quot;http://codepen.io/SaraSoueidan&quot;&gt;@SaraSoueidan&lt;/a&gt;) on &lt;a href=&quot;http://codepen.io&quot;&gt;CodePen&lt;/a&gt;.&lt;/p&gt;
&lt;script async=&quot;&quot; src=&quot;//assets.codepen.io/assets/embed/ei.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;The three colored points are the points required to draw the sector. So let’s do some simple calculations to determine the coordinates of these points.&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;determining-point-coordinates&quot;&gt;Determining Point Coordinates&lt;/h3&gt;

&lt;p&gt;In order to build the circular menu, we are going to start with a square SVG canvas that has &lt;strong&gt;500px by 500px&lt;/strong&gt; dimensions. So, the menu will be centered inside it. The center of the circle will be at the center of the square. The radius will be &lt;strong&gt;250px&lt;/strong&gt;. So the blue dot in the above demo (point A) will have the coordinates (250px, 250px), and the orange one (point B) will be positioned at (500px, 250px).&lt;/p&gt;

&lt;p&gt;To determine the coordinates of the third point (C), we need the value of the angle (that is determined based on the number of menu items); then, using the given data and the values of the angle’s sine and cosine, we can get &lt;strong&gt;the polar coordinates&lt;/strong&gt; of the third point. The &lt;strong&gt;y&lt;/strong&gt; value of the pink dot in the polar coordinate system is equal to &lt;strong&gt;sin(angle)&lt;/strong&gt; multiplied by the radius &lt;strong&gt;r&lt;/strong&gt;. The &lt;strong&gt;x&lt;/strong&gt; value is equal to the &lt;strong&gt;cos(angle)&lt;/strong&gt; mutiplied by the radius &lt;strong&gt;r&lt;/strong&gt;.&lt;/p&gt;

&lt;figure&gt;
    &lt;img src=&quot;https://sarasoueidan.com/images/svg-circ-menu-points.svg&quot; alt=&quot;&quot; /&gt;
    &lt;figcaption&gt;
        The &lt;strong&gt;y&lt;/strong&gt; polar coordinate value of the pink dot in the polar coordinate system is equal to &lt;strong&gt;sin(a)&lt;/strong&gt; (h/r) multiplied by the radius &lt;strong&gt;r&lt;/strong&gt;. The &lt;strong&gt;x&lt;/strong&gt; polar coordinate value is equal to the &lt;strong&gt;cos(a)&lt;/strong&gt; (w/r) mutiplied by the radius &lt;strong&gt;r&lt;/strong&gt;. 
    &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;For the path data, we need the &lt;strong&gt;cartesian coordinates&lt;/strong&gt; of the point, which means that we now need to convert the polar x and y coordinates we have to cartesian coordinates. Using a simple math conversion we can transform those coordinates into cartesian coordinates &lt;strong&gt;which will represent the coordinates of the pink dot in the SVG canvas&lt;/strong&gt;. The conversion formula looks something like this (speaking in JS):&lt;/p&gt;

&lt;pre class=&quot;brush:js&quot;&gt;
//polar to cartesian coordinates conversion
//knowing the value of your angle in degrees..
//get value of the angle in radians
angleInRadians = -angleInDegrees * Math.PI / 180.0;
//get the cartesian x coordinate (centerX = x coordinate of the center of the circle == 250px in our case)
x = centerX + radius * Math.cos(angleInRadians);
//get the cartesian y coordinate (centerY = y coordinate of the center of the circle == 250px in our case)
y = centerY + radius * Math.sin(angleInRadians);
&lt;/pre&gt;

&lt;p&gt;Once you have all coordinates, you are ready to draw the sector.&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;drawing-the-sector&quot;&gt;Drawing The Sector's Lines And Arc&lt;/h3&gt;

&lt;p&gt;Using the SVG &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;path&amp;gt;&lt;/code&gt; element, each sector can be drawn using one line of SVG:&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;path fill=&quot;transparent&quot; stroke=&quot;#111&quot; stroke-width=&quot;2&quot; d=&quot;M250,250 l250,0 A250,250 0 0,0 466.5063509461097,125 z&quot; /&amp;gt;
&lt;/pre&gt;

&lt;p&gt;The part we’re interested in is the content of the &lt;code class=&quot;highlighter-rouge&quot;&gt;d&lt;/code&gt; (= data) attribute; it is where our coodinates will come in use. Here is a colored breakdown of the path data:&lt;/p&gt;

&lt;p&gt;
&lt;code&gt;
&lt;strong&gt;M&lt;/strong&gt;&lt;span style=&quot;color: #32BAFC&quot;&gt;250,250&lt;/span&gt; &lt;strong&gt;l&lt;/strong&gt;&lt;span style=&quot;color: orange&quot;&gt;250,0&lt;/span&gt; &lt;strong&gt;A&lt;/strong&gt;&lt;span style=&quot;color: #aaa&quot;&gt;250,250 0 0,0&lt;/span&gt; &lt;span style=&quot;color: deepPink&quot;&gt;466.5063509461097,125&lt;/span&gt; &lt;strong&gt;z&lt;/strong&gt;
&lt;/code&gt;
&lt;/p&gt;

&lt;p&gt;We’re using four path commands here: &lt;strong&gt;M&lt;/strong&gt;, &lt;strong&gt;l&lt;/strong&gt; (small L), &lt;strong&gt;A&lt;/strong&gt; and &lt;strong&gt;z&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;First, &lt;strong&gt;move to (M)&lt;/strong&gt; the point of coordinates 250,250—the center of the circle.&lt;/p&gt;

&lt;p&gt;Next, draw a &lt;strong&gt;line to (l)&lt;/strong&gt; the point that is at 250,0 &lt;strong&gt;&lt;em&gt;relative to the current position&lt;/em&gt;&lt;/strong&gt;. In other words, when we move to the orange dot, we are not using that point’s coordinates. We are calculating the horizontal and vertical distance of this point relative to the current position—which in this case is the center of the circle. You can, however, use the point’s coordinates if you use the &lt;strong&gt;L&lt;/strong&gt; command (capital letter), which draws a line using absolute coordinates instead of relative ones.&lt;/p&gt;

&lt;p&gt;Okay so, move from the center 250 units to the right, drawing a line in that direction.&lt;/p&gt;

&lt;p&gt;Next, draw an &lt;strong&gt;elliptical arc (A)&lt;/strong&gt; defined by: &lt;strong&gt;250,250 0 0,0&lt;/strong&gt; (we’ll get back to these shortly), from the current position to the point at 466.5063509461097,125—which are the cartesian coordinates of the pink dot. The capital letter &lt;strong&gt;A&lt;/strong&gt; command will draw an arc using absolute values; that is, it will draw an arc from the current position to the position you specify in the coordinates, and these coordiates will be absolute, &lt;em&gt;not&lt;/em&gt; relative to the current position.&lt;/p&gt;

&lt;p&gt;Then, &lt;strong&gt;close the path (z)&lt;/strong&gt;: a line is drawn from the pink dot back to the center, and the sector is complete.&lt;/p&gt;

&lt;p&gt;But what is that &lt;strong&gt;250,250 0 0,0&lt;/strong&gt; part all about?&lt;/p&gt;

&lt;p&gt;The elliptical arc command takes in a few parameters: &lt;strong&gt;(rx ry x-axis-rotation large-arc-flag sweep-flag x y)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For our circular menu, the &lt;strong&gt;250,250&lt;/strong&gt; part determines the horizontal and vertical radii (&lt;strong&gt;rx ry&lt;/strong&gt;). Both values are equal since we are drawing a &lt;em&gt;circular&lt;/em&gt; sector, not an elliptical one. You set these to be equal to the radius of the circle you are working on.&lt;/p&gt;

&lt;p&gt;For the sake of brevity, I’ll skip the next three parameters for now. What you need to know is that, for our circular menu, you need to set these three parameters to zero since we are drawing small circular arcs.&lt;/p&gt;

&lt;p&gt;Finally, the coordinates of the point to which the arc will be drawn are provided (&lt;strong&gt;x y&lt;/strong&gt;).&lt;/p&gt;

&lt;p&gt;With one sector drawn, the others follow the same way. Draw as many sectors as you need. Then, the remaining sectors are rotated by the necessary angle to position them along the circle as we saw in demonstration from the previous section.&lt;/p&gt;

&lt;p&gt;Since CSS transforms on SVG elements are not supported in IE, I’ve used &lt;a href=&quot;https://sarasoueidan.com/blog/svg-transformations&quot;&gt;SVG’s native transformations&lt;/a&gt; to rotate the items. The &lt;code class=&quot;highlighter-rouge&quot;&gt;transform&lt;/code&gt; attribute takes three parameters: the angle of rotation, and the x and y position of the center of rotation. The center of rotation is the center of the circle at (250px, 250px), and the angle of rotation is calculated based on the number of menu items you choose in the GUI and whether the menu is a full circle or a semi circle.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;adding-icons&quot;&gt;Adding Icons To The Menu Items&lt;/h2&gt;

&lt;p&gt;Since each icon could be more than just an icon—for example, an icon and a label, or just a label, it is best if we had a “wrapper” for whatever the contents of each item’s icon will be. The first thing that comes to mind in this case is using a group element: &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;g&amp;gt;&lt;/code&gt;. However, &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;g&amp;gt;&lt;/code&gt; has its limitations as it does not come with a &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; attribute, nor does it create a coordinate system for its content to be positioned inside. The next logical option is using an &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; element as a wrapper.&lt;/p&gt;

&lt;p&gt;The icons &lt;em&gt;could&lt;/em&gt; be wrapped in &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; elements which will create a coordinate system for the icon’s content. That being said, I chose not to go with this option because it would have required you to get your hands dirtier with the code whenever you wanted to modify or change the icons. I wanted to make it as simple as possible. For that reason, I chose the next best option: &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;symbol&amp;gt;&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;use&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;symbol&amp;gt;&lt;/code&gt; accepts a &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; attribute, and &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;use&amp;gt;&lt;/code&gt; accepts &lt;code class=&quot;highlighter-rouge&quot;&gt;width&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;height&lt;/code&gt; attributes that serve as the viewport for the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; specified on the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;symbol&amp;gt;&lt;/code&gt;. Thus, combined, &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;symbol&amp;gt;&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;use&amp;gt;&lt;/code&gt; provide us with what &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; would have provided us, plus a way to separate icon definitions from their actual use throughout the menu. Perfect.&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;positioning-the-icons&quot;&gt;Positioning The Icons&lt;/h3&gt;

&lt;p&gt;We don’t have relative positioning in SVG that allows us to position an element relative to another element, but we can use &lt;a href=&quot;https://sarasoueidan.com/blog/nesting-svgs&quot;&gt;nested SVGs&lt;/a&gt; to work around that. That being said, nesting SVGs to position the icons “relative” to the sectors (or: relative to a common container, to be more accurate) would have been overkill given that we I could do it another way.&lt;/p&gt;

&lt;p&gt;My objective was to position the icons at the center of the sections and rotate them by an angle so that they look as if they are rotated &lt;em&gt;with&lt;/em&gt; the sector. Visually speaking, the icons would be positioned along a virtual line that bisects the sector’s angle, and does not extend beyond the chord joining the orange and pink dots from the previous section’s illustration.&lt;/p&gt;

&lt;p&gt;Using the SVG DOM API, we can translate the above logic into code by first determining the virtual alignment line for the icon in the middle of the sector, the maximum length of that line which we can specify after getting the point on the chord where the virtual line and the chord would intersect, and then using the SVG &lt;code class=&quot;highlighter-rouge&quot;&gt;getPointAtLength()&lt;/code&gt; method to specify where on that line the icon should be positioned. Then, what is left after that is simply rotating the icon by half the angle of the sector and nudging it a little bit so that its center is positioned at the point on the line that we want.&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/svg-menu-icons-alignment.svg&quot; alt=&quot;&quot; style=&quot;max-width: 600px&quot; /&gt;
	&lt;figcaption&gt;Illustration showing icons positioned along a virtual line inside each menu item's sector.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;The two black dots in the above illustration show the position of the icon along the line (that we can get using &lt;code class=&quot;highlighter-rouge&quot;&gt;getPointAtLength()&lt;/code&gt;) and the point of intersection of the line with the sector’s chord. The range control in the app’s GUI that allows you to change the position of the icon inside each item actually changes the result of &lt;code class=&quot;highlighter-rouge&quot;&gt;getPointAtLength()&lt;/code&gt;, making sure it does not exceed the point of intersection with the chord.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;linking&quot;&gt;Linking In The Menu&lt;/h2&gt;

&lt;p&gt;Each menu item is made up of the path representing the sector shape and a &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;use&amp;gt;&lt;/code&gt; element referencing a &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;symbol&amp;gt;&lt;/code&gt;. These two elements are wrapped in an anchor element: &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;a&amp;gt;&lt;/code&gt; to make an item clickable.&lt;/p&gt;

&lt;p&gt;Just like HTML &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;a&amp;gt;&lt;/code&gt; elements, an SVG anchor also has &lt;code class=&quot;highlighter-rouge&quot;&gt;href&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;title&lt;/code&gt; attributes, with one important difference: namespacing. Before the &lt;code class=&quot;highlighter-rouge&quot;&gt;href&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;title&lt;/code&gt; parts, you need to add the &lt;code class=&quot;highlighter-rouge&quot;&gt;xlink&lt;/code&gt; namespace such that the link would be marked up like this:&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;a xlink:href=&quot;..&quot; xlink:title=&quot;..&quot;&amp;gt; &amp;lt;!-- item contents --&amp;gt; &amp;lt;/a&amp;gt;
&lt;/pre&gt;

&lt;p&gt;Additionally, the menu generator adds the &lt;code class=&quot;highlighter-rouge&quot;&gt;role&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;tabindex&lt;/code&gt; attributes for accessibility. And that’s pretty much all you need for the items.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;styling-and-interactivity&quot;&gt;About Styling And Interactivity&lt;/h2&gt;

&lt;p&gt;In the CSS version of this menu, pointer events were buggy in some browsers and &lt;code class=&quot;highlighter-rouge&quot;&gt;z-index&lt;/code&gt; was needed to handle stacking the elements on top of each other. With SVG, and because of the nature of elements in SVG, the pointer events are restricted to each shape without having to do anything extra. Since each shape is independent from the other and they do not overlap, no stack handling is required. Everything just works as you’d expect it to.&lt;/p&gt;

&lt;p&gt;Moreover, the SVG elements can be selected and styled in CSS. In order to make styling the elements and icons easier, I avoided adding any unnecessary presentation attributes.&lt;/p&gt;

&lt;p&gt;You can interact with the menu items and animate them independently using CSS or JavaScript. The app comes with a guide that includes a few animated examples using JavaScript. I chose the latter over CSS for browser compatibility because, again, IE does not support CSS transformations on SVG elements yet.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;final-words&quot;&gt;Final Words&lt;/h2&gt;

&lt;p&gt;SVG is very powerful, and SVG paths are one of the most powerful of its features. The very nature of SVG elements gives us more flexibility for creating and animating non-rectangular UI elements. And the fact that these elements can be drawn while remaining semantic and fully accessible gives SVG an edge for creating visually and functionally superior interfaces.&lt;/p&gt;

&lt;p&gt;I hope you found this article useful. Thank you for reading.&lt;/p&gt;


    
  </content>
 </entry>
 
 <entry>
   <title>Extending the Color Cascade with the CSS currentColor Variable</title>
   <link href="http://sarasoueidan.com//blog/currentcolor/"/>
   <id>https://sarasoueidan.com/blog/currentcolor</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;
	If you use Sass or LESS, then you probably already use variables in your style sheets and know how useful they are. If you don’t use a preprocessor, then you might be curious what the fuss is all about and why variables are so popular and how they can be useful. In this article, we’re going to get an overview of why variables are useful, and get acquainted with one particular variable: currentColor.
&lt;/p&gt;

    

     <div>
        <p>
          <em>This article is <a href="http://blogs.adobe.com/dreamweaver/2015/02/extending-the-color-cascade-with-the-css-currentcolor-variable.html">published @ <strong>Adobe Dreamweaver Team Blog</strong>.</a></em>
        </p>
    </div>
    
  </content>
 </entry>
 
 <entry>
   <title>Better SVG Fallback and Art Direction With The &lt;picture&gt; Element</title>
   <link href="http://sarasoueidan.com//blog/svg-picture/"/>
   <id>https://sarasoueidan.com/blog/svg-picture</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;Besides using an SVG as a background image in CSS, you can serve SVG foreground images in HTML using  one of several embedding techniques, each of which has its advantages and use cases. Unless you’re in need of interactivity or external styling, &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt; is the standard way for loading an SVG image, but it has one disadvantage: you currently need JavaScript to provide fallback and/or change the image source for art direction. In this post, I’ll talk about a better way to do that, using the &lt;code&gt;&amp;lt;picture&amp;gt;&lt;/code&gt; element.&lt;/p&gt;

&lt;p&gt;This is not a primer to using &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;picture&amp;gt;&lt;/code&gt;. There are a lot of great resources in the wild that contain everything you need to know about the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;picture&amp;gt;&lt;/code&gt; element, so if you’re not familiar with it, refer to the last section of the article for a list of resources to learn all about it. That being said, the article does not require any special or strong &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;picture&amp;gt;&lt;/code&gt; skills, as the examples are mostly self-explanatory as you will see.&lt;/p&gt;

&lt;p class=&quot;note&quot;&gt;
  This article is also &lt;a href=&quot;http://css-live.ru/articles/obespechenie-luchshej-rezervnoj-podderzhki-svg-i-upravlenie-oformleniem-s-pomoshhyu-elementa-picture.html&quot;&gt;available in Russian&lt;/a&gt;.
&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;the-current-img&quot;&gt;The Current &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;Before getting into why &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;picture&amp;gt;&lt;/code&gt; is a great option for SVG, let’s lay down (an overview of) the limitations and disadvantages of using &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;img&amp;gt;&lt;/code&gt; for responsive SVG work.&lt;/p&gt;

&lt;p&gt;Normally, if you load an SVG using an &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;img&amp;gt;&lt;/code&gt; tag, you can provide fallback and change the source of the image on different viewport sizes using feature detection and media queries in JavaScript. My choice for both would be to use &lt;a href=&quot;http://modernizr.com/&quot;&gt;Modernizr&lt;/a&gt; for both; that is, unless you’re only serving one image, in which case adding Modernizr might be overwork, and something like this:&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;img src=&quot;logo.svg&quot; onerror=&quot;this.src=logo-fallback.png;this.onerror=null;&quot; /&amp;gt;
&lt;/pre&gt;

&lt;p&gt;..would be simpler and faster.&lt;/p&gt;

&lt;p&gt;Using Modernizr, you can detect browser support for SVG, and provide an alternative image &lt;code class=&quot;highlighter-rouge&quot;&gt;src&lt;/code&gt; for when SVG is not supported. The alternative image URL can be stored in a custom data attribute. This approach is particularly useful for when you have multiple images on the page whose &lt;code class=&quot;highlighter-rouge&quot;&gt;src&lt;/code&gt; you need to switch.&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;img src=&quot;logo.svg&quot; data-fallback=&quot;logo.png&quot; /&amp;gt;
&lt;/pre&gt;

&lt;p&gt;Using Modernizr, you can detect whether or not the browser supports SVG and then take necessary steps to provide the fallback based on the test result:&lt;/p&gt;

&lt;pre class=&quot;brush:js&quot;&gt;
if (!Modernizr.svg) {
  // fetch fallback and replace img src with it
}
&lt;/pre&gt;

&lt;p&gt;You can also use Modernizr’s media query detection to change the img src on based on viewport width for when you want to do art direction and not load the same big SVG on smaller screens:&lt;/p&gt;

&lt;pre class=&quot;brush:js&quot;&gt;
if (Modernizr.mq('(max-width: 640px)')) {
  // replace image source with the smaller SVG source
}
&lt;/pre&gt;

&lt;p&gt;You don’t need to store any URLs in data attributes in this case if you are following a specific naming convention for your images. For example, if your images are named &lt;code class=&quot;highlighter-rouge&quot;&gt;view-small.svg&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;view-big.svg&lt;/code&gt;, you can just replace the &lt;code class=&quot;highlighter-rouge&quot;&gt;view-*&lt;/code&gt; part with the appropriate one in the JavaScript and be done with it.&lt;/p&gt;

&lt;p&gt;Now, if you want to provide a PNG or JPEG fallback for your SVG &lt;em&gt;and&lt;/em&gt; also provide different sizes of that fallback image to match the viewport size, Modernizr will also do, but it will get slightly more complicated. And the most important part is: you need JavaScript to do it.&lt;/p&gt;

&lt;p&gt;With the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;picture&amp;gt;&lt;/code&gt; element, we can do all that and more, without JavaScript. Well, kind of. Read on.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;the-bigger-picture&quot;&gt;The Bigger &lt;code&gt;&amp;lt;picture&amp;gt;&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;picture&amp;gt;&lt;/code&gt; element provides us with a better JavaScript-less way to change the image we are serving based on different media queries &lt;em&gt;and&lt;/em&gt; a for providing fallback for non-supporting browsers (or browsers that can’t load the SVG for any reason).&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;loading-and-providing-fallback&quot;&gt;Loading An SVG and Providing Fallback For Non-Supporting Browsers&lt;/h3&gt;

&lt;p&gt;Let’s start with fallback first. Providing fallback for browsers that can’t load the SVG is as simple as wrapping your &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;img&amp;gt;&lt;/code&gt; fallback in a &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;picture&amp;gt;&lt;/code&gt; element, and referencing your SVG in a &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;source&amp;gt;&lt;/code&gt; element:&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;picture&amp;gt;
    &amp;lt;source type=&quot;image/svg+xml&quot; srcset=&quot;path/to/image.svg&quot;&amp;gt;
    &amp;lt;img src=&quot;path/to/fallback.png&quot; alt=&quot;Image description&quot;&amp;gt;
&amp;lt;/picture&amp;gt;
&lt;/pre&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;picture&amp;gt;&lt;/code&gt; element is just a wrapper for the elements used to load the image(s) you want and for the fallback provided with the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;img&amp;gt;&lt;/code&gt; element. The &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;img&amp;gt;&lt;/code&gt; element is &lt;em&gt;required&lt;/em&gt; for &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;picture&amp;gt;&lt;/code&gt; to work and is used to provide backwards compatibility for browsers that don’t support &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;picture&amp;gt;&lt;/code&gt; or, like our case here, browsers that can’t load or don’t support the the image(s) [type] you load in the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;source&amp;gt;&lt;/code&gt; element.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;source&amp;gt;&lt;/code&gt; element is where we specify the image(s) we want. We’re specifying the type of the image we want (SVG) in the &lt;code class=&quot;highlighter-rouge&quot;&gt;type&lt;/code&gt; attribute, and providing the URL to that image in the &lt;code class=&quot;highlighter-rouge&quot;&gt;srcset&lt;/code&gt; attribute.
And that’s all there really is to it; this is how simple it is to provide fallback for an SVG image using &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;picture&amp;gt;&lt;/code&gt;—no JavaScript is needed.&lt;/p&gt;

&lt;p&gt;You can take this even further and provide multiple fallback images that take screen resolution into account; to do that you can specify those images using the &lt;code class=&quot;highlighter-rouge&quot;&gt;srcset&lt;/code&gt; attribute on the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;img&amp;gt;&lt;/code&gt;. For example:&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;picture&amp;gt;
    &amp;lt;source type=&quot;image/svg+xml&quot; srcset=&quot;path/to/logo.svg&quot;&amp;gt;
    &amp;lt;img src=&quot;path/to/logo-1x.png&quot; srcset=&quot;path/to/logo-2x.png 2x, path/to/logo-3x.png 3x&quot; alt=&quot;Logo description&quot;&amp;gt;
&amp;lt;/picture&amp;gt;
&lt;/pre&gt;

&lt;p&gt;The browser can then choose the image it finds appropriate based on the screen resolution. This is useful for when you are serving the same image size (for example, a one-size logo) but want to provide 2x and 3x versions for higher resolutions.&lt;/p&gt;

&lt;p&gt;But if you want you can take it even &lt;em&gt;further&lt;/em&gt;. With the help of the &lt;code class=&quot;highlighter-rouge&quot;&gt;sizes&lt;/code&gt; attribute, you can use media queries on the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;img&amp;gt;&lt;/code&gt; to change the fallback image size on different screen sizes, providing a bigger image for bigger screens and a smaller one for small screens.&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;picture&amp;gt;
    &amp;lt;source type=&quot;image/svg+xml&quot; srcset=&quot;path/to/banner.svg&quot;&amp;gt;
    &amp;lt;img
      sizes=&quot;(min-width: 640px) 80vw, 100vw&quot;
      srcset=&quot;banner-300.jpg 300w,
              banner-400.jpg 400w,
              banner-700.jpg 700w,
              banner-1200.jpg 1200w,
              banner-1600.jpg 1600w,
              banner-2000.jpg 2000w&quot;
      src=&quot;banner-default-fallback.jpg&quot;
      alt=&quot;Banner description&quot;&amp;gt;
&amp;lt;/picture&amp;gt;
&lt;/pre&gt;

&lt;p&gt;What we’ve done here is we told the browser in the &lt;code class=&quot;highlighter-rouge&quot;&gt;sizes&lt;/code&gt; attribute what size our image will occupy on the page. In this case, if the width of the viewport is 640px or more, the image will be 80% the width of the viewport, and 100% the width otherwise.&lt;/p&gt;

&lt;p&gt;Then, in the &lt;code class=&quot;highlighter-rouge&quot;&gt;srcset&lt;/code&gt; attribute, we provided the browser with a list of images—they are all the same image, but in different sizes. Based on the sizes specified in &lt;code class=&quot;highlighter-rouge&quot;&gt;sizes&lt;/code&gt;, the browser will pick the best fit among these images and display it.&lt;/p&gt;

&lt;p&gt;If a browser does not support &lt;code class=&quot;highlighter-rouge&quot;&gt;srcset&lt;/code&gt; on &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;img&amp;gt;&lt;/code&gt;, it will simply display the fallback specified in the &lt;code class=&quot;highlighter-rouge&quot;&gt;src&lt;/code&gt; attribute. For a detailed explanation of how this works, refer to &lt;a href=&quot;http://alistapart.com/article/responsive-images-in-practice&quot;&gt;this article&lt;/a&gt; over at A List Apart.&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;art-direction&quot;&gt;Art Direction: Loading a Different SVG on Different Screen Sizes&lt;/h3&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;source&amp;gt;&lt;/code&gt; element we use to specify the image(s) we want comes with another attribute: &lt;code class=&quot;highlighter-rouge&quot;&gt;media&lt;/code&gt;. This attribute provides us with the same flexibility we have for changing background images in CSS using CSS media queries, by allowing us to pair image sources with layout conditions (the media queries) right in the source code.&lt;/p&gt;

&lt;p&gt;Since we’re serving an SVG image, we don’t need to serve multiple versions of the image for different screen resolutions because of the infinitely scalable nature of SVG which makes it look great on any resolution. (Yay!)&lt;/p&gt;

&lt;p&gt;But if we have an SVG that we’re serving on desktop—for example, a wide header image, this image could be hundreds of kilobytes in size. Serving the same big image for small screens might not be the best idea if you look at it from a performance angle. Moreover, maybe you just &lt;em&gt;don’t want&lt;/em&gt; to serve the same image on smaller sizes, but a “cropped” version of that image. I recently worked on a client project that required just that. Not only did my client want different images on smaller sizes, but the full composition was more than 100KB in size, which is obviously too much to serve on mobile devices, so we served cropped versions of that image.&lt;/p&gt;

&lt;p&gt;In such a case, you can specify different SVGs to load on different media conditions using the &lt;code class=&quot;highlighter-rouge&quot;&gt;media&lt;/code&gt; attribute on the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;source&amp;gt;&lt;/code&gt;. In the &lt;code class=&quot;highlighter-rouge&quot;&gt;media&lt;/code&gt; attribute, you specify the media conditions similar to how you do it in CSS media queries.&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;picture&amp;gt;
    &amp;lt;source
        media=&quot;(max-width: 640px)&quot;
        srcset=&quot;header--small.svg&quot;
        type=&quot;image/svg+xml&quot;&amp;gt;
    &amp;lt;source
        media=&quot;(max-width: 1024px)&quot;
        srcset=&quot;header--medium.svg&quot;
        type=&quot;image/svg+xml&quot;&amp;gt;
    &amp;lt;source
        srcset=&quot;header--full.svg&quot;
        type=&quot;image/svg+xml&quot;&amp;gt;

    &amp;lt;img src=&quot;header--default-fallback.jpg&quot; alt=&quot;Header description..&quot;&amp;gt;
&amp;lt;/picture&amp;gt;

&lt;/pre&gt;

&lt;p&gt;Of course, you can specify different fallback images for different resolutions and sizes, similar to what we did in the previous section. For the sake of brevity, I’m going to skip that step in this section, but you get the picture. (See what I did there?)&lt;/p&gt;

&lt;p&gt;You can also specify multiple sizes of each SVG image and let the browser pick the one it finds best, like we did for the fallback image before. However, due to the scalable nature of SVG, this might not be necessary.&lt;/p&gt;

&lt;p&gt;As a matter of fact, the options the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;picture&amp;gt;&lt;/code&gt; element comes with cover practically any scenario. &lt;a href=&quot;https://dev.opera.com/articles/responsive-images/&quot;&gt;This article&lt;/a&gt; on the dev.opera blog covers a lot of these use cases with practical examples and snippets to help you get started.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;So, you see, with the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;picture&amp;gt;&lt;/code&gt; element, we no longer need to use JavaScript to provide fallback and/or change the image based on different media conditions. Well, kind of…&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;browser-support-and-polyfilling&quot;&gt;Browser Support and Polyfilling&lt;/h2&gt;

&lt;p&gt;At the time of writing of this article, browser support for &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;picture&amp;gt;&lt;/code&gt; isn’t at its best, but it is getting better. A lot of smart people are working on &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;picture&amp;gt;&lt;/code&gt; implementation across browsers. Keep an eye on &lt;a href=&quot;http://caniuse.com/#feat=picture&quot;&gt;the compatibility table over at CanIUse.com&lt;/a&gt; to stay up-to-date on browser support in the future.&lt;/p&gt;

&lt;p&gt;In the meantime, and until browser support becomes more decent, you can use a JavaScript polyfill for non-supporting browsers. So yes, we do need JavaScript at the moment, but the code you write will be future-proof and all you need to do in the future when support gets better is to remove the polyfill, and your code will work without it as expected. Using &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;img&amp;gt;&lt;/code&gt; you’d need to do much more, or, at least, just keep using Javacript.&lt;/p&gt;

&lt;p&gt;The &lt;a href=&quot;http://scottjehl.github.io/picturefill/&quot;&gt;Picturefill&lt;/a&gt; polyfill by the folks at the Filament Group is the current de facto for cross-browser &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;picture&amp;gt;&lt;/code&gt; support today. The polyfill homepage contains extensive documentation on how to use the polyfill and tips on using &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;picture&amp;gt;&lt;/code&gt; in general along with general patterns.&lt;/p&gt;

&lt;p&gt;Using the polyfill is as simple as including the script in your page’s &lt;code class=&quot;highlighter-rouge&quot;&gt;head&lt;/code&gt;:&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;script src=&quot;picturefill.js&quot; async&amp;gt;&amp;lt;/script&amp;gt;
&lt;/pre&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;async&lt;/code&gt; attribute tells the browser that it can load picturefill asynchronously, without waiting for it to finish before loading the rest of the document. According to the Picturefill documentation, If you add this attribute, you’ll need to add a line of script before the script tag as well to allow older browsers to recognize &lt;code class=&quot;highlighter-rouge&quot;&gt;picture&lt;/code&gt; elements if it encounters them in the page before Picturefill has finished loading.&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;script&amp;gt;
    // Picture element HTML5 shiv
    document.createElement( &quot;picture&quot; );
&amp;lt;/script&amp;gt;
&amp;lt;script src=&quot;picturefill.js&quot; async&amp;gt;&amp;lt;/script&amp;gt;
&lt;/pre&gt;

&lt;p&gt;If you are &lt;a href=&quot;http://www.paulirish.com/2011/the-history-of-the-html5-shiv/&quot;&gt;familiar with HTML5 Shiv&lt;/a&gt;, then you already know what this line is needed for. As a matter of fact, if you are already including a recent version of the HTML5 Shiv (sometimes packaged with Modernizr), you may not need this line as it is included there as well.&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;fixing-ie9&quot;&gt;Fixing IE9&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;p&gt;While most versions of IE (even older ones!) are supported [by Picturefill] well, IE9 has a little conflict to work around. To support IE9, you will need to wrap a &lt;code class=&quot;highlighter-rouge&quot;&gt;video&lt;/code&gt; element wrapper around the &lt;code class=&quot;highlighter-rouge&quot;&gt;source&lt;/code&gt; elements in your &lt;code class=&quot;highlighter-rouge&quot;&gt;picture&lt;/code&gt; tag. You can do this using conditional comments.
— &lt;a href=&quot;http://scottjehl.github.io/picturefill/&quot;&gt;Picturefill homepage&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As the documentation says, the polyfill provides support for &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;picture&amp;gt;&lt;/code&gt; across browsers, but IE9 requires that you wrap your &lt;code class=&quot;highlighter-rouge&quot;&gt;source&lt;/code&gt; elements in a &lt;code class=&quot;highlighter-rouge&quot;&gt;video&lt;/code&gt; tag. And since this fix is only required for IE9, you can place it in IE9-targeting conditional comments:&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;picture&amp;gt;
&amp;lt;!--[if IE 9]&amp;gt;&amp;lt;video style=&quot;display: none;&quot;&amp;gt;&amp;lt;![endif]--&amp;gt;
    &amp;lt;source srcset=&quot;..&quot; type=&quot;..&quot; &amp;gt;
    &amp;lt;source srcset=&quot;..&quot; type=&quot;..&quot; &amp;gt;
    &amp;lt;source srcset=&quot;..&quot; type=&quot;..&quot; &amp;gt;
&amp;lt;!--[if IE 9]&amp;gt;&amp;lt;/video&amp;gt;&amp;lt;![endif]--&amp;gt;
    &amp;lt;img src=&quot;..&quot; alt=&quot;..&quot; /&amp;gt;
&amp;lt;/picture&amp;gt;
&lt;/pre&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;foreground-svg-images&quot;&gt;Foreground SVG Images with Interactivity and Styleability&lt;/h2&gt;

&lt;p&gt;As mentioned at the beginning of the article, the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;img&amp;gt;&lt;/code&gt; element, and naturally the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;picture&amp;gt;&lt;/code&gt; element, only allow you to load a static SVG image, or an SVG with animations defined internally. If you need to load a foreground image and you want that image to be interactive and styleable, you can use one of four available ways: &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;object&amp;gt;&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;iframe&amp;gt;&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;embed&amp;gt;&lt;/code&gt; and inline &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Both the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;iframe&amp;gt;&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;object&amp;gt;&lt;/code&gt; come with a default fallback mechanism.&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;object data=&quot;image.svg&quot; type=&quot;image/svg+xml&quot;&amp;gt;
    &amp;lt;!-- fallback here --&amp;gt;
&amp;lt;/object&amp;gt;

&amp;lt;iframe src=&quot;image.svg&quot;&amp;gt;
    &amp;lt;!-- fallback here --&amp;gt;
&amp;lt;/iframe&amp;gt;
&lt;/pre&gt;

&lt;p&gt;An inline &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; requires a different approach to provide fallbacks; one such approach uses the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;foreignObject&amp;gt;&lt;/code&gt; element. You can read all about it &lt;a href=&quot;http://www.kaizou.org/2009/03/inline-svg-fallback/&quot;&gt;here&lt;/a&gt;. Chris has also written about providing fallback for SVG &lt;a href=&quot;http://css-tricks.com/svg-fallbacks/#the-fallbacks&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;final-words&quot;&gt;Final Words&lt;/h2&gt;

&lt;p&gt;While using &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;picture&amp;gt;&lt;/code&gt; currently does require adding a JavaScript polyfill, using standard HTML5 markup and getting the flexibility of switching images using native elements is extremely powerful, and plugging the polyfill in is as easy as 1. download it, 2. add script to page, 3. you’re done. It’s absolutely worth it if you are doing art direction or providing fallback for multiple foreground SVG images.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;picture&amp;gt;&lt;/code&gt; is more likely to become the standard way for art-directing SVG and providing &lt;code class=&quot;highlighter-rouge&quot;&gt;img&lt;/code&gt; fallback in the future, so why start using it today? Both &lt;code class=&quot;highlighter-rouge&quot;&gt;img&lt;/code&gt; way and the new &lt;code class=&quot;highlighter-rouge&quot;&gt;picture&lt;/code&gt; require some JavaScript—for now, but the latter is definitely cleaner and more future-proof. Yes, &lt;code class=&quot;highlighter-rouge&quot;&gt;img&lt;/code&gt; is also future-proof, but at some point, you get to ditch the polyfill and keep your code unchanged if you use &lt;code class=&quot;highlighter-rouge&quot;&gt;picture&lt;/code&gt;, while &lt;code class=&quot;highlighter-rouge&quot;&gt;img&lt;/code&gt; will either require you to keep using JavaScript &lt;em&gt;or&lt;/em&gt; refactor your markup to make the switch to JavaScript-less &lt;code class=&quot;highlighter-rouge&quot;&gt;picture&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Whether you decide to start using &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;picture&amp;gt;&lt;/code&gt; for SVG today or not, it is definitely worth getting to know better and using it for serving other responsive image formats. So here is a list of recommended articles to get you up and running:&lt;/p&gt;

&lt;h3 id=&quot;recommended-reading-on-picture&quot;&gt;Recommended Reading on &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;picture&amp;gt;&lt;/code&gt;&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.html5rocks.com/en/tutorials/responsive/picture-element/&quot;&gt;Introducing the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;picture&amp;gt;&lt;/code&gt; element&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://dev.opera.com/articles/native-responsive-images/&quot;&gt;Native Responsive Images&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://alistapart.com/article/responsive-images-in-practice&quot;&gt;Responsive Images in Practice&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://dev.opera.com/articles/responsive-images/&quot;&gt;Responsive Images: Use Cases and Documented Code Snippets to Get You Started&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://demosthenes.info/blog/936/Responsive-Images-For-Designers-The-HTML5-picture-element&quot;&gt;Responsive Images For Designers: The HTML5 picture element&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

    
  </content>
 </entry>
 
 <entry>
   <title>I Wrote A CSS <del>Book</del> Reference.</title>
   <link href="http://sarasoueidan.com//blog/codrops-css-reference/"/>
   <id>https://sarasoueidan.com/blog/codrops-css-reference</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;Yesterday, we finally released a long-awaited project over at Codrops: &lt;a href=&quot;http://tympanus.net/codrops/css_reference/&quot;&gt;Codrops' new &lt;strong&gt;CSS Reference&lt;/strong&gt;&lt;/a&gt;, authored by yours truly. Even though
I wrote &lt;a href=&quot;&quot;&gt;a post over at Codrops&lt;/a&gt; introducing the reference and its features, I want to share a little bit more about my experience writing it.&lt;/p&gt;

&lt;p class=&quot;note update--neutral&quot;&gt;
    Please note that as of January 2016, I am no longer the maintainer of the Codrops CSS Reference.
&lt;/p&gt;

&lt;p&gt;Thus, unlike my usual highly-technical blog posts, this is a (possibly-boring, but short!) post sharing a little more about the CSS Reference and how it came to be, and answering a couple of questions I got via Twitter since the release.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;how-it-started&quot;&gt;How It All Started&lt;/h2&gt;

&lt;p&gt;I started writing for Codrops in 2013. Some time at the end of the year, I was thinking to myself: “Codrops would be an ultimate CSS reference if it had a CSS reference” (no pun intended). I thought that it would be fantastic if we had a CSS reference at hand for when someone needs to learn more about a specific CSS property used in one of the many creative demos found there. This idea came to my mind since I used to google for some of the properties I saw in the Codrops demos when I first started learning CSS and checking Codrops out regularly.&lt;/p&gt;

&lt;p&gt;Around the same time that year, Manoela approached me with the same idea. It was an idea in my head, and part of a vision that Manoela and Pedro have for Codrops. It was only a few days later that I started digging into CSS specifications and writing the reference entries.&lt;/p&gt;

&lt;p&gt;It took around 7–8 months to finish all of the entries. During that time, I had little time for side projects, but I did keep writing—albeit sporadically—on my blog and other blogs such as A List Apart, among others.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;writing-the-entries&quot;&gt;Writing The Reference Entries&lt;/h2&gt;

&lt;p&gt;One of the main reasons I looked forward to starting the journey of writing the reference was knowing how much I will learn in the process. For me, that was enough of a reason to instantly say yes when Manoela approached me with the idea.&lt;/p&gt;

&lt;blockquote class=&quot;pull-quote&quot;&gt;
One of the main reasons I looked forward to starting the journey of writing the reference was knowing how much I will learn in the process.
&lt;/blockquote&gt;

&lt;p&gt;I knew it was a chance for me to know much more about CSS than I would otherwise know in such a short period of time. In order to write the description and information about a specific CSS feature, I had to dig deeper than I usually do into the specification where that CSS feature is defined.&lt;/p&gt;

&lt;p&gt;As many of you may know, the specifications don’t &lt;em&gt;always&lt;/em&gt; contain everything you need to know about a property, and the writing style is not always the clearest (which is one of the reasons we wrote the reference, too!), so I had to do a lot of research for many of the entries, reading great resources here and there, picking up a lot of knowledge and getting a lot of “ah-ha” moments in the process. There was &lt;em&gt;a lot&lt;/em&gt; about CSS that I didn’t know, and that I &lt;em&gt;still&lt;/em&gt; don’t know.&lt;/p&gt;

&lt;p&gt;I wrote the CSS entries the same way and same style I usually write my articles. Now, some entries are exceptions because they don’t require a lot of elaboration. For example, properties like &lt;code class=&quot;highlighter-rouge&quot;&gt;border-color&lt;/code&gt; are self-explanatory, so the tone of those entries is more “official” and less “me”.
&lt;br /&gt;So, if you happen to enjoy my writing style, I can tell will enjoy reading the reference entries too.&lt;/p&gt;

&lt;p&gt;Manoela and Pedro gave me a lot of freedom and flexibility to write the reference at my own pace and my own schedule and time, making sure I still had the time to work on other things as well so that writing the reference does not limit my creativity, my other client work, my blog, and, of course, my life!&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;releasing-the-reference&quot;&gt;Releasing The Reference&lt;/h2&gt;

&lt;p&gt;I can’t even begin to describe how excited I was to finally get to the point of releasing the reference! I usually get super nervous (read: almost get a heart attack) before publishing a blog post on my own low-profile blog, so publishing &lt;strong&gt;more than 300&lt;/strong&gt; short to long articles on Codrops was extremely overwhelming and nerve-wrecking. I can always hear my heart beat when I tweet about a new blog post, and I certainly did when we finally got the word out about the reference.&lt;/p&gt;

&lt;p&gt;It took us a little over a year to release the reference because of the amount of work each of us at Codrops had, besides the reference. 2014 was the year I started speaking at conferences, and in the second half of it (i.e. after having finished writing the entries) I got my hands full with conference work, writing, and other stuff that took my attention away from the reference for some time. But we finally made it.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;questions&quot;&gt;Questions&lt;/h2&gt;

&lt;p&gt;I got a few of questions from followers and commenters, and two of these questions kept popping up.&lt;/p&gt;

&lt;h4 id=&quot;is-it-the-new-mdn&quot;&gt;Is it “the new MDN”?&lt;/h4&gt;

&lt;p&gt;No, it’s not. At least it wasn’t our goal to make it so. It’s not even meant as competition to any other CSS Reference out there. It is simply another knowledge base added to what Codrops already offers on a regular basis.&lt;/p&gt;

&lt;p&gt;I write about CSS (and SVG) a lot—be it on my blog, on Codrops, or one of the several other places I write at. The CSS Reference is, for me, simply an archive where I collected &lt;strong&gt;blog posts&lt;/strong&gt; about a specific topic (CSS, in this case), and organized them in a way that is easier to browse and simpler for my readers to find what they are looking for.&lt;/p&gt;

&lt;blockquote class=&quot;pull-quote&quot;&gt;
The CSS Reference is, for me, simply an archive where I collected &lt;strong&gt;blog posts&lt;/strong&gt; about a specific topic (CSS, in this case), and organized them in a way that is easier to browse and simpler for my readers to find what they are looking for.
&lt;/blockquote&gt;

&lt;p&gt;I sometimes even think of it as a book! Instead of writing a printed CSS book, I wrote an online book, that is open to your contributions and suggestions via a Github repo that we shared on Codrops. So it has an advantage over a regular book in that you, my dear readers, get to pinpoint any miskates, errors, ask for additions and suggestions, etc.&lt;/p&gt;

&lt;h4 id=&quot;why-didnt-you-contribute-to-mdn-instead&quot;&gt;Why didn’t you contribute to MDN instead?&lt;/h4&gt;

&lt;p&gt;I honestly don’t understand the reasoning behind this question. I mean, why would I &lt;em&gt;not&lt;/em&gt; write the reference and choose to contribute to MDN &lt;em&gt;instead&lt;/em&gt;?&lt;/p&gt;

&lt;p&gt;That being said, I did mention a few reasons earlier why I chose to write the reference. Add to those that I love Codrops, and Manoela and Pedro gave me a lot of freedom and flexibility to give the reference its own character and style. They asked me to write it &lt;em&gt;because&lt;/em&gt; they liked my writing style, and thus gave me the freedom to structure the entries the way I wanted. We did have some specifics—for example, the sections for the official syntax, browser support, etc. that we needed to have in every entry, and the rest was all up to me to write my own way.&lt;/p&gt;

&lt;p&gt;Now, I don’t know if MDN would have allowed me to do the same, but Manoela knows how much I value the freedom and flexibility of work in my work, and she gave me just that, making the experience much more enjoyable, and feeling a lot less as a task or job.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;final-words&quot;&gt;Final Words&lt;/h2&gt;

&lt;p&gt;Writing the CSS reference has been a wonderful experience, and I am happy to have gotten the chance to do it. I know I haven’t literally written a book here (as in: a &lt;em&gt;printed&lt;/em&gt; book), but I like to think that I have—maybe because it makes me feel a &lt;em&gt;liiiitle bit&lt;/em&gt; less guilty for not having started writing my SVG book yet!&lt;/p&gt;

&lt;p&gt;I wholeheartedly hope you like it, find it useful, and learn from it as much as I have learned from writing it. Your feedback on Twitter, in the comments and your contributions in the Github repo have been amazing—thank you so much for your support!&lt;/p&gt;

&lt;p&gt;And thank you for reading this post, &lt;em&gt;and&lt;/em&gt; reading the reference entries in the future.&lt;/p&gt;

&lt;p&gt;–S&lt;/p&gt;

    
  </content>
 </entry>
 
 <entry>
   <title>Compositing And Blending In CSS</title>
   <link href="http://sarasoueidan.com//blog/compositing-and-blending-in-css/"/>
   <id>https://sarasoueidan.com/blog/compositing-and-blending-in-css</id>
   <content type="html">
    &lt;p&gt;If you’re a designer, then you’ve probably already come across blending effects some time or the other. Blending is one of the most frequently used effects in graphic and print design. You can add texture to text by blending it with its textured backdrop, create an illusion of &lt;em&gt;merged&lt;/em&gt; images by blending these images together, and create a wide range of colorful effects that would not be possible without that fine level of color blending control.&lt;/p&gt;

&lt;figure&gt;
&lt;img src=&quot;https://sarasoueidan.com/images/blending-examples.png&quot; /&gt;
&lt;figcaption&gt;Examples of typographic and image effects created using CSS blend modes. See following sections for live demos.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Graphics editors such as Photoshop or Illustrator come with a set of blending &lt;em&gt;modes&lt;/em&gt;. &lt;strong&gt;Blend modes&lt;/strong&gt; allow you to specify &lt;em&gt;how&lt;/em&gt; you want your elements to blend together, thus changing the colors of the area where these elements intersect. Each mode uses a specific color formula to mix the colors of the &lt;em&gt;source&lt;/em&gt; and the &lt;em&gt;destination&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Different modes give different end results. Before we talk about the different blend modes, and since we mentioned the &lt;strong&gt;source&lt;/strong&gt; and &lt;strong&gt;destination&lt;/strong&gt; elements, let’s have a quick look at the concept of compositing, and how it is related to blending in CSS.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;compositing&quot;&gt;What Is Compositing?&lt;/h2&gt;

&lt;p&gt;Compositing is the combining of a graphic element with its &lt;em&gt;backdrop&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;backdrop&lt;/strong&gt; is &lt;strong&gt;the content behind the element&lt;/strong&gt; and is what the element is composited with.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://sarasoueidan.com/images/backdrop.png&quot; alt=&quot;Backdrop Visual Illustration&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Compositing defines how what you want to draw will be blended with what is already drawn on the canvas. The &lt;strong&gt;source&lt;/strong&gt; is what you want to draw, and the &lt;strong&gt;destination&lt;/strong&gt; is what is already drawn (the backdrop).&lt;/p&gt;

&lt;p&gt;So, if you have two elements, and these elements overlap, you can think of the element on top as being the source, and the parts of the element behind that lie beneath it, will be the destination.&lt;/p&gt;

&lt;p&gt;Using different composite operations (there are 16 operations), you can specify which parts of the two overlapping elements will be drawn, and which will not.&lt;/p&gt;

&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://sarasoueidan.com/images/porter-duff.png&quot; alt=&quot;Porter Duff Compositing Operations Visual&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;These composite operations are known as &lt;strong&gt;Porter Duff compositing operations&lt;/strong&gt;. These operations specify what portions of the source and destination will be drawn, and blend modes specify how how the colors from the graphic element (source) and the backdrop (destination) interact. The illustrations in the above image are from the Compositing and Blending spec.
In HTML5 Canvas context, these oprations are specified using the &lt;code class=&quot;highlighter-rouge&quot;&gt;globalCompositeOperation&lt;/code&gt; property, and can be used to clip backgrounds to specific shapes, such as text, thus creating the effect of texture-filled text in Canvas. I have written about this process in &lt;a href=&quot;http://tympanus.net/codrops/2013/12/02/techniques-for-creating-textured-text/&quot;&gt;this article&lt;/a&gt; over at Codrops.&lt;/p&gt;

&lt;p&gt;Together, Porter Duff Compositing and blending form the overall compositing operation for intersecting elements. According to the specification, “typically, the blending step is performed first, followed by the Porter-Duff compositing step. In the blending step, the resultant color from the mix of the element and the the backdrop is calculated. The graphic element’s color is replaced with this resultant color. The graphic element is then composited with the backdrop using the specified compositing operator.”&lt;/p&gt;

&lt;p&gt;Therefore, the way two intersecting or overlapping elements are handled is by blending their colors based on a blend mode, and then drawing only the parts specified by the composite operation.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;In CSS, we have no way to specify a composite operation. The default composite operation used is &lt;code class=&quot;highlighter-rouge&quot;&gt;source-over&lt;/code&gt;. Both the source and the destination elements remain, and the area where they intersect is blended using the blend mode specified.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Before the &lt;a href=&quot;http://www.w3.org/TR/compositing-1/&quot;&gt;Compositing and Blending specification&lt;/a&gt; was introduced, CSS allowed one type of composite operations: simple alpha compositing. This is what the &lt;code class=&quot;highlighter-rouge&quot;&gt;opacity&lt;/code&gt; property is for. By changing an element’s opacity, the browser makes it translucent so that the colors of its backdrop can show through.&lt;/p&gt;

&lt;p&gt;Today, two main properties exist that allow us to blend elements and backround images by specifying one of 16 available blend modes. These two properties are &lt;code class=&quot;highlighter-rouge&quot;&gt;background-blend-mode&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;mix-blend-mode&lt;/code&gt;. Let’s get to know each.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;background-blend-mode&quot;&gt;Blending Background Layers: The &lt;code&gt;background-blend-mode&lt;/code&gt; Property&lt;/h2&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;background-blend-mode&lt;/code&gt; property, as its name suggests, is used to specify a blend mode for an element’s background layer. A background layer can be an image, or the background color.&lt;/p&gt;

&lt;p&gt;In other words, &lt;code class=&quot;highlighter-rouge&quot;&gt;background-blend-mode&lt;/code&gt; allows you to blend together an element’s background image with the images and/or background color behind it.&lt;/p&gt;

&lt;p&gt;If the element has more than one background image, you can specify multiple blend modes—each blend mode will be used for a background image such that the first blend mode in the list corresponds to the first background image in the list of background images, and so on.&lt;/p&gt;

&lt;p&gt;Then, each background layer is blended with the element’s background layer that is below it and the element’s background color. Which means that, if you have two background images and a background color:&lt;/p&gt;

&lt;pre class=&quot;brush:css&quot;&gt;
background-image: url(first-image.png), url(second-image.png);
background-color: orange;
background-blend-mode: screen, multiply;
&lt;/pre&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;second-image.png&lt;/code&gt; background will blend with the background color using the &lt;code class=&quot;highlighter-rouge&quot;&gt;multiply&lt;/code&gt; mode, and then the &lt;code class=&quot;highlighter-rouge&quot;&gt;first-image.png&lt;/code&gt; background will blend with the second image and the background color using the &lt;code class=&quot;highlighter-rouge&quot;&gt;screen&lt;/code&gt; blend mode. (Reminder: the first background image in the list is the one on top, and the ones following it are beneath it.)&lt;/p&gt;

&lt;p&gt;Note that an element’s background layers must not blend with the content that is behind the element, instead they must act as if they are rendered into an &lt;strong&gt;isolated group&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Also note that if the &lt;code class=&quot;highlighter-rouge&quot;&gt;background&lt;/code&gt; shorthand is used, the &lt;code class=&quot;highlighter-rouge&quot;&gt;background-blend-mode&lt;/code&gt; property for that element must be reset to its initial value.&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;blend-modes&quot;&gt;The Blend Modes&lt;/h3&gt;

&lt;p&gt;Okay so we’ve established that each background layer can get its own blend mode which specifies how it blends with the layers beneath it. But what blend mode options do we have?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;There are 16 blend modes available in CSS&lt;/strong&gt;: &lt;code class=&quot;highlighter-rouge&quot;&gt;normal&lt;/code&gt; (which is the default blend mode and means that no blending is applied), &lt;code class=&quot;highlighter-rouge&quot;&gt;multiply&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;screen&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;overlay&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;darken&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;lighten&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;color-dodge&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;color-burn&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;hard-light&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;soft-light&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;difference&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;exclusion&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;hue&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;saturation&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;color&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;luminosity&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Each filter, when applied to an image, for example, will give a different end result—the colors of the image are going to be changed based on the mode you choose.&lt;/p&gt;

&lt;dl&gt;
&lt;dt&gt;&lt;code&gt;normal&lt;/code&gt;&lt;/dt&gt; 
&lt;dd&gt;This is the default mode which specifies no blending. The blending formula simply selects the source color.
&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;multiply&lt;/code&gt;&lt;/dt&gt; 
&lt;dd&gt;The source color is multiplied by the destination color and replaces the destination. The resultant color is always at least as dark as either the source or destination color. &lt;strong&gt;Multiplying any color with black results in black. Multiplying any color with white preserves the original color.&lt;/strong&gt;
&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;screen&lt;/code&gt;&lt;/dt&gt; 
&lt;dd&gt;Multiplies the complements of the backdrop and source color values, then complements the result. The result color is always at least as light as either of the two constituent colors. &lt;strong&gt;Screening any color with white produces white; screening with black leaves the original color unchanged.&lt;/strong&gt; The effect is similar to projecting multiple photographic slides simultaneously onto a single screen.
&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;overlay&lt;/code&gt;&lt;/dt&gt; 
&lt;dd&gt;Multiplies or screens the colors, depending on the backdrop color value. Source colors overlay the backdrop while preserving its highlights and shadows. The backdrop color is not replaced but is mixed with the source color to reflect the lightness or darkness of the backdrop.
&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;darken&lt;/code&gt;&lt;/dt&gt; 
&lt;dd&gt;Selects the darker of the backdrop and source colors. The backdrop is replaced with the source where the source is darker; otherwise, it is left unchanged.
&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;lighten&lt;/code&gt;&lt;/dt&gt; 
&lt;dd&gt;Selects the lighter of the backdrop and source colors. The backdrop is replaced with the source where the source is lighter; otherwise, it is left unchanged.
&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;color-dodge&lt;/code&gt;&lt;/dt&gt; 
&lt;dd&gt;Brightens the backdrop color to reflect the source color. Painting with black produces no changes.
&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;color-burn&lt;/code&gt;&lt;/dt&gt; 
&lt;dd&gt;Darkens the backdrop color to reflect the source color. Painting with white produces no change.
&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;hard-light&lt;/code&gt;&lt;/dt&gt; 
&lt;dd&gt;Multiplies or screens the colors, depending on the source color value. The effect is similar to shining a harsh spotlight on the backdrop.
&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;soft-light&lt;/code&gt;&lt;/dt&gt; 
&lt;dd&gt;Darkens or lightens the colors, depending on the source color value. The effect is similar to shining a diffused spotlight on the backdrop.
&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;difference&lt;/code&gt;&lt;/dt&gt; 
&lt;dd&gt;Subtracts the darker of the two constituent colors from the lighter color. Painting with white inverts the backdrop color; painting with black produces no change.
&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;exclusion&lt;/code&gt;&lt;/dt&gt; 
&lt;dd&gt;Produces an effect similar to that of the Difference mode but lower in contrast. Painting with white &lt;strong&gt;inverts the backdrop color&lt;/strong&gt;; painting with black produces no change.
&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;hue&lt;/code&gt;&lt;/dt&gt; 
&lt;dd&gt;Creates a color with the hue of the source color and the saturation and luminosity of the backdrop color.
&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;saturation&lt;/code&gt;&lt;/dt&gt; 
&lt;dd&gt;Creates a color with the saturation of the source color and the hue and luminosity of the backdrop color. Painting with this mode in an area of the backdrop that is a pure gray (no saturation) produces no change.
&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;color&lt;/code&gt;&lt;/dt&gt; 
&lt;dd&gt;Creates a color with the hue and saturation of the source color and the luminosity of the backdrop color. This preserves the gray levels of the backdrop and is useful for coloring monochrome images or tinting color images.
&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;luminosity&lt;/code&gt;&lt;/dt&gt; 
&lt;dd&gt;Creates a color with the luminosity of the source color and the hue and saturation of the backdrop color. This produces an inverse effect to that of the Color mode.
This mode is the one you can use to create monchrome &quot;tinted&quot; image effects like the ones you can see in different website headers.
&amp;lt;/dl&amp;gt;

The following image shows the result of applying the different blend modes to an image, in the same order mentioned above, starting from the top left corner.

&lt;img src=&quot;https://sarasoueidan.com/images/background-blend-mode-examples.png&quot; alt=&quot;The result of applying 16 blend modes applied to an image.&quot; /&gt;

For more information about these blend modes, I refer you to [this article](http://www.slrlounge.com/school/photoshop-blend-modes/) on the SLR Lounge blog. It claims to be the ultimate visual guide to blend modes, and does include some nice explanations of the blend modes.

Personally, I think that even with the definition for each mode at hand, it can be really hard (if not impossible) to predict the result of applying these modes to an image. 

Picking a suitable blend mode will be—in most cases—a case of trial and error. If you use Instagram you know that sometimes you just go over each of the available filters, applying them one after the other, till you get the effect you're after. (I know I do that!)
With CSS blend modes, it's practically the same. 

For that reason, I've created a simple interactive demo that you can use to pick the right blend mode for your effects.

&lt;figure&gt;
&lt;img src=&quot;https://sarasoueidan.com/images/css-blender-demo-screenshot.png&quot; alt=&quot;Screenshot of the CSS Blender demo.&quot; /&gt;
&lt;figcaption&gt;Screenshot of the CSS Blender demo.&lt;/figcaption&gt;
&lt;/figure&gt;

You can upload an image, and choose a background color to blend it with. The background color of the live preview (thumbnails) will live-update as you make your way around the color picker. **Clicking on a thumbnail will preview the selected blend mode in the larger preview area.**

&lt;a href=&quot;http://sarasoueidan.com/demos/css-blender&quot; class=&quot;button&quot;&gt;Try the blend modes in the demo out.&lt;/a&gt;

&lt;br /&gt;

Of course, the effects will be visible only in browsers that support the `background-blend-mode` property. **For more information about browser support, refer to [the compatibility table over at CanIUse.com](http://caniuse.com/#feat=css-backgroundblendmode).**


&lt;hr /&gt;

In addition to blending a background image with a background color in the interactive demo, you can also blend a piece of text with the element that has this background.

But blending separate elements together requires a property other than the `background-blend-mode` property. Let's have a look at that next.

&lt;h2 class=&quot;deeplink&quot; id=&quot;mix-blend-mode&quot;&gt;Blending An Element With Elements In Its Backdrop: The &lt;code&gt;mix-blend-mode&lt;/code&gt; Property&lt;/h2&gt; 

As we mentioned earlier, a **backdrop** is **the content behind the element** and is what the element is composited with. 

The content behind the element can be anything—including other elements. And this is where the interesting effects come in. Think fixed headers blending with the content as the page scrolls down, or text blended with an image in the background, or text blending with other text, etc.

Blending elements together is done using the `mix-blend-mode` property. 

The `mix-blend-mode` property is similar to the `background-blend-mode` property, and takes the same blend mode values. So, you can specify any blend mode to get different blending effects.

For example, the text in the following image blends with the image behind it using `mix-blend-mode: difference`, giving the illusion of the water bubbles passing through and in front of the text. The reason this effect is established is the color inversion process of the `difference` mode.

&lt;img src=&quot;https://sarasoueidan.com/images/mix-blend-mode-example-1.png&quot; alt=&quot;Example of text blending with an image.&quot; /&gt;

The area of the image where it overlaps with the text is the text's backdrop, and that is where the blending happens. You can check the live demo out [here](http://codepen.io/SaraSoueidan/pen/e90334f6ffdbb2a2cdd5604e769054e4?editors=110).

Using `mix-blend-mode`, you can create many creative effects—far more than I could list in this post. One particularly interesting effect you can create is see-through text. Without CSS blend modes, you would need CSS masking and/or background clipping, along with some CSS hackery to create this effect and make it work.

With blend modes, and using the `difference` blend mode again, you can create this effect by—again—leveraging the color inversion process.

The following image shows this effect in action. It is merely a piece of text, positioned on top of an image, and blended with it.

![Example of see-through text effect created using CSS blending.](https://sarasoueidan.com/images/see-through-text-mix-blend-mode.png)

That's pretty cool, isn't it? You can check the live demo out [here](http://codepen.io/SaraSoueidan/pen/887433527fac4e926e84b613be483bfc?editors=110).

It is worth noting here that the colors you choose strongly affect the end result. That being said, the interactive demo can make picking the right colors for such an effect easier and faster.

In the interactive demo, you have an option to add editable text to the preview area, and then style that text and blend it with the preview image using `mix-blend-mode`. The following is a screenshot showing an example.

&lt;figure&gt;
&lt;img src=&quot;https://sarasoueidan.com/images/css-blender-demo-screenshot-with-text.png&quot; alt=&quot;Screenshot of the CSS Blender demo.&quot; /&gt;
&lt;figcaption&gt;Screenshot of the CSS Blender demo, with a piece of text blended into the preview image.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;a href=&quot;http://sarasoueidan.com/demos/css-blender&quot; class=&quot;button&quot;&gt;Check the demo out.&lt;/a&gt;

&lt;hr /&gt;

Since we're talking about blending elements together, it only makes sense that we mention stacking contexts, especially considering that they affect how and what elements can be blended together.

According to the specification, applying a blend mode other than `normal` to the element must establish a new stacking context on that element, forming a _group_. This group must then be blended and composited with the stacking context that contains the element.

Also, an element that has blending applied, must blend with all the underlying content **_of the stacking context that that element belongs to_**. It will not blend with contents outside that context.  

For example, the following image shows the result of mix-blending two images with the `overlay` mode. ([Live demo](http://codepen.io/SaraSoueidan/pen/09efabde7114d37a736525b5ab616bc5?editors=110))

&lt;img src=&quot;https://sarasoueidan.com/images/mix-blend-mode-example-2.png&quot; alt=&quot;Example of text blending with an image.&quot; /&gt;

The code for the above simple example looks like so:

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;div class=&quot;container&quot;&amp;gt;
  &amp;lt;img src=&quot;path/to/destination.png&quot; alt=&quot;&quot; /&amp;gt;
  &amp;lt;div class=&quot;img-wrapper&quot;&amp;gt;
    &amp;lt;img src=&quot;path/to/source.png&quot; alt=&quot;&quot; class=&quot;source&quot;/&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/pre&gt;

I've wrapped the image on top (the `.source`) in a `div` that I'm going to create a stacking context on. Since the `opacity` property leads to the creation of a new stacking context when given a value other than th default (`1`), I'm going to use that.

&lt;pre class=&quot;brush:css&quot;&gt;
.img-wrapper {
  opacity: .99;
}
&lt;/pre&gt;

(Try it in the [live demo](http://codepen.io/SaraSoueidan/pen/09efabde7114d37a736525b5ab616bc5?editors=110).)

By creating a stacking context, the `.source` image no longer blends with the bottom image, because the latter lies outside the stacking context containing the former.

This is because we have __*isolated*__ the image (and any other contents of the `.img-wrapper` context) from the rest of the elements, and thus they don't blend with their backdrops anymore. 

This brings us to the `isolation` property. But before we move on, note that the `mix-blend-mode` property also applies to SVG elements, and can be used to blend overlapping SVG elements together as well. As a matter of fact, the logo for the CSS Blender demo is a result of applying a `mix-blend-mode` to the three squares that make the demo up. You can see how the areas where these squares overlap have different colors due to the color blending applied.

Browser support for the `mix-blend-mode` property is not as wide as that of the `background-blend-mode` property. For details, refer to [the browser compatibility table over at CanIUse.com](http://caniuse.com/#feat=css-mixblendmode).

&lt;h3 class=&quot;deeplink&quot; id=&quot;isolation&quot;&gt;Isolating Elements: The &lt;code&gt;isolation&lt;/code&gt; Property&lt;/h3&gt; 

When a property that leads to the creation of a stacking context is applied to an element, that element is said to be **isolated**. The last example in the previous section is an example of this happening.

On the other hand, you can use the `isolation` property to isolate elements.

In SVG, this property defines whether an element is isolated or not. For CSS, setting `isolation` to `isolate` will turn the element into a stacking context, and thus affect whether or not the element's contents can blend with their backdrop lying outside this context. By default, the `isolation` property is set to `auto`—which implies that they are not isolated.

If we were to go back to the previous example with the two blended images, we can prevent the image on top from blending with the image behind it by using the `isolation` property instead of the `opacity` property.

&lt;pre class=&quot;brush:css&quot;&gt;
.img-wrapper {
  isolation: isolate;
}
&lt;/pre&gt;

This has the same effect as using `opacity` in the previous example. If you use this rule instead of `opacity` in the live demo, you will get the same result.

Note that by creating a stacking context on an element, you are isolating the content of that element and preventing them from blending with the backdrop of that element. However, you can still apply a blend mode to the entire context to blend it with its backdrop.

Moreover, If you are using the`background-blend-mode` property, the `isolation` property is not needed since background layers must not blend with the content that is behind the element, instead they must act as if they are rendered into an isolated group (the element itself), as specified in the specification. This is why the `isolation` property will have an effect when used with the `mix-blend-mode` property, but not with the `background-blend-mode` property.


&lt;h2 class=&quot;deeplink&quot; id=&quot;order-of-operations&quot;&gt;Note: Order Of Graphical Operations&lt;/h2&gt; 

CSS blending modes, [filters](http://www.w3.org/TR/filter-effects-1/) and [masks](http://www.w3.org/TR/css-masking-1/), can all be applied to the same element. But which effect is applied first?

According to the specification, first any filter effect is applied, then any clipping, masking, blending and compositing.

&lt;h2 class=&quot;deeplink&quot; id=&quot;final-words&quot;&gt;Final Words&lt;/h2&gt; 

With all the graphical operations available to us via CSS, we are getting more possibilities for designing in the browsers—this is particularly interesting if you—like me—are not into graphics editors and don't know your way around them well.

The web platform team at Adobe have been doing a tremendous job bringing many of their tools' graphical capabilities to the web. From filters, to blend modes, clipping and masking, and even [CSS Shapes](http://sarasoueidan.com/blog/css-shapes), we are gaining more control over layout and graphics on the web.

Many creative effects can be created using CSS blend modes, and when combined with other technologies, they open a door to endless creative possibilities.

&lt;hr /&gt;

I hope you liked this article and found it useful.

Thank you for reading!
&lt;/dd&gt;&lt;/dl&gt;

    
  </content>
 </entry>
 
 <entry>
   <title>Useful SVGO[ptimization] Tools</title>
   <link href="http://sarasoueidan.com//blog/svgo-tools/"/>
   <id>https://sarasoueidan.com/blog/svgo-tools</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;One of the steps you need to do when working with SVG is optimizing the SVG code after exporting it from the editor and before embedding in on your web page. For that, several standalone optimization tools exits. The two tools I usually mention in my &lt;a href=&quot;http://www.smashingmagazine.com/2014/11/03/styling-and-animating-svgs-with-css/&quot;&gt;articles&lt;/a&gt; and &lt;a href=&quot;http://slides.com/sarasoueidan/working-with-svg-a-primer#/&quot;&gt;talks&lt;/a&gt; are &lt;a href=&quot;http://petercollingridge.appspot.com/svg-editor&quot;&gt;Peter Collingridge's online editor&lt;/a&gt;, and &lt;a href=&quot;https://github.com/svg/svgo&quot;&gt;SVGO&lt;/a&gt;. In this article, I'm going to introduce you to a new SVGO Tool that provides us with everything Peter's tool does, and a bit more.&lt;/p&gt;

&lt;p&gt;This is not to say that peter’s tool is no longer useful—it certainly is. But if you use SVGO, then you know how convenient it is with all the available tools it comes with.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;what-is-svgo&quot;&gt;What is SVGO?&lt;/h2&gt;

&lt;p&gt;For those of you who are not familiar with SVGO: it is a node-js based SVG optimization tool. (SVGO is an abbreviation for &lt;strong&gt;SVG Optimization&lt;/strong&gt;). It comes with a set of tools and plugins that make it a great tool that you can integrate into almost any kind of workflow. (We’ll go over these tools shortly.)&lt;/p&gt;

&lt;p&gt;However, SVGO has one disadvantage: it can easily break your SVG—especially if it is animated or scripted, the document structure will change and eventually break any animations or scripting applied.&lt;/p&gt;

&lt;p&gt;Even with the many SVGO tools and plugins available, unfortunately, we had no way to preview the result of applying SVGO optimizations to an SVG, to tell whether they will break it or not.. until now.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;introducing-svgomg&quot;&gt;Introducing SVGOMG&lt;/h2&gt;

&lt;p&gt;Last month, when I wrote the &lt;a href=&quot;http://calendar.perfplanet.com/2014/tips-for-optimising-svg-delivery-for-the-web/&quot;&gt;SVG performance article&lt;/a&gt; for Perf Calendar, Google’s &lt;a href=&quot;http://twitter.com/jaffathecake&quot;&gt;Jake Archibald&lt;/a&gt; tech-reviewed the article. And while discussing the code optimization section, I mentioned how SVGO lacks a GUI that allows us to preview the result of running the optimizations on our SVGs, and  how it should be used with caution because of that.&lt;/p&gt;

&lt;p&gt;A week or two later, Jake made the &lt;a href=&quot;http://jakearchibald.github.io/svgomg/&quot;&gt;SVGOMG GUI&lt;/a&gt;—an interface that allows you to optimize SVGs by selecting your optimizations, and getting a live preview of how your SVG looks with these optimizations applied.&lt;/p&gt;

&lt;figure&gt;
&lt;img src=&quot;https://sarasoueidan.com/images/svgomg.png&quot; alt=&quot;Screenshot of the SVGOMG GUI.&quot; /&gt;
&lt;figcaption&gt;Screenshot of the SVGOMG GUI.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;If you’ve used Peter’s tool before, you can expect the same from SVGOMG, and &lt;em&gt;more&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;You can upload an SVG file, paste SVG code in, or load the default demo SVG—if you’re just trying the app out. A set of options will then be revealed on the right side of the screen (see screenshot above). These options represent the optimizations built into SVGO. The SVGO optimizations are plugins that you can enable and disable as needed, and SVGOMG offers you a visual way of doing so.&lt;/p&gt;

&lt;p&gt;The live preview section will update as you choose your optimizations, allowing you to detect and disable any optimizations that would break your SVG.&lt;/p&gt;

&lt;p&gt;In the top right corner, you can see the current file size and the optimization percentage. You also have an option to show the original SVG—which will also display the original file size, to compare your it to your optimized version.&lt;/p&gt;

&lt;p&gt;SVGOMG is an online tool. But if you know Jake then you’ll probably expect it to also work offline, considering that he might just be the biggest proponent of ServiceWorker and offline-first out there. He has written about it a lot &lt;a href=&quot;http://jakearchibald.com/&quot;&gt;on his blog&lt;/a&gt;. Thus—expectedly—&lt;strong&gt;the GUI works offline too&lt;/strong&gt; in any browser that’s got ServiceWorker. (For an overview of the state of ServiceWorker, refer to &lt;a href=&quot;https://jakearchibald.github.io/isserviceworkerready/&quot;&gt;this browser compatibility page&lt;/a&gt;.) Once you’ve opened the app (try it in Chrome, for example), you will get a notification a couple of seconds later telling you that the GUI is ready to work offline.&lt;/p&gt;

&lt;figure&gt;
&lt;img src=&quot;https://sarasoueidan.com/images/svgomg-offline.png&quot; alt=&quot;Screenshot of SVGOMG showing the notification that the GUI works offline.&quot; /&gt;
&lt;figcaption&gt;Screenshot of SVGOMG showing the notification that the GUI works offline.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;&lt;em&gt;Very&lt;/em&gt; useful, isn’t it?&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;other-svgo-tools&quot;&gt;Other SVGO Tools&lt;/h2&gt;

&lt;p&gt;There are quite a few SVGO plugins available that can be used in a variety of ways, depending on your preferred workflow:&lt;/p&gt;

&lt;h4 class=&quot;deeplink&quot; id=&quot;adobe-illustrator-plugin&quot;&gt;Adobe Illustrator Plugin&lt;/h4&gt;

&lt;p&gt;A GUI allowing you to select the optimizations you want to apply also exists in another SVGO tool called &lt;a href=&quot;https://github.com/davidderaedt/SVG-NOW&quot;&gt;&lt;strong&gt;SVG NOW&lt;/strong&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://sarasoueidan.com/images/svgnow.png&quot; alt=&quot;SVG NOW plugin logo.&quot; /&gt;&lt;/p&gt;

&lt;p&gt;SVG NOW is an Illustrator plugin that brings SVGO’s optimizations into Illustrator. It is an alternative SVG exporter for AI, aimed at optimizing SVG files by post-processing the generated SVG code using SVGO. This is useful for sure, &lt;strong&gt;but SVG NOW does not show you a live preview of how the SVG is affected by the optimizations you choose&lt;/strong&gt;.&lt;/p&gt;

&lt;h4 class=&quot;deeplink&quot; id=&quot;inkscape-plugin&quot;&gt;Inkscape Plugin&lt;/h4&gt;

&lt;p&gt;Similar to SVG NOW, &lt;a href=&quot;https://github.com/juanfran/svgo-inkscape&quot;&gt;&lt;strong&gt;SVGO-Inkscape&lt;/strong&gt;&lt;/a&gt; is an Inkscape plugin that allows you to remove a lot of redundant and useless information such as editor metadata, comments, hidden elements, default or non-optimal values and other stuff generated by Inkscape and that can be safely removed or converted without affecting SVG rendering result.&lt;/p&gt;

&lt;h4 class=&quot;deeplink&quot; id=&quot;sketch-plugin&quot;&gt;Sketch Plugin&lt;/h4&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/BohemianCoding/svgo-compressor&quot;&gt;SVGO Compressor&lt;/a&gt; is the Sketch version of the SVGO plugin, which compresses SVG assets using SVGO, right when you export them. As with other SVGO tools, there’s no way of telling how the exported SVG will be affected by the optimizations applied.&lt;/p&gt;

&lt;p&gt;From my personal experience I’ve found that SVGs exported using Sketch were more likely to break when optimized with SVGO, because of how Sketch exports certain SVG shapes using clip paths and masks. So it’s useful to remember that you might need to re-export and re-optimize the SVG another way if and when this happens.&lt;/p&gt;

&lt;p&gt;Note that the plugin &lt;em&gt;requires&lt;/em&gt; Sketch 3.8, and so it won’t work with older versions of the application.&lt;/p&gt;

&lt;h4 class=&quot;deeplink&quot; id=&quot;drag-n-drop-gui&quot;&gt;Drag'n'Drop GUI&lt;/h4&gt;

&lt;p&gt;Another kind of GUI for SVGO exists — called &lt;a href=&quot;https://github.com/svg/svgo-gui&quot;&gt;&lt;strong&gt;SVGO GUI&lt;/strong&gt;&lt;/a&gt; — that allows you to drag-and-drop your SVGs and then optimizes those SVG on-the-fly, replacing your original ones with the optimized versions.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://sarasoueidan.com/images/svgogui.png&quot; alt=&quot;Screenshot of the SVGO GUI.&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This GUI is useful for quick bulk optimizations, but it risky considering that 1) you cannot preview the optimized SVG 2) your original SVGs are instantly replaced by the optimized versions, which means that your SVGs might break, and the broken ones will replace the original ones. If you’re going to use this GUI, make sure you have a backup before you optmize.&lt;/p&gt;

&lt;h4 class=&quot;deeplink&quot; id=&quot;grunt-and-gulp-plugins&quot;&gt;Grunt and Gulp Plugins&lt;/h4&gt;

&lt;p&gt;SVGO is probably mostly known for its &lt;a href=&quot;https://github.com/sindresorhus/grunt-svgmin&quot;&gt;&lt;strong&gt;Grunt plugin&lt;/strong&gt;&lt;/a&gt;, and its sister the &lt;a href=&quot;https://github.com/ben-eb/gulp-svgmin&quot;&gt;&lt;strong&gt;Gulp plugin&lt;/strong&gt;&lt;/a&gt;. Both of these plugins allow you to enable and disable SVGO optimizations as needed.
For example, a default SVGO optimization will remove the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; attribute from your SVG. This is something you should completely avoid, because, without the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; attribute, the SVG cannot be &lt;a href=&quot;http://tympanus.net/codrops/2014/08/19/making-svgs-responsive-with-css/&quot;&gt;made responsive&lt;/a&gt;. (For more information about the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; attribute, refer to my tutorial &lt;a href=&quot;http://sarasoueidan.com/blog/svg-coordinate-systems&quot;&gt;here&lt;/a&gt;.)&lt;/p&gt;

&lt;p&gt;When you set up SVGO in Grunt, for example, you can disable any plugins you want. For example, to disable removing the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt;, add &lt;code class=&quot;highlighter-rouge&quot;&gt;removeViewBox: false&lt;/code&gt; to the &lt;code class=&quot;highlighter-rouge&quot;&gt;Plugins&lt;/code&gt; object in your configuration file.&lt;/p&gt;

&lt;pre class=&quot;brush:js&quot;&gt;
// Source: https://github.com/sindresorhus/grunt-svgmin
grunt.initConfig({
    svgmin: {
        options: {
            plugins: [
                {
                    removeViewBox: false
                }, {
                    removeUselessStrokeAndFill: false
                }
            ]
        },
        dist: {
            files: {
                'dist/unicorn.svg': 'app/unicorn.svg'
            }
        }
    }
});
&lt;/pre&gt;

&lt;p&gt;You can refer to the plugin’s Github repo for more information.&lt;/p&gt;

&lt;h4 class=&quot;deeplink&quot; id=&quot;osx-folder-action&quot;&gt;OS X Folder Action&lt;/h4&gt;

&lt;p&gt;SVGO also comes with an &lt;a href=&quot;https://github.com/svg/svgo-osx-folder-action&quot;&gt;&lt;strong&gt;OS X folder action&lt;/strong&gt;&lt;/a&gt; that you can attach to any folder, and then have your SVGs optimized on-the-fly as soon as you place them in that folder. Again, optmized versions will replace the original ones, so be careful.&lt;/p&gt;

&lt;h2 id=&quot;final-words&quot;&gt;Final Words&lt;/h2&gt;

&lt;p&gt;No matter what your workflow is, SVGO can probably fit into it, one way or another.&lt;/p&gt;

&lt;p&gt;I’m sure other SVG optimization tools exits, but with all the options SVGO comes with, if you ask me, this is the tool I would recommend.&lt;/p&gt;

&lt;p&gt;Optimize those SVGs, build something beautiful, and share it with the world.&lt;/p&gt;

&lt;p&gt;Thank you for reading.&lt;/p&gt;


    
  </content>
 </entry>
 
 <entry>
   <title>Using The CSS :target Selector To Create JavaScript-less UI Effects</title>
   <link href="http://sarasoueidan.com//blog/css-target-uses/"/>
   <id>https://sarasoueidan.com/blog/css-target-uses</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;
	 You may or may not have used the :target selector before; and you may or may not have used it to show and hide elements without having to resort to JavaScript to handle this event for you. This article I wrote for the Adobe Dreamweaver team blog, serves as a short introduction to the :target selector, showing how you can use it to create JavaScript-less UI effects—for example, overlays, modals, etc.
&lt;/p&gt;

    

     <div>
        <p>
          <em>This article is <a href="http://blogs.adobe.com/dreamweaver/2015/01/using-the-css-target-selector-to-create-javascript-less-ui-effects.html">published @ <strong>Adobe Dreamweaver Team Blog</strong>.</a></em>
        </p>
    </div>
    
  </content>
 </entry>
 
 <entry>
   <title>Tips For Optimizing SVG Delivery For The Web</title>
   <link href="http://sarasoueidan.com//blog/svg-optimization/"/>
   <id>https://sarasoueidan.com/blog/svg-optimization</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;
	SVGs are a great asset in our responsive web design toolkit. But just like any other image format, there are certain steps you should take to make sure you’re delivering optimised resources that don’t have a negative impact on your page’s performance. Here are some things that you can do to make sure you’re delivering better SVGs for the web.
&lt;/p&gt;

    

     <div>
        <p>
          <em>This article is <a href="http://calendar.perfplanet.com/2014/tips-for-optimising-svg-delivery-for-the-web/">published @ <strong>Performance calendar</strong>.</a></em>
        </p>
    </div>
    
  </content>
 </entry>
 
 <entry>
   <title>An Overview Of SVG Sprite Creation Techniques</title>
   <link href="http://sarasoueidan.com//blog/overview-of-svg-sprite-creation-techniques/"/>
   <id>https://sarasoueidan.com/blog/overview-of-svg-sprite-creation-techniques</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;
	SVG can be used as an icon system to replace icon fonts, and there are several ways to create SVG sprites. This article I wrote for this year's 24Ways will give you an overview of three of them. While we’re at it, we’re going to take a look at some of the available tools used to automate sprite creation and fallback for us.
&lt;/p&gt;

    

     <div>
        <p>
          <em>This article is <a href="http://24ways.org/2014/an-overview-of-svg-sprite-creation-techniques/">published @ <strong>24 Ways</strong>.</a></em>
        </p>
    </div>
    
  </content>
 </entry>
 
 <entry>
   <title>Styling and Animating SVGs with CSS</title>
   <link href="http://sarasoueidan.com//blog/styling-and-animating-svgs-with-css/"/>
   <id>https://sarasoueidan.com/blog/styling-and-animating-svgs-with-css</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;
	CSS can be used to style and animate scalable vector graphics, much like it is used to style and animate HTML elements. In this article I wrote for Smashing Magazine, which is a modified transcript of a talk I recently gave at CSSconf EU and From the Front, I’ll go over the prerequisites and techniques for working with CSS in SVG.
&lt;/p&gt;

    

     <div>
        <p>
          <em>This article is <a href="http://www.smashingmagazine.com/2014/11/03/styling-and-animating-svgs-with-css/">published @ <strong>Smashing Magazine</strong>.</a></em>
        </p>
    </div>
    
  </content>
 </entry>
 
 <entry>
   <title>A Guide to SVG Animations (SMIL)</title>
   <link href="http://sarasoueidan.com//blog/guide-svg-animations-smil/"/>
   <id>https://sarasoueidan.com/blog/guide-svg-animations-smil</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;
	What the title says: a complete guide to SVG animations derived from the SMIl specification. The extensive guide features a lot of demos and goes over the animations syntax, covering almost everything you need to know to get started with SVG Animations today.
&lt;/p&gt;

    

     <div>
        <p>
          <em>This article is <a href="http://css-tricks.com/guide-svg-animations-smil/">published @ <strong>CSS-Tricks</strong>.</a></em>
        </p>
    </div>
    
  </content>
 </entry>
 
 <entry>
   <title>Making SVGs Responsive With CSS</title>
   <link href="http://sarasoueidan.com//blog/responsive-svgs/"/>
   <id>https://sarasoueidan.com/blog/responsive-svgs</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;
	An article on how to make embedded SVGs cross-browser responsive. We're going to cover embedding techniques, how to apply the &quot;Padding Hack&quot; and how to use inline media queries to make SVGs adaptive.
&lt;/p&gt;

    

     <div>
        <p>
          <em>This article is <a href="http://tympanus.net/codrops/2014/08/19/making-svgs-responsive-with-css/">published @ <strong>Codrops</strong>.</a></em>
        </p>
    </div>
    
  </content>
 </entry>
 
 <entry>
   <title>Understanding SVG Coordinate Systems and Transformations (Part 3) — Establishing New Viewports</title>
   <link href="http://sarasoueidan.com//blog/nesting-svgs/"/>
   <id>https://sarasoueidan.com/blog/nesting-svgs</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;
	 At any point in an SVG drawing, you can establish new viewports and user coordinate systems by either nesting &lt;code&gt;&amp;lt;svg&amp;gt;&lt;/code&gt;s or using elements such as the &lt;code&gt;&amp;lt;symbol&amp;gt;&lt;/code&gt; element, among others. In this article we're going to have a look at how we can do that and how this can be useful for controlling SVG elements and making them more flexible (and/or fluid).
&lt;/p&gt;

&lt;p&gt;This is the third and last article in a series of articles about SVG coordinate systems and transformations. In the first article, I covered everything you need to know to understand the basics of SVG coordinate systems; more specifically, the SVG viewport, and the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectRatio&lt;/code&gt; attributes. In the second article, you can find everything you need to know about how SVG system transformations work.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://sarasoueidan.com/blog/svg-coordinate-systems&quot;&gt;Understanding SVG Coordinate Systems &amp;amp; Transformations (Part 1) – The viewport, &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt;, and &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectRatio&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://sarasoueidan.com/blog/svg-transformations&quot;&gt;Understanding SVG Coordinate Systems &amp;amp; Transformations (Part 2) – The &lt;code&gt;transform&lt;/code&gt; Attribute&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Understanding SVG Coordinate Systems &amp;amp; Transformations (Part 3) – Establishing New Viewports&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Throughout this article, &lt;strong&gt;I’m going to assume that you read the &lt;u&gt;first&lt;/u&gt; part of this series about SVG viewports and the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectRatio&lt;/code&gt; attributes&lt;/strong&gt;. You don’t need to have read the second one about coordinate system transformations to follow along this article.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;nesting-svgs&quot;&gt; Nesting &lt;code&gt;&amp;lt;svg&amp;gt;&amp;gt;&lt;/code&gt; Elements&lt;/h2&gt;

&lt;p&gt;In the &lt;a href=&quot;https://sarasoueidan.com/blog/svg-coordinate-systems&quot;&gt;first part&lt;/a&gt; we talked about how the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; element establishes a viewport for the content of the SVG canvas. At any point in an SVG drawing, you can establish a new viewport into which all contained graphics is drawn by including an &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; element inside another &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt;. By establishing a new viewport, you also implicitly establish a new viewport coordinate system and a new user coordinate system.&lt;/p&gt;

&lt;p&gt;For example, suppose you have an &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; and some content inside it:&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&amp;gt;
	&amp;lt;!-- some SVG content --&amp;gt;
    &amp;lt;svg&amp;gt;
    	&amp;lt;!-- some inner SVG content --&amp;gt;
    &amp;lt;/svg&amp;gt;
&amp;lt;svg&amp;gt;
&lt;/pre&gt;

&lt;p&gt;The first thing to note here is that the inner &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; element does not require specifying a namespace &lt;code class=&quot;highlighter-rouge&quot;&gt;xmlns&lt;/code&gt; on it because it is assumed to be the same namespace as the outer &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt;’s namespace. Of course, even the outer &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; does not require a namespace if it is embedded inline in an HTML5 document.&lt;/p&gt;

&lt;p&gt;You can use a nested SVG to group elements together and then position them inside the parent SVG. Now, you can also group elements together and position them using the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;g&amp;gt;&lt;/code&gt; group—by wrapping elements inside a &lt;a href=&quot;https://sarasoueidan.com/blog/structuring-grouping-referencing-in-svg&quot;&gt;group &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;g&amp;gt;&lt;/code&gt; element&lt;/a&gt;, you can position them on the canvas by &lt;a href=&quot;https://sarasoueidan.com/blog/svg-transformations&quot;&gt;using the &lt;code class=&quot;highlighter-rouge&quot;&gt;transform&lt;/code&gt; attribute&lt;/a&gt;. However, an &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; element has certain advantages over using &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;g&amp;gt;&lt;/code&gt;. Positioning using &lt;code class=&quot;highlighter-rouge&quot;&gt;x&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;y&lt;/code&gt; coordinates is, in most cases, more convenient than using transforms. Moreover, an &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; element accepts &lt;code class=&quot;highlighter-rouge&quot;&gt;width&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;height&lt;/code&gt; attributes, which the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;g&amp;gt;&lt;/code&gt; element doesn’t. That said, the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; may not always be needed or necessary, because it leads to the creation of a new viewport and coordinate systems, which you may not need or want.&lt;/p&gt;

&lt;p&gt;By specifying a width and height to the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt;, you restrict the content inside it to the bounds of the viewport that is defined by the &lt;code class=&quot;highlighter-rouge&quot;&gt;x&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;y&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;width&lt;/code&gt;, and &lt;code class=&quot;highlighter-rouge&quot;&gt;height&lt;/code&gt; attributes. Any content that lies beyond these bounds will be clipped.&lt;/p&gt;

&lt;p&gt;If you don’t specify &lt;code class=&quot;highlighter-rouge&quot;&gt;x&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;y&lt;/code&gt; attributes, they’re assumed to be zero. If you don’t specify &lt;code class=&quot;highlighter-rouge&quot;&gt;height&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;width&lt;/code&gt; attributes, the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; will be 100% the width and height of its parent SVG.&lt;/p&gt;

&lt;p&gt;Moreover, specifying a user coordinate system other than the default one will also affect the content inside the inner &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt;, too.&lt;/p&gt;

&lt;p&gt;Percentage values specified for elements inside the inner &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; will be calculated relative to it, not relative to the outer &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt;. Percentage values specified on the inner itself &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; will be calculated relative to the outer &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt;. For example, the following will result in the inner SVG being equal to 400 units, and the rectangle inside it will be 200 units:&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;svg width=&quot;800&quot; height=&quot;600&quot;&amp;gt;
	&amp;lt;svg width=&quot;50%&quot; ..&amp;gt;
		&amp;lt;rect width=&quot;50%&quot; ... /&amp;gt;
	&amp;lt;/svg&amp;gt;
&amp;lt;/svg&amp;gt;
&lt;/pre&gt;

&lt;p&gt;If the width of the outermost &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; is set to 100% (for example, if it is embedded inline in a document and you want it to be fluid), then the inner SVG will expand and shrink as necessary to maintain a width that is half of that of the outer SVG – this is powerful.&lt;/p&gt;

&lt;p&gt;Nested SVGs are particularly useful for adding flexibility and fluidness to elements on the SVG canvas. We know that, using &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; values and &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectRatio&lt;/code&gt;, we can already create responsive SVGs. The outermost &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt;’s with can be set to 100% to make sure it expands and shrinks as its container (or the page) grows or shrinks. Then, by using &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; values and &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectRatio&lt;/code&gt;, we can make sure that the SVG canvas also responds to the changes in the viewport (outermost &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt;). I’ve written about responsifying SVGs in my &lt;a href=&quot;https://docs.google.com/presentation/d/1Iuvf3saPCJepVJBDNNDSmSsA0_rwtRYehSmmSSLYFVQ/pub?start=false&amp;amp;loop=false&amp;amp;delayms=3000#slide=id.p&quot;&gt;CSSConf talk slides&lt;/a&gt;. You can check the technique out &lt;a href=&quot;https://docs.google.com/presentation/d/1Iuvf3saPCJepVJBDNNDSmSsA0_rwtRYehSmmSSLYFVQ/pub?start=false&amp;amp;loop=false&amp;amp;delayms=3000#slide=id.g180585666_036&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;However, when we do responsify an SVG like that, the entire canvas with &lt;em&gt;all&lt;/em&gt; the elements drawn on it will respond and change simultaneously. But sometimes, you may want to have only one element inside the graphic to be flexible, while keeping others “fixed” in position and/or size. This is where nested &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt;s can be useful.&lt;/p&gt;

&lt;p&gt;An &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt; element can have its own coordinate system independent of its parent, and it can have its own &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectRatio&lt;/code&gt; attributes that allow you to size and position the content inside it any way you want.&lt;/p&gt;

&lt;p&gt;So, in order to make one element flexible, we can wrap it in an &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; element, and give that &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt; a flexible width so that it adjusts to the width of the outermost SVG, and then specify &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectRatio=&quot;none&quot;&lt;/code&gt; so that the graphic inside it also stretches and shrinks with the container width. (Note that &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt;s can be nested to many levels, but in order to keep things simple, I’m nesting only one level deep in this article.)&lt;/p&gt;

&lt;p&gt;To demonstrate how nested &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt;s can be useful, let’s look at some examples.&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;example&quot;&gt;Example&lt;/h3&gt;

&lt;p&gt;Suppose we have the following SVG:&lt;/p&gt;

&lt;figure&gt;
    &lt;img src=&quot;https://sarasoueidan.com/images/svg-nesting-example-1.png&quot; alt=&quot;&quot; /&gt;
&lt;/figure&gt;

&lt;p&gt;The above SVG is responsive. Resizing the screen will result in the entire SVG graphic responding as necessary. The following screenshot shows the result of shrinking the page, and how the SVG becomes smaller. Notice how &lt;strong&gt;the contents of the SVG maintain all their initial positions with respect to the SVG viewport and with respect to each other&lt;/strong&gt;.&lt;/p&gt;

&lt;figure&gt;
    &lt;img src=&quot;https://sarasoueidan.com/images/svg-nesting-example-1-resized.png&quot; alt=&quot;&quot; /&gt;
&lt;/figure&gt;

&lt;p&gt;Using nested SVGs, we’re going to change that. We can specify a position for individual elements inside the SVG relative to the SVG’s viewport, so that as the SVG viewport size changes (i.e the size of the outermost &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt; changes), the elements respond independently of each other.&lt;/p&gt;

&lt;p class=&quot;note&quot;&gt;
    Note that, at this point, it is necessary that you be familiar with how the SVG viewport, `viewBox`, and `preserveAspectRatio` work.
&lt;/p&gt;

&lt;p&gt;We’re going to create an effect such that, when the screen is resized, the upper part of the egg is going to be moved so that the cute chicken inside it peeks out, as shown in the following image:&lt;/p&gt;

&lt;figure&gt;
    &lt;img src=&quot;https://sarasoueidan.com/images/svg-nesting-example-1-new.png&quot; alt=&quot;&quot; /&gt;
&lt;/figure&gt;

&lt;p&gt;In order to get that effect, the egg’s upper part has to be separated from the rest by wrapping it inside an &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt; of its own. This &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt; wrapper will get an ID &lt;code class=&quot;highlighter-rouge&quot;&gt;upper-shell&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Then, we’re going to make sure the new &lt;code class=&quot;highlighter-rouge&quot;&gt;svg#upper-shell&lt;/code&gt; has the same height and width as the outer SVG. This can be achieved by either specifying &lt;code class=&quot;highlighter-rouge&quot;&gt;width=&quot;100%&quot; height=&quot;100%&quot;&lt;/code&gt; on the &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt;, or by not specifying any height and width at all. If no width and height are specified on the inner SVG, it automatically expands to 100% the width and height of the outer SVG.&lt;/p&gt;

&lt;blockquote class=&quot;pull-quote&quot;&gt;
    If no width and height are specified on the inner SVG, it automatically expands to 100% the width and height of the outer SVG.
&lt;/blockquote&gt;

&lt;p&gt;And finally, to make sure the upper shell is “lifted” up or positioned at the top center of the &lt;code class=&quot;highlighter-rouge&quot;&gt;svg#upper-shell&lt;/code&gt;, we’re going to use the appropriate &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectRatio&lt;/code&gt; value which makes sure the viewBox is positioned at the top center of the viewport—the value is &lt;code class=&quot;highlighter-rouge&quot;&gt;xMidYMin&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The code for the SVG graphic becomes:&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;svg version=&quot;1.1&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&amp;gt;
    &amp;lt;!-- ... --&amp;gt;
    &amp;lt;svg viewBox=&quot;0 0 315 385&quot; preserveAspectRatio=&quot;xMidYMid meet&quot;&amp;gt;
        &amp;lt;!-- the chicken illustration --&amp;gt;
        &amp;lt;g id=&quot;chicken&quot;&amp;gt;
            &amp;lt;!-- ... --&amp;gt;
        &amp;lt;/g&amp;gt;
        &amp;lt;!-- path forming the lower shell --&amp;gt;
        &amp;lt;path id=&quot;lower-shell&quot; fill=&quot;url(#gradient)&quot; stroke=&quot;#000000&quot; stroke-width=&quot;1.5003&quot; d=&quot;...&quot;/&amp;gt;
    &amp;lt;/svg&amp;gt;

    &amp;lt;svg id=&quot;upper-shell&quot; viewBox=&quot;0 0 315 385&quot; preserveAspectRatio=&quot;xMidYMin meet&quot;&amp;gt;
        &amp;lt;!-- path forming the upper shell --&amp;gt;
        &amp;lt;path id=&quot;the-upper-shell&quot; fill=&quot;url(#gradient)&quot; stroke=&quot;#000000&quot; stroke-width=&quot;1.5003&quot; d=&quot;...&quot;/&amp;gt;
    &amp;lt;/svg&amp;gt;
&amp;lt;/svg&amp;gt;
&lt;/pre&gt;

&lt;p class=&quot;note&quot;&gt;I've stripped out the parts that relevant to the article like the gradient used to color the egg shells and the paths forming the shapes, just for the sake of brevity in the example code.&lt;/p&gt;

&lt;p&gt;At this point, note that the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; specified on the nested &lt;code class=&quot;highlighter-rouge&quot;&gt;svg#upper-shell&lt;/code&gt; has the same value as that of the outermost &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt; (before it was removed). The reason we used the same &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; value is so that, the SVG maintains its original look on big screens.&lt;/p&gt;

&lt;p&gt;So, the way this goes is: we start with an SVG—in our case, it’s the image of a cracked egg with a chicken hidden inside it.
Then, we create another “layer” and promote the upper shell to it—this layer is created by using a nested &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt;.
The nested &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt; has the same dimensions as the outer &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt; and the same &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt;.
And finally, the viewBox of the inner SVG is set to “stick” to the top of the viewport no matter what the screen size is—this makes sure that, when the screen size is narrow and the SVG is elongated, the upper shell will be lifted upwards, thus showing the chicken “behind” it on the canvas.&lt;/p&gt;

&lt;figure&gt;
    &lt;img src=&quot;https://sarasoueidan.com/images/svg-nesting-example-1-layered.png&quot; alt=&quot;&quot; /&gt;
&lt;/figure&gt;

&lt;p&gt;Once the screen size shrinks, the SVG is elongated, and the viewBox containing the upper shell is positioned at the top of the viewport using &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectratio=&quot;xMidYMin meet&quot;&lt;/code&gt;.&lt;/p&gt;

&lt;figure&gt;
    &lt;img src=&quot;https://sarasoueidan.com/images/svg-nesting-example-1-viewbox.png&quot; alt=&quot;&quot; /&gt;
    &lt;figcaption&gt;
        The translucent purple layer shows the viewport established by the nested &lt;code&gt;svg&lt;/code&gt;. The translucent orange box shows the &lt;code&gt;viewBox&lt;/code&gt; inside the &lt;code&gt;svg&lt;/code&gt;&amp;mdash;it is positioned at the top center of the viewport using &lt;code&gt;preserveAspectratio=&quot;xMidYMin meet&quot;&lt;/code&gt;.
    &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Click on the following button to see the live SVG. Remember to resize your browser to see the SVG adapt.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://sarasoueidan.com/images/svg-nesting-chick.svg&quot; class=&quot;button&quot;&gt;View Live Example&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Nesting or “layering” SVGs allows you to position parts of the SVG relative to the changing viewport, while maintaining the elements’ aspect ratio. So the image adapts without distorting the elements inside it.&lt;/p&gt;

&lt;p&gt;If we wanted the entire egg to come off the chicken, we could always wrap the lower shell in an &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt; layer of its own, having the same &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt;, too. Then, to make sure the lower shell moves down and sticks to the bottom center of the viewport, we position it using &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectRatio=&quot;xMidYMax meet&quot;&lt;/code&gt;. The code would look like this:&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;svg version=&quot;1.1&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&amp;gt;
    &amp;lt;svg id=&quot;chick&quot; viewBox=&quot;0 0 315 385&quot; preserveAspectRatio=&quot;xMidYMid meet&quot;&amp;gt;
        &amp;lt;!-- the chicken illustration --&amp;gt;
        &amp;lt;g id=&quot;chick&quot;&amp;gt;
            &amp;lt;!-- ... --&amp;gt;
        &amp;lt;/g&amp;gt;
    &amp;lt;/svg&amp;gt;

    &amp;lt;svg id=&quot;upper-shell&quot; viewBox=&quot;0 0 315 385&quot; preserveAspectRatio=&quot;xMidYMid meet&quot;&amp;gt;
        &amp;lt;!-- path forming the upper shell --&amp;gt;
        &amp;lt;path id=&quot;the-upper-shell&quot; fill=&quot;url(#gradient)&quot; stroke=&quot;#000000&quot; stroke-width=&quot;1.5003&quot; d=&quot;...&quot;/&amp;gt;
    &amp;lt;/svg&amp;gt;

    &amp;lt;svg id=&quot;lower-shell&quot; viewBox=&quot;0 0 315 385&quot; preserveAspectRatio=&quot;xMidYMax meet&quot;&amp;gt;
        &amp;lt;!-- path forming the lower shell --&amp;gt;
        &amp;lt;path id=&quot;the-lower-shell&quot; fill=&quot;url(#gradient)&quot; stroke=&quot;#000000&quot; stroke-width=&quot;1.5003&quot; d=&quot;...&quot;/&amp;gt;
    &amp;lt;/svg&amp;gt;
&amp;lt;/svg&amp;gt;
&lt;/pre&gt;

&lt;p&gt;Each of the &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt; layers/viewports is equal to 100% the width and height of the outermost &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt;. So we basically have three clones. Each layer contains an element—the upper shell, the lower shell, or the chick. The &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; for the three layers is the same, with only the &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectRatio&lt;/code&gt; being different.&lt;/p&gt;

&lt;figure&gt;
    &lt;img src=&quot;https://sarasoueidan.com/images/svg-nesting-example-1-2.png&quot; alt=&quot;&quot; /&gt;
    &lt;figcaption&gt;
        The three &lt;code&gt;svg&lt;/code&gt;s have a 100% height and width value covering the entire outermost viewport. Their &lt;code&gt;viewBox&lt;/code&gt;es are also equal as you can see in the image. Only the position of these &lt;code&gt;viewBox&lt;/code&gt;es is different (specified using &lt;code&gt;preserveAspectRatio&lt;/code&gt;).
    &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Of course, in this example I started with a graphic of a chicken hiding inside an egg, and that is revealed when the screen becomes smaller. However, you could do something different: you could start with a graphic on a small screen, that then reveals something new as the screen becomes bigger; i.e as the &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt; becomes wider and there is more horizontal space to show elements.&lt;/p&gt;

&lt;p&gt;You could get a lot more creative, and show and hide elements according to different screen sizes—using media queries—and have the new elements be positioned in a certain way to achieve a certain effect. The sky is the limit here.&lt;/p&gt;

&lt;p&gt;Also note that the nested &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt;s don’t need to have the same height and width as their containing &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt;; you can specify a height and width and have the content of the &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt; be limited to and clipped by the boundaries specified by that height and width—it all boils down to what you’re trying to achieve.&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;making-elements-fluid&quot;&gt;Making Elements Fluid Using Nested SVGs&lt;/h3&gt;

&lt;p&gt;In addition to positioning elements while preserving their aspect ratios, we can use nested &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt;s allow only certain elements to be fluid—this can be done by &lt;em&gt;not&lt;/em&gt; preserving the aspect ratio of these particular elements.&lt;/p&gt;

&lt;p&gt;For example, if you want only one element in the SVG to be fluid, you can wrap it in an &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt;, and use &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectRatio=&quot;none&quot;&lt;/code&gt; to have that element expand to fill the entire width of the viewport at all times, while maintaining the aspect ratio and positioning of other elements like we did in the previous example.&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;svg&amp;gt;
    &amp;lt;!-- ... --&amp;gt;
    &amp;lt;svg viewBox=&quot;..&quot; preserveAspectRatio=&quot;none&quot;&amp;gt;
        &amp;lt;!-- this content will be fluid --&amp;gt;
    &amp;lt;/svg&amp;gt;
    &amp;lt;svg viewBox=&quot;..&quot; preserveAspectRatio=&quot;..&quot;&amp;gt;
        &amp;lt;!-- content positioned somewhere in the viewport --&amp;gt;
    &amp;lt;/svg&amp;gt;
    &amp;lt;!-- ... --&amp;gt;
&amp;lt;/svg&amp;gt;
&lt;/pre&gt;

&lt;p&gt;&lt;a href=&quot;http://jakearchibald.com&quot;&gt;Jake Archibald&lt;/a&gt; created a simple and practical use case for nested SVGs that does exactly that: a simple UI that contains elements positioned at the corners of the outermost &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt;, maintaining their aspect ratios, and a middle part of the UI is fluid and responds to the change in the &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt; width by shrinking and expanding with it. You can check his demo out &lt;a href=&quot;https://jsbin.com/loceqo/1&quot;&gt;here&lt;/a&gt;. Make sure you inspect the code in the dev tools to select and visualize the different &lt;code class=&quot;highlighter-rouge&quot;&gt;viewbox&lt;/code&gt;es and &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt;s used.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;establishing-viewports&quot;&gt;Other ways to establish new viewports&lt;/h2&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt; elements are not the only elements that establish new viewports in SVG. In the following sections, we’re going to go over the other ways to establish new viewports using other SVG elements.&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;using-symbol&quot;&gt;Establishing a new viewport by &lt;code&gt;&amp;lt;use&amp;gt;&lt;/code&gt;ing &lt;code&gt;&amp;lt;symbol&amp;gt;&lt;/code&gt;&lt;/h3&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;symbol&lt;/code&gt; element defines a new viewport whenever it is instantiated by the &lt;code class=&quot;highlighter-rouge&quot;&gt;use&lt;/code&gt; element.&lt;/p&gt;

&lt;p&gt;A &lt;code class=&quot;highlighter-rouge&quot;&gt;symbol&lt;/code&gt; element can be &lt;code class=&quot;highlighter-rouge&quot;&gt;use&lt;/code&gt;d by referencing it in the &lt;code class=&quot;highlighter-rouge&quot;&gt;xlink:href&lt;/code&gt; attribute of the &lt;code class=&quot;highlighter-rouge&quot;&gt;use&lt;/code&gt; element:&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;svg&amp;gt;
    &amp;lt;symbol id=&quot;my-symbol&quot; viewBox=&quot;0 0 300 200&quot;&amp;gt;
        &amp;lt;!-- contents of the symbol --&amp;gt;
        &amp;lt;!-- this content is only rendered when `use`d --&amp;gt;
    &amp;lt;/symbol&amp;gt;
    &amp;lt;use xlink:href=&quot;#my-symbol&quot; x=&quot;?&quot; y=&quot;?&quot; width=&quot;?&quot; height=&quot;?&quot;&amp;gt;
&amp;lt;/svg&amp;gt;
&lt;/pre&gt;

&lt;p&gt;The question marks used as values above are used only to indicate that these values may or  may not be specified—if &lt;code class=&quot;highlighter-rouge&quot;&gt;x&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;y&lt;/code&gt; are not specified, they default to zero, and you don’t need to specify a height and width either.&lt;/p&gt;

&lt;p&gt;You see, when you &lt;code class=&quot;highlighter-rouge&quot;&gt;use&lt;/code&gt; a &lt;code class=&quot;highlighter-rouge&quot;&gt;symbol&lt;/code&gt; element, and then inspect the DOM using the dev tools, you will not see the contents of the &lt;code class=&quot;highlighter-rouge&quot;&gt;symbol&lt;/code&gt; inside the &lt;code class=&quot;highlighter-rouge&quot;&gt;use&lt;/code&gt; tag. The reason for this is that the contents of &lt;code class=&quot;highlighter-rouge&quot;&gt;use&lt;/code&gt; are rendered into a shadow tree, which you can inspect if you enable inspecting the shadow DOM in the dev tools.&lt;/p&gt;

&lt;p&gt;When the &lt;code class=&quot;highlighter-rouge&quot;&gt;symbol&lt;/code&gt; is used, it is deeply cloned into the generated shadow tree, with the exception that the &lt;code class=&quot;highlighter-rouge&quot;&gt;symbol&lt;/code&gt; is replaced by an &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt;. This generated &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt; will always have explicit values for attributes &lt;code class=&quot;highlighter-rouge&quot;&gt;width&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;height&lt;/code&gt;. If attributes &lt;code class=&quot;highlighter-rouge&quot;&gt;width&lt;/code&gt; and/or &lt;code class=&quot;highlighter-rouge&quot;&gt;height&lt;/code&gt; are provided on the &lt;code class=&quot;highlighter-rouge&quot;&gt;use&lt;/code&gt; element, then these attributes will be transferred to the generated &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt;. If attributes &lt;code class=&quot;highlighter-rouge&quot;&gt;width&lt;/code&gt; and/or &lt;code class=&quot;highlighter-rouge&quot;&gt;height&lt;/code&gt; are not specified, the generated &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt; element will use values of 100% for these attributes.&lt;/p&gt;

&lt;blockquote class=&quot;pull-quote&quot;&gt;
    When the &lt;code&gt;symbol&lt;/code&gt; is used, it is deeply cloned into the generated shadow tree, with the exception that the &lt;code&gt;symbol&lt;/code&gt; is replaced by an &lt;code&gt;svg&lt;/code&gt;.
&lt;/blockquote&gt;

&lt;p&gt;Because we end up with an &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt; in the DOM, and because this &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt; is practically contained in the outer &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt; where &lt;code class=&quot;highlighter-rouge&quot;&gt;use&lt;/code&gt; is used, we’re left with a nested &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt; situation not very different from the one we talked about in the previous section—the nested &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt; forms a new viewport. The &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; for the nested &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt; is the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; specified on the &lt;code class=&quot;highlighter-rouge&quot;&gt;symbol&lt;/code&gt; element. (The &lt;code class=&quot;highlighter-rouge&quot;&gt;symbol&lt;/code&gt; element accepts a &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; attribute value. For more information, refer to the article: &lt;a href=&quot;https://sarasoueidan.com/blog/structuring-grouping-referencing-in-svg/&quot;&gt;Structuring, Grouping, and Referencing in SVG – The &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;g&amp;gt;&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;use&amp;gt;&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;defs&amp;gt;&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;symbol&amp;gt;&lt;/code&gt; Elements&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;So we now have a new viewport whose dimensions and position can be specified in the &lt;code class=&quot;highlighter-rouge&quot;&gt;use&lt;/code&gt; element (&lt;code class=&quot;highlighter-rouge&quot;&gt;x&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;y&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;width&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;height&lt;/code&gt;), and whose &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; value can also be specified in the &lt;code class=&quot;highlighter-rouge&quot;&gt;symbol&lt;/code&gt; element. The content of the &lt;code class=&quot;highlighter-rouge&quot;&gt;symbol&lt;/code&gt; is then rendered and positioned inside this viewport and viewBox.&lt;/p&gt;

&lt;p&gt;And last but not least, the &lt;code class=&quot;highlighter-rouge&quot;&gt;symbol&lt;/code&gt; element also accepts a &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectratio&lt;/code&gt; attribute value, that allows you to position the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; inside the new viewport established by &lt;code class=&quot;highlighter-rouge&quot;&gt;use&lt;/code&gt;. Pretty neat, right? You can control the newly created nested &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt; just like we did in the previous sections.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://eleqtriq.com&quot;&gt;Dirk Weber&lt;/a&gt; has also created a demo that uses nested SVGs and &lt;code class=&quot;highlighter-rouge&quot;&gt;symbol&lt;/code&gt; elements to mimic the behavior of CSS border images. You can check his article out &lt;a href=&quot;http://w3.eleqtriq.com/2014/02/the-4-slice-scaling-technique-for-svg/&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;referencing-svg-using-image&quot;&gt;Establishing a new viewport by referencing an SVG image in &lt;code&gt;&amp;lt;image&amp;gt;&lt;/code&gt;&lt;/h3&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;image&lt;/code&gt; element indicates that the contents of a complete file are to be rendered into a given rectangle within the current user coordinate system. The &lt;code class=&quot;highlighter-rouge&quot;&gt;image&lt;/code&gt; element can refer to raster image files such as PNG or JPEG or to files with MIME type of “image/svg+xml”.&lt;/p&gt;

&lt;p&gt;An &lt;code class=&quot;highlighter-rouge&quot;&gt;image&lt;/code&gt; element that references an SVG file will result in the establishment of a temporary new viewport since the referenced resource by definition will have an &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt; element.&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;image xlink:href=&quot;myGraphic.svg&quot; x=&quot;?&quot; y=&quot;?&quot; width=&quot;?&quot; height=&quot;?&quot; preserveAspectRatio=&quot;?&quot; /&amp;gt;
&lt;/pre&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;image&amp;gt;&lt;/code&gt; element accepts many attributes, some of these attributes—the ones that are relevant to this article—are &lt;code class=&quot;highlighter-rouge&quot;&gt;x&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;y&lt;/code&gt; position attributes, &lt;code class=&quot;highlighter-rouge&quot;&gt;width&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;height&lt;/code&gt; attributes, and &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectratio&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Normally, an SVG file will contain a root &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; element; this element may or may not have position and dimensions specified, in addition to a &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; and a &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectratio&lt;/code&gt; value.&lt;/p&gt;

&lt;p&gt;When an &lt;code class=&quot;highlighter-rouge&quot;&gt;image&lt;/code&gt; element references an SVG image file, the &lt;code class=&quot;highlighter-rouge&quot;&gt;x&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;y&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;width&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;height&lt;/code&gt; attributes on the root &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt; are ignored. Unless the value of &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectRatio&lt;/code&gt; on the &lt;code class=&quot;highlighter-rouge&quot;&gt;image&lt;/code&gt; element starts with ‘defer’, the &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectRatio&lt;/code&gt; attribute on the root element in the referenced SVG image is also ignored. Instead, the &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectRatio&lt;/code&gt; attribute on the referencing &lt;code class=&quot;highlighter-rouge&quot;&gt;image&lt;/code&gt; element defines how the SVG image content is fitted into the viewport.&lt;/p&gt;

&lt;p&gt;The value of the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; attribute to use when evaluating the &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectRatio&lt;/code&gt; attribute is defined by the referenced content. For content that clearly identifies a viewBox (e.g. an SVG file with the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; attribute on the outermost svg element) that value should be used. For most raster content (PNG, JPEG) the bounds of the image should be used (i.e. the &lt;code class=&quot;highlighter-rouge&quot;&gt;image&lt;/code&gt; element has an implicit &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; of ‘0 0 raster-image-width raster-image-height’). Where no value is readily available (e.g. an SVG file with no &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; attribute on the outermost svg element) the &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectRatio&lt;/code&gt; attribute is ignored, and only the translation due to the &lt;code class=&quot;highlighter-rouge&quot;&gt;x&lt;/code&gt; &amp;amp; &lt;code class=&quot;highlighter-rouge&quot;&gt;y&lt;/code&gt; attributes of the viewport is used to display the content.&lt;/p&gt;

&lt;p&gt;For example, if the image element referenced a PNG or JPEG and &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectRatio=&quot;xMinYMin meet&quot;&lt;/code&gt;, then the aspect ratio of the raster would be preserved, the raster would be sized as large as possible while ensuring that the entire raster fits within the viewport, and the top/left of the raster would be aligned with the top/left of the viewport as defined by the attributes &lt;code class=&quot;highlighter-rouge&quot;&gt;x&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;y&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;width&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;height&lt;/code&gt; on the &lt;code class=&quot;highlighter-rouge&quot;&gt;image&lt;/code&gt; element.&lt;br /&gt;
If the value of &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectRatio&lt;/code&gt; was ‘none’ then aspect ratio of the image would not be preserved. The image would be fitted such that the top/left corner of the raster exactly aligns with coordinate (&lt;code class=&quot;highlighter-rouge&quot;&gt;x&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;y&lt;/code&gt;) and the bottom/right corner of the raster exactly aligns with coordinate (&lt;code class=&quot;highlighter-rouge&quot;&gt;x&lt;/code&gt;+&lt;code class=&quot;highlighter-rouge&quot;&gt;width&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;y&lt;/code&gt;+&lt;code class=&quot;highlighter-rouge&quot;&gt;height&lt;/code&gt;).&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;using-iframe&quot;&gt;Establishing a new viewport using &lt;code&gt;&amp;lt;iframe&amp;gt;&lt;/code&gt;&lt;/h3&gt;

&lt;p&gt;An &lt;code class=&quot;highlighter-rouge&quot;&gt;iframe&lt;/code&gt; element that references an SVG file establishes new viewport similar to the situation of &lt;code class=&quot;highlighter-rouge&quot;&gt;image&lt;/code&gt; element explained above. An &lt;code class=&quot;highlighter-rouge&quot;&gt;iframe&lt;/code&gt; element can also have &lt;code class=&quot;highlighter-rouge&quot;&gt;x&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;y&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;width&lt;/code&gt;, and &lt;code class=&quot;highlighter-rouge&quot;&gt;height&lt;/code&gt; attributes, in addition to its own &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectratio&lt;/code&gt;.&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;using-foreignobject&quot;&gt;Establishing a new viewport using &lt;code&gt;&amp;lt;foreignObject&amp;gt;&lt;/code&gt;&lt;/h3&gt;

&lt;p&gt;A &lt;code class=&quot;highlighter-rouge&quot;&gt;foreignObject&lt;/code&gt; element creates a new viewport for rendering the content that is within the element.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;foreignObject&lt;/code&gt; tag allows you to add non-SVG content into an SVG file. Usually, the contents of foreignObject are assumed to be from a different namespace. For example, you could drop some HTML in the middle of an SVG element.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;foreignObject&lt;/code&gt; element accepts attributes, among which are &lt;code class=&quot;highlighter-rouge&quot;&gt;x&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;y&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;height&lt;/code&gt;, and &lt;code class=&quot;highlighter-rouge&quot;&gt;width&lt;/code&gt;, which are used to position the object and size it, creating the bounds used to render the contents referenced inside it.&lt;/p&gt;

&lt;p&gt;There is a lot to say about the &lt;code class=&quot;highlighter-rouge&quot;&gt;foreignObject&lt;/code&gt; element besides its creation of a new viewport for its content. If you’re interested, you can check the &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/SVG/Element/foreignObject&quot;&gt;MDN entry&lt;/a&gt; or check &lt;a href=&quot;http://thenittygritty.co/css-masking&quot;&gt;this practical use case&lt;/a&gt; by &lt;a href=&quot;http://twitter.com/derschepp&quot;&gt;Christian Schaeffer&lt;/a&gt; on &lt;a href=&quot;http://thenittygritty.co/&quot;&gt;The Nitty Gritty Blog&lt;/a&gt;.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;wrapping-up&quot;&gt;Wrapping Up&lt;/h2&gt;

&lt;p&gt;Establishing new viewports and coordinate systems—be that by nesting &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt;s or another element from the ones mentioned above—allows you to control parts of the SVG that you would otherwise not be able to control the same way.&lt;/p&gt;

&lt;p&gt;The entire time that I was working on this article and thinking of demos and use cases, all I kept thinking of is how nesting SVGs can give us finer control and flexibility for when we’re dealing with SVGs. Adaptive SVGs can be created with neat effects, fluid elements inside SVGs that are independent of the other elements on the page are possible, mimicing CSS border images for crispier backgrounds on high-resolution screens, and so much more.&lt;/p&gt;

&lt;p&gt;Have you created any interesting examples using nested viewports in SVG? Can you think of more creative examples?&lt;/p&gt;

&lt;p&gt;This article concludes the series of “Understanding SVG Coordinate Systems &amp;amp; Transformations”. Next up, we’ll be diving into animations, and more! Stay tuned, and thank you for reading!&lt;/p&gt;

    
  </content>
 </entry>
 
 <entry>
   <title>Understanding SVG Coordinate Systems and Transformations (Part 2) — The <code>transform</code> Attribute</title>
   <link href="http://sarasoueidan.com//blog/svg-transformations/"/>
   <id>https://sarasoueidan.com/blog/svg-transformations</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;
SVG elements can be transformed by scaling, translating, skewing, and rotating&amp;mdash;much like HTML elements can be transformed using CSS Transforms. However, there are certain inevitable differences when it comes to the coordinate systems used and affected by these transformations. In this article we'll go over the SVG &lt;code&gt;transform&lt;/code&gt; attribute and CSS property, covering how to use it, and things you should know about SVG coordinate system transformations.
&lt;/p&gt;

&lt;p&gt;This is the second article in a series I’m writing about SVG coordinate systems and transformations. In the first article, I covered everything you need to know to understand the basics of SVG coordinate systems; more specifically, the SVG viewport, and the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectRatio&lt;/code&gt; attributes.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://sarasoueidan.com/blog/svg-coordinate-systems&quot;&gt;Understanding SVG Coordinate Systems &amp;amp; Transformations (Part 1) – The viewport, &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt;, and &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectRatio&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Understanding SVG Coordinate Systems &amp;amp; Transformations (Part 2) – The &lt;code&gt;transform&lt;/code&gt; Attribute&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://sarasoueidan.com/blog/nesting-svgs&quot;&gt;Understanding SVG Coordinate Systems &amp;amp; Transformations (Part 3) – Establishing New Viewports&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this part I’m going to assume you read the first one, so, if you haven’t, make sure you do before you continue reading this article.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;transform-attribute-values&quot;&gt;The &lt;code&gt;transform&lt;/code&gt; Attribute Values&lt;/h2&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;transform&lt;/code&gt; attribute is used to specify one or more transformations on an element. It takes a &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;transform-list&amp;gt;&lt;/code&gt; as a value which is defined as a list of transform definitions, which are applied in the order provided. The individual transform definitions are separated by whitespace and/or a comma. An example of applying a transformation to an element may look like the following:&lt;/p&gt;

&lt;p&gt;The possible SVG transformations are: &lt;strong&gt;rotation&lt;/strong&gt;, &lt;strong&gt;scaling&lt;/strong&gt;, &lt;strong&gt;translation&lt;/strong&gt;, and &lt;strong&gt;skewing&lt;/strong&gt;. The transformation functions used in the &lt;code class=&quot;highlighter-rouge&quot;&gt;transform&lt;/code&gt; attribute work similar to the way CSS transform functions work in the &lt;code class=&quot;highlighter-rouge&quot;&gt;transform&lt;/code&gt; property, except that they take different arguments.&lt;/p&gt;

&lt;p class=&quot;note update&quot;&gt;
	Note that the function syntax defined below only works in the &lt;code&gt;transform&lt;/code&gt; attribute. See the &lt;a href=&quot;#css-transformation-properties&quot;&gt;section about transforming SVGs with CSS properties&lt;/a&gt; for information on the syntax used in the CSS transform properties.
&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;matrix&quot;&gt;Matrix&lt;/h3&gt;

&lt;p&gt;You can apply one or more transformations to an SVG element using the &lt;code class=&quot;highlighter-rouge&quot;&gt;matrix()&lt;/code&gt; function. The syntax for the matrix transformation is:&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
matrix(&amp;lt;a&amp;gt; &amp;lt;b&amp;gt; &amp;lt;c&amp;gt; &amp;lt;d&amp;gt; &amp;lt;e&amp;gt; &amp;lt;f&amp;gt;)
&lt;/pre&gt;

&lt;p&gt;The above declaration specifies a transformation in the form of a transformation matrix of six values. &lt;code class=&quot;highlighter-rouge&quot;&gt;matrix(a,b,c,d,e,f)&lt;/code&gt; is equivalent to applying the transformation &lt;strong&gt;matrix [a b c d e f]&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For those of you who are not math-savvy, you’re probably not going to be using this function. Those of you who are, you can read more about the math behind it &lt;a href=&quot;http://www.w3.org/TR/SVG/coords.html#TransformMatrixDefined&quot;&gt;here&lt;/a&gt;. Since this function is rarely used—if ever—I’m just going to skip to the other transformation functions.&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;translation&quot;&gt;Translation&lt;/h3&gt;

&lt;p&gt;To translate an SVG element, you can use the &lt;code class=&quot;highlighter-rouge&quot;&gt;translate()&lt;/code&gt; function. The syntax for the translation function is:&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
translate(&amp;lt;tx&amp;gt; [&amp;lt;ty&amp;gt;])
&lt;/pre&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;translate()&lt;/code&gt; function takes one or two values which specify the horizontal and vertical translation values, respectively. &lt;code class=&quot;highlighter-rouge&quot;&gt;tx&lt;/code&gt; represents the translation value along the x-axis; &lt;code class=&quot;highlighter-rouge&quot;&gt;ty&lt;/code&gt; represents the translation value along the y-axis.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;ty&lt;/code&gt; value is optional; and, if omitted, it defaults to zero. The &lt;code class=&quot;highlighter-rouge&quot;&gt;tx&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;ty&lt;/code&gt; values can be either space-separated or comma-separated, and they don’t take any units inside the function—they default to the current user coordinate system units.&lt;/p&gt;

&lt;p&gt;The following example translates an element by 100 user units to the right, and 300 user units to the bottom:&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;circle cx=&quot;0&quot; cy=&quot;0&quot; r=&quot;100&quot; transform=&quot;translate(100 300)&quot; /&amp;gt;
&lt;/pre&gt;

&lt;p&gt;The above example is still valid if the transformation was applied using &lt;code class=&quot;highlighter-rouge&quot;&gt;translate(100, 300)&lt;/code&gt; where the values are comma-separated.&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;scaling&quot;&gt;Scaling&lt;/h3&gt;

&lt;p&gt;You can resize an SVG element by scaling it up or down using the &lt;code class=&quot;highlighter-rouge&quot;&gt;scale()&lt;/code&gt; function transformation. The syntax for the scale transformation is:&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
scale(&amp;lt;sx&amp;gt; [&amp;lt;sy&amp;gt;])
&lt;/pre&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;scale()&lt;/code&gt; function takes one or two values which specify the horizontal and vertical scaling values, respectively. &lt;code class=&quot;highlighter-rouge&quot;&gt;sx&lt;/code&gt; represents the scaling value along the x-axis, used to stretch or shrink the element horizontally; &lt;code class=&quot;highlighter-rouge&quot;&gt;sy&lt;/code&gt; represents the scaling value along the y-axis, used to stretch or shrink the element vertically.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;sy&lt;/code&gt; value is optional; and, if omitted, it is assumed to be equal to &lt;code class=&quot;highlighter-rouge&quot;&gt;sx&lt;/code&gt;. The &lt;code class=&quot;highlighter-rouge&quot;&gt;sx&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;sy&lt;/code&gt; values can be either space-separated or comma-separated, and they are unitless numbers.&lt;/p&gt;

&lt;p&gt;The following example doubles the size of an element by scaling it to twice its original size:&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;rect width=&quot;150&quot; height=&quot;100&quot; transform=&quot;scale(2)&quot; x=&quot;0&quot; y=&quot;0&quot; /&amp;gt;
&lt;/pre&gt;

&lt;p&gt;The following stretches an element horizontally to 1.5 its original width, and shrinks it vertically to half its original height:&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;rect width=&quot;150&quot; height=&quot;100&quot; transform=&quot;scale(2 0.5)&quot; x=&quot;0&quot; y=&quot;0&quot; /&amp;gt;
&lt;/pre&gt;

&lt;p&gt;The above example is still valid if the transformation was applied using &lt;code class=&quot;highlighter-rouge&quot;&gt;scale(2, .5)&lt;/code&gt; where the values are comma-separated.&lt;/p&gt;

&lt;p&gt;It is important to note here that &lt;strong&gt;when an SVG element is scaled, its entire current coordinate system is scaled, resulting in the element also being repositioned inside the viewport&lt;/strong&gt;. Don’t worry about this now, we’ll get into it in more detail in the next section.&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;skewing&quot;&gt;Skew&lt;/h3&gt;

&lt;p&gt;An SVG element can also be skewed. To skew it, you can use one or both of the two skew transformation functions: &lt;code class=&quot;highlighter-rouge&quot;&gt;skewX&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;skewY&lt;/code&gt;.&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
skewX(&amp;lt;skew-angle&amp;gt;)
skewY(&amp;lt;skew-angle&amp;gt;)
&lt;/pre&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;skewX&lt;/code&gt; function specifies a skew transformation along the x-axis; and the &lt;code class=&quot;highlighter-rouge&quot;&gt;skewY&lt;/code&gt; function specifies a skew transformation along the y-axis.&lt;/p&gt;

&lt;p&gt;The skew angle specified is a &lt;strong&gt;unitless&lt;/strong&gt; angle that defaults to degrees.&lt;/p&gt;

&lt;p&gt;Note that skewing an element may result in the element being repositioned inside the viewport. More about this in the next section.&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;rotation&quot;&gt;Rotation&lt;/h3&gt;

&lt;p&gt;You can rotate an SVG element using the &lt;code class=&quot;highlighter-rouge&quot;&gt;rotate()&lt;/code&gt; function. The syntax for the function is:&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
rotate(&amp;lt;rotate-angle&amp;gt; [&amp;lt;cx&amp;gt; &amp;lt;cy&amp;gt;])
&lt;/pre&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;rotate()&lt;/code&gt; function specifies a rotation by &lt;code class=&quot;highlighter-rouge&quot;&gt;rotate-angle&lt;/code&gt; &lt;strong&gt;degrees&lt;/strong&gt; about a given point. Unlike rotation transformations in CSS, you cannot specify an angle unit other than degrees. The angle value is specified &lt;strong&gt;unitless&lt;/strong&gt;, and is considered a degrees value by default.&lt;/p&gt;

&lt;p&gt;The optional &lt;code class=&quot;highlighter-rouge&quot;&gt;cx&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;cy&lt;/code&gt; values represent the &lt;strong&gt;unitless&lt;/strong&gt; coordinates of the point used as a center of rotation. If &lt;code class=&quot;highlighter-rouge&quot;&gt;cx&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;cy&lt;/code&gt; are not provided, the rotation is about &lt;strong&gt;the origin of the current user coordinate system&lt;/strong&gt;. (See &lt;a href=&quot;http://sarasoueidan.com/blog/svg-coordinate-systems&quot;&gt;Part 1&lt;/a&gt; if you’re not sure what a user coordinate system is.)&lt;/p&gt;

&lt;p&gt;Specifying a center of rotation inside the &lt;code class=&quot;highlighter-rouge&quot;&gt;rotate()&lt;/code&gt; function is like a shorthand way for setting &lt;code class=&quot;highlighter-rouge&quot;&gt;transform: rotate()&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;transform-origin&lt;/code&gt; in CSS. Since the default center of rotation in SVG is the upper left corner of the current user coordinate system in use, and since that may not allow you to create the rotation effect you want, you will probably end up specifying a new center inside &lt;code class=&quot;highlighter-rouge&quot;&gt;rotate()&lt;/code&gt;. If you know your element’s dimensions and position in the SVG canvas, you can easily specify its center as the center of rotation.&lt;/p&gt;

&lt;p&gt;The following example rotates a group of elements around a specified center of rotation positioned at (50, 50) in the current user coordinate system:&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;g id=&quot;parrot&quot; transform=&quot;rotate(45 50 50)&quot; x=&quot;0&quot; y=&quot;0&quot;&amp;gt;
	&amp;lt;!-- elements making up a parrot shape --&amp;gt;
&amp;lt;/g&amp;gt;
&lt;/pre&gt;

&lt;p&gt;However, if you want an element to rotate around its center, you’d probably rather specify the center as &lt;code class=&quot;highlighter-rouge&quot;&gt;50% 50%&lt;/code&gt; like you would do in CSS; but unfortunately doing that inside the &lt;code class=&quot;highlighter-rouge&quot;&gt;rotate()&lt;/code&gt; function is not possible—you need to use absolute coordinates. However, you &lt;em&gt;can&lt;/em&gt; do this using the CSS &lt;code class=&quot;highlighter-rouge&quot;&gt;transform-origin&lt;/code&gt; property in conjunction with the CSS &lt;code class=&quot;highlighter-rouge&quot;&gt;transform&lt;/code&gt; property. More about this later in the article.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;coordinate-system-transformations&quot;&gt;Coordinate System Transformations&lt;/h2&gt;

&lt;p&gt;Now that we’ve covered all possible SVG transformation functions, we’ll dig into the visual part and the effect of applying each transformation to an SVG element. This is the most important aspect of SVG transformations. And they are called “coordinate system transformations” not just “element transformations” for a reason.&lt;/p&gt;

&lt;p&gt;In the &lt;a href=&quot;http://www.w3.org/TR/SVG/coords.html&quot;&gt;specification&lt;/a&gt;, the &lt;code class=&quot;highlighter-rouge&quot;&gt;transform&lt;/code&gt; attribute is defined as being one of the two attributes that &lt;strong&gt;establish a new user space (current coordinate system)&lt;/strong&gt; on the element it is applied to — the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; attribute is the second of the two attributes that create a new user space. So what exactly does this mean?&lt;/p&gt;

&lt;blockquote class=&quot;pull-quote&quot;&gt;
	The &lt;code&gt;transform&lt;/code&gt; attribute establishes a new user space (current coordinate system) on the element it is applied to.
&lt;/blockquote&gt;

&lt;p&gt;This behavior is similar to the behavior of CSS transformations applied to an HTML element—the HTML element’s coordinate system is transformed, and this is usually most obvious when you’re chaining tranformations (we’ll get to this later). Despite being similar in many aspects, HTML and SVG transformations have some differences.&lt;/p&gt;

&lt;p&gt;The main difference is the coordinate system. The coordinate system of an HTML element is established on the element itself. Meanwhile, in SVG, the coordinate system of an element is, initially, the current coordinate system or user space in use.&lt;/p&gt;

&lt;p&gt;When you apply the &lt;code class=&quot;highlighter-rouge&quot;&gt;transform&lt;/code&gt; attribute to an SVG element, that element gets a “copy” of the current user coordinate system in use. You can think of it as just creating a new “layer” for the transformed element, where the new layer has its own copy of the current user coordinate system (the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Then, &lt;strong&gt;the element’s new current coordinate system is transformed by the transformation functions specified inside the &lt;code class=&quot;highlighter-rouge&quot;&gt;transform&lt;/code&gt; attribute&lt;/strong&gt;, thus resulting in the transformation of the element itself. It is as if the elements are drawn onto the canvas in the transformed coordinate system.&lt;/p&gt;

&lt;p&gt;To understand how SVG transformations are applied, let’s start with the visual part. The following image shows the SVG canvas we’re going to be working with.&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/svg-transforms-canvas.png&quot; alt=&quot;&quot; /&gt;
&lt;/figure&gt;

&lt;p&gt;The parrot and the dog are the elements (groups &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;g&amp;gt;&lt;/code&gt;) that we’re going to be transforming.&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;svg width=&quot;800&quot; height=&quot;800&quot; viewBox=&quot;0 0 800 600&quot;&amp;gt;
	&amp;lt;g id=&quot;parrot&quot;&amp;gt;
		&amp;lt;!-- shapes and paths forming the parrot --&amp;gt;
	&amp;lt;/g&amp;gt;
	&amp;lt;g id=&quot;dog&quot;&amp;gt;
		&amp;lt;!-- shapes and paths forming the dog --&amp;gt;
	&amp;lt;/g&amp;gt;
&amp;lt;/svg&amp;gt;
&lt;/pre&gt;

&lt;p&gt;The grey coordinate system is the initial coordinate system of the canvas established by the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt;. For simplicity’s sake, I’m going to not change the initial coordinate system—I’m using a &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; that is the same size as the viewport, as you see in the above code.&lt;/p&gt;

&lt;blockquote class=&quot;pull-quote&quot;&gt;
	When you apply the &lt;code&gt;transform&lt;/code&gt; attribute to an SVG element, that element gets a &quot;copy&quot; of the current user coordinate system in use.
&lt;/blockquote&gt;

&lt;p&gt;Now that we’ve established our canvas and an initial user space, we’re going to start transforming elements. Let’s start by translating the parrot by 150 units to the left and 200 units downwards.&lt;/p&gt;

&lt;p&gt;The parrot is, of course, made of several paths and shapes. It’s enough to apply the &lt;code class=&quot;highlighter-rouge&quot;&gt;transform&lt;/code&gt; attribute to the group wrapping these shapes &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;g&amp;gt;&lt;/code&gt;; this will in turn apply the transformation to the entire set of shapes and paths, and the parrot will be translated as one whole item. See the &lt;a href=&quot;http://sarasoueidan.com/blog/structuring-grouping-referencing-in-svg&quot;&gt;article on structuring and grouping SVGs&lt;/a&gt; for more information.&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;svg width=&quot;800&quot; height=&quot;800&quot; viewBox=&quot;0 0 800 600&quot;&amp;gt;
	&amp;lt;g id=&quot;parrot&quot; transform=&quot;translate(150 200)&quot;&amp;gt;
		&amp;lt;!-- shapes and paths forming the parrot --&amp;gt;
	&amp;lt;/g&amp;gt;
	&amp;lt;!-- ... --&amp;gt;
&amp;lt;/svg&amp;gt;
&lt;/pre&gt;

&lt;p&gt;The following image shows the result of translating the parrot by the above translation. The translucent version of the parrot shows the initial position before the transformation was applied.&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/svg-transformations-translate.png&quot; alt=&quot;&quot; /&gt;
&lt;/figure&gt;

&lt;p&gt;The translation transformation in SVG is as simple and straightforward as it is in CSS when applied on an HTML element. We mentioned earlier that applying the &lt;code class=&quot;highlighter-rouge&quot;&gt;transform&lt;/code&gt; attribute to an element establishes a new current user coordinate system on it. The following image shows the “copy” of the initial coordinate system, that is established on the parrot element when it was transformed. Notice how the parrot’s current coordinate system is translated.&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/svg-transformations-translate-system.png&quot; alt=&quot;&quot; /&gt;
&lt;/figure&gt;

&lt;p&gt;It’s important to notice here that &lt;strong&gt;the new current coordinate system established on the element is a replicate of the initial user space, with the position of the element preserved inside it. This means that it is &lt;em&gt;not&lt;/em&gt; established on the element’s bounding box, nor is the size of the new current coordinate system restricted to the size of the element&lt;/strong&gt;. This is where the difference between HTML and SVG coordinate system shines.&lt;/p&gt;

&lt;blockquote class=&quot;pull-quote&quot;&gt;
	The new current coordinate system established on a transformed element is &lt;code&gt;not&lt;/code&gt; established on the element's bounding box, nor is its size restricted to the size of the element.
&lt;/blockquote&gt;

&lt;p&gt;This is more evident if we are to transform the dog at the bottom right of the canvas. Suppose we want to translate the dog by 50 units to the right and then 50 units downwards. This is how the dog, its initial position, and the new current coordinate system (that is also translated with the dog) will look. Notice how the origin of the dog’s new current coordinate system is not positioned at the top left cornder of the dog’s bounding box. Also notice how the dog and its new coordinate system seem as if they were moved to a new “layer” on top of the canvas.&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/svg-transformations-translate-dog.png&quot; alt=&quot;&quot; /&gt;
&lt;/figure&gt;

&lt;p&gt;Now let’s try something else. Instead of translating the parrot, let’s try scaling it. We’re going to scale the parrot to double its size:&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;svg width=&quot;800&quot; height=&quot;800&quot; viewBox=&quot;0 0 800 600&quot;&amp;gt;
	&amp;lt;g id=&quot;parrot&quot; transform=&quot;scale(2)&quot;&amp;gt;
		&amp;lt;!-- shapes and paths forming the parrot --&amp;gt;
	&amp;lt;/g&amp;gt;
	&amp;lt;!-- ... --&amp;gt;
&amp;lt;/svg&amp;gt;
&lt;/pre&gt;

&lt;p&gt;The result of scaling an SVG element differs from that of scaling an HTML element. The scaled SVG’s element’s position changes inside the viewport when it is scaled. The following image shows the result of doubling the size of the parrot. Notice the initial position and size, and the final size and position.&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/svg-transformations-scale.png&quot; alt=&quot;&quot; /&gt;
&lt;/figure&gt;

&lt;p&gt;What we can notice from the above image is that not only the size (width and height) of the parrot were doubled, but the coordinates (x and y) were also multiplied by the scaling factor (which is two, here).&lt;/p&gt;

&lt;p&gt;The reason we ended up with this result is something we’ve mentioned earlier: the element’s  current coordinate system is transformed, and then the parrot is drawn into the new system. So, in this example, the current coordinate system was scaled. This effect is similar to the effect of using &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox = &quot;0 0 400 300&quot;&lt;/code&gt;, which “zooms in” to the coordinate system, thus doubling the size of the content inside it (see &lt;a href=&quot;http://sarasoueidan.com/blog/svg-coordinate-systems&quot;&gt;part 1&lt;/a&gt; of the series if you haven’t already).&lt;/p&gt;

&lt;p&gt;So, if we were to visualize the coordinate system transformation showing the current transformed system of the parrot, we’d get the following result:&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/svg-transformations-scale-system.png&quot; alt=&quot;&quot; /&gt;
&lt;/figure&gt;

&lt;p&gt;The new current coordinate system of the parrot is scaled up, “zooming in” to the parrot at the same time. Notice that, inside its current coordinate system, the parrot is not repositioned—it is only the effect of scaling the coordinate system that repositions it inside the viewport. The parrot is simply drawn at its original x and y coordinates inside the new scaled up system.&lt;/p&gt;

&lt;p&gt;Let’s trying scaling the parrot in both directions using different scaling factors. If we scale the parrot by applying &lt;code class=&quot;highlighter-rouge&quot;&gt;transform=&quot;scale(2 0.5)&lt;/code&gt;, we’re doubling its width while making it half its original height. The effect will be similar to applying &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox=&quot;0 0 400 1200&quot;&lt;/code&gt;.&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/svg-transformations-scale-2.png&quot; alt=&quot;&quot; /&gt;
&lt;/figure&gt;

&lt;p&gt;Notice the position of the parrot inside the scaled coordinate system, and compare it to the position in the initial system (translucent parrot): the x and y position coordinates are preserved.&lt;/p&gt;

&lt;p&gt;Skewing an element in SVG also results in the element being “moved” as a result of its current coordinate system being skewed.&lt;/p&gt;

&lt;p&gt;Suppose we apply a skew transformation to the dog along the x-axis using the &lt;code class=&quot;highlighter-rouge&quot;&gt;skewX&lt;/code&gt; function. We’re going to skew the dog by 25 degrees horizontally.&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;svg width=&quot;800&quot; height=&quot;800&quot; viewBox=&quot;0 0 800 600&quot;&amp;gt;
	&amp;lt;!-- ... --&amp;gt;
	&amp;lt;g id=&quot;dog&quot; transform=&quot;skewX(25)&quot;&amp;gt;
		&amp;lt;!-- shapes and paths forming the dog --&amp;gt;
	&amp;lt;/g&amp;gt;
&amp;lt;/svg&amp;gt;
&lt;/pre&gt;

&lt;p&gt;The following image shows the result of applying the skewing transformation to the dog. Its coordinate system is skewed, and so is the dog itself.&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/svg-transformations-skew-system.png&quot; alt=&quot;&quot; /&gt;
&lt;/figure&gt;

&lt;p&gt;Note that the position of the dog with respect to its original position also changes, as a result of skewing its coordinate system.&lt;/p&gt;

&lt;p&gt;The following image shows the result of skewing the dog by the same angle using &lt;code class=&quot;highlighter-rouge&quot;&gt;skewY()&lt;/code&gt; instead of &lt;code class=&quot;highlighter-rouge&quot;&gt;skewX&lt;/code&gt;:&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/svg-transformations-skew-system-2.png&quot; alt=&quot;&quot; /&gt;
&lt;/figure&gt;

&lt;p&gt;And last but not least, let’s try rotating the parrot. The default center of rotation is the upper left corner of the current user coordinate system. The new current system established on the rotated element will also be rotated. In the following example, we’re going to rotate the parrot by 45 degrees. The positive direction of rotation is clockwise.&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;svg width=&quot;800&quot; height=&quot;800&quot; viewBox=&quot;0 0 800 600&quot;&amp;gt;
	&amp;lt;g id=&quot;parrot&quot; transform=&quot;rotate(45)&quot;&amp;gt;
		&amp;lt;!-- shapes and paths forming the parrot --&amp;gt;
	&amp;lt;/g&amp;gt;
	&amp;lt;!-- ... --&amp;gt;
&amp;lt;/svg&amp;gt;
&lt;/pre&gt;

&lt;p&gt;The result of applying the above transformation looks like this:&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/svg-transformations-rotate.png&quot; alt=&quot;&quot; /&gt;
&lt;/figure&gt;

&lt;p&gt;You are probably going to want to rotate an element around a point other than the default origin of the coordinate system. Using the &lt;code class=&quot;highlighter-rouge&quot;&gt;rotate()&lt;/code&gt; function in the &lt;code class=&quot;highlighter-rouge&quot;&gt;transform&lt;/code&gt; attribute, you can specify that point explicitly. Suppose we want to rotate the parrot in this example around its own center. According to the width, height, and position of the parrot, I can determine its center to be at approximately (150, 170). The parrot can then be rotated around this point:&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;svg width=&quot;800&quot; height=&quot;800&quot; viewBox=&quot;0 0 800 600&quot;&amp;gt;
	&amp;lt;g id=&quot;parrot&quot; transform=&quot;rotate(45 150 170)&quot;&amp;gt;
		&amp;lt;!-- shapes and paths forming the parrot --&amp;gt;
	&amp;lt;/g&amp;gt;
	&amp;lt;!-- ... --&amp;gt;
&amp;lt;/svg&amp;gt;
&lt;/pre&gt;

&lt;p&gt;At this point, the parrot is rotated and will look like so:&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/svg-transformations-rotate-center.png&quot; alt=&quot;&quot; /&gt;
&lt;/figure&gt;

&lt;p&gt;We said that the transformations are applied to the coordinate system, and because of that, the element is eventually affected and transformed. So how exactly does changing the center of rotation work on the coordinate system whose origin is at the point (0, 0)?&lt;/p&gt;

&lt;p&gt;When you change the center or rotation, the coordinate system is translated, rotated by the specified angle, and then translated again by specific values depending on the center of rotation you specify. In this example, this:&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;g id=&quot;parrot&quot; transform=&quot;rotate(45 150 170)&quot;&amp;gt;	
&lt;/pre&gt;

&lt;p&gt;is performed by the browser as a series of translation and rotation operations equivalent to:&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;g id=&quot;parrot&quot; transform=&quot;translate(150 170) rotate(45) translate(-150 -170)&quot;&amp;gt;	
&lt;/pre&gt;

&lt;p&gt;The current coordinate system is translated to the point you want to be the center. It is then rotated by the angle you specify. And then finally the system is translated by the negation of the values. The above transformation applied on the system looks like this:&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/svg-transformations-rotate-center-system.png&quot; alt=&quot;&quot; /&gt;
&lt;/figure&gt;

&lt;p&gt;Before we move on to the next section where we’re going to nest and chain transformations, I want to note that the current user coordinate system established on a transformed element is independent from other coordinate systems established on other transformed elements. The following image shows the two coordinate systems established on the dog and the parrot, and how they are independent from each other.&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/svg-transformations-multiple.png&quot; alt=&quot;&quot; /&gt;
	&lt;figcaption&gt;
		The new current user coordinate systems established on the parrot and the dog when they are transformed.
	&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Also note that each current coordinate system still lies inside the main coordinate system of the canvas established using the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; attribute on the containing &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt;. Any transformations applied to the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; will affect the entire canvas and all elements inside it as well, whether they have their own established coordinate systems or not.&lt;/p&gt;

&lt;p&gt;For example, the following is the result of changing the user space of the entire canvas from &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox=&quot;0 0 800 600&quot;&lt;/code&gt; to &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox=&quot;0 0 600 450&quot;&lt;/code&gt;. The entire canvas is scaled up, preserving any transformations applied to the individual elements.&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/svg-transformations-multiple-2.png&quot; alt=&quot;&quot; /&gt;
	&lt;figcaption&gt;
		The result of changing the user coordinate system on the entire canvas.
	&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;nested-and-chained-transformations&quot;&gt;Nested and Chained Transformations&lt;/h3&gt;

&lt;p&gt;A lot of times you may want to apply several transformations to an element. Applying multiple transformations in a raw is what is referred to as “chaining” transformations.&lt;/p&gt;

&lt;p&gt;When transformations are chained, the most important thing to be aware of is that, just like with HTML element transformations, each transformation is applied to the coordinate system after that system is transformed by the previous transformations.&lt;/p&gt;

&lt;p&gt;For example, if you’re going to apply a rotation to an element, followed by a translation, the translation happens according to the new coordinate system, not the inital non-rotated one.&lt;/p&gt;

&lt;p&gt;The following example does just that. We’re applying the previous rotation, and then translating the parrot using by 200 units along the positive x-axis&lt;code class=&quot;highlighter-rouge&quot;&gt;transform=&quot;rotate(45 150 170) translate(200)&quot;&lt;/code&gt;.&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/svg-transformations-rotate-translate.png&quot; alt=&quot;&quot; /&gt;
&lt;/figure&gt;

&lt;p&gt;Depending on the final position and transformation you’re after, you need to chain your transformations accordingly. Always keep the coordinate system in mind.&lt;/p&gt;

&lt;p&gt;Note that when you skew an element—and its coordinate system with it—the coordinate system will no longer be an orthogonal one, and the coordinates will no longer be calculated as orthogonal ones—they will be &lt;a href=&quot;http://en.wikipedia.org/wiki/Skew_coordinates&quot;&gt;skew coordinates&lt;/a&gt;. Simply put, this just means that the coordinate system’s origin is no longer a 90 degrees angle, and hence the new coordinates will be computed based on this new angle.&lt;/p&gt;

&lt;p&gt;Nested transformations occur when a child of a transformed element is also transformed. The transformation applied to the child element will be an accumulation of the transformations applied on its ancestors and the transformation applied on it.&lt;/p&gt;

&lt;blockquote class=&quot;pull-quote&quot;&gt;
	[For nested transformations,] the transformation applied to the child element will be an accumulation of the transformations applied on its ancestors and the transformation applied on it. 
&lt;/blockquote&gt;

&lt;p&gt;So, in effect, nesting transforms is similar to chaining them; only difference is that instead of applying a series of transformations on one element, it automatically gets the transformations applied on its acestors, and then finally its own transformations are applied to it, just like we applied transformations in the chain above—one after the other.&lt;/p&gt;

&lt;p&gt;This is particularly useful for when you want to transform one element relative to another. For example, suppose you want to animate the tail of the dog. The tail is a descendant of the &lt;code class=&quot;highlighter-rouge&quot;&gt;#dog&lt;/code&gt; group.&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;svg width=&quot;800&quot; height=&quot;800&quot; viewBox=&quot;0 0 800 600&quot;&amp;gt;
	&amp;lt;!-- ... --&amp;gt;
	&amp;lt;g id=&quot;dog&quot; transform=&quot;translate(..)&quot;&amp;gt;
		&amp;lt;!-- shapes and paths forming the dog --&amp;gt;
		&amp;lt;g id=&quot;head&quot;&amp;gt;
			&amp;lt;!-- .. --&amp;gt;
		&amp;lt;/g&amp;gt;
		&amp;lt;g id=&quot;body&quot; transform=&quot;rotate(.. .. ..)&quot;&amp;gt;
			&amp;lt;path id=&quot;tail&quot; d=&quot;...&quot; transform=&quot;rotate(..)&quot;&amp;gt;
				&lt;!-- animateTransform here --&gt;
			&amp;lt;/path&amp;gt;
			&amp;lt;g id=&quot;legs&quot;&amp;gt;
				&amp;lt;!-- ... --&amp;gt;
			&amp;lt;/g&amp;gt;
		&amp;lt;/g&amp;gt;
	&amp;lt;/g&amp;gt;
&amp;lt;/svg&amp;gt;
&lt;/pre&gt;

&lt;p&gt;Suppose we translate the dog group; then rotate its body by some angle around some point, and then we want to animate the tail by rotating it and animating that rotation.&lt;/p&gt;

&lt;p&gt;When the tail is to be rotated, it “inherits” the transformed coordinate system of its ancestor (&lt;code class=&quot;highlighter-rouge&quot;&gt;#body&lt;/code&gt;), which in turn inherits the transformed coordinate system of its transformed ancestor (&lt;code class=&quot;highlighter-rouge&quot;&gt;#dog&lt;/code&gt;) as well. So, in effect, when the taill is rotated, it is as though it has been translated (by the same translation of the &lt;code class=&quot;highlighter-rouge&quot;&gt;#dog&lt;/code&gt; group), then rotated (by the same rotation of the &lt;code class=&quot;highlighter-rouge&quot;&gt;#body&lt;/code&gt; group), and &lt;em&gt;then&lt;/em&gt; rotated by its own rotation. And the effect of applying a series of transformations here is the same as we explained in the chaining example above.&lt;/p&gt;

&lt;p&gt;So, you see, nesting transformations has practically the same effect as chaining them on the &lt;code class=&quot;highlighter-rouge&quot;&gt;#tail&lt;/code&gt;.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;transforming-svgs-with-css&quot;&gt; Transforming SVGs using CSS Properties&lt;/h2&gt;

&lt;p&gt;In SVG 2, the &lt;code class=&quot;highlighter-rouge&quot;&gt;transform&lt;/code&gt; attribute is referred to as the &lt;code class=&quot;highlighter-rouge&quot;&gt;transform&lt;/code&gt; property; this is because SVG transformations have been “exported” into the &lt;a href=&quot;http://dev.w3.org/csswg/css-transforms/&quot;&gt;CSS3 Transforms specification&lt;/a&gt;. The latter combines the SVG Transforms, CSS 2D Transforms, and CSS 3D Transforms specifications, and introduces features like &lt;code class=&quot;highlighter-rouge&quot;&gt;transform-origin&lt;/code&gt; and 3D transformations into SVG.&lt;/p&gt;

&lt;p&gt;The CSS transform properties specified in the CSS Transforms specifications can be applied to SVG elements. However, the values for the &lt;code class=&quot;highlighter-rouge&quot;&gt;transform&lt;/code&gt; property functions need to follow the syntax specified in the CSS spec: function arguments must be separated with commas — space-separation alone isn’t valid, but you can include one or more white space before and/or after the comma; and the &lt;code class=&quot;highlighter-rouge&quot;&gt;rotate()&lt;/code&gt; function does not take &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;cx&amp;gt; &amp;lt;cy&amp;gt;&lt;/code&gt; values anymore — the center of rotation is specified using the &lt;code class=&quot;highlighter-rouge&quot;&gt;transform-origin&lt;/code&gt; property. Also, the CSS transformatio functions do accept units for angles and coordinates, such as &lt;code class=&quot;highlighter-rouge&quot;&gt;rad&lt;/code&gt; (radians) for angles (among others) and &lt;code class=&quot;highlighter-rouge&quot;&gt;px&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;em&lt;/code&gt;, etc. for coordinate values.&lt;/p&gt;

&lt;p&gt;An example of rotating an SVG element using CSS may look like the following:&lt;/p&gt;

&lt;pre class=&quot;brush:css&quot;&gt;
#parrot {
	transform-origin: 50% 50%; /* center of rotation is set to the center of the element */
	transform: rotate(45deg);
}
&lt;/pre&gt;

&lt;p&gt;And SVG element can also be transformed in three-dimensional space using CSS 3D Transforms. Note that the coordinate systems are still, however, different from the coordinate systems established on HTML elements. This means that 3D rotations will also look different unless you change the center of rotation.&lt;/p&gt;

&lt;pre class=&quot;brush:css&quot;&gt;
#SVGel {
	transform: perspective(800px) rotate3d(1, 1, 0, 45deg);
}
&lt;/pre&gt;

&lt;p&gt;Because transforming SVG elements with CSS is pretty much the same as transforming HTML elements with CSS—syntax-wise—I’m going to skip elaborating on this topic in this article.&lt;/p&gt;

&lt;p&gt;That said, at the time of writing of this article, implementations are still incomplete in some browsers and for some features. Because of how fast browser support changes, I recommend you experiment with the properties to determine what currently works and what doesn’t, and decide on what you can start using today and what not.&lt;/p&gt;

&lt;p&gt;Note that once CSS Transforms are fully implemented for SVG elements, it is recommended that you use the CSS transforms function syntax even when you apply the transformation in the form of a &lt;code class=&quot;highlighter-rouge&quot;&gt;transform&lt;/code&gt; attribute. That said, the above mentioned syntax of the &lt;code class=&quot;highlighter-rouge&quot;&gt;transform&lt;/code&gt; attribute functions will still be valid.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;animating-transform&quot;&gt;Animating &lt;code&gt;transform&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;SVG transformations can be animated, just like CSS transforms can be. If you’re using the CSS &lt;code class=&quot;highlighter-rouge&quot;&gt;transform&lt;/code&gt; property to transform the SVG, you can animate the transformation using CSS animations and transitions just like you would animate CSS transforms on HTML elements.&lt;/p&gt;

&lt;p&gt;The SVG &lt;code class=&quot;highlighter-rouge&quot;&gt;transform&lt;/code&gt; attribute can be animated using the SVG &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;animateTransform&amp;gt;&lt;/code&gt; element. The &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;animateTransform&amp;gt;&lt;/code&gt; element is one of three elements in SVG that are used to animate different SVG attributes.&lt;/p&gt;

&lt;p&gt;Details of the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;animateTransform&amp;gt;&lt;/code&gt; element are outside the scope of this article. Stay tuned for an article I’ll be writing about the different SVG animation elements, including &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;animateTransform&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;final-words&quot;&gt;Final Words&lt;/h2&gt;

&lt;p&gt;Working with SVGs can be really frustrating at first, if the concepts behind the coordinate system transformations aren’t very clear, especially if you’re coming from a CSS HTML transformations background, and naturally expect SVG elements to respond the same way to transformations as HTML elements do.&lt;/p&gt;

&lt;p&gt;However, once you get a grip of how they work, you gain a better control over your SVG canvas, and can manipulate elements more easily.&lt;/p&gt;

&lt;p&gt;In the last part of this series, I’m going to go over nesting SVGs and establishing new viewports and viewboxes. Stay tuned!&lt;/p&gt;

&lt;p class=&quot;note&quot;&gt;
	The SVG parrot &amp;amp; dog illustrations used are freebies from &lt;a href=&quot;http://freepik.com&quot;&gt;Freepik.com&lt;/a&gt;.
&lt;/p&gt;

&lt;p&gt;I hope you liked this article and found it useful. Thank you for reading!&lt;/p&gt;

    
  </content>
 </entry>
 
 <entry>
   <title>Understanding SVG Coordinate Systems and Transformations (Part 1) — The viewport, <code>viewBox</code>, and <code>preserveAspectRatio</code></title>
   <link href="http://sarasoueidan.com//blog/svg-coordinate-systems/"/>
   <id>https://sarasoueidan.com/blog/svg-coordinate-systems</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;SVG elements aren't governed by a CSS box model like HTML elements are. This makes positioning and transforming these elements trickier and may seem&amp;mdash;at first glance&amp;mdash;less intuitive. However, once you understand how SVG coordinate systems and transformations work, manipulating SVGs becomes a lot easier and makes a lot more sense. In this article we're going to go over three of the most important SVG attributes that control SVG coordinate systems: &lt;code&gt;viewport&lt;/code&gt;, &lt;code&gt;viewBox&lt;/code&gt;, and &lt;code&gt;preserveAspectRatio&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is the first in a series of three articles covering the topic of coordinate systems and transformations in SVG.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Understanding SVG Coordinate Systems &amp;amp; Transformations (Part 1) – The viewport, &lt;code&gt;viewBox&lt;/code&gt;, &amp;amp; &lt;code&gt;preserveAspectRatio&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://sarasoueidan.com/blog/svg-transformations&quot;&gt;Understanding SVG Coordinate Systems &amp;amp; Transformations (Part 2) – The &lt;code&gt;transform&lt;/code&gt; Attribute&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://sarasoueidan.com/blog/nesting-svgs&quot;&gt;Understanding SVG Coordinate Systems &amp;amp; Transformations (Part 3) – Establishing New Viewports&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For the sake of visualizing the concepts and explanations in the article even further, I created an interactive demo that allows you to play with the values of the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectRatio&lt;/code&gt; attributes.&lt;/p&gt;

&lt;p&gt;&lt;br /&gt;
&lt;a class=&quot;button&quot; href=&quot;https://sarasoueidan.com/demos/interactive-svg-coordinate-system/index.html&quot;&gt;Check the interactive demo out.&lt;/a&gt;&lt;/p&gt;

&lt;p class=&quot;note&quot;&gt;The demo is the cherry on top of the cake, so do make sure you come back to read the article if you check it out before you do!&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;svg-canvas&quot;&gt;The SVG Canvas&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;canvas&lt;/strong&gt; is the space or area where the SVG content is drawn. Conceptually, this canvas is infinite in both dimensions. The SVG can therefore be of any size. However, it is rendered on the screen relative to a &lt;strong&gt;finite region&lt;/strong&gt; known as &lt;em&gt;the viewport&lt;/em&gt;. Areas of the SVG that lie beyond the boundaries of the viewport are clipped off and not visible.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;svg-viewport&quot;&gt;The viewport&lt;/h2&gt;

&lt;p&gt;The viewport is the viewing area where the SVG will be visible. You can think of the viewport as a window through which you can see a particular scene. The scene may be entirely or partially visible through that window.&lt;/p&gt;

&lt;p&gt;The SVG viewport is similar to the viewport of the browser you’re viewing this page through. A web page can be of any size; it can be wider than the viewport’s width, and is in most cases also longer than the viewport’s length. However, only portions of a web page are visible through the viewport at a time.&lt;/p&gt;

&lt;p class=&quot;note&quot;&gt;Whether or not the entire SVG canvas or part of it is visible depends on the &lt;strong&gt;size of that canvas*&lt;/strong&gt; and &lt;strong&gt;the value of the &lt;code&gt;preserveAspectRatio&lt;/code&gt; attribute&lt;/strong&gt;. You don't have to worry about these now; we'll talk about them further in more detail.&lt;/p&gt;

&lt;p&gt;You specify the size of the viewport using the &lt;code class=&quot;highlighter-rouge&quot;&gt;width&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;height&lt;/code&gt; attributes on the outermost &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; element.&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;!-- the viewport will be 800px by 600px --&amp;gt;
&amp;lt;svg width=&quot;800&quot; height=&quot;600&quot;&amp;gt;
	&amp;lt;!-- SVG content drawn onto the SVG canvas --&amp;gt;
&amp;lt;/svg&amp;gt;
&lt;/pre&gt;

&lt;p&gt;In SVG, values can be set with or without a unit identifier. A unitless value is said to be specified &lt;em&gt;in user space&lt;/em&gt; using &lt;em&gt;user units&lt;/em&gt;. If a value is specified in user units, then the value is assumed to be equivalent to the same number of “px” units. This means that the viewport in the above example will be rendered as a 800px by 600px viewport.&lt;/p&gt;

&lt;p&gt;You can also specify values using units. The supported length unit identifiers in SVG are: &lt;code&gt;em&lt;/code&gt;, &lt;code&gt;ex&lt;/code&gt;, &lt;code&gt;px&lt;/code&gt;, &lt;code&gt;pt&lt;/code&gt;, &lt;code&gt;pc&lt;/code&gt;, &lt;code&gt;cm&lt;/code&gt;, &lt;code&gt;mm&lt;/code&gt;, &lt;code&gt;in&lt;/code&gt;, and percentages.&lt;/p&gt;

&lt;p&gt;Once the width and height of the outermost SVG element are set, the browser establishes an initial &lt;em&gt;viewport coordinate system&lt;/em&gt; and an initial &lt;em&gt;user coordinate system&lt;/em&gt;.&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;initial-coordinate-system&quot;&gt;The initial coordinate system&lt;/h3&gt;

&lt;p&gt;The initial &lt;strong&gt;viewport coordinate system&lt;/strong&gt; is a coordinate system established on the viewport, with the origin at the top left corner of the viewport at point (0, 0), the positive x-axis pointing towards the right, the positive y-axis pointing down, and one unit in the initial coordinate system equals one “pixel” in the viewport. This coordinate system is similar to the coordinate system established on an HTML element with a CSS box model.&lt;/p&gt;

&lt;p&gt;The initial &lt;strong&gt;user coordinate system&lt;/strong&gt; is the coordinate system established on the SVG canvas. This coordinate system is initially identical to the viewport coordinate system—it has its origin at the top left corner of the viewport with the positive x-axis pointing towards the right, the positive y-axis pointing down. Using the &lt;code&gt;viewBox&lt;/code&gt; attribute, the initial user coordinate system—also known as &lt;strong&gt;the current coordinate system&lt;/strong&gt;, or &lt;strong&gt;user space in use&lt;/strong&gt;—can be modified so that it is not identical to the viewport coordinate system anymore. We’ll talk about modifying it in the next section.&lt;/p&gt;

&lt;p&gt;For now, we won’t specify a &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; attribute value. The user coordinate system of the SVG canvas is identical to that of the viewport.&lt;/p&gt;

&lt;p&gt;In the following image, the viewport coordinate system “ruler” is grey, and that of the user coordinate system (the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt;) is blue. Since they are both identical at this point, the two coordinate systems overlap.&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/initial-coordinate-systems.jpg&quot; alt=&quot;Viewport Coordinate System&quot; /&gt;
	&lt;figcaption&gt;
		The initial coordinate systems established on the viewport and SVG canvas. Grey units represent the viewport coordinate system; blue units represent the user coordinate system. Both coordinate systems are identical and coincide.
	&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;The parrot in the above SVG has a bounding box that is 200 units (200 pixels in this case) in width and 300 units in height. The parrot is drawn on the canvas based on the initial coordinate system.&lt;/p&gt;

&lt;p class=&quot;note&quot;&gt;A new user space (i.e., a new current coordinate system) can also be established by specifying &lt;strong&gt;transformations&lt;/strong&gt; using the &lt;code&gt;transform&lt;/code&gt; attribute on a container element or graphics element. We'll talk about transformations in the second part of this article, and then in more details in the third and last part.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;svg-viewbox&quot;&gt;The &lt;code&gt;viewBox&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;I like to think of the &lt;code&gt;viewBox&lt;/code&gt; as the “real” coordinate system. After all, it is &lt;em&gt;the&lt;/em&gt; coordinate system used to draw the SVG graphics onto the canvas. This coordinate system can be smaller or bigger than the viewport, and it can be fully or partially visible inside the viewport too.&lt;/p&gt;

&lt;p&gt;In the previous section, this coordinate system—the user coordinate system—was identical to the viewport coordinate system. The reason for that is that we did not specify it to be otherwise. That’s why all the positioning and drawing seemed to be done relative to the viewport coordinate system. Because once we created a viewport coordinate system (using &lt;code class=&quot;highlighter-rouge&quot;&gt;width&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;height&lt;/code&gt;), the browser created a default user coordinate system that is identical to it.&lt;/p&gt;

&lt;p&gt;You specify your own user coordinate system using the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; attribute. If the user coordinate system you choose has the same aspect ratio (ratio of height to width) as the viewport coordinate system, it will stretch to fill the viewport area (we’ll talk examples in a minute). However, if your user coordinate system does not have the same aspect ratio, you  can use the &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectRatio&lt;/code&gt; attribute to specify whether or not the entire system will be visible inside the viewport or not, and you can also use it to specify how it is positioned inside the viewport. We’ll get into details and lots of examples for this case in the next section. In this section, we’ll stick to examples where the aspect ratio of the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; matches that of the viewport—in these examples, &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectRatio&lt;/code&gt; has no effect.&lt;/p&gt;

&lt;p&gt;Before we get into the examples, we’ll go over the syntax of &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt;.&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;viewbox-syntax&quot;&gt;The &lt;code&gt;viewBox&lt;/code&gt; syntax&lt;/h3&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox attribute&lt;/code&gt; takes four parameters as a value: &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;min-x&amp;gt;&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;min-y&amp;gt;&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;width&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;height&lt;/code&gt;.&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
viewBox = &amp;lt;min-x&amp;gt; &amp;lt;min-y&amp;gt; &amp;lt;width&amp;gt; &amp;lt;height&amp;gt;
&lt;/pre&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;min-x&amp;gt;&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;min-y&amp;gt;&lt;/code&gt; values determine the upper left corner of the viewbox, and the &lt;code class=&quot;highlighter-rouge&quot;&gt;width&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;height&lt;/code&gt; determine the width and height of that viewBox. Note here that the width and height of the viewBox need not be the same as the width and height set on the parent &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; element. A negative value for &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;width&amp;gt;&lt;/code&gt; or &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;height&amp;gt;&lt;/code&gt; is invalid. A value of zero disables rendering of the element.&lt;/p&gt;

&lt;p class=&quot;note&quot;&gt;
Note that the width of the viewport can also be set in CSS to any value. For example, setting &lt;code&gt;width: 100%&lt;/code&gt; will make the SVG viewport fluid in a document. Whatever value of the &lt;code&gt;viewBox&lt;/code&gt;, it will then be mapped to the computed width of the outer SVG element.
&lt;/p&gt;

&lt;p&gt;An example of setting &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; would look like the following:&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;!-- The viewbox in this example is equal to the viewport, but it can be different --&amp;gt;
&amp;lt;svg width=&quot;800&quot; height=&quot;600&quot; viewbox=&quot;0 0 800 600&quot;&amp;gt;
	&amp;lt;!-- SVG content drawn onto the SVG canvas --&amp;gt;
&amp;lt;/svg&amp;gt;
&lt;/pre&gt;

&lt;p&gt;If you’ve read about the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; somewhere before, you may have come across a few definitions saying that you can use the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; attribute to transform the SVG graphic by scaling or translating it. This is true. I’m going to go further and say that you can even &lt;em&gt;crop&lt;/em&gt; the SVG graphic using &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The best way to understand the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; and differentiate it from the viewport is by visualizing it. So let’s start with some examples. We’ll start with examples where th aspect ratio of the viewbox is the same as the aspect ratio of the viewport, so we won’t need to dig into &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectRatio&lt;/code&gt; yet.&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;viewbox-aspect-ratio&quot;&gt;&lt;code&gt;viewBox&lt;/code&gt; with aspect ratio equal to the viewport's aspect ratio&lt;/h3&gt;

&lt;p&gt;We’ll start with a simple example. The &lt;code class=&quot;highlighter-rouge&quot;&gt;viewbox&lt;/code&gt; in this example will be half the size of the viewport. We won’t change the origin of the viewbox in this one, so both &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;min-x&amp;gt;&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;min-y&amp;gt;&lt;/code&gt; will be set to zero. The width and height of the viewbox will be half the width and height of the viewport. This means that we’re preserving the aspect ratio.&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;svg width=&quot;800&quot; height=&quot;600&quot; viewbox=&quot;0 0 400 300&quot;&amp;gt;
	&amp;lt;!-- SVG content drawn onto the SVG canvas --&amp;gt;
&amp;lt;/svg&amp;gt;
&lt;/pre&gt;

&lt;p&gt;So, what does &lt;code class=&quot;highlighter-rouge&quot;&gt;viewbox=&quot;0 0 400 300&quot;&lt;/code&gt; exactly do?&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;It specifies a specific region of the canvas spanning from a top left point at (0, 0) to a point at (400, 300).&lt;/li&gt;
  &lt;li&gt;The SVG graphic is then &lt;strong&gt;cropped&lt;/strong&gt; to that region.&lt;/li&gt;
  &lt;li&gt;The region is &lt;strong&gt;scaled up&lt;/strong&gt; (in a zoom-in-like effect) to fill the entire viewport.&lt;/li&gt;
  &lt;li&gt;The user coordinate system is mapped to the viewport coordinate system so that—in this case—one user unit is equal to two viewport units.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The following image shows the result of applying the above viewbox to the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; canvas in our example. The grey units represent the viewport coordinate system, and the blue coordinate system represents the user coordinate system established by the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt;.&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/viewbox-400-300-crop.jpg&quot; alt=&quot;&quot; /&gt;
	&lt;figcaption&gt;
		Specifying a viewbox has a result similar to cropping the graphic to that viewbox and then zooming it in so that it fills the entire viewport area. Remember that we're still maintaining the same aspect ratio as the viewport in this case.
	&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Anything you draw on the SVG canvas will be drawn relative to the new user coordinate system.&lt;/p&gt;

&lt;p class=&quot;note&quot;&gt;I like to visualize the SVG canvas with a &lt;code&gt;viewBox&lt;/code&gt; the same way as Google maps. You can zoom in to a specific region or area in Google maps; that area will be the only area visible, scaled up, inside the viewport of the browser. However, you know that the rest of the map is still there, but it's not visible because it extends beyond the boundaries of the viewport&amp;mdash;it's being clipped out.&lt;/p&gt;

&lt;p&gt;Now let’s try changing the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;min-x&amp;gt;&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;min-y&amp;gt;&lt;/code&gt; values. We’ll set both to &lt;code class=&quot;highlighter-rouge&quot;&gt;100&lt;/code&gt;. They can be any number you want. The width and height ratio will also be the same as width and height ratio of the viewport.&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;svg width=&quot;800&quot; height=&quot;600&quot; viewbox=&quot;100 100 200 150&quot;&amp;gt;
	&amp;lt;!-- SVG content drawn onto the SVG canvas --&amp;gt;
&amp;lt;/svg&amp;gt;
&lt;/pre&gt;

&lt;p&gt;The effect of applying &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox=&quot;100 100 200 150&quot;&lt;/code&gt; is also a crop effect like the one in the previous example. The graphic is cropped and scaled up to fill the entire viewport area.&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/viewbox-200-150-crop.jpg&quot; alt=&quot;&quot; /&gt;
	&lt;figcaption&gt;
		The result of &quot;cropping&quot; the graphic to a viewbox with an origin at (100, 100) and width 200 and height 150.
	&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Again, the user coordinate system is mapped to the viewport coordinate system—200 user units are mapped to 800 viewport units so that every user unit is equal to four viewport units. This results in a zoom-in effect like the one you can see in the above screenshot.&lt;/p&gt;

&lt;p&gt;Also note, at this point, that specifying non-zero values for the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;min-x&amp;gt;&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;min-y&amp;gt;&lt;/code&gt; values has a transformation effect on the graphic; more specifically, it is as though the SVG canvas was translated by 100 units to the top and 100 units to the left (&lt;code class=&quot;highlighter-rouge&quot;&gt;transform=&quot;translate(-100 -100)&quot;&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Indeed, as the specification states, &lt;q&gt;the effect of the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; attribute is that the user agent automatically supplies the appropriate transformation matrix to map the specified rectangle in user space to the bounds of a designated region (often, the viewport)&lt;/q&gt;.&lt;/p&gt;

&lt;p&gt;This is just a fancy way of saying what we already mentioned before: the graphic is cropped and then &lt;strong&gt;scaled&lt;/strong&gt; to fit into the viewport. The spec then goes on to add a note: &lt;q&gt;in some cases the user agent will need to supply a &lt;strong&gt;translate transformation&lt;/strong&gt; in addition to a scale transformation. For example, on an outermost svg element, &lt;strong&gt;a translate transformation will be needed if the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; attributes specifies values other than zero for &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;min-x&amp;gt;&lt;/code&gt; or &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;min-y&amp;gt;&lt;/code&gt;&lt;/strong&gt;.)&lt;/q&gt;&lt;/p&gt;

&lt;p&gt;To demonstrate the translation transformation even better, let’s try applying negative values (-100) to &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;min-x&amp;gt;&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;min-y&amp;gt;&lt;/code&gt;. The translation effect would be then similar to &lt;code class=&quot;highlighter-rouge&quot;&gt;transform=&quot;translate(100 100)&quot;&lt;/code&gt;; meaning that the graphic will be translated to the bottom and to the right after being cropped and scaled. If were to revisit the second to last example with a crop size of 400 by 300, and then add the new negative &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;min-x&amp;gt;&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;min-y&amp;gt;&lt;/code&gt; values, this would be our new code:&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;svg width=&quot;800&quot; height=&quot;600&quot; viewbox=&quot;-100 -100 400 300&quot;&amp;gt;
	&amp;lt;!-- SVG content drawn onto the SVG canvas --&amp;gt;
&amp;lt;/svg&amp;gt;
&lt;/pre&gt;

&lt;p&gt;The result of applying the above &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; transformation to the graphic is shown in the following image:&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/viewbox-400-300-crop-translate.jpg&quot; alt=&quot;&quot; /&gt;
	&lt;figcaption&gt;

	&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Note that, unlike the &lt;code class=&quot;highlighter-rouge&quot;&gt;transform&lt;/code&gt; attribute, the automatic transformation that is created due to a &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; does not affect the &lt;code class=&quot;highlighter-rouge&quot;&gt;x&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;y&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;width&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;height&lt;/code&gt; attributes on the element with the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; attribute. Thus, in the example above which shows an &lt;code class=&quot;highlighter-rouge&quot;&gt;svg&lt;/code&gt; element which has attributes &lt;code class=&quot;highlighter-rouge&quot;&gt;width&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;height&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt;, the &lt;code class=&quot;highlighter-rouge&quot;&gt;width&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;height&lt;/code&gt; attributes represent values in the coordinate system that exists before the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; transformation is applied. You can see this in the above examples as the initial (grey) viewport coordinate system remains unaffected even after using the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; attribute on the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;On the other hand, like the &lt;code class=&quot;highlighter-rouge&quot;&gt;transform&lt;/code&gt; attribute, it does establish a new coordinate system for all other attributes and for descendant elements. You can also see that in the above examples as the user coordinate system established is a new one—it does not remain as the initial user coordinate system which was identical to the viewport coordinate system before the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; was used. And any descendants of the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; will be positioned and sized in the &lt;strong&gt;new&lt;/strong&gt; user coordinate system, not the initial one.&lt;/p&gt;

&lt;p&gt;Our last &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; example is similar to the previous ones, but instead of cropping the canvas, we’re going to &lt;em&gt;extend&lt;/em&gt; it inside the viewport and see how it affects the graphic. We’re going to specify a viewbox with a width and height that are larger than those of the viewport, while also maintaining the aspect ratio of the viewport. We’ll deal with different aspect ratios in the next section.&lt;/p&gt;

&lt;p&gt;In this example, we’ll make the viewbox 1.5 times the size of the viewport.&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;svg width=&quot;800&quot; height=&quot;600&quot; viewbox=&quot;0 0 1200 900&quot;&amp;gt;
	&amp;lt;!-- SVG content drawn onto the SVG canvas --&amp;gt;
&amp;lt;/svg&amp;gt;
&lt;/pre&gt;

&lt;p&gt;What will happen now is that the user coordinate system is going to be scaled up to 1200x900. It will then be mapped to the viewport coordinate system so that every 1 unit in the user coordinate system is equal to &lt;code class=&quot;highlighter-rouge&quot;&gt;viewport-width / viewBox-width&lt;/code&gt; horizontally, and &lt;code class=&quot;highlighter-rouge&quot;&gt;viewport-height / viewBox-height&lt;/code&gt; units vertically in the viewport coordinate system. This means that, in this case, every one x-unit in the user coordinate system is equal to 0.66 x-units in the viewport coordinate system, and every one user y-unit is mapped to 0.66 viewport y-units.&lt;/p&gt;

&lt;p&gt;Of course, the best way to understand this is to visualize the result. The viewbox is scaled so that it fits inside the viewport as shown in the following image. And because the graphic is drawn on the canvas based on the new user coordinate system, not the viewport coordinate system, it will look smaller inside the viewport.&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/viewbox-1200-900.jpg&quot; alt=&quot;&quot; /&gt;
	&lt;figcaption&gt;
		A 1200x900 user coordinate system mapped into the 800x600 viewport coordinate system. The grey units represent the viewport coordinate system; the blue units represent the system established from the &lt;code&gt;viewBox&lt;/code&gt;.
	&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;So far, all of our examples have been in conformity with the viewport’s height to width aspect ratio. But what happens if the height and width specified in the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; have a different aspect ratio than that of the viewport’s? For example, suppose we set the dimensions of the viewbox to be 1000x500. The aspect ratio of height to width is no longer the same as that of the viewport. The result of using &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox = &quot;0 0 1000 500&quot;&lt;/code&gt; in our example looks like the following:&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/viewbox-1000-500.jpg&quot; alt=&quot;&quot; /&gt;
	&lt;figcaption&gt;
		The result of defining a 1000x500 user coordinate system in a 800x600 viewport.
	&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;The user coordinate system and hence the graphic is positioned inside the viewport so that:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The entire viewbox fits inside the viewport.&lt;/li&gt;
  &lt;li&gt;The aspect ratio of the viewbox is preserved. The viewbox was &lt;em&gt;not&lt;/em&gt; stretched to cover the viewport area.&lt;/li&gt;
  &lt;li&gt;THe viewbox is centered inside the viewport both vertically and horizontally.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is the default behavior. What controls this behavior? And what if we want to change the position of the viewbox inside the viewport? This is where the &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectRatio&lt;/code&gt; attribute comes in.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;preserveaspectratio&quot;&gt;The &lt;code&gt;preserveAspectRatio&lt;/code&gt; Attribute&lt;/h2&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectRatio&lt;/code&gt; attribute is used to force a uniform scaling for the purposes of preserving the aspect ratio of a graphic.&lt;/p&gt;

&lt;p&gt;If you define a user coordinate system with an aspect ratio different from that of the viewport’s, and if the browser were to stretch the viewbox to fit into the viewport as we’ve seen in previous examples, the difference in aspect ratios will cause the graphic to be distorted in either direction. So if the viewbox in the last example were to be stretched to fill the viewport in both directions, the graphic would look like so:&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/viewbox-1000-500-stretched.jpg&quot; alt=&quot;&quot; /&gt;
	&lt;figcaption&gt;
		The result of mapping the user coordinate system to the viewport without preserving its aspect ratio. The graphic is distorted and looks shrunk horizontally while also being stretched vertically.
	&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;The distortion is also clearly visible (and unwanted, of course) when using a viewbox value of &lt;code class=&quot;highlighter-rouge&quot;&gt;0 0 200 300&lt;/code&gt;, which would be smaller than the dimensions of the viewport. I chose this value in particular so that the viewbox matches the size of the bounding box of the parrot. If the browser were to stretch the graphic to fill the entire viewport, it would look like the so:&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/viewbox-200-300-stretched.jpg&quot; alt=&quot;&quot; /&gt;
	&lt;figcaption&gt;
		The result of mapping the user coordinate system to the viewport without preserving its aspect ratio. The graphic is distorted.
	&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectRatio&lt;/code&gt; attribute allows you to force uniform scaling of the viewbox, while maintaining the aspect ratio, and it allows you to specify how to position the viewbox inside the viewport if you don’t want it to be centered by default.&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;preserveAspectRatio-syntax&quot;&gt;The &lt;code&gt;preserveAspectRatio&lt;/code&gt; syntax&lt;/h3&gt;

&lt;p&gt;The official syntax for &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectRatio&lt;/code&gt; is:&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
preserveAspectRatio = defer? &amp;lt;align&amp;gt; &amp;lt;meetOrSlice&amp;gt;?
&lt;/pre&gt;

&lt;p&gt;It is usable on any element that establishes a new viewport (we’ll get into these in the next parts of the series).&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;defer&lt;/code&gt; argument is optional, and is used only when you’re applying &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectRatio&lt;/code&gt; to an &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;image&amp;gt;&lt;/code&gt;. It is ignored when used on any other element. Since &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;image&amp;gt;&lt;/code&gt; it outside the scope of this article, we’ll skip the &lt;code class=&quot;highlighter-rouge&quot;&gt;defer&lt;/code&gt; option for now.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;align&lt;/code&gt; parameter indicates whether to force uniform scaling and, if so, the alignment method to use in case the aspect ratio of the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; doesn’t match the aspect ratio of the viewport.&lt;/p&gt;

&lt;p&gt;If the &lt;code class=&quot;highlighter-rouge&quot;&gt;align&lt;/code&gt; value is set to &lt;code class=&quot;highlighter-rouge&quot;&gt;none&lt;/code&gt;, for example:&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
preserveAspectRatio = &quot;none&quot;
&lt;/pre&gt;

&lt;p&gt;The graphic will be scaled to fit inside the viewport without maintaining the aspect ratio, just like we saw in the last two examples.&lt;/p&gt;

&lt;p&gt;All other values of &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectRatio&lt;/code&gt; force uniform scaling while preserving the viewbox’s aspect ratio, and specify how to align the viewbox inside the viewport. We’ll get into the values of &lt;code class=&quot;highlighter-rouge&quot;&gt;align&lt;/code&gt; shortly.&lt;/p&gt;

&lt;p&gt;The last argument, &lt;code class=&quot;highlighter-rouge&quot;&gt;meetOrSlice&lt;/code&gt; is also optional, and it defaults to &lt;code class=&quot;highlighter-rouge&quot;&gt;meet&lt;/code&gt;. This argument specifies whether or not the entire &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; should be visible inside the viewport. If provided, it is separated from the &lt;code class=&quot;highlighter-rouge&quot;&gt;align&lt;/code&gt; parameter by one or more spaces. For example:&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
preserveAspectRatio = &quot;xMinYMin slice&quot;
&lt;/pre&gt;

&lt;p&gt;These values may seem foreign at first. To make understanding them easier and make them more familiar, you can think of the &lt;code class=&quot;highlighter-rouge&quot;&gt;meetOrSlice&lt;/code&gt; value as being similar to the &lt;code class=&quot;highlighter-rouge&quot;&gt;background-size&lt;/code&gt; values &lt;code class=&quot;highlighter-rouge&quot;&gt;contain&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;cover&lt;/code&gt;; they work pretty much the same. &lt;code class=&quot;highlighter-rouge&quot;&gt;meet&lt;/code&gt; is similar to &lt;code class=&quot;highlighter-rouge&quot;&gt;contain&lt;/code&gt;, and &lt;code class=&quot;highlighter-rouge&quot;&gt;slice&lt;/code&gt; is similar to &lt;code class=&quot;highlighter-rouge&quot;&gt;cover&lt;/code&gt;. Here are the definitions and meaning of each value:&lt;/p&gt;

&lt;dl&gt;
	&lt;dt&gt;meet (The default value)&lt;/dt&gt;
	&lt;dd&gt;
		Scale the graphic as much as possible while maintaining the following two criteria:
		&lt;ul&gt;
			&lt;li&gt;aspect ratio is preserved&lt;/li&gt;
			&lt;li&gt;the entire &lt;code&gt;viewBox&lt;/code&gt; is visible within the viewport&lt;/li&gt;
		&lt;/ul&gt;
		&lt;p&gt;
			In this case, if the aspect ratio of the graphic does not match the viewport, some of the viewport will extend beyond the bounds of the &lt;code&gt;viewBox&lt;/code&gt; (i.e., the area into which the &lt;code&gt;viewBox&lt;/code&gt; will draw will be smaller than the viewport). (See the last example of The viewBox section.) In this case, the boundaries of the `viewBox` are contained inside the viewport such that the boundaries *meet*.
		&lt;/p&gt;
		&lt;p class=&quot;note&quot;&gt;
			This value is similar to  &lt;code&gt;background-size: contain&lt;/code&gt;. The background image is scaled as much as possible while preserving its aspect ratio and making sure it fits entirely into the background painting area. If the aspect ratio of the background is not the same as that of the element it is being applied to, parts of the background painting area will not be covered by the background image.
		&lt;/p&gt;
	&lt;/dd&gt;
	&lt;dt&gt;slice&lt;/dt&gt;
	&lt;dd&gt;
		Scale the graphic so that the &lt;code&gt;viewBox&lt;/code&gt; covers the entire viewport area, while maintaining its aspect ratio. The &lt;code&gt;viewBox&lt;/code&gt; is scaled &lt;strong&gt;just enough&lt;/strong&gt; to cover the viewport area (in both dimensions), but it is not scaled any more than needed to achieve that. In other words, it is scaled to the smallest size such that the width and height of the &lt;code&gt;viewBox&lt;/code&gt; can completely cover the viewport.
		&lt;p&gt;
			In this case, if the aspect ratio of the &lt;code&gt;viewBox&lt;/code&gt; does not match the viewport, some of the &lt;code&gt;viewBox&lt;/code&gt; will extend beyond the bounds of the viewport (i.e., the area into which the &lt;code&gt;viewBox&lt;/code&gt; will draw is larger than the viewport). This will result in part of the `viewBox` being *sliced off*.
		&lt;/p&gt;
		&lt;p class=&quot;note&quot;&gt;
			You can think of this as being similar to &lt;code&gt;background-size: cover&lt;/code&gt;. In the case of a background image, the image is scaled while preserving its intrinsic aspect ratio (if any), to the smallest size such that both its width and its height can completely cover the background positioning area.
		&lt;/p&gt;
	&lt;/dd&gt;
&lt;/dl&gt;

&lt;p&gt;So, &lt;code class=&quot;highlighter-rouge&quot;&gt;meetOrSlice&lt;/code&gt; is used to specify whether or not the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; will be completely contained inside the viewport, or if it should be scaled as much as needed to cover the entire viewport, even if this means that a part of the viewbox will be “sliced off”.&lt;/p&gt;

&lt;p&gt;For example, if we were to apply &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; size of 200x300, and using both the &lt;code class=&quot;highlighter-rouge&quot;&gt;meet&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;slice&lt;/code&gt; values, keeping the &lt;code class=&quot;highlighter-rouge&quot;&gt;align&lt;/code&gt; value set to the default by the browser, the result for each value will look like the following:&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/viewbox-200-300-meet-vs-slice.jpg&quot; alt=&quot;&quot; /&gt;
	&lt;figcaption&gt;
		The result of applying the &lt;code&gt;meet&lt;/code&gt; parameter vs the &lt;code&gt;slice&lt;/code&gt; parameter on a &lt;code&gt;viewBox&lt;/code&gt; with an aspect ratio different from that of the viewport's aspect ratio.
		&lt;br /&gt;
		&lt;br /&gt;
		The default value for &lt;code&gt;align&lt;/code&gt; is &lt;code&gt;xMidYMid&lt;/code&gt;, so, in both cases, the graphic is scaled so that its mid axes align with the mid axes of the viewport.
	&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;align&lt;/code&gt; parameter takes one of nine values, or the &lt;code class=&quot;highlighter-rouge&quot;&gt;none&lt;/code&gt; value. Any value other than &lt;code class=&quot;highlighter-rouge&quot;&gt;none&lt;/code&gt; is used to uniformly scale the image preserving its aspect ratio, &lt;em&gt;&lt;strong&gt;and&lt;/strong&gt;&lt;/em&gt; it is also used to &lt;em&gt;align&lt;/em&gt; the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; inside the viewport.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;align&lt;/code&gt; values works similar to the way &lt;code class=&quot;highlighter-rouge&quot;&gt;background-position&lt;/code&gt; works when used with percentage values. You can think of the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; as being the background image. The way the positioning with &lt;code class=&quot;highlighter-rouge&quot;&gt;align&lt;/code&gt; differs from &lt;code class=&quot;highlighter-rouge&quot;&gt;background-position&lt;/code&gt; is that instead of positioning a specific point of the viewbox over a corresponding point of the viewport, it &lt;em&gt;aligns&lt;/em&gt; specific “axes” of the viewBox with corresponding “axes” of the viewport.&lt;/p&gt;

&lt;p&gt;In order to understand the meaning of each of the &lt;code class=&quot;highlighter-rouge&quot;&gt;align&lt;/code&gt; values, we’re going to first introduce each of the “axes”.&lt;/p&gt;

&lt;p&gt;Remember the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;min-x&amp;gt;&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;min-y&amp;gt;&lt;/code&gt; values of the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt;? We’re going to use each of these to define the “min-x” axis and “min-y” axis on the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt;. Additionally, we’re going to define two axes “max-x” and “max-y”, which will be positioned at &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;min-x&amp;gt; + &amp;lt;width&amp;gt;&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;min-y&amp;gt; + &amp;lt;height&amp;gt;&lt;/code&gt;, respectively. And last but not least, we’ll define two axes “mid-x” and “mid-y”, which are positioned at &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;min-x&amp;gt; + (&amp;lt;width&amp;gt;/2)&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;min-y&amp;gt; + (&amp;lt;height&amp;gt;/2)&lt;/code&gt;, respectively.&lt;/p&gt;

&lt;p&gt;Did that make things more confusing? If so, have a look at the following image to see what each of those axes represents. In the image, both &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;min-x&amp;gt;&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;min-y&amp;gt;&lt;/code&gt; are set to their default &lt;code class=&quot;highlighter-rouge&quot;&gt;0&lt;/code&gt; values. The &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; is set to &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox = &quot;0 0 300 300&quot;&lt;/code&gt;.&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/viewbox-x-y-axes.jpg&quot; alt=&quot;&quot; /&gt;
	&lt;figcaption&gt;
		The pink and orange solid lines represent the min-y and min-x values respectively. The dashed pink and orange lines represent the mid and max x and y values.
	&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;The dashed grey lines in the above image represent the mid-x and mid-y axes of the viewport. We’re going to use those to align the mid-x and mid-y of axes of the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; for some values. For the viewport, the min-x value is equal to 0, the min-y value is also 0, the max-x value is equal to the width of the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt;, the max-y value is equal to its height, and the mid-x and mid-y represent the middle values of the width and height, respectively.&lt;/p&gt;

&lt;p&gt;The alignment values are:&lt;/p&gt;

&lt;dl&gt;
				&lt;dt&gt;none&lt;/dt&gt;
				&lt;dd&gt;
					Do not force uniform scaling. Scale the graphic content of the given element non-uniformly (without preserving aspect ratio) if necessary such that the element's bounding box exactly matches the viewport rectangle.
					&lt;p class=&quot;note&quot;&gt;
						In other words, the &lt;code&gt;viewBox&lt;/code&gt; is stretched or shrunk as necssary so that it fills the entire viewport exactly, disregarding the aspect ratio. The graphic may be distorted.
					&lt;/p&gt;
					&lt;p&gt;
						(Note: if &lt;code&gt;&amp;lt;align&amp;gt;&lt;/code&gt; is none, then the optional &lt;code&gt;&amp;lt;meetOrSlice&amp;gt;&lt;/code&gt; value is ignored.)
					&lt;/p&gt;
				&lt;/dd&gt;
				&lt;dt&gt;xMinYMin&lt;/dt&gt;
				&lt;dd&gt;
					Force uniform scaling.
					&lt;br /&gt;
					Align the &lt;code&gt;&amp;lt;min-x&amp;gt;&lt;/code&gt; of the element's &lt;code&gt;viewBox&lt;/code&gt; with the smallest X value of the viewport.
					&lt;br /&gt;
					Align the &lt;code&gt;&amp;lt;min-y&amp;gt;&lt;/code&gt; of the element's &lt;code&gt;viewBox&lt;/code&gt; with the smallest Y value of the viewport.
					&lt;div class=&quot;note&quot;&gt;
						Think of this as being similar to &lt;code&gt;background-position: 0% 0%;&lt;/code&gt;.
					&lt;/div&gt;
				&lt;/dd&gt;
				&lt;dt&gt;xMinYMid&lt;/dt&gt;
				&lt;dd&gt;
					Force uniform scaling.
					&lt;br /&gt;
					Align the &lt;code&gt;&amp;lt;min-x&amp;gt;&lt;/code&gt; of the element's &lt;code&gt;viewBox&lt;/code&gt; with the smallest X value of the viewport.
					&lt;br /&gt;
					Align the midpoint Y value of the element's &lt;code&gt;viewBox&lt;/code&gt; with the midpoint Y value of the viewport.
					&lt;div class=&quot;note&quot;&gt;
						Think of this as being similar to &lt;code&gt;background-position: 0% 50%;&lt;/code&gt;.
					&lt;/div&gt;
				&lt;/dd&gt;
				&lt;dt&gt;xMinYMax&lt;/dt&gt;
				&lt;dd&gt;
					Force uniform scaling.
					&lt;br /&gt;
					Align the &lt;code&gt;&amp;lt;min-x&amp;gt;&lt;/code&gt; of the element's &lt;code&gt;viewBox&lt;/code&gt; with the smallest X value of the viewport.
					&lt;br /&gt;
					Align the &lt;code&gt;&amp;lt;min-y&amp;gt;&lt;/code&gt;+&lt;code&gt;&amp;lt;height&amp;gt;&lt;/code&gt; of the element's &lt;code&gt;viewBox&lt;/code&gt; with the maximum Y value of the viewport.
					&lt;div class=&quot;note&quot;&gt;
						Think of this as being similar to &lt;code&gt;background-position: 0% 100%;&lt;/code&gt;.
					&lt;/div&gt;
				&lt;/dd&gt;
				&lt;dt&gt;xMidYMin&lt;/dt&gt;
				&lt;dd&gt;
					Force uniform scaling.
					&lt;br /&gt;
					Align the midpoint X value of the element's &lt;code&gt;viewBox&lt;/code&gt; with the midpoint X value of the viewport.
					&lt;br /&gt;
					Align the &lt;code&gt;&amp;lt;min-y&amp;gt;&lt;/code&gt; of the element's &lt;code&gt;viewBox&lt;/code&gt; with the smallest Y value of the viewport.
					&lt;div class=&quot;note&quot;&gt;
						Think of this as being similar to &lt;code&gt;background-position: 50% 0%;&lt;/code&gt;.
					&lt;/div&gt;
				&lt;/dd&gt;
				&lt;dt&gt;xMidYMid (The default value)&lt;/dt&gt;
				&lt;dd&gt;						
					Force uniform scaling.
					&lt;br /&gt;
					Align the midpoint X value of the element's &lt;code&gt;viewBox&lt;/code&gt; with the midpoint X value of the viewport.
					&lt;br /&gt;
					Align the midpoint Y value of the element's &lt;code&gt;viewBox&lt;/code&gt; with the midpoint Y value of the viewport.
					&lt;div class=&quot;note&quot;&gt;
						Think of this as being similar to &lt;code&gt;background-position: 50% 50%;&lt;/code&gt;.
					&lt;/div&gt;
				&lt;/dd&gt;
				&lt;dt&gt;xMidYMax&lt;/dt&gt;
				&lt;dd&gt;
					Force uniform scaling.
					&lt;br /&gt;
					Align the midpoint X value of the element's &lt;code&gt;viewBox&lt;/code&gt; with the midpoint X value of the viewport.
					&lt;br /&gt;
					Align the &lt;code&gt;&amp;lt;min-y&amp;gt;&lt;/code&gt;+&lt;code&gt;&amp;lt;height&amp;gt;&lt;/code&gt; of the element's &lt;code&gt;viewBox&lt;/code&gt; with the maximum Y value of the viewport.
					&lt;div class=&quot;note&quot;&gt;
						Think of this as being similar to &lt;code&gt;background-position: 50% 100%;&lt;/code&gt;.
					&lt;/div&gt;
				&lt;/dd&gt;
				&lt;dt&gt;xMaxYMin&lt;/dt&gt;
				&lt;dd&gt;
					Force uniform scaling.
					&lt;br /&gt;
					Align the &lt;code&gt;&amp;lt;min-x&amp;gt;&lt;/code&gt;+&lt;code&gt;&amp;lt;width&amp;gt;&lt;/code&gt; of the element's &lt;code&gt;viewBox&lt;/code&gt; with the maximum X value of the viewport.
					&lt;br /&gt;
					Align the &lt;code&gt;&amp;lt;min-y&amp;gt;&lt;/code&gt; of the element's &lt;code&gt;viewBox&lt;/code&gt; with the smallest Y value of the viewport.
					&lt;div class=&quot;note&quot;&gt;
						Think of this as being similar to &lt;code&gt;background-position: 100% 0%;&lt;/code&gt;.
					&lt;/div&gt;
				&lt;/dd&gt;
				&lt;dt&gt;xMaxYMid&lt;/dt&gt;
				&lt;dd&gt;
					Force uniform scaling.
					&lt;br /&gt;
					Align the &lt;code&gt;&amp;lt;min-x&amp;gt;&lt;/code&gt;+&lt;code&gt;&amp;lt;width&amp;gt;&lt;/code&gt; of the element's &lt;code&gt;viewBox&lt;/code&gt; with the maximum X value of the viewport.
					&lt;br /&gt;
					Align the midpoint Y value of the element's &lt;code&gt;viewBox&lt;/code&gt; with the midpoint Y value of the viewport.
					&lt;div class=&quot;note&quot;&gt;
						Think of this as being similar to &lt;code&gt;background-position: 100% 50%;&lt;/code&gt;.
					&lt;/div&gt;
				&lt;/dd&gt;
				&lt;dt&gt;xMaxYMax&lt;/dt&gt;
				&lt;dd&gt;
					Force uniform scaling.
					&lt;br /&gt;
					Align the &lt;code&gt;&amp;lt;min-x&amp;gt;&lt;/code&gt;+&lt;code&gt;&amp;lt;width&amp;gt;&lt;/code&gt; of the element's &lt;code&gt;viewBox&lt;/code&gt; with the maximum X value of the viewport.
					&lt;br /&gt;
					Align the &lt;code&gt;&amp;lt;min-y&amp;gt;&lt;/code&gt;+&lt;code&gt;&amp;lt;height&amp;gt;&lt;/code&gt; of the element's &lt;code&gt;viewBox&lt;/code&gt; with the maximum Y value of the viewport.
					&lt;div class=&quot;note&quot;&gt;
						Think of this as being similar to &lt;code&gt;background-position: 100% 100%;&lt;/code&gt;.
					&lt;/div&gt;
				&lt;/dd&gt;
			&lt;/dl&gt;

&lt;p&gt;So, using the &lt;code class=&quot;highlighter-rouge&quot;&gt;align&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;meetOrSlice&lt;/code&gt; values of the &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectRatio&lt;/code&gt; attribute, you can specify whether or not to scale the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; uniformly, how to align it inside the viewport, and whether or not it should be entirely visible inside the viewport.&lt;/p&gt;

&lt;p&gt;Sometimes, and depending on the size of the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt;, some values may have similar results. For example, in the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox=&quot;0 0 200 300&quot;&lt;/code&gt; example from earlier, some alignments are identical using different &lt;code class=&quot;highlighter-rouge&quot;&gt;align&lt;/code&gt; values. The value of &lt;code class=&quot;highlighter-rouge&quot;&gt;meetOrSlice&lt;/code&gt; is set to &lt;code class=&quot;highlighter-rouge&quot;&gt;meet&lt;/code&gt; in this case so that the entire &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; is contained inside the viewport.&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/viewbox-meet-align-same.jpg&quot; alt=&quot;&quot; /&gt;
	&lt;figcaption&gt;
		The result of aligning the &lt;code&gt;viewBox&lt;/code&gt; using different align values. &lt;strong&gt;The &lt;code&gt;meetOrSlice&lt;/code&gt; value is set to &lt;code&gt;meet&lt;/code&gt;&lt;/strong&gt;.
	&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;If we were to change the &lt;code class=&quot;highlighter-rouge&quot;&gt;meetOrSlice&lt;/code&gt; value to &lt;code class=&quot;highlighter-rouge&quot;&gt;slice&lt;/code&gt;, we’d get different results for different values. Notice how the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; is stretched so that it covers the entire viewport. The x-axis is stretched so that the 200 units cover the viewport’s 800 units. In order for this to happen, and to maintain the aspect ratio of the viewbox, the y-axis gets “sliced off” at the bottom, but you can image it extending below the viewport’s height.&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/viewbox-slice-align-same.jpg&quot; alt=&quot;&quot; /&gt;
	&lt;figcaption&gt;
		The result of aligning the &lt;code&gt;viewBox&lt;/code&gt; using different align values. &lt;strong&gt;The &lt;code&gt;meetOrSlice&lt;/code&gt; value is set to &lt;code&gt;slice&lt;/code&gt;&lt;/strong&gt;.
	&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Of course, different &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; values will also look different from the 200x300 we’re using here. For the sake of brevity, I won’t get into more examples, and I’ll leave you to play with an interactive demo I created to help you better visualize how the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; and different &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectRatio&lt;/code&gt; values work together when different values are used. You can check the interactive demo out by visiting the link in the next section.&lt;/p&gt;

&lt;p&gt;But before we move to that, I just want to note that the mid-x, mid-y, max-x, and max-y values &lt;em&gt;change if the values of the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;min-x&amp;gt;&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;min-y&amp;gt;&lt;/code&gt; change&lt;/em&gt;. You can play with the interactive demo and change these values to see how the axes and thus the alignment of the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; changes accordingly.&lt;/p&gt;

&lt;p&gt;The following image shows the effect of using &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox = &quot;100 0 200 300&quot;&lt;/code&gt; on the position of the alignment axes. We’re using the same example as earlier, but instead of having the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;min-x&amp;gt;&lt;/code&gt; value be set to zero, we’re setting it to 100. You can set them to any number value you want. Notice how the min-x, mid-x, and max-x axes change. The &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectRatio&lt;/code&gt; used here is the default &lt;code class=&quot;highlighter-rouge&quot;&gt;xMidYMid meet&lt;/code&gt;, which means that the mid-* axes are aligned with the middle axes of the viewport.&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/viewbox-axes-changed-min-x-min-y.jpg&quot; alt=&quot;&quot; /&gt;
	&lt;figcaption&gt;
		The effect of changing the value of &lt;code&gt;&amp;lt;min-x&amp;gt;&lt;/code&gt; on the position of the x-axes. &lt;strong&gt;The translucent blue area shows the area which is considered to be the &lt;code&gt;viewBox&lt;/code&gt; area after changing the value of &lt;code&gt;&amp;lt;min-x&amp;gt;&lt;/code&gt;&lt;/strong&gt;.
	&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;interactive-demo&quot;&gt;The Interactive Demo&lt;/h2&gt;

&lt;p&gt;The best way to understand how the viewport, &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt;, and different &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectRatio&lt;/code&gt; values work and interact together is by visualizing them.&lt;/p&gt;

&lt;p&gt;For that purpose, I created a simple interactive demo that allows you to change the values of these attributes and see the result of the new values live.&lt;/p&gt;
&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/viewbox-demo-screenshot.jpg&quot; alt=&quot;&quot; /&gt;
	&lt;figcaption&gt;
		Screenshot of the interactive demo.
	&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;&lt;a class=&quot;button&quot; href=&quot;https://sarasoueidan.com/demos/interactive-svg-coordinate-system/index.html&quot;&gt;Check the interactive demo out.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope you found this article useful in understanding the SVG viewport, &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt;, and &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectRatio&lt;/code&gt; concepts. If you’d like to learn more about SVG coordinate systems, like nesting coordinate systems, establishing new ones, and transformations in SVG, stay tuned for the remaining parts of this series. You can subscribe to the RSS (link below) or follow me on Twitter to stay updated. Thank you very much for reading!&lt;/p&gt;

&lt;p class=&quot;note&quot;&gt;
	The SVG parrot illustration used is a freebie from &lt;a href=&quot;http://freepik.com&quot;&gt;Freepik.com&lt;/a&gt;.
&lt;/p&gt;

&lt;p&gt;If you enjoyed this article you may also be interested in:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;&lt;a href=&quot;http://sarasoueidan.com/blog/structuring-grouping-referencing-in-svg/&quot;&gt;Structuring, Grouping, and Referencing in SVG – The &lt;code&gt;&amp;lt;g&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;use&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;defs&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;symbol&amp;gt;&lt;/code&gt; Elements&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;http://sarasoueidan.com/blog/css-svg-clipping&quot;&gt;Clipping in CSS and SVG – The &lt;code&gt;clip-path&lt;/code&gt; Property and &lt;code&gt;&amp;lt;clipPath&amp;gt;&lt;/code&gt; Element&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

    
  </content>
 </entry>
 
 <entry>
   <title>Clipping in CSS and SVG — The <code>clip-path</code> Property and <code>&lt;clipPath&gt;</code> Element</title>
   <link href="http://sarasoueidan.com//blog/css-svg-clipping/"/>
   <id>https://sarasoueidan.com/blog/css-svg-clipping</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;CSS and SVG have a lot in common. A lot of the features that we have in CSS today were imported from SVG. One of these features is the &lt;em&gt;&lt;strong&gt;Clipping&lt;/strong&gt;&lt;/em&gt; operation. Both CSS and SVG allow us to &quot;clip&quot; elements into custom non-rectangular shapes. In this article we will go over the clipping techniques in both CSS and SVG, covering everything you need to know to get started.&lt;/p&gt;

&lt;p class=&quot;note warning&quot;&gt;
	Please note that the demos in this article may not work in your browser. You should check &lt;a href=&quot;https://github.com/awgreenblatt/css-graphics&quot;&gt;this compatibility table&lt;/a&gt; out for more information.&lt;strong&gt; You don't &lt;em&gt;need&lt;/em&gt; to view the live demos to follow up with the article.&lt;/strong&gt; Not all clipping features are implemented and some features may be buggy. The purpose of this article is to go over how clipping works in CSS and SVG, and serves as a reference for when these features are fully implemented.  I'm  also not including any vendor prefixes in the code samples here, but they are included in the live demos.
&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;clipping&quot;&gt;What is clipping?&lt;/h2&gt;

&lt;p&gt;Clipping is a graphical operation that allows you to fully or partially hide portions of an element. &lt;strong&gt;The clipped element can be any container or graphics element.&lt;/strong&gt; The portions of the element that are shown or hidden are determined by a &lt;em&gt;clipping path&lt;/em&gt;.&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/clipping-illustration.svg&quot; alt=&quot;&quot; /&gt;
&lt;/figure&gt;

&lt;p&gt;A &lt;strong&gt;clipping path&lt;/strong&gt; defines a region where everything on the “inside” of this region is allowed to show through but everything on the outside is “clipped out” and does not appear on the canvas. This region is known as the &lt;em&gt;clipping region&lt;/em&gt;. Any parts of the element that lie outside of a clipping region are not drawn. This includes any content, background, borders, text decoration, outline and visible scrolling mechanism of the element to which the clipping path is applied, and those of its descendants.&lt;/p&gt;

&lt;blockquote class=&quot;pull-quote&quot;&gt;
    The clipped element can be any container or graphics element.
&lt;/blockquote&gt;

&lt;p&gt;A clipping path is conceptually equivalent to a custom viewport for the element it applies to. It influences what parts of the element are rendered on the screen, but it does &lt;em&gt;not&lt;/em&gt; affect the element’s inherent geometry—the element will affect the flow around it as it normally would, and every other element on the page will still see and treat the element as if it were still rectangular, even if it’s clipped to a non-rectangular shape. If you want to change the way the content around the element flows and have it respond to the defined shape of the clip path, you can use the &lt;a href=&quot;http://www.w3.org/TR/css-shapes/&quot;&gt;CSS Shapes&lt;/a&gt; properties. If you want to learn more about how to do that, you can check &lt;a href=&quot;http://alistapart.com/article/css-shapes-101&quot;&gt;the&lt;/a&gt; &lt;a href=&quot;http://sarasoueidan.com/blog/css-shapes/&quot;&gt;articles&lt;/a&gt; I wrote about this topic.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;clip-path&quot;&gt;Clipping in CSS &amp;ndash; The &lt;code&gt;clip-path&lt;/code&gt; Property&lt;/h2&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;clip-path&lt;/code&gt; property is part of the CSS &lt;a href=&quot;http://www.w3.org/TR/2014/WD-css-masking-1-20140213/&quot;&gt;Masking Module&lt;/a&gt;. The clipping operation has been a part of SVG since 2000, and has moved into the CSS Masking module so that it now allows clipping HTML elements as well as SVG elements.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;clip-path&lt;/code&gt; property is used to specify a clipping path that is to be applied to an element.  Using &lt;code class=&quot;highlighter-rouge&quot;&gt;clip-path&lt;/code&gt;, you can apply an SVG &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;clipPath&amp;gt;&lt;/code&gt; to an element by referencing that path in the property value. You can also define a clipping path using one of the &lt;strong&gt;&lt;a href=&quot;http://dev.w3.org/csswg/css-shapes-2/#ltbasic-shapegt&quot;&gt;basic shapes&lt;/a&gt;&lt;/strong&gt; defined in the CSS Shapes module. These shapes can be created using &lt;strong&gt;shape functions&lt;/strong&gt;. The shape functions available are &lt;code class=&quot;highlighter-rouge&quot;&gt;polygon()&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;circle()&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;inset()&lt;/code&gt; (used to define inset rectangles), and &lt;code class=&quot;highlighter-rouge&quot;&gt;ellipse()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Applying a clipping path to an element using the &lt;code class=&quot;highlighter-rouge&quot;&gt;clip-path&lt;/code&gt; property is very simple and straightforward:&lt;/p&gt;

&lt;pre class=&quot;brush:css&quot;&gt;
/* Referencing an SVG clipPath */
.element {
	clip-path: url(#svgClipPathID);
}

/* Using a CSS basic shape function */
.element {
	clip-path: polygon(...); /* or one of the other shape functions */
}
&lt;/pre&gt;

&lt;p&gt;For example, if we were to define a polygonal clipping path using the &lt;code class=&quot;highlighter-rouge&quot;&gt;polygon()&lt;/code&gt; function, and then apply it to an image, the code would look like the following:&lt;/p&gt;

&lt;pre class=&quot;brush:css&quot;&gt;
img {
	clip-path: polygon(626px 463px,765px 236px,687px 31px,271px 100px,70px 10px,49px 250px,133px 406px,374px 462px,529px 393px);
}
&lt;/pre&gt;

&lt;p&gt;The result of applying the above line of CSS to an image would look like the following:&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/clipping-example-1.png&quot; alt=&quot;&quot; /&gt;
&lt;/figure&gt;

&lt;p&gt;&lt;a href=&quot;https://sarasoueidan.com/demos/css-svg-clipping/html-img-clipped-with-css/index.html&quot; class=&quot;button&quot;&gt;View Live Demo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The basic shape functions allow us to create a limited number of shapes; the most complex of these shapes is a polygon. If you want to use a more complex shape that looks like more than just a group of connected straight lines, you can use the SVG &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;clipPath&amp;gt;&lt;/code&gt; element. As the name &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;clipPath&amp;gt;&lt;/code&gt; implies, you can clip to any arbitrary path. This means that you can use the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;path&amp;gt;&lt;/code&gt; element to create any arbitrary path and use that as a clipping path.&lt;/p&gt;

&lt;p&gt;In our second example, we’re going to define and use an SVG &lt;code class=&quot;highlighter-rouge&quot;&gt;clipPath&lt;/code&gt;. The code for the clip path looks like the following:&lt;/p&gt;

&lt;pre class=&quot;brush:html;&quot;&gt;
&amp;lt;svg height=&quot;0&quot; width=&quot;0&quot;&amp;gt;
	&amp;lt;defs&amp;gt;
		&amp;lt;clipPath id=&quot;svgPath&quot;&amp;gt;
			&amp;lt;path fill=&quot;#FFFFFF&quot; stroke=&quot;#000000&quot; stroke-width=&quot;1.5794&quot; stroke-miterlimit=&quot;10&quot; d=&quot;M215,100.3c97.8-32.6,90.5-71.9,336-77.6
        c92.4-2.1,98.1,81.6,121.8,116.4c101.7,149.9,53.5,155.9,14.7,178c-96.4,54.9,5.4,269-257,115.1c-57-33.5-203,46.3-263.7,20.1
        c-33.5-14.5-132.5-45.5-95-111.1C125.9,246.6,98.6,139.1,215,100.3z&quot;/&amp;gt;
		&amp;lt;/clipPath&amp;gt;
	&amp;lt;/defs&amp;gt;
&amp;lt;/svg&amp;gt;
&lt;/pre&gt;

&lt;p&gt;And this is how the clipping path looks like; it is just a simple path with no fill and a black stroke.&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/the-svg-clippath.png&quot; alt=&quot;&quot; /&gt;
&lt;/figure&gt;

&lt;p&gt;We’ll be talking more about SVG &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;clipPath&amp;gt;&lt;/code&gt; in the next section. But for now, we’re just going to reference it and apply it to the image we have.&lt;/p&gt;

&lt;pre class=&quot;brush:css&quot;&gt;
img {
	clip-path: url(#svgPath);
}
&lt;/pre&gt;

&lt;p&gt;And the result would look like the following:&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/clipping-example-2.png&quot; alt=&quot;&quot; /&gt;
&lt;/figure&gt;

&lt;p&gt;&lt;a href=&quot;https://sarasoueidan.com/demos/css-svg-clipping/html-img-clipped-with-css-svg-clippath/index.html&quot; class=&quot;button&quot;&gt;View Live Demo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Indeed, the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;clipPath&amp;gt;&lt;/code&gt; element can contain any number of basic shapes (&lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;rect&amp;gt;&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;circle&amp;gt;&lt;/code&gt;, etc.), &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;path&amp;gt;&lt;/code&gt; elements, or even &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;text&amp;gt;&lt;/code&gt; elements.&lt;/p&gt;

&lt;p&gt;If you specify a piece of &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;text&amp;gt;&lt;/code&gt; inside a &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;clipPath&amp;gt;&lt;/code&gt;, that text will be used as a clipping path. Whatever’s under the text will be visible “through” it, and anything outside the text area will not be rendered.&lt;/p&gt;

&lt;p&gt;Note here that you can clip &lt;em&gt;anything&lt;/em&gt; to the text. This opens a door for a lot of possibilities and effects. You can use animated images (such as GIFs) or even videos, and then clip them to some text of your choice. The sky is the limit here.&lt;/p&gt;

&lt;p&gt;The following is an example of a piece of text used as a clipping path.&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;svg height=&quot;0&quot; width=&quot;0&quot;&amp;gt;
	&amp;lt;defs&amp;gt;
		&amp;lt;clipPath id=&quot;svgTextPath&quot;&amp;gt;
			&amp;lt;text x=&quot;0&quot; y=&quot;300&quot; textLength=&quot;800px&quot; lengthAdjust=&quot;spacing&quot; font-family=&quot;Vollkorn&quot; font-size=&quot;230px&quot; font-weight=&quot;700&quot; font-style=&quot;italic&quot;&amp;gt; Blossom &amp;lt;/text&amp;gt;
		&amp;lt;/clipPath&amp;gt;
	&amp;lt;/defs&amp;gt;
&amp;lt;/svg&amp;gt;
&lt;/pre&gt;

&lt;p&gt;The cool thing about SVG &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;text&amp;gt;&lt;/code&gt; is that it can be displayed using a custom font, just like HTML text can. In this example I’m using the &lt;a href=&quot;http://www.google.com/fonts/specimen/Vollkorn&quot;&gt;Vollkorn font&lt;/a&gt; from Google Web Fonts. I’ve set the width of the text to be the same as the width of the image, using the &lt;code class=&quot;highlighter-rouge&quot;&gt;textLength&lt;/code&gt; attribute, and positioned the text using the &lt;code class=&quot;highlighter-rouge&quot;&gt;x&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;y&lt;/code&gt; coordinates. Note here that the &lt;code class=&quot;highlighter-rouge&quot;&gt;x&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;y&lt;/code&gt; coordinates determine the position of the bottom left corner of the text (where the bottom stands for the baseline of the text).&lt;/p&gt;

&lt;p&gt;The result of applying the above text clip path to the image looks like so:&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/clipping-example-3.png&quot; alt=&quot;&quot; /&gt;
&lt;/figure&gt;

&lt;p&gt;&lt;a href=&quot;https://sarasoueidan.com/demos/css-svg-clipping/html-img-clipped-with-css-svg-clippath-text/index.html&quot; class=&quot;button&quot;&gt;View Live Demo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And as we mentioned, you can also use multiple basic shapes inside &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;clipPath&amp;gt;&lt;/code&gt;. We’ll dig into &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;clipPath&amp;gt;&lt;/code&gt; and its contents in the next section, so, for now, we’ll keep it simple. In this example I’m using multiple &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;circle&amp;gt;&lt;/code&gt;s, each with a different size and position.&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;svg height=&quot;0&quot; width=&quot;0&quot;&amp;gt;
    &amp;lt;defs&amp;gt;
        &amp;lt;clipPath id=&quot;svgPath&quot;&amp;gt;
            &amp;lt;circle stroke=&quot;#000000&quot; stroke-miterlimit=&quot;10&quot; cx=&quot;50&quot; cy=&quot;50&quot; r=&quot;40&quot; /&amp;gt;
            &amp;lt;circle stroke=&quot;#000000&quot; stroke-miterlimit=&quot;10&quot; cx=&quot;193.949&quot; cy=&quot;235&quot; r=&quot;74.576&quot;/&amp;gt;
            &amp;lt;circle stroke=&quot;#000000&quot; stroke-miterlimit=&quot;10&quot; cx=&quot;426.576&quot; cy=&quot;108.305&quot; r=&quot;47.034&quot;/&amp;gt;
            &amp;lt;circle stroke=&quot;#000000&quot; stroke-miterlimit=&quot;10&quot; cx=&quot;346.915&quot; cy=&quot;255.763&quot; r=&quot;43.644&quot;/&amp;gt;
            &amp;lt;circle stroke=&quot;#000000&quot; stroke-miterlimit=&quot;10&quot; cx=&quot;255.39&quot; cy=&quot;82.882&quot; r=&quot;35.17&quot;/&amp;gt;
            &amp;lt;!-- more circles... --&amp;gt;
        &amp;lt;/clipPath&amp;gt;
    &amp;lt;/defs&amp;gt;
&amp;lt;/svg&amp;gt;
&lt;/pre&gt;

&lt;p&gt;The image will show through these circles combined, but will not be rendered outside them.&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/clipping-example-4.png&quot; alt=&quot;&quot; /&gt;
&lt;/figure&gt;

&lt;p&gt;&lt;a href=&quot;https://sarasoueidan.com/demos/css-svg-clipping/html-img-clipped-with-css-svg-clippath-multiple-shapes/index.html&quot; class=&quot;button&quot;&gt;View Live Demo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we mentioned at the beginning of this article, you can apply clip paths using the &lt;code class=&quot;highlighter-rouge&quot;&gt;clip-path&lt;/code&gt; property to SVG elements too. In all of the above examples, the clipping paths were applied to an HTML &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;img&amp;gt;&lt;/code&gt;. In the following example, a clipping path is applied to the root &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; element. The same cherry blossoms image we used above is now part of the SVG, referenced using the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;image&amp;gt;&lt;/code&gt; element.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;image&amp;gt;&lt;/code&gt; element in SVG is used to include a graphic that can be either an entire SVG or a raster image. If it’s an SVG you’re referencing in &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;image&amp;gt;&lt;/code&gt;, the &lt;code class=&quot;highlighter-rouge&quot;&gt;width&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;height&lt;/code&gt; attributes will be used to establish the viewport of that SVG. If you’re referencing a raster image (which is what we’re doing here), the image will be scaled to fit in the specified &lt;code class=&quot;highlighter-rouge&quot;&gt;width&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;height&lt;/code&gt;. So I made sure the aspect ratio of the &lt;code class=&quot;highlighter-rouge&quot;&gt;width&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;height&lt;/code&gt; attribute match the aspect ratio of the image I’m using, to prevent it from being distorted.&lt;/p&gt;

&lt;p&gt;When you create an &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; document, you establish its viewport by specifying the width
and height of the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; element. Anything drawn outside the limits of the viewport will be clipped out and will not be displayed. You can establish a new custom viewport of your own with the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;clipPath&amp;gt;&lt;/code&gt; element.&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;svg height=&quot;500&quot; width=&quot;800&quot;&amp;gt;
    &amp;lt;image xlink:href=&quot;flowers.jpg&quot; x=&quot;0&quot; y=&quot;0&quot; width=&quot;800&quot; height=&quot;500&quot;/&amp;gt;
    &amp;lt;defs&amp;gt;
        &amp;lt;clipPath id=&quot;theSVGPath&quot;&amp;gt;
            &amp;lt;rect x=&quot;0&quot; y=&quot;0&quot; stroke=&quot;#000000&quot; stroke-miterlimit=&quot;10&quot; width=&quot;108&quot; height=&quot;500&quot;/&amp;gt;
            &amp;lt;rect x=&quot;121.5&quot; y=&quot;25.5&quot; stroke=&quot;#000000&quot; stroke-miterlimit=&quot;10&quot; width=&quot;55&quot; height=&quot;455&quot;/&amp;gt;
            &amp;lt;rect x=&quot;192.5&quot; y=&quot;9.5&quot; stroke=&quot;#000000&quot; stroke-miterlimit=&quot;10&quot; width=&quot;60&quot; height=&quot;484&quot;/&amp;gt;
            &amp;lt;rect x=&quot;271.5&quot; y=&quot;44.5&quot; stroke=&quot;#000000&quot; stroke-miterlimit=&quot;10&quot; width=&quot;63&quot; height=&quot;416&quot;/&amp;gt;
            &amp;lt;rect x=&quot;349.5&quot; y=&quot;25.5&quot; stroke=&quot;#000000&quot; stroke-miterlimit=&quot;10&quot; width=&quot;208&quot; height=&quot;447&quot;/&amp;gt;
            &amp;lt;rect x=&quot;574.5&quot; y=&quot;44.5&quot; stroke=&quot;#000000&quot; stroke-miterlimit=&quot;10&quot; width=&quot;60&quot; height=&quot;446&quot;/&amp;gt;
            &amp;lt;rect x=&quot;644.5&quot; y=&quot;9.5&quot; stroke=&quot;#000000&quot; stroke-miterlimit=&quot;10&quot; width=&quot;68&quot; height=&quot;471&quot;/&amp;gt;
            &amp;lt;rect x=&quot;736.5&quot; y=&quot;18.5&quot; stroke=&quot;#000000&quot; stroke-miterlimit=&quot;10&quot; width=&quot;49&quot; height=&quot;462&quot;/&amp;gt;
        &amp;lt;/clipPath&amp;gt;
    &amp;lt;/defs&amp;gt;
&amp;lt;/svg&amp;gt;
&lt;/pre&gt;

&lt;p&gt;Using &lt;code class=&quot;highlighter-rouge&quot;&gt;clip-path&lt;/code&gt;, we’re going to apply the &lt;code class=&quot;highlighter-rouge&quot;&gt;clipPath&lt;/code&gt; to the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;svg&amp;gt;&lt;/code&gt;:&lt;/p&gt;

&lt;pre class=&quot;brush:css&quot;&gt;
svg {
    clip-path: url(#theSVGPath);
}
&lt;/pre&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/clipping-example-5.png&quot; alt=&quot;&quot; /&gt;
&lt;/figure&gt;
&lt;p&gt;&lt;a href=&quot;https://sarasoueidan.com/demos/css-svg-clipping/svg-image-clipped-with-css/index.html&quot; class=&quot;button&quot;&gt;View Live Demo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;More examples applying a clipping path to an SVG element in the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;clipPath&amp;gt;&lt;/code&gt; section below.&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;reference-box&quot;&gt;A Clipping Path's Reference Box&lt;/h3&gt;

&lt;p&gt;In addition to the clipping path itself, you can define a &lt;em&gt;reference box&lt;/em&gt; in the &lt;code class=&quot;highlighter-rouge&quot;&gt;clip-path&lt;/code&gt; property when the clipping path applied is a &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;basic-shape&amp;gt;&lt;/code&gt;; that is, a clipping path created using one of the basic shape functions. The reference box is hence only specified for CSS shapes used as clip paths, not for SVG &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;clipPath&amp;gt;&lt;/code&gt;s. For an SVG &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;clipPath&amp;gt;&lt;/code&gt;, the reference box is the border box of an HTML element.&lt;/p&gt;

&lt;p&gt;So a reference box is specified for a &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;basic-shape&amp;gt;&lt;/code&gt; clip path. If the element being clipped is an HTML element, the reference box can be one of the four basic box model boxes: &lt;code class=&quot;highlighter-rouge&quot;&gt;margin-box&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;border-box&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;padding-box&lt;/code&gt;, or &lt;code class=&quot;highlighter-rouge&quot;&gt;content-box&lt;/code&gt;. Each of these is self-explanatory.&lt;/p&gt;

&lt;p&gt;If the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;basic-shape&amp;gt;&lt;/code&gt; clip path is applied to an SVG element, the reference box can be set to one of three keyword values:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;fill-box – uses the object bounding box as the reference.&lt;/li&gt;
  &lt;li&gt;stroke-box – uses the stroke bounding box as the reference.&lt;/li&gt;
  &lt;li&gt;view-box – uses the uses the nearest SVG viewport as the reference box if no &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; is specified. If a &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; is indeed specified, then the coordinate system is established by the origin and dimensions specified by the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you set any of the CSS box model boxes as a reference box for an SVG element, the &lt;code class=&quot;highlighter-rouge&quot;&gt;fill-box&lt;/code&gt; will be used. And if you use one of the SVG reference boxes on an HTML element, the &lt;code class=&quot;highlighter-rouge&quot;&gt;border-box&lt;/code&gt; will be used.&lt;/p&gt;

&lt;pre class=&quot;brush:css&quot;&gt;
.element {
    clip-path: polygon(...) padding-box;
}
&lt;/pre&gt;

&lt;p&gt;If &lt;em&gt;only&lt;/em&gt; a reference box is specified in the &lt;code class=&quot;highlighter-rouge&quot;&gt;clip-path&lt;/code&gt; property—that is, no basic shape is defined—the browser uses the edges of the specified box, including any corner shaping (e.g. defined by the &lt;code class=&quot;highlighter-rouge&quot;&gt;border-radius&lt;/code&gt; property), as clipping path.&lt;/p&gt;

&lt;p&gt;For example, using the following snippet, the element will be clipped to the rounded corners specified by &lt;code class=&quot;highlighter-rouge&quot;&gt;border-radius&lt;/code&gt;:&lt;/p&gt;

&lt;pre class=&quot;brush:css&quot;&gt;
.el {
    clip-path: border-box;
    border-radius: 25%;
}
&lt;/pre&gt;

&lt;p class=&quot;note&quot;&gt;Note that at the time of writing of this article, specifying a reference box in the `clip-path` property doesn't work in Webkit because it's not yet implemented.&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;clip-path-notes&quot;&gt;&lt;code&gt;clip-path&lt;/code&gt; Notes: Stacking Contexts, Pointer Events and Animations&lt;/h3&gt;

&lt;p&gt;It is important to know that any value other than the default &lt;code class=&quot;highlighter-rouge&quot;&gt;none&lt;/code&gt; for the &lt;code class=&quot;highlighter-rouge&quot;&gt;clip-path&lt;/code&gt; property results in the creation of a stacking context on the element the same way the &lt;code class=&quot;highlighter-rouge&quot;&gt;opacity&lt;/code&gt; property does.&lt;/p&gt;

&lt;blockquote class=&quot;pull-quote&quot;&gt;
    Any value other than the default `none` for the `clip-path` property results in the creation of a stacking context on the element.
&lt;/blockquote&gt;

&lt;p&gt;Furthermore, according to the Masking specification, &lt;strong&gt;pointer events must not be dispatched on the clipped-out (non-visible) regions of a shape&lt;/strong&gt;. This means that the element should not respond to pointer events outside the remaining visible area.&lt;/p&gt;

&lt;p&gt;A clipping path can also be animated. If the clipping path used is an SVG &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;clipPath&amp;gt;&lt;/code&gt;, it can be animated by including an animation inside it (See next section for details). If the cipping path is a basic shape created using a basic shape function, it can be animated using CSS animations and transitions. For details on how to animate a shape created using a shape function, check out the &lt;a href=&quot;http://sarasoueidan.com/blog/animating-css-shapes&quot;&gt;Animating CSS Shapes with CSS Animations and Transitions&lt;/a&gt; article I wrote a while back.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;svg-clippath-element&quot;&gt;Clipping in SVG &amp;ndash; The &lt;code&gt;&amp;lt;clipPath&amp;gt;&lt;/code&gt; Element&lt;/h2&gt;

&lt;p&gt;In SVG, the &lt;code class=&quot;highlighter-rouge&quot;&gt;clipPath&lt;/code&gt; element is used to define a clipping path to clip elements. If you don’t want to use CSS to apply the clipping path to an element, you can do it in SVG using the &lt;code class=&quot;highlighter-rouge&quot;&gt;clip-path&lt;/code&gt; presentation attribute.&lt;/p&gt;

&lt;p class=&quot;note&quot;&gt;
Have you seen/read my &quot;Styling and Animating Scalable Vector Graphics with CSS&quot; slides? If not, you may want to have a look at them for more information about SVG presentation attributes and CSS properties used to style SVG elements. You can check them out &lt;a href=&quot;https://docs.google.com/presentation/d/1Iuvf3saPCJepVJBDNNDSmSsA0_rwtRYehSmmSSLYFVQ/pub?start=false&amp;amp;loop=false&amp;amp;delayms=3000#slide=id.p&quot;&gt;here&lt;/a&gt;.
&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;svg&amp;gt;
    &amp;lt;defs&amp;gt;
        &amp;lt;clipPath id=&quot;myClippingPath&quot;&amp;gt;
            &amp;lt;!-- ... --&amp;gt;
        &amp;lt;/clipPath&amp;gt;
    &amp;lt;/defs&amp;gt;
    &amp;lt;!-- the element you want to apply the clipPath to can be any SVG element --&amp;gt;
    &amp;lt;g id=&quot;my-graphic&quot; clip-path=&quot;url(#myClippingPath)&quot;&amp;gt;
        &amp;lt;!-- ... --&amp;gt;
    &amp;lt;/g&amp;gt;
&amp;lt;/svg&amp;gt;
&lt;/pre&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;contents-of-clippath&quot;&gt;Contents Of a &lt;code&gt;&amp;lt;clipPath&amp;gt;&lt;/code&gt;&lt;/h3&gt;

&lt;p&gt;We mentioned earlier that an SVG &lt;code class=&quot;highlighter-rouge&quot;&gt;clipPath&lt;/code&gt; can contain any number of basic shapes, arbitrary &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;path&amp;gt;&lt;/code&gt;s, and/or &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;text&amp;gt;&lt;/code&gt; elements. It can even contain more than that, and this is where it can get interesting.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;clipPath&amp;gt;&lt;/code&gt; content can be descriptive (&lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;title&amp;gt;&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;desc&amp;gt;&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;metadata&amp;gt;&lt;/code&gt;). It can also be a shape (&lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;circle&amp;gt;&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;ellipse&amp;gt;&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;line&amp;gt;&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;path&amp;gt;&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;polygon&amp;gt;&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;polyline&amp;gt;&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;rect&amp;gt;&lt;/code&gt;) or a &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;text&amp;gt;&lt;/code&gt;. A &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;clipPath&amp;gt;&lt;/code&gt; can also contain a &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;use&amp;gt;&lt;/code&gt; element or a &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;script&amp;gt;&lt;/code&gt;. Note that &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;use&amp;gt;&lt;/code&gt; in &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;clipPath&amp;gt;&lt;/code&gt; can only reference the simple SVG shapes mentioned above—it cannot be used to reference groups inside &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;clipPath&amp;gt;&lt;/code&gt;, for example; that simply won’t work.&lt;/p&gt;

&lt;p&gt;And last but not least, a &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;clipPath&amp;gt;&lt;/code&gt; can contain an &lt;strong&gt;animation&lt;/strong&gt; using &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;animate&amp;gt;&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;animateColor&amp;gt;&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;animateMotion&amp;gt;&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;animateTransform&amp;gt;&lt;/code&gt;, or &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;set&amp;gt;&lt;/code&gt;. This opens a door for a lot of creativity, as you can imagine.&lt;/p&gt;

&lt;p&gt;To demonstrate, I’m just going to add a simple animation to the demo using multiple &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;circle&amp;gt;&lt;/code&gt;s as a clipping path. Every &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;circle&amp;gt;&lt;/code&gt; will get an animation. Because I want to keep it simple, I’m just gonna use the same animation on all of the circles. You can create fancier effects using different effects and playing with animation delays, of course. But, since this is a 101 article, I’m gonna stay on the simple side. The code with the animations look like so:&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;svg height=&quot;0&quot; width=&quot;0&quot;&amp;gt;
    &amp;lt;defs&amp;gt;
        &amp;lt;clipPath id=&quot;svgPath&quot;&amp;gt;
            &amp;lt;circle stroke=&quot;#000000&quot; stroke-miterlimit=&quot;10&quot; cx=&quot;50&quot; cy=&quot;50&quot; r=&quot;40&quot;&amp;gt;
                        &amp;lt;animate
                            attributeName=&quot;r&quot;
                            attributeType=&quot;XML&quot;
                            from=&quot;0&quot; to=&quot;250&quot;
                            begin=&quot;0s&quot; dur=&quot;3s&quot;
                            fill=&quot;freeze&quot;
                            repeatCount=&quot;indefinite&quot;/&amp;gt;
                    &amp;lt;/circle&amp;gt;
            &amp;lt;circle stroke=&quot;#000000&quot; stroke-miterlimit=&quot;10&quot; cx=&quot;193.949&quot; cy=&quot;235&quot; r=&quot;74.576&quot;&amp;gt;
                &amp;lt;animate
                    attributeName=&quot;r&quot;
                    attributeType=&quot;XML&quot;
                    from=&quot;0&quot; to=&quot;250&quot;
                    begin=&quot;0s&quot; dur=&quot;3s&quot;
                    fill=&quot;freeze&quot;
                    repeatCount=&quot;indefinite&quot;/&amp;gt;
            &amp;lt;/circle&amp;gt;
            &amp;lt;!-- ... --&amp;gt;
        &amp;lt;/clipPath&amp;gt;
    &amp;lt;/defs&amp;gt;
&amp;lt;/svg&amp;gt;
&lt;/pre&gt;

&lt;p&gt;The animation specified for each circle will animate the size of the circle—more specifically, its radius (&lt;code class=&quot;highlighter-rouge&quot;&gt;r&lt;/code&gt;)—over the course of three seconds, from &lt;code class=&quot;highlighter-rouge&quot;&gt;0&lt;/code&gt; to &lt;code class=&quot;highlighter-rouge&quot;&gt;250&lt;/code&gt; pixels. I’ve also set the animation to repeat indefinitely.&lt;/p&gt;

&lt;p&gt;Click on the following button to view the live demo. But before you do, note that there is a bug (see bug details &lt;a href=&quot;https://code.google.com/p/chromium/issues/detail?id=391604&quot;&gt;here&lt;/a&gt;), so the demo may not work for you if you’re on Chrome or Safari. For now, I recommend using Firefox to see the working live demo, until the bug has been fixed.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://sarasoueidan.com/demos/css-svg-clipping/html-img-clipped-with-css-svg-clippath-animated/index.html&quot; class=&quot;button&quot;&gt;View Animated clipPath Demo Live&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note that the content of a &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;clipPath&amp;gt;&lt;/code&gt; also cannot involve groups (&lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;g&amp;gt;&lt;/code&gt;s). For example, if we were to add a group element to the demo that uses multiple circles as a clipping path, the demo will no longer work—the clipping path will no longer be applied to the image.&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;svg height=&quot;0&quot; width=&quot;0&quot;&amp;gt;
    &amp;lt;defs&amp;gt;
        &amp;lt;clipPath id=&quot;svgPath&quot;&amp;gt; &amp;lt;!-- WILL NOT WORK --&amp;gt;
            &amp;lt;g&amp;gt; &amp;lt;!-- WILL NOT WORK --&amp;gt;
                &amp;lt;circle stroke=&quot;#000000&quot; stroke-miterlimit=&quot;10&quot; cx=&quot;193.949&quot; cy=&quot;235&quot; r=&quot;74.576&quot;/&amp;gt;
                &amp;lt;circle stroke=&quot;#000000&quot; stroke-miterlimit=&quot;10&quot; cx=&quot;426.576&quot; cy=&quot;108.305&quot; r=&quot;47.034&quot;/&amp;gt;
                &amp;lt;!-- ... --&amp;gt;
            &amp;lt;/g&amp;gt;
        &amp;lt;/clipPath&amp;gt;
    &amp;lt;/defs&amp;gt;
&amp;lt;/svg&amp;gt;
&lt;/pre&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;clippathunits&quot;&gt;The &lt;code&gt;clipPathUnits&lt;/code&gt; Attribute&lt;/h3&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;clipPath&amp;gt;&lt;/code&gt; element can have several attributes, including &lt;code class=&quot;highlighter-rouge&quot;&gt;id&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;class&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;transform&lt;/code&gt;s, and &lt;a href=&quot;http://www.w3.org/TR/2011/REC-SVG11-20110816/intro.html#TermPresentationAttribute&quot;&gt;presentation attributes&lt;/a&gt; like &lt;code class=&quot;highlighter-rouge&quot;&gt;fill&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;stroke&lt;/code&gt;, among &lt;a href=&quot;http://www.w3.org/TR/2011/REC-SVG11-20110816/styling.html#SVGStylingProperties&quot;&gt;many others&lt;/a&gt;. But the one attribute that stands out, and that is particularly useful, is the &lt;code class=&quot;highlighter-rouge&quot;&gt;clipPathUnits&lt;/code&gt; attribute.&lt;/p&gt;

&lt;blockquote class=&quot;pull-quote&quot;&gt;
    The &lt;code&gt;clipPathUnits&lt;/code&gt; attribute is used to specify a coordinate system for the contents of the &lt;code&gt;&amp;lt;clipPath&amp;gt;&lt;/code&gt;.
&lt;/blockquote&gt;
&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;clipPathUnits&lt;/code&gt; attribute is used to specify a coordinate system for the contents of the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;clipPath&amp;gt;&lt;/code&gt;. It takes one of two values: &lt;code class=&quot;highlighter-rouge&quot;&gt;objectBoundingBox&lt;/code&gt; or &lt;code class=&quot;highlighter-rouge&quot;&gt;userSpaceOnUse&lt;/code&gt;. The default value is &lt;code class=&quot;highlighter-rouge&quot;&gt;userSpaceOnUse&lt;/code&gt;.&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
clipPathUnits = &quot;userSpaceOnUse | objectBoundingBox&quot;
&lt;/pre&gt;

&lt;dl&gt;
    &lt;dt&gt;userSpaceOnUse&lt;/dt&gt;
    &lt;dd&gt;
        The contents of the &lt;code&gt;clipPath&lt;/code&gt; represent values in the current &lt;em&gt;&lt;strong&gt;user coordinate system&lt;/strong&gt;&lt;/em&gt; in place at the time when the &lt;code&gt;clipPath&lt;/code&gt; element is referenced (i.e., the user coordinate system for the element referencing the &lt;code&gt;clipPath&lt;/code&gt; element via the &lt;code&gt;clip-path&lt;/code&gt; property).
        &lt;p&gt;
            The current &lt;strong&gt;user coordinate system&lt;/strong&gt; (a.k.a &lt;em&gt;&lt;strong&gt;local coordinate system&lt;/strong&gt;&lt;/em&gt;) is the coordinate system that is currently active and which is used to define how coordinates and lengths are located and computed. The user coordinate system for an HTML element with an associated CSS box model is different from that of an SVG element with no such box model.
        &lt;/p&gt;
        &lt;p&gt;
            For elements that have an associated CSS layout box, the current user coordinate system has its origin at the top left corner of a reference box and one unit equals one CSS pixel. The viewport for resolving percentage values is defined by the width and height of the reference box. I'm sure you're already familiar with this. So if you have a &lt;code&gt;&amp;lt;clipPath&amp;gt;&lt;/code&gt; containing a &lt;code&gt;&amp;lt;circle&amp;gt;&lt;/code&gt; whose center is positioned at &lt;code&gt;cx = &quot;100&quot;&lt;/code&gt; and &lt;code&gt;cy = &quot;100&quot;&lt;/code&gt;, the center will be positioned 100 pixels to the left and 100 pixels down inside the boundaries of the reference box.
        &lt;/p&gt;
        &lt;p&gt;
            If the element is an SVG element and thus does not have an associated CSS layout box, the current user coordinate system has its origin at the top left corner of the element's &lt;strong&gt;nearest viewport&lt;/strong&gt;. In most cases, the nearest viewport is the viewport established by the width and height of the closest &lt;code&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; ancestor. If you're not nesting &lt;code&gt;&amp;lt;svg&amp;gt;&lt;/code&gt;s, it's simply the viewport you establish on the root &lt;code&gt;&amp;lt;svg&amp;gt;&lt;/code&gt;.
        &lt;/p&gt;
        &lt;p&gt;
            Note that the coordinate system on an SVG element can be modified using the &lt;code&gt;viewBox&lt;/code&gt; attribute, among other attributes which may contribute to changing the coordinate system. However, that's outside the scope of this article. So in this article I'm going to work under the assumption that no &lt;code&gt;viewBox&lt;/code&gt; is modified, and hence the browser will use the default coordinate system with the origin at the top left corner, and dimensions equal to the dimensions of the &lt;code&gt;&amp;lt;svg&amp;gt;&lt;/code&gt;.
        &lt;/p&gt;
    &lt;/dd&gt;
    &lt;dt&gt;objectBoundingBox&lt;/dt&gt;
    &lt;dd&gt;
        The coordinate system has its origin at the top left corner of the &lt;strong&gt;&lt;em&gt;bounding box&lt;/em&gt;&lt;/strong&gt; of the element to which the clipping path applies to and the same width and height of this bounding box. A bounding box is the object bounding box for all SVG elements (it contains only an element's geometric shape) and the border box for HTML elements with an associated box model.
        &lt;p&gt;
            This value is particularly useful for SVG elements because it allows you to apply the clip path to the boundaries of the element itself, not the coordinate system on use. To demonstrate, here is an image showing the result of applying the clip path to an image inside an SVG canvas using &lt;code&gt;userSpaceOnUse&lt;/code&gt; versus &lt;code&gt;objectBoundingBox&lt;/code&gt;. The grey border represents the border of the &lt;code&gt;&amp;lt;svg&amp;gt;&lt;/code&gt; element where the viewport is set. For the image on the right, I've added a grey border around the clipped image just to show the boundaries of the bounding box.
        &lt;/p&gt;
        &lt;figure&gt;
            &lt;img src=&quot;https://sarasoueidan.com/images/clippathunits.png&quot; alt=&quot;&quot; /&gt;
            &lt;figcaption&gt;The result of applying the &lt;code&gt;clipPath&lt;/code&gt; to an image inside the SVG canvas using &lt;code&gt;userSpaceOnUse&lt;/code&gt; (left) and &lt;code&gt;objectBoundingBox&lt;/code&gt; (right). &lt;/figcaption&gt;
        &lt;/figure&gt;
        &lt;p&gt;
            In the image on the left, the clipping path is positioned in the coordinate system established on the viewport of the SVG. When using &lt;code&gt;objectBoundingBox&lt;/code&gt;, the bounding box of the image itself is used as the coordinate system of the clipping path.
        &lt;/p&gt;
        &lt;p&gt;
            One important thing to note here is that &lt;strong&gt;when you use the &lt;code&gt;objectBoundingBox&lt;/code&gt; value, the coordinates specified for the contents of the &lt;code&gt;&amp;lt;clipPath&amp;gt;&lt;/code&gt; must be in the range [0, 1]&lt;/strong&gt;&amp;mdash;the coordinate system becomes a unit system, and the coordinates of the shapes inside a &lt;code&gt;clipPath&lt;/code&gt; have to be fractions in that range.
        &lt;/p&gt;
        &lt;figure&gt;
            &lt;img src=&quot;https://sarasoueidan.com/images/clippathunits-system.jpg&quot; alt=&quot;&quot; /&gt;
            &lt;figcaption&gt;
                The coordinate system used for the &lt;code&gt;objectBoundingBox&lt;/code&gt; value on the right, versus that used for the &lt;code&gt;userSpaceOnuse&lt;/code&gt; on the left.
            &lt;/figcaption&gt;
        &lt;/figure&gt;
        &lt;p&gt;
            For example, if the clip path being applied to an element contains a circle positioned so that its center lies at the center of the clipped element:
        &lt;/p&gt;
        &lt;pre class=&quot;brush:html&quot;&gt;
            &amp;lt;clipPath&amp;gt;
                &amp;lt;circle cx=&quot;350&quot; cy=&quot;350&quot; r=&quot;300&quot; /&amp;gt;
            &amp;lt;/clipPath&amp;gt;
        &lt;/pre&gt;
        &lt;p&gt;
            the circle position (and radius) would be expressed in fractions like so:
        &lt;/p&gt;
        &lt;pre class=&quot;brush:html&quot;&gt;
            &amp;lt;clipPath clipPathUnits=&quot;objectBoundingBox&quot;&amp;gt;
                &amp;lt;circle cx=&quot;.5&quot; cy=&quot;.5&quot; r=&quot;.45&quot; /&amp;gt;
            &amp;lt;/clipPath&amp;gt;
        &lt;/pre&gt;
        &lt;p&gt;
            The fractions are like percentage values in this case.
        &lt;/p&gt;
    &lt;/dd&gt;
&lt;/dl&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;-svg-clippath-notes&quot;&gt;&lt;code&gt;&amp;lt;clipPath&amp;gt;&lt;/code&gt; Notes&lt;/h3&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;clipPath&lt;/code&gt; elements are never rendered directly; their only usage is as something that can be referenced using the &lt;code class=&quot;highlighter-rouge&quot;&gt;clip-path&lt;/code&gt; property. The &lt;code class=&quot;highlighter-rouge&quot;&gt;display&lt;/code&gt; property does not apply to the &lt;code class=&quot;highlighter-rouge&quot;&gt;clipPath&lt;/code&gt; element; thus, &lt;code class=&quot;highlighter-rouge&quot;&gt;clipPath&lt;/code&gt; elements are not directly rendered even if the &lt;code class=&quot;highlighter-rouge&quot;&gt;display&lt;/code&gt; property is set to a value other than &lt;code class=&quot;highlighter-rouge&quot;&gt;none&lt;/code&gt;, and &lt;code class=&quot;highlighter-rouge&quot;&gt;clipPath&lt;/code&gt; elements are available for referencing even when the &lt;code class=&quot;highlighter-rouge&quot;&gt;display&lt;/code&gt; property on the &lt;code class=&quot;highlighter-rouge&quot;&gt;clipPath&lt;/code&gt; element or any of its ancestors is set to &lt;code class=&quot;highlighter-rouge&quot;&gt;none&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Remember what we said earlier about pointer events when an HTML element is clipped? The same standard behavior is defined in the SVG Clipping and Masking specification: By default, pointer-events must not be dispatched on the clipped (non-visible) regions of an SVG element. The spec then mentions that &lt;q&gt;later versions of SVG may define new properties to enable fine-grained control over the interactions between hit testing and clipping&lt;/q&gt;.&lt;/p&gt;

&lt;p&gt;Firefox implements the same non-standard behavior we mentioned before—areas outside the clipping regions do not respond to pointer events.&lt;/p&gt;

&lt;p&gt;Even though Chrome implements the standard behavior for the &lt;code class=&quot;highlighter-rouge&quot;&gt;clip-path&lt;/code&gt; property on HTML elements, when you apply a &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;clipPath&amp;gt;&lt;/code&gt; to an SVG element, the behavior is the same as the one implemented in Firefox—only the visible areas respond to pointer events. I’m not sure if this is a feature or a bug.&lt;/p&gt;

&lt;p&gt;In the following example, an SVG &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;clipPath&amp;gt;&lt;/code&gt; is applied to an SVG &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;image&amp;gt;&lt;/code&gt;. The clip path is similar to the one we used before, where the image is clipped by a number of rectangles. The image becomes translucent when you hover over it.&lt;/p&gt;

&lt;pre class=&quot;brush:css&quot;&gt;
image {
    clip-path: url(#svgPath);
}
image:hover {
    opacity: .5;
}
&lt;/pre&gt;
&lt;p&gt;&lt;a href=&quot;https://sarasoueidan.com/demos/css-svg-clipping/svg-img-clipped-pointer-events/index.html&quot; class=&quot;button&quot;&gt;Try The Demo Out Live&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also, note that an empty clipping path will completely clip away the element that had the &lt;code class=&quot;highlighter-rouge&quot;&gt;clip-path&lt;/code&gt; property applied.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;final-words&quot;&gt;Final Words&lt;/h2&gt;

&lt;p&gt;Clipping is one of those graphical operations that allow us to create irregular shapes in an otherwise rectangular web page. Indeed, clipping is a perfect companion to CSS shapes. If you’ve read any of my &lt;a href=&quot;http://alistapart.com/article/css-shapes-101&quot;&gt;previous&lt;/a&gt; &lt;a href=&quot;http://sarasoueidan.com/blog/css-shapes/&quot;&gt;articles&lt;/a&gt; about CSS Shapes, then you already know that the &lt;code class=&quot;highlighter-rouge&quot;&gt;clip-path&lt;/code&gt; property can be an indispensable companion to CSS Shapes in some use cases. And once CSS Shapes properties can &lt;a href=&quot;http://dev.w3.org/csswg/css-shapes-2/#referencing-svg-shapes&quot;&gt;reference SVG paths&lt;/a&gt; (&lt;a href=&quot;http://dev.w3.org/csswg/css-shapes-2/&quot;&gt;CSS Shapes Module Level 2&lt;/a&gt;), in addition to the basic CSS shapes, the combination of Shapes and Clipping will allow us to create visually compelling designs that break the norms of the rectangle.&lt;/p&gt;

&lt;p&gt;I hope you found this article useful. Thank you for reading!&lt;/p&gt;

    
  </content>
 </entry>
 
 <entry>
   <title>Structuring, Grouping, and Referencing in SVG — The <code>&lt;g&gt;</code>, <code>&lt;use&gt;</code>, <code>&lt;defs&gt;</code> and <code>&lt;symbol&gt;</code> Elements</title>
   <link href="http://sarasoueidan.com//blog/structuring-grouping-referencing-in-svg/"/>
   <id>https://sarasoueidan.com/blog/structuring-grouping-referencing-in-svg</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;SVG comes with its own ways for structuring a document by means of certain SVG elements that allow us to define, group, and reference objects within the document. These elements make reusing elements easy, while maintaining clean and readable code. In this article we'll go over these elements, highlighting the difference between them and the advantages of each one.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;the-g-element&quot;&gt;Grouping with the &lt;code&gt;&amp;lt;g&amp;gt;&lt;/code&gt; element&lt;/h2&gt;

&lt;p&gt;The ‘g’ in &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;g&amp;gt;&lt;/code&gt; stands for ‘group’. The group element is used for logically grouping together sets of related graphical elements. In terms of graphics editors, such as Adobe Illustrator, the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;g&amp;gt;&lt;/code&gt; element serves a similar functionality as the &lt;em&gt;Group Objects&lt;/em&gt; function. You can also think of a group as being similar to the concept of a &lt;em&gt;layer&lt;/em&gt; in a graphics editor, since a layer is also a grouping of elements.&lt;/p&gt;

&lt;blockquote class=&quot;pull-quote&quot;&gt;
	The group element is used for logically grouping together sets of related graphical elements.
&lt;/blockquote&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;g&amp;gt;&lt;/code&gt; element groups all of its descendants into one group. It usually has an &lt;code class=&quot;highlighter-rouge&quot;&gt;id&lt;/code&gt; attribute to give that group a name. Any styles you apply to the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;g&amp;gt;&lt;/code&gt; element will also be applied to all of its descendants. This makes it easy to add styles, transformations, interactivity, and even animations to entire groups of objects.&lt;/p&gt;

&lt;p&gt;For example, the following is an SVG bird. The bird is made up of several shapes such as circles and paths.&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/grouping-bird.svg&quot; alt=&quot;&quot; /&gt;
&lt;/figure&gt;

&lt;p&gt;If you wanted to move the entire bird from one place to another in Illustrator, you will also want to group the elements together so that you wouldn’t have to select each and every one of them every time you wanted to do so.&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/grouping-in-illustrator.png&quot; alt=&quot;&quot; /&gt;
&lt;/figure&gt;

&lt;p&gt;Grouping in SVG using the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;g&amp;gt;&lt;/code&gt; element works the same way. In this example we’ve grouped the elements of the body together, the elements of the head together, and then we grouped the two groups into one group with an &lt;code class=&quot;highlighter-rouge&quot;&gt;id&lt;/code&gt; of &lt;code class=&quot;highlighter-rouge&quot;&gt;bird&lt;/code&gt;.&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;svg width=&quot;1144.12px&quot; height=&quot;400px&quot; viewBox=&quot;0 0 572.06 200&quot;&amp;gt;
	&amp;lt;style&amp;gt;
		svg{background-color:white;}
		#wing{fill:#81CCAA;}
		#body{fill:#B8E4C2;}
		#pupil{fill:#1F2600;}
		#beak{fill:#F69C0D;}
		.eye-ball{fill:#F6FDC4;}
	&amp;lt;/style&amp;gt;
	&amp;lt;g id=&quot;bird&quot;&amp;gt;
		&amp;lt;g id=&quot;body&quot;&amp;gt;
			&amp;lt;path d=&quot;M48.42,78.11c0-17.45,14.14-31.58,31.59-31.58s31.59,14.14,31.59,31.58c0,17.44-14.14,31.59-31.59,31.59
			S48.42,95.56,48.42,78.11&quot;/&amp;gt;
			&amp;lt;path d=&quot;M109.19,69.88c0,0-8.5-27.33-42.51-18.53c-34.02,8.81-20.65,91.11,45.25,84.73
			c40.39-3.65,48.59-24.6,48.59-24.6S124.68,106.02,109.19,69.88&quot;/&amp;gt;
			&amp;lt;path id=&quot;wing&quot; d=&quot;M105.78,75.09c4.56,0,8.84,1.13,12.62,3.11c0,0,0.01-0.01,0.01-0.01l36.23,12.38c0,0-13.78,30.81-41.96,38.09
			c-1.51,0.39-2.82,0.59-3.99,0.62c-0.96,0.1-1.92,0.16-2.9,0.16c-15.01,0-27.17-12.17-27.17-27.17
			C78.61,87.26,90.78,75.09,105.78,75.09&quot;/&amp;gt;
		&amp;lt;/g&amp;gt;
		&amp;lt;g id=&quot;head&quot;&amp;gt;
			&amp;lt;path id=&quot;beak&quot; d=&quot;M50.43,68.52c0,0-8.81,2.58-10.93,4.86l9.12,9.87C48.61,83.24,48.76,74.28,50.43,68.52&quot;/&amp;gt;
			&amp;lt;path class=&quot;eye-ball&quot; d=&quot;M60.53,71.68c0-6.33,5.13-11.46,11.46-11.46c6.33,0,11.46,5.13,11.46,11.46c0,6.33-5.13,11.46-11.46,11.46
				C65.66,83.14,60.53,78.01,60.53,71.68&quot;/&amp;gt;
			&amp;lt;path id=&quot;pupil&quot; d=&quot;M64.45,71.68c0-4.16,3.38-7.53,7.54-7.53c4.16,0,7.53,3.37,7.53,7.53c0,4.16-3.37,7.53-7.53,7.53
				C67.82,79.22,64.45,75.84,64.45,71.68&quot;/&amp;gt;
			&amp;lt;path class=&quot;eye-ball&quot; d=&quot;M72.39,74.39c0-2.73,2.22-4.95,4.95-4.95c2.73,0,4.95,2.21,4.95,4.95c0,2.74-2.22,4.95-4.95,4.95
				C74.6,79.34,72.39,77.13,72.39,74.39&quot;/&amp;gt;
		&amp;lt;/g&amp;gt;
	&amp;lt;/g&amp;gt;
&amp;lt;/svg&amp;gt;
&lt;/pre&gt;

&lt;p&gt;If you were to change the fill color of the &lt;code class=&quot;highlighter-rouge&quot;&gt;#body&lt;/code&gt; group, the fill color of all the elements inside the group will change to the color you specify. This is very convenient.&lt;/p&gt;

&lt;p&gt;Grouping elements is very useful, not just for organizational and structural purposes. It’s particularly useful for when you want to add interactivity or transformations to an SVG graphic that is made up of several items. You can associate those items together in a group and then define transformations to move, scale, or rotate all the items together so that their spatial relations to one another are maintained.&lt;/p&gt;

&lt;p&gt;If you wanted to scale the entire bird up and make it twice its size, you will be able to do it with one line of CSS if all the elements are grouped together.&lt;/p&gt;

&lt;pre class=&quot;brush:css&quot;&gt;
#bird {
	transform: scale(2);
}
&lt;/pre&gt;

&lt;p&gt;Grouping makes interactivity, in particular, more convenient. You can attach mouse events to the entire bird and have it respond to the events as a whole group, instead of having to apply the same interactions and/or transformations to every element in that group.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;g&amp;gt;&lt;/code&gt; element has one more important and great feature: it can have its own &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;title&amp;gt;&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;desc&amp;gt;&lt;/code&gt; tags that help make it more accessible to screen readers, and overall make the code more readable to humans as well. For example:&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;g id=&quot;bird&quot;&amp;gt;
	&amp;lt;title&amp;gt;Bird&amp;lt;/title&amp;gt;
	&amp;lt;desc&amp;gt;An image of a cute little green bird with an orange beak.&amp;lt;/desc&amp;gt;
	&amp;lt;!-- ... --&amp;gt;
&amp;lt;/g&amp;gt;
&lt;/pre&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;the-use-element&quot;&gt; Reusing elements with the &lt;code&gt;&amp;lt;use&amp;gt;&lt;/code&gt; element&lt;/h2&gt;

&lt;p&gt;Often times, there are elements that are repeated in a graphic. If you’re working in Illustrator and you want to repeat an element somewhere in the graphic, you would copy the element and then paste it and use it wherever it is you want to use it. Copying and then pasting an existing element is more convenient than having to recreate that element from scratch.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;use&amp;gt;&lt;/code&gt; element lets you reuse existing elements, giving you a similar functionality to the copy-paste functionality in a graphics editor. It can be used to reuse a single element, or a group of element defined with the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;g&amp;gt;&lt;/code&gt; element.&lt;/p&gt;

&lt;blockquote class=&quot;pull-quote&quot;&gt;
	The &lt;code&gt;&amp;lt;use&amp;gt;&lt;/code&gt; element lets you reuse existing elements, giving you a similar functionality to the copy-paste functionality in a graphics editor.
&lt;/blockquote&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;use&amp;gt;&lt;/code&gt; element takes &lt;code class=&quot;highlighter-rouge&quot;&gt;x&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;y&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;height&lt;/code&gt;, and &lt;code class=&quot;highlighter-rouge&quot;&gt;width&lt;/code&gt; attributes, and it references other content using the &lt;code class=&quot;highlighter-rouge&quot;&gt;xlink:href&lt;/code&gt; attribute. So if you have defined a group somewhere with a given &lt;code class=&quot;highlighter-rouge&quot;&gt;id&lt;/code&gt;, and you want to reuse it somewhere else, you give its URI in an &lt;code class=&quot;highlighter-rouge&quot;&gt;xlink:href&lt;/code&gt;
attribute, and specify the &lt;code class=&quot;highlighter-rouge&quot;&gt;x&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;y&lt;/code&gt; location where the group’s &lt;code class=&quot;highlighter-rouge&quot;&gt;(0, 0)&lt;/code&gt; point should be moved to.&lt;/p&gt;

&lt;p&gt;For example, if we were to create another bird in our SVG canvas, we could reuse the existing bird like so:&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;use x=&quot;100&quot; y=&quot;100&quot; xlink:href=&quot;#bird&quot; /&amp;gt;
&lt;/pre&gt;

&lt;p&gt;Note that you can reference any SVG element inside the &lt;code class=&quot;highlighter-rouge&quot;&gt;xlink:href&lt;/code&gt; attribute, even if that element is in an external file. The referenced element or group does not have to exist in the same file. This is great for organizing files (for example you could have a file for reusable components) and for caching the used files. If the bird in our example were created in a seperate file called &lt;code class=&quot;highlighter-rouge&quot;&gt;animals.svg&lt;/code&gt; for example, we could have referenced it like so:&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;use x=&quot;100&quot; y=&quot;100&quot; xlink:href=&quot;path/to/animals.svg#bird&quot; /&amp;gt;
&lt;/pre&gt;

&lt;p&gt;However, referencing external SVG in &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;use&amp;gt;&lt;/code&gt; doesn’t work in most versions of IE (up to IE 11). I recommend you read &lt;a href=&quot;http://css-tricks.com/svg-use-external-source/&quot;&gt;this article&lt;/a&gt; by Chris Coyier for details and fallback mechanisms.&lt;/p&gt;

&lt;p&gt;Now, those of you with a sharp bird’s eye (pun not intended), may have noticed that I said that the &lt;code class=&quot;highlighter-rouge&quot;&gt;x&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;y&lt;/code&gt; attributes of &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;use&amp;gt;&lt;/code&gt; specify the location where the group’s upper left corner should be &lt;strong&gt;moved to&lt;/strong&gt;. Moving an element means that you’re starting from its current position and then changing it to another one. Had I said “should be positioned at”, it would have implied that the element will be positioned according to the coordinate system in use on the entire canvas, right?&lt;/p&gt;

&lt;p&gt;As it turns out, the &lt;code class=&quot;highlighter-rouge&quot;&gt;x&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;y&lt;/code&gt; coordinates are &lt;strong&gt;a shorthand for translating an element using the &lt;code class=&quot;highlighter-rouge&quot;&gt;transform&lt;/code&gt; attribute&lt;/strong&gt;. More specifically, the above &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;use&amp;gt;&lt;/code&gt; use is equivalent to:&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;use xlink:href=&quot;#bird&quot; transform=&quot;translate(100, 100)&quot; /&amp;gt;
&lt;/pre&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/bird-reuse.jpg&quot; alt=&quot;&quot; /&gt;
&lt;/figure&gt;

&lt;p&gt;This fact means that &lt;strong&gt;the position of the new reused element is relative to the position of the original element that we’re reusing&lt;/strong&gt;. This isn’t always optimal and can have some drawbacks.&lt;/p&gt;

&lt;blockquote class=&quot;pull-quote&quot;&gt;
	The &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; coordinates are a shorthand for translating an element using the &lt;code&gt;transform&lt;/code&gt; attribute.
&lt;/blockquote&gt;

&lt;p&gt;Another drawback of the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;use&amp;gt;&lt;/code&gt; element is that the “copies” of the original element will have the exact same styles as the original element. Whenever you apply any style or transformation changes to the &lt;code class=&quot;highlighter-rouge&quot;&gt;#bird&lt;/code&gt; group in the above example, all the copies of the bird will get the same styles and transformations.&lt;/p&gt;

&lt;p&gt;You can &lt;code class=&quot;highlighter-rouge&quot;&gt;use&lt;/code&gt; an element and apply an independent transformation to it. For example, the following line will reuse the bird, but it will make it half its original size using a scale transformation:&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;use x=&quot;100&quot; y=&quot;100&quot; xlink:href=&quot;#bird&quot; transform=&quot;scale(0.5)&quot; /&amp;gt;
&lt;/pre&gt;

&lt;p&gt;However, you &lt;strong&gt;cannot override the styles of the original element (such as fill and strokes) on the copy&lt;/strong&gt;. This means that if you want to create multiple birds or multiple icons (if you’re working with icons) and you want every icon to have a different color, this won’t be possible with the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;use&amp;gt;&lt;/code&gt; element (unless the original element is defined inside a &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;defs&amp;gt;&lt;/code&gt; element without these styles applied to it. See next section for more information).&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;use&amp;gt;&lt;/code&gt; element allows you to reuse an element &lt;strong&gt;that is already rendered on the canvas&lt;/strong&gt;. But what if you want to define an element &lt;em&gt;without&lt;/em&gt; displaying it, and then draw it at specific positions when you need or want to? This is where the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;defs&amp;gt;&lt;/code&gt; element comes in.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;the-defs-element&quot;&gt;Reusing Stored elements with the &lt;code&gt;&amp;lt;defs&amp;gt;&lt;/code&gt; element&lt;/h2&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;defs&amp;gt;&lt;/code&gt; element can be used to &lt;strong&gt;store&lt;/strong&gt; content that will not be directly displayed. In other words, the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;defs&amp;gt;&lt;/code&gt; element is used to &lt;em&gt;define&lt;/em&gt; elements without directly rendering them. This stored hidden content can then be referenced and displayed by other SVG elements,
which makes it ideal for things such as patterns that contain reusable graphics.&lt;/p&gt;

&lt;blockquote class=&quot;pull-quote&quot;&gt;
	The &lt;code&gt;&amp;lt;defs&amp;gt;&lt;/code&gt; element is used to &lt;em&gt;define&lt;/em&gt; elements without directly rendering them [..] and that element will serve as a &lt;strong&gt;template&lt;/strong&gt; for future use.
&lt;/blockquote&gt;

&lt;p&gt;So, using &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;defs&amp;gt;&lt;/code&gt; we can define an element that we want to use. This element can be anything, ranging from a group of elements like the bird we saw earlier, to a clipping path, mask, or a linear gradient. Basically, anything that we want to define and store away for later use can be defined inside the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;defs&amp;gt;&lt;/code&gt; element, and that element will serve as a &lt;strong&gt;template&lt;/strong&gt; for future use, or as a tool that is available for use whenever needed. The template is never rendered, only instances of it are displayed.&lt;/p&gt;

&lt;p&gt;The following is an example defining an SVG gradient, and then uses it as a fill color for a simple SVG rectangle:&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&lt;svg&gt;
	&lt;defs&gt;
		&lt;linearGradient id=&quot;gradient&quot;&gt;
			&lt;stop offset=&quot;0%&quot; style=&quot;stop-color: deepPink&quot; /&gt;
			&lt;stop offset=&quot;100%&quot; style=&quot;stop-color: #009966&quot; /&gt;
		&lt;/linearGradient&gt;
	&lt;/defs&gt;

	&lt;rect stroke=&quot;#eee&quot; stroke-width=&quot;5&quot; fill=&quot;url(#gradient)&quot; /&gt;
&lt;/svg&gt;
&lt;/pre&gt;

&lt;p&gt;Defining the linear gradient inside the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;defs&amp;gt;&lt;/code&gt; element like that ensures that the gradient will not be rendered unless it is referenced somewhere it is needed.&lt;/p&gt;

&lt;p&gt;In the previous section we mentioned two drawbacks of the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;use&amp;gt;&lt;/code&gt; element:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The position of the new element is relative to the position of the original element.&lt;/li&gt;
  &lt;li&gt;The styles of the original element cannot be overridden in the individual copies.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That, in addition to the fact that the re&lt;code class=&quot;highlighter-rouge&quot;&gt;use&lt;/code&gt;d element will be rendered on the canvas.&lt;/p&gt;

&lt;p&gt;All of these drawbacks can be avoided using the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;defs&amp;gt;&lt;/code&gt; element. Not only is the original element not rendered, but when you want to reuse an element inside &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;defs&amp;gt;&lt;/code&gt;, the position you specify for each instance will be set relative to the origin of the user coordinate system, not relative to the position of the original element (which makes sense considering the original element is a template that’s not even rendered on the canvas).&lt;/p&gt;

&lt;p&gt;In this example we have a tree. The tree is made up of a bark and a group of leaves. The leaves are grouped into a group with &lt;code class=&quot;highlighter-rouge&quot;&gt;id=&quot;leaves&quot;&lt;/code&gt;, and then this group is grouped with the bark into the larger &lt;code class=&quot;highlighter-rouge&quot;&gt;tree&lt;/code&gt; group.&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;svg width=&quot;500.79px&quot; height=&quot;200px&quot; viewBox=&quot;0 0 500.79 200&quot;&amp;gt;
	&amp;lt;style type=&quot;text/css&quot;&amp;gt;
		#leaves{fill:#8CC63F;}
		#bark{fill:#A27729;}
	&amp;lt;/style&amp;gt;
	&amp;lt;g id=&quot;tree&quot;&amp;gt;
		&amp;lt;path id=&quot;bark&quot; d=&quot;M91.33,165.51c0,0,4.18-27.65,1.73-35.82l-18.55-25.03l3.01-2.74l17.45,19.87l1.91-37.6h4.44l1.83,24.53
		l15.26-16.35l3.27,4.36l-16.07,19.34c0,0-2.72,0-1.09,19.34c1.63,19.34,3,29.7,3,29.7L91.33,165.51z&quot;/&amp;gt;
		&amp;lt;g id=&quot;leaves&quot;&amp;gt;
			&amp;lt;path class=&quot;leaf&quot; d=&quot;M96.97,79.07c0,0-14.92,4.34-23.52-14.05c0,0,19.4-7.98,24.37,11.9c0,0-9.68-3.57-13.07-6.73
				C84.75,70.2,91.82,77.99,96.97,79.07z&quot;/&amp;gt;
			&amp;lt;path class=&quot;leaf&quot; d=&quot;M74.07,100.91c0,0-15.94-1.51-17.2-22.39c0,0,21.62-0.27,18.83,20.66c0,0-7.92-7.1-9.97-11.41
				C65.73,87.77,69.55,97.92,74.07,100.91z&quot;/&amp;gt;
			&amp;lt;!-- ... --&amp;gt;
		&amp;lt;/g&amp;gt;
	&amp;lt;/g&amp;gt;
&amp;lt;/svg&amp;gt;
&lt;/pre&gt;

&lt;p&gt;And this is how the tree looks like:&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/defined-tree.jpg&quot; alt=&quot;&quot; /&gt;
&lt;/figure&gt;

&lt;p&gt;If we were to wrap the &lt;code class=&quot;highlighter-rouge&quot;&gt;#tree&lt;/code&gt; group in a &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;defs&amp;gt;&lt;/code&gt; element, the tree would no longer be rendered on the canvas.&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;svg width=&quot;500.79px&quot; height=&quot;200px&quot; viewBox=&quot;0 0 500.79 200&quot;&amp;gt;
	&amp;lt;style type=&quot;text/css&quot;&amp;gt;
		#leaves{fill:#8CC63F;}
		#bark{fill:#A27729;}
	&amp;lt;/style&amp;gt;
	&amp;lt;defs&amp;gt;
		&amp;lt;g id=&quot;tree&quot;&amp;gt;
			&amp;lt;!-- ... --&amp;gt;
		&amp;lt;/g&amp;gt;
	&amp;lt;/defs&amp;gt;
&amp;lt;/svg&amp;gt;
&lt;/pre&gt;

&lt;p&gt;Now the tree serves as a template for use. We can use it using the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;use&amp;gt;&lt;/code&gt; element just like we would &lt;code class=&quot;highlighter-rouge&quot;&gt;use&lt;/code&gt; any other element. The only difference in this case is that the &lt;code class=&quot;highlighter-rouge&quot;&gt;x&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;y&lt;/code&gt; attributes are now set relative to the user coordinate system, not relative to the position of the used element.&lt;/p&gt;

&lt;p&gt;For example, if we were to create three copies of the tree and position them on the SVG canvas, and assuming in this case that the user coordinate system matches the viewport’s height and width with the origin being on the top left corner of the SVG viewport, we’d get the following code with the following result:&lt;/p&gt;

&lt;pre class=&quot;brush:css&quot;&gt;
&amp;lt;use xlink:href=&quot;#tree&quot; x=&quot;50&quot; y=&quot;100&quot; /&amp;gt;
&amp;lt;use xlink:href=&quot;#tree&quot; x=&quot;200&quot; y=&quot;100&quot; /&amp;gt;
&amp;lt;use xlink:href=&quot;#tree&quot; x=&quot;350&quot; y=&quot;100&quot; /&amp;gt;
&lt;/pre&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/tree.svg&quot; alt=&quot;&quot; /&gt;
&lt;/figure&gt;

&lt;p&gt;As you can see in the image above, each of the trees was positioned relative to the origin of the coordinate system which in this case is the upper left corner of the SVG. So the upper left corner of each tree is positioned at its own (&lt;code class=&quot;highlighter-rouge&quot;&gt;x&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;y&lt;/code&gt;) position in the user coordinate system, independent of the other trees and independent of the tree template defined inside &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;defs&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;When you use &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;defs&amp;gt;&lt;/code&gt; to reuse an element, you can apply different styles and fill colors to each individual tree, &lt;strong&gt;as long as these styles are not defined on the original tree template&lt;/strong&gt;. If the tree inside &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;defs&amp;gt;&lt;/code&gt; has styles applied to it, those styles still won’t be overridden by any new ones. So &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;defs&amp;gt;&lt;/code&gt; is great for creating minimal templates and then styling the copies as needed. Without &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;defs&amp;gt;&lt;/code&gt;, this wouldn’t be possible with &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;use&amp;gt;&lt;/code&gt; alone.&lt;/p&gt;

&lt;p&gt;Note that elements inside the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;defs&amp;gt;&lt;/code&gt; element are prevented from becoming part of the rendering tree just as if the &lt;code class=&quot;highlighter-rouge&quot;&gt;defs&lt;/code&gt; element were a &lt;code class=&quot;highlighter-rouge&quot;&gt;g&lt;/code&gt; element and the &lt;code class=&quot;highlighter-rouge&quot;&gt;display&lt;/code&gt; property were set to &lt;code class=&quot;highlighter-rouge&quot;&gt;none&lt;/code&gt;. However, that the descendants of a &lt;code class=&quot;highlighter-rouge&quot;&gt;defs&lt;/code&gt; are always present in the source tree and thus can always be referenced by other elements; thus, the value of the &lt;code class=&quot;highlighter-rouge&quot;&gt;display&lt;/code&gt; property on the &lt;code class=&quot;highlighter-rouge&quot;&gt;defs&lt;/code&gt; element or any of its descendants does not prevent those elements from being referenced by other elements, even if it is set to &lt;code class=&quot;highlighter-rouge&quot;&gt;none&lt;/code&gt;.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;the-symbol-element&quot;&gt;Grouping elements with the &lt;code&gt;&amp;lt;symbol&amp;gt;&lt;/code&gt; element&lt;/h2&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;symbol&amp;gt;&lt;/code&gt; element is similar to the group element &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;g&amp;gt;&lt;/code&gt;—it provides a way to group elements together. However, it differs from the group element in two main things:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;symbol&amp;gt;&lt;/code&gt; element is not rendered. It is actually similar to &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;defs&amp;gt;&lt;/code&gt; in this manner. It is only displayed when it is &lt;code class=&quot;highlighter-rouge&quot;&gt;use&lt;/code&gt;d.&lt;/li&gt;
  &lt;li&gt;A &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;symbol&amp;gt;&lt;/code&gt; element can have its own &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectRatio&lt;/code&gt; attributes. This allows it to fit into the viewport it is rendered into any way you want it to, instead of fitting in the default way.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;symbol&amp;gt;&lt;/code&gt; is then mostly suitable for &lt;strong&gt;defining&lt;/strong&gt; reusable elements (or &lt;em&gt;symbols&lt;/em&gt;). It also serves as a template that is &lt;strong&gt;instantiated&lt;/strong&gt; using the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;use&amp;gt;&lt;/code&gt; element. And having &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectRatio&lt;/code&gt; attributes, it can scale-to-fit within a rectangular viewport defined by the referencing &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;use&amp;gt;&lt;/code&gt; element. Note that &lt;code class=&quot;highlighter-rouge&quot;&gt;symbol&lt;/code&gt; elements define new viewports whenever they are instanced by a &lt;code class=&quot;highlighter-rouge&quot;&gt;use&lt;/code&gt; element.&lt;/p&gt;

&lt;blockquote class=&quot;pull-quote&quot;&gt;
	`symbol` elements define new viewports whenever they are instanced by a `use` element.
&lt;/blockquote&gt;

&lt;p&gt;This feature is great because it allows you to define elements that are independent of the viewport they’re rendered into, hence allowing you to make sure the symbol you’re referencing will always display a certain way inside the viewport.&lt;/p&gt;

&lt;p&gt;You need to be familiar with the way the &lt;code class=&quot;highlighter-rouge&quot;&gt;viewBox&lt;/code&gt; works, and the values of the &lt;code class=&quot;highlighter-rouge&quot;&gt;preserveAspectratio&lt;/code&gt; attribute to make the best use of this feature. Chris Coyier wrote &lt;a href=&quot;http://css-tricks.com/svg-symbol-good-choice-icons/&quot;&gt;an article&lt;/a&gt; explaining why the &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;symbol&amp;gt;&lt;/code&gt; element can be a good choice for icons, and how to use it.&lt;/p&gt;

&lt;p class=&quot;note&quot;&gt;I will also be writing an extensive article about the `viewport`, `viewBox`, and `preserveAspectRatio` attributes that explains how these attributes work together and how they can be used to control and scale graphics in SVG, so stay tuned for that if you're interested.&lt;/p&gt;
&lt;div class=&quot;update note&quot;&gt;
	&lt;strong&gt;UPDATE:&lt;/strong&gt; Article is live here: &lt;a href=&quot;http://sarasoueidan.com/blog/svg-coordinate-systems&quot;&gt;Understanding SVG Coordinate Systems and Transformations (Part 1) &amp;ndash; The viewport, &lt;code&gt;viewBox&lt;/code&gt;, and &lt;code&gt;preserveAspectRatio&lt;/code&gt;&lt;/a&gt;
&lt;/div&gt;

&lt;p&gt;Note that the &lt;code class=&quot;highlighter-rouge&quot;&gt;display&lt;/code&gt; property does not apply to the &lt;code class=&quot;highlighter-rouge&quot;&gt;symbol&lt;/code&gt; element; thus, &lt;code class=&quot;highlighter-rouge&quot;&gt;symbol&lt;/code&gt; elements are not directly rendered even if the &lt;code class=&quot;highlighter-rouge&quot;&gt;display&lt;/code&gt; property is set to a value other than &lt;code class=&quot;highlighter-rouge&quot;&gt;none&lt;/code&gt;, and &lt;code class=&quot;highlighter-rouge&quot;&gt;symbol&lt;/code&gt; elements are available for referencing even when the &lt;code class=&quot;highlighter-rouge&quot;&gt;display&lt;/code&gt; property on the &lt;code class=&quot;highlighter-rouge&quot;&gt;symbol&lt;/code&gt; element or any of its ancestors is set to &lt;code class=&quot;highlighter-rouge&quot;&gt;none&lt;/code&gt;.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;wrapping-up&quot;&gt;Wrapping up&lt;/h2&gt;

&lt;p&gt;All of these elements are container structural elements in SVG that helps make reusing elements easier while keeping the code cleaner and more readable. And each of the elements we went over in this article has its own use cases. Now that you know what each one does and how they differ, you can decide for your own which one to use, depending on your needs. That said, don’t forget to &lt;a href=&quot;https://www.sitepoint.com/tips-accessible-svg/&quot;&gt;keep your SVGs accessible&lt;/a&gt; at all times.&lt;/p&gt;

&lt;p&gt;I hope you liked this article and found it useful. Thank you for reading!&lt;/p&gt;


    
  </content>
 </entry>
 
 <entry>
   <title>Everything You Need To Know About The CSS <code>will-change</code> Property</title>
   <link href="http://sarasoueidan.com//blog/css-will-change-property/"/>
   <id>https://sarasoueidan.com/blog/css-will-change-property</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;
	What the title says! I wrote this article for the Opera Developers' blog, and it literally contains everything you need to know about the new CSS &lt;code&gt;will-change&lt;/code&gt; property, including but not limited to: how to use it, when to use it, when &lt;em&gt;not&lt;/em&gt; to use it, performance considerations, and more.
&lt;/p&gt;

    

     <div>
        <p>
          <em>This article is <a href="http://dev.opera.com/articles/css-will-change-property/">published @ <strong>Dev.Opera</strong>.</a></em>
        </p>
    </div>
    
  </content>
 </entry>
 
 <entry>
   <title>Moving Forward With CSS Shapes</title>
   <link href="http://sarasoueidan.com//blog/moving-forward-with-css-shapes/"/>
   <id>https://sarasoueidan.com/blog/moving-forward-with-css-shapes</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;
	Following up with the CSS Shapes 101 article, I share a list of great resources to learn more about CSS Shapes, including tutorials, examples, and developer tools.
&lt;/p&gt;

    

     <div>
        <p>
          <em>This article is <a href="http://alistapart.com/blog/post/moving-forward-with-css-shapes/">published @ <strong>A List Apart</strong>.</a></em>
        </p>
    </div>
    
  </content>
 </entry>
 
 <entry>
   <title>CSS Shapes 101</title>
   <link href="http://sarasoueidan.com//blog/css-shapes-101/"/>
   <id>https://sarasoueidan.com/blog/css-shapes-101</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;
	In this article, you'll learn everything you need to know to get started using CSS Shapes today. We will cover all the prerequisites: establishing a coordinate system, conditions for elements to be eligible for shapes, and more, then moving on to the properties used, their values and how each one affects the shape created, with lots of visual explanations and examples.
&lt;/p&gt;

    

     <div>
        <p>
          <em>This article is <a href="http://alistapart.com/article/css-shapes-101">published @ <strong>A List Apart</strong>.</a></em>
        </p>
    </div>
    
  </content>
 </entry>
 
 <entry>
   <title>CSS Regions Matter</title>
   <link href="http://sarasoueidan.com//blog/css-regions-matter/"/>
   <id>https://sarasoueidan.com/blog/css-regions-matter</id>
   <content type="html">
    &lt;p&gt;&lt;em&gt;I wrote this article before &lt;a href=&quot;http://news.cnet.com/8301-1023_3-57617840-93/reversing-course-google-rejects-adobe-web-publishing-tech/&quot;&gt;the news&lt;/a&gt; came out that Google decided to pull Regions out of Blink, which, in my opinion, is a big loss for the web community. So, even though the content of the article may go in vain, I think it’s still worth sharing why I think CSS Regions mattered, and wish they weren’t ditched like that. I hope you guys like it anyway.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Disclaimer&lt;/strong&gt; In no way is this article meant as an attack on Mr. Håkon Wium Lie’s &lt;a href=&quot;http://alistapart.com/blog/post/css-regions-considered-harmful&quot;&gt;article&lt;/a&gt; about CSS Regions. Mr. Håkon’s article served as an incentive for me to dig deeper into CSS Regions, and during the process, I learned a lot about them, and about other future CSS features, and this article is the result of what I found out and my personal opinion regarding the usefulness of CSS Regions.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Whether or not CSS Regions can be used to create multi-column or responsive layouts, fact remains that, unlike Flexbox, Multi-Columns, and Grids, &lt;strong&gt;CSS Regions are not a layout feature&lt;/strong&gt;—they’re &lt;strong&gt;a fragmentation feature&lt;/strong&gt; that allows us to control or change the flow of content across containers in a page, or across pages.&lt;/p&gt;

&lt;p&gt;Since CSS Regions are a &lt;strong&gt;fragmentation feature&lt;/strong&gt;, they define &lt;em&gt;where&lt;/em&gt; content &lt;em&gt;flows&lt;/em&gt; on the screen—unlike Flexbox, floats, Grids, and other positioning features that define &lt;em&gt;how&lt;/em&gt; content is &lt;em&gt;laid out&lt;/em&gt; on a screen. Layout features position elements and containers; Regions can alter the flow of content that shows up in them.&lt;/p&gt;

&lt;p&gt;In this article I want to go over some useful examples of use cases for CSS Regions, and highlight some of the challenges that we may face when using CSS Regions, and talk about possible solutions to common Regions problems.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;regions-problems-and-solutions&quot;&gt;CSS Regions Problems and Possible Solutions&lt;/h2&gt;

&lt;p&gt;CSS regions have two prominent “problems” today: non-semantic presentational elements, and the non-responsiveness of regions.&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;non-semantic-markup&quot;&gt;The Problem Of Non-Semantic Markup, and Possible Solutions&lt;/h3&gt;

&lt;p&gt;The reason we are using non-semantic elements to create Regions today is that other layout features, such as Grids, for example, have not yet been finalized and are not yet ready to be used. So, instead of using portions or fragments of a CSS Grid layout, for example, as regions, we’re currently stuck with defining regions using “normal” elements such as &lt;code&gt;div&lt;/code&gt;s.&lt;/p&gt;

&lt;p&gt;CSS Regions are designed so that they work &lt;em&gt;now&lt;/em&gt;, with normal elements, and also work with future features, instead of waiting for things to be perfect. It is only a matter of time before Regions can be used with other layout features, and then a lot of the current Regions problems can be solved.&lt;/p&gt;

&lt;p&gt;When Regions are used with other features, such as Grids and Flexbox, they allow us to use fragments of a layout as regions by simply applying the &lt;code&gt;flow-from&lt;/code&gt; property to them. No empty elements will be needed to define regions, whatsoever.&lt;/p&gt;

&lt;p&gt;To demonstrate how Regions can be defined without creating empty elements, I will show a code example provided to me by Adobe’s &lt;a href=&quot;https://twitter.com/cataling&quot;&gt;Catalin Grigoroscuta&lt;/a&gt;. In the demo, the pseudo-element &lt;code&gt;::after&lt;/code&gt; is used to define regions to flow content into. So we do not need to add any empty elements to the page to create our regions, and therefore the semantics of the content are not compromised.&lt;/p&gt;

&lt;p&gt;This demo is a variation of &lt;a href=&quot;http://css-tricks.com/content-folding/&quot;&gt;an excellent use case for Regions that Chris Coyier came up with&lt;/a&gt;: using regions to &lt;strong&gt;shuffle&lt;/strong&gt; contents on a page, allowing us to reposition ads that are placed inside a sidebar on big screens, and place them in between the text content of an article on small screens, so that they don’t stack up down at the bottom of the article.&lt;/p&gt;

&lt;p&gt;The layout would start with a sidebar having the ads inside it, much like most blogs and magazines do today.&lt;/p&gt;

&lt;figure&gt;
  &lt;img src=&quot;https://sarasoueidan.com/images/layout-wide.png&quot; alt=&quot;Picture showing a typical layout with ads stacked in the sidebar&quot; /&gt;
    &lt;figcaption&gt;
      The layout would contain the ads in a sidebar on big screens. &lt;em&gt;&lt;a href=&quot;http://css-tricks.com/content-folding/&quot;&gt;Source&lt;/a&gt;&lt;/em&gt;
    &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;And then using CSS Regions, the content is “folded” on smaller screens, allowing ads to be placed between the text content of the article. Without CSS Regions, the ads would appear at the bottom of the article, way down at the bottom.&lt;/p&gt;

&lt;figure&gt;
  &lt;img src=&quot;https://sarasoueidan.com/images/folding.jpg&quot; alt=&quot;Picture showing the effect of using regions to place ads inside content on small screens&quot; /&gt;
    &lt;figcaption&gt;
      Without using CSS Regions, the ads would normally stack at the bottom of the content as in the left picture. Using CSS Regions, the content can be &quot;folded&quot; and ads placed in different spots inside the article. &lt;em&gt;&lt;a href=&quot;http://css-tricks.com/content-folding/&quot;&gt;Source&lt;/a&gt;&lt;/em&gt;
    &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Chris did it by introducing empty elements into the article where he wants the ads to appear on small screens. And then, using media queries, he flowed the ads from the sidebar into these elements using the CSS Regions properties &lt;code&gt;flow-from&lt;/code&gt; and &lt;code&gt;flow-into&lt;/code&gt;.&lt;/p&gt;

&lt;pre class=&quot;brush:html;&quot;&gt;
&lt;!-- Source: http://css-tricks.com/content-folding/ --&gt;
&amp;lt;section class=&quot;main-content&quot;&amp;gt;

  &amp;lt;article&amp;gt; ... &amp;lt;/article&amp;gt;

  &amp;lt;div class=&quot;ad-region-1&quot;&amp;gt;
    &amp;lt;!-- empty, ads flow into here as needed --&amp;gt;
  &amp;lt;/div&amp;gt;

  &amp;lt;article&amp;gt; ... &amp;lt;/article&amp;gt;

  &amp;lt;div class=&quot;ad-region-2&quot;&amp;gt;
    &amp;lt;!-- empty, ads flow into here as needed --&amp;gt;
  &amp;lt;/div&amp;gt;

  &amp;lt;article&amp;gt; ... &amp;lt;/article&amp;gt;

  &amp;lt;div class=&quot;ad-region-3&quot;&amp;gt;
    &amp;lt;!-- empty, ads flow into here as needed --&amp;gt;
  &amp;lt;/div&amp;gt;

&amp;lt;/section&amp;gt;

&amp;lt;aside&amp;gt;
   &amp;lt;!-- Fallback content in this flow region, probably clone #ad-source --&amp;gt;
&amp;lt;/aside&amp;gt;

&amp;lt;!-- Source of flowing content, essentially hidden as an element --&amp;gt;
&amp;lt;div id=&quot;ad-source&quot;&amp;gt;
  &amp;lt;a href=&quot;#&quot;&amp;gt;&amp;lt;img src=&quot;ads/1.jpg&quot;&amp;gt;&amp;lt;/a&amp;gt;
  &amp;lt;a href=&quot;#&quot;&amp;gt;&amp;lt;img src=&quot;ads/2.jpg&quot;&amp;gt;&amp;lt;/a&amp;gt;
  &amp;lt;a href=&quot;#&quot;&amp;gt;&amp;lt;img src=&quot;ads/3.jpg&quot;&amp;gt;&amp;lt;/a&amp;gt;
  &amp;lt;a href=&quot;#&quot;&amp;gt;&amp;lt;img src=&quot;ads/4.png&quot;&amp;gt;&amp;lt;/a&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/pre&gt;

&lt;p&gt;The CSS needed to define the flow of the ads into the regions is shown below.&lt;/p&gt;

&lt;pre class=&quot;brush:css;&quot;&gt;
/* define the source and destination of the ads flow */
#ad-source {
  -webkit-flow-into: ads;
  -ms-flow-into: ads;
}
aside, [class^='ad-region'] {
  -webkit-flow-from: ads;
  -ms-flow-from: ads;
}
/* initially hide the regions (empty elements)  */
[class^='ad-region'] {
  display: none;
  height: 155px;
  width: 100%; 
}

/* show regions on small screens and hide the sidebar where the ads were on big screens */
@media (max-width: 800px) {
  [class^='ad-region'] {
    display: block;
  }
  [class^='ad-region']:last-child {
    height: 300px; /* I wish height auto worked */
  }
  aside {
    display: none;
  }
}
&lt;/pre&gt;

&lt;p&gt;You can see that this use case for Regions is brilliant and very practical. But it has one problem: non-semantic empty elements.&lt;/p&gt;

&lt;p&gt;A way to avoid using empty elements to define our regions would be to use the &lt;code&gt;::after&lt;/code&gt; pseudo-element on each of the &lt;code&gt;&amp;lt;article&amp;gt;&lt;/code&gt; elements, and use the pseudo-element as the region we flow the ads into. The following code shows how this can be achieved using just a few lines of CSS. No code bloating, no non-semantic elements.&lt;/p&gt;

&lt;p&gt;First we create our markup, similar to the way Chris did it, but we leave out the empty elements.&lt;/p&gt;

&lt;pre class=&quot;brush:html;&quot;&gt;
&amp;lt;section&amp;gt;
&amp;lt;article&amp;gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quos possimus tenetur aut natus error aperiam ipsam atque laboriosam quod accusamus velit deserunt non quo blanditiis officiis pariatur eveniet fugiat neque.&amp;lt;/article&amp;gt;
&amp;lt;article&amp;gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit. Possimus reiciendis eveniet ex suscipit nam ad voluptas cumque beatae qui maxime repellat quos consequuntur sapiente esse animi ea accusantium dicta perspiciatis?&amp;lt;/article&amp;gt;
&amp;lt;article&amp;gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit. Autem numquam tenetur natus ad unde quisquam illo fuga deserunt quibusdam accusamus provident officiis laboriosam hic nihil dolorum aperiam reprehenderit adipisci eveniet?&amp;lt;/article&amp;gt;
&amp;lt;/section&amp;gt;
&amp;lt;aside&amp;gt;
&amp;lt;img class=&quot;ad&quot; src=&quot;&quot; alt=&quot;Ad 1&quot; width=&quot;300px&quot; height=&quot;80px&quot;&amp;gt;&amp;lt;/img&amp;gt;
&amp;lt;img class=&quot;ad&quot; src=&quot;&quot; alt=&quot;Ad 2&quot; width=&quot;300px&quot; height=&quot;80px&quot;&amp;gt;&amp;lt;/img&amp;gt;
&amp;lt;img class=&quot;ad&quot; src=&quot;&quot; alt=&quot;Ad 3&quot; width=&quot;300px&quot; height=&quot;80px&quot;&amp;gt;&amp;lt;/img&amp;gt;
&amp;lt;/aside&amp;gt;
&lt;/pre&gt;

&lt;p&gt;And then using Regions properties, we define our regions on the &lt;code&gt;::after&lt;/code&gt; pseudo-element of each article.&lt;/p&gt;

&lt;pre class=&quot;brush:css;&quot;&gt;
/*
* Regions Code that makes the Folding Work
* 1) remove ads from sidebar and put them into a flow
* 2) &quot;pour&quot; the ads into the pseudo-element
*/
@media (max-width: 600px) {
  .ad {
    -webkit-flow-into: ads; /* 1 */
  }
  
  article::after {
    display: block;
    height: 5em;
    margin: 1em 0;
    -webkit-flow-from: ads; /* 2 */
  }
  section {
    width: 100%;
  }
  
}
&lt;/pre&gt;

&lt;p&gt;This is a brilliant idea that solves the problem of non-semantic elements, and also shows how well Regions can work with other CSS features. You can see a fork of Catalin’s live demo &lt;a href=&quot;http://codepen.io/SaraSoueidan/pen/ca006aa143bd27380bf1d99375299506&quot;&gt;here&lt;/a&gt;. And what’s even more awesome is that this technique and the code used for it are 100% reusable.&lt;/p&gt;

&lt;p&gt;This example shows that the non-semantic elements that are now sometimes needed to define regions, will no longer be a problem when Regions are used in conjunction with other layouts features. Just like we defined regions on pseudo-elements, we would be able to define regions on fragments of the layout that are part of a Grid or Flexbox, for example, and the semantics wouldn’t be affected. Grids and other layout features would provide “slots” to use as regions, just like pseudo-elements can be used as regions.&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;responsiveness&quot;&gt;The Responsiveness Of Regions Or Lack Thereof&lt;/h3&gt;

&lt;p&gt;CSS Regions are not responsive by themselves, they need media queries to change. But then again, Flexbox does too, and so do CSS Grids, and that does not mean these features are not useful. Relying on Regions alone to fully “responsify” a layout is not a good idea, but that’s not surprising, considering that it’s not what Regions are made for.&lt;/p&gt;

&lt;p&gt;The biggest challenge that we could face when making  Regions responsive is fitting the content inside a Region that has a fixed height but different widths on different screen sizes. We all know how limiting that is. If we start mobile-first, the content will be too little for the element on bigger screens when it expands horizontally, and if we start desktop-first, the content will overflow the element on small screens. The thing about regions is that it does not define a way to generate new boxes to fit the overflowing content. This is a curse, but also a blessing, because it allows Regions to work well with any other specification that &lt;em&gt;can&lt;/em&gt; generate boxes.&lt;/p&gt;

&lt;p&gt;The repsonsiveness of CSS regions would no longer be a problem when they are combined with other features that can do what Regions can’t do, such as the &lt;a href=&quot;http://dev.w3.org/csswg/css-overflow-3/&quot;&gt;CSS Overflow Module&lt;/a&gt;. If these technologies were to combine, then we would be able to create responsive layouts that have regions defined in them, without having to worry about content overflowing the regions.&lt;/p&gt;

&lt;p&gt;The CSS Overflow Module is described in the specification as follows:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;This module contains the features of CSS relating to new mechanisms of overflow handling in visual media (e.g., screen or paper). In interactive media, it describes features that allow the overflow from a fixed size container to be handled by pagination (displaying one page at a time). It also describes features, applying to all visual media, &lt;span style=&quot;text-decoration: underline&quot;&gt;that allow the contents of an element to be spread across multiple fragments, allowing the contents to flow across multiple regions or to have different styles for different fragments&lt;/span&gt;. (bold emphasis added by me)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Considering how CSS Regions are meant to help us create and control fragments of a layout, this Overflow module looks like a great match for Regions. Together, they could allow us to make Regions responsive by “extending” a region. The Overflow module handles overflow into “fragments”—it duplicates the source element and flows content into it as many times as necessary to fit the whole content. So as the screen becomes smaller,  the region can be “extended” to fit its content without having to overflow that content into another region or element. Extending a region would be as simple as defining &lt;code&gt;overflow: fragments;&lt;/code&gt; on it, and we would end up with a solution to the fixed region height problem. That sounds really great, doesn’t it?&lt;/p&gt;

&lt;p&gt;There is also another proposal from Adobe—&lt;a href=&quot;http://adobe.github.io/css-pseudo-elements/&quot;&gt;multiple pseudo-elements&lt;/a&gt;—which allows the creation of multiple &lt;code&gt;::before&lt;/code&gt; and &lt;code&gt;::after&lt;/code&gt; pseudo-elements on an element. Similar to the Overflow module, it would allow us to extend an element and use its extension as a region, much like we did in the example in the previous section, where we used the pseudo-element as a region to flow the ads into. We could define as many pseudo-elements as we want to “extend” a region whenever needed, without having to add a single empty element to the DOM. Razvan Caliman has even created &lt;a href=&quot;http://adobe.github.io/css-pseudo-elements/&quot;&gt;a polyfill&lt;/a&gt; for pseudo-elements if you’re interested you can check that out.&lt;/p&gt;

&lt;p&gt;Another challenge we may face when creating regions is having text overflow a region when the text is scaled up (page zoomed in) by the user, or when the contents of a region change such as when the page is translated into another language, for example. A lot of times, we may want to keep the contents of a region inside that region, and &lt;em&gt;not&lt;/em&gt; have them flow into another region, even if that other region is an extension of the main region.&lt;/p&gt;

&lt;p&gt;For this, the CSS Regions specification extends break properties from the Multi-Column Layout specification. These properties can be used to specify whether content inside a region is allowed to overflow to another region or not. More specifically, they allow us to decide whether a region breaks or does not break relative to the content inside it. This technique is useful for keeping related elements grouped together and for preventing important elements from being split.&lt;/p&gt;

&lt;p&gt;The following image is borrowed from &lt;a href=&quot;https://twitter.com/cjgammon&quot;&gt;CJ Gammon&lt;/a&gt;’s &lt;a href=&quot;http://coding.smashingmagazine.com/2013/11/05/killer-responsive-layouts-with-css-regions/&quot;&gt;article on Smashing Magazine&lt;/a&gt; and shows how the break properties can work when shuffling a layout to fit on small screens. They allow us to prevent the regions from breaking until after each image in the image flow. This is an excellent use case for CSS Regions. You can read more about the example in the article on Smashing Magazine.&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/break-demo.gif&quot; alt=&quot;Image showing how regions break on small screens&quot; /&gt;
&lt;/figure&gt;

&lt;p&gt;Brian Rinaldi has also touched on the topic of using CSS Regions in responsive layouts more then once. You can read more about creating simple and more complex layouts with CSS Regions in the following two articles he wrote:&lt;/p&gt;

&lt;ul&gt;
    &lt;li&gt;&lt;a href=&quot;http://flippinawesome.org/2014/01/27/using-css-regions-in-responsive-designs/&quot;&gt;Using CSS Regions In Responsive Designs&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;http://www.adobe.com/inspire/2014/01/complex-layouts-reflow-css-regions.html&quot;&gt;Creating complex layouts for the web with CSS Regions and Adobe Edge Reflow CC&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;use-cases-and-reusability&quot;&gt;CSS Regions Use Cases And Reusability Of Code&lt;/h2&gt;

&lt;p&gt;We can create reusable code using CSS Regions, similar to the way it is possible to create usable components using &lt;a href=&quot;http://w3c.github.io/webcomponents/explainer/&quot;&gt;Web Components&lt;/a&gt;. The following examples show some use cases for CSS Regions that would otherwise not be possible without them. The code used to create the functionality in these examples is reusable.&lt;/p&gt;

&lt;p&gt;I’m sure that once the new features like Grids and Flexbox are more widely supported, a lot more use cases for Regions may rise, so please do not take these examples as the &lt;em&gt;only&lt;/em&gt; use cases for CSS Regions.&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;shuffling-layouts&quot;&gt;Using Regions To Shuffle Layouts On Different Screen Sizes&lt;/h3&gt;

&lt;blockquote class=&quot;twitter-tweet&quot; lang=&quot;en&quot;&gt;&lt;p&gt;Regions give you the possibility to freely reshuffle the whole layout at any breakpoint. Even better than flex &lt;/p&gt;&amp;mdash; Christian Schaefer (@derSchepp) &lt;a href=&quot;https://twitter.com/derSchepp/statuses/426282824002727936&quot;&gt;January 23, 2014&lt;/a&gt;&lt;/blockquote&gt;
&lt;script async=&quot;&quot; src=&quot;//platform.twitter.com/widgets.js&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;

&lt;p&gt;The example we saw earlier about “folding” content and displaying ads in the middle of articles on small screens is a perfect example of the ability of CSS Regions to shuffle layouts, which makes them a fantastic tool to have in our responsive web design toolkit.&lt;/p&gt;

&lt;p&gt;The code used to create the ad-shuffling layout can be reused in any layout that needs the same functionality—you add the necessary regions where you want the ads to appear (flow into), and use the same couple of CSS lines shown earlier to flow the contents of the sidebar (or whichever container contains the ads) into these regions on smaller screens. And, of course, you could also avoid using those extra elements as regions and use the pseudo-element technique for adding the regions between the sections of the article.&lt;/p&gt;

&lt;p&gt;In addition to defining regions where you want the ads to flow into, you can also style these regions’ content using the &lt;code&gt;@region&lt;/code&gt; rule. A &lt;code&gt;@region&lt;/code&gt; rule contains style declarations specific to a particular region. This also allows for even more modular style sheets that are specific to certain regions. This way, you can add your regions into a page, and hook in their specific style sheet, and end up with contents flowing into those regions being styled the way you expect them to, based on the styles u specify using the &lt;code&gt;@region&lt;/code&gt; rule.&lt;/p&gt;

&lt;p&gt;For example, the following snippet will style all paragraphs that flow into a region by applying an italic font style to them.&lt;/p&gt;

&lt;pre class=&quot;brush:css;&quot;&gt;
@region #my-region {
    p {
    font-style: italic;
  }
}
&lt;/pre&gt;

&lt;p&gt;It is important to note here that, at the time of writing of this article, only a subset of styles are implemented for region styling. Only styles that don’t affect layout can now be applied to content inside regions using &lt;code&gt;@region&lt;/code&gt;, like, for example, text and background colors. Styles that do affect layout like display, float, and margin properties do not yet work inside regions. But once they do, we can style region-specific content better and use it to create modular and reusable code. Also, the &lt;strong&gt;styles inside the region are applied to the source element&lt;/strong&gt; which is going to be moved into the region, so you’ll need to remember to use the appropriate class names or IDs as the ones you’re using on your content.&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;repositioning-menus&quot;&gt;Repositioning Menus On Small Screens&lt;/h3&gt;

&lt;p&gt;In this example I am going to use just a couple of lines of CSS to move a navigation from the header into a hidden off-canvas sidebar on small screens. For the sake of brevity and clarity of code, I kept the sidebar visible because I wanted to focus only on the code that accomplishes the responsive menu effect using Regions, and not the actual hiding and showing of that menu.&lt;/p&gt;

&lt;p&gt;The example is pretty basic, so I’ll just create a navigation menu in a header, and a sidebar to flow the menu into on small screens.&lt;/p&gt;

&lt;pre class=&quot;brush:html;&quot;&gt;
&amp;lt;header&amp;gt;
  &amp;lt;a href=&quot;&quot; class=&quot;logo&quot;&amp;gt;Logo&amp;lt;/a&amp;gt;
  &amp;lt;nav role='navigation' class=&quot;my-nav&quot;&amp;gt;
    &amp;lt;ul&amp;gt;
      &amp;lt;li&amp;gt;&amp;lt;a href=&quot;#&quot;&amp;gt;Home&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
      &amp;lt;li&amp;gt;&amp;lt;a href=&quot;#&quot;&amp;gt;About&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
      &amp;lt;li&amp;gt;&amp;lt;a href=&quot;#&quot;&amp;gt;Clients&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
      &amp;lt;li&amp;gt;&amp;lt;a href=&quot;#&quot;&amp;gt;Contact Us&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
    &amp;lt;/ul&amp;gt;
  &amp;lt;/nav&amp;gt;  
&amp;lt;/header&amp;gt;
&amp;lt;aside id=&quot;my-region&quot;&amp;gt;
&amp;lt;/aside&amp;gt;
&lt;/pre&gt;

&lt;p&gt;Only two lines of CSS are needed to move the navigation from the header into the sidebar. And only a few lines of CSS inside an &lt;code&gt;@region&lt;/code&gt; rule are needed to apply styles to the contents inside our region. The navigation which is going to be moved into out region has new styles declared for it inside the &lt;code&gt;@region&lt;/code&gt; rule. You can reuse this piece of code anywhere. Just drop that sidebar in and hook those few lines of CSS. Of course make sure the class names and IDs match those of the elements on the page you’re using them for.&lt;/p&gt;

&lt;p&gt;Because, at this time, only a subset of styles are supported for region styling, I just changed the color of the links inside the sidebar for demonstration purposes, but when more styles are possible, we could have the navigation items stack on top of each other and be styled just like all off-canvas menus we see today.&lt;/p&gt;

&lt;pre class=&quot;brush:css;&quot;&gt;
/* region-specific styles */
@-webkit-region #my-region {
  .my-nav a{
    color: white;
  }
}
/* flow the menu from the header into the sidebar on small screens */
@media screen and (max-width:32em){
  header nav {
    -webkit-flow-into: navigation;
  }
  #my-region {
    -webkit-flow-from: navigation;
  }
}
&lt;/pre&gt;

&lt;p&gt;You can see the live demo and check the code out &lt;a href=&quot;http://codepen.io/SaraSoueidan/pen/0be636b17d2cbdef26ce2e57c3624478&quot;&gt;here&lt;/a&gt;. Resize the screen to see the effect.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://twitter.com/razvancaliman&quot;&gt;Razvan Caliman&lt;/a&gt; has also created a slightly more complex responsive menu that uses CSS Regions to flow menu items into a drop-down menu when these items no longer fit on the menu bar. You can see Razvan’s demo &lt;a href=&quot;http://codepen.io/oslego/pen/tdHEg&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;regions-for-creating-columns&quot;&gt;Examples Of Where One Might Want To Use CSS Regions To Create Columns&lt;/h2&gt;

&lt;p&gt;There is no way in the CSS Multi-Columns module (that I know of) that gives designers the ability to style each column of text individually. The module allows content to be displayed in multiple columns, but it does not give us the power to treat each individual column separately, and give the columns different widths and heights, for example. And this is where CSS Regions have an advantage over Columns.&lt;/p&gt;

&lt;p&gt;Columns created using CSS Regions are stylable any way we want them to. Each column is a region and each region can have its own styling independently of the other columns. And by styling I mean all kinds of styling: from adding backgrounds to a column, to styling text inside a column, to positioning columns, changing column dimensions, and even transforming columns using CSS Transforms.&lt;/p&gt;

&lt;p&gt;Now, if creating a simple multi-column layout is the goal, and that layout can be achieved using Multi-Columns alone, then by all means we &lt;em&gt;should&lt;/em&gt; use CSS Columns to do it. &lt;strong&gt;CSS Regions could do a lot of things, but they should not be used to do everything&lt;/strong&gt;. Just because we can do something with Regions, does not mean that we should.&lt;/p&gt;

&lt;p&gt;The first example shows three columns on the right page of a magazine design, each with different heights. Giving different heights to columns is not possible with CSS Multi-Columns alone. But using CSS Regions, each column can be defined as a region and given any height we want.&lt;/p&gt;

&lt;figure&gt;
    &lt;img src=&quot;https://sarasoueidan.com/images/styled-columns-1.png&quot; alt=&quot;Picture showing a magazine layout with columns of different heights&quot; /&gt;
&lt;/figure&gt;

&lt;p&gt;And another example is one I recently created for &lt;a href=&quot;http://sarasoueidan.com/blog/css-regions-with-shapes-for-readability&quot;&gt;an article&lt;/a&gt; I wrote about combining CSS Regions with CSS Shapes to improve the readability of layouts created using CSS Shapes.&lt;/p&gt;

&lt;figure&gt;
    &lt;img src=&quot;https://sarasoueidan.com/images/styled-columns-3.png&quot; alt=&quot;Screenshot showing a magazine layout with columns styled using CSS Shapes&quot; /&gt;
&lt;/figure&gt;

&lt;p&gt;Each column shown in the above picture is a region, and each region is shaped using CSS Shapes to get the above result. You can view the live demo &lt;a href=&quot;http://sarasoueidan.com/demos/fragmented-magazine-layout/&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;None of these examples would be possible to create using just CSS Multi-Columns, so it is great to have such a tool at our disposal.&lt;/p&gt;

&lt;p&gt;And the final example I want to show is one created by Opera’s Chris Mills for an &lt;a href=&quot;http://dev.opera.com/articles/view/an-introduction-to-css-regions/&quot;&gt;introductory article&lt;/a&gt; he wrote about CSS Regions. The demo is an example of how CSS Regions can be used to create text containers that are separate from each other, transformed using CSS Transforms, and positioned on the screen, and then have the text content flow into these containers in the order they appear in the original source.&lt;/p&gt;

&lt;blockquote&gt;
    In effect, we are completely separating out our content from any kind of layout, bringing the original intention of CSS to a welcome extreme. The content can be flowed into your layout containers regardless of how they are sized and positioned, and will reflow when those containers change size.&amp;mdash;Chris Mills 
&lt;/blockquote&gt;

&lt;p&gt;This is a screenshot of the demo:&lt;/p&gt;

&lt;figure&gt;
    &lt;img src=&quot;https://sarasoueidan.com/images/opera-demo.png&quot; alt=&quot;Screenshot of a demo using CSS Regions to flow content into containers transformed using CSS Transforms&quot; /&gt;
&lt;/figure&gt;

&lt;p&gt;The demo is a nice demonstration of how CSS Regions offer us finer control over fragments of a page and how they allow us to flow content inside containers any way we want them to, while giving us the ability to completely separate content from layout concerns.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;regions-vs-future-layouts&quot;&gt;CSS Regions Versus Future Layout Features&lt;/h2&gt;

&lt;p&gt;CSS Regions can be used today to create and shuffle layouts in many ways. But what happens if a better solution is someday introduced that allows us to do what Regions allow us to do today in a better and more efficient way?&lt;/p&gt;

&lt;p&gt;Mr. Håkon Wium Lie has already shown some good solutions to certain layout problems he discussed in his article by using &lt;a href=&quot;http://figures.spec.whatwg.org/&quot;&gt;CSS Figures&lt;/a&gt; instead of CSS Regions. This is actually great, considering that the Figures and Multi-Columns specs are made to do what he used them to do. And when these specifications are implemented, we will probably use &lt;em&gt;them&lt;/em&gt; to do what he did too, as they offer a more flexible way to create those layouts than Regions do.&lt;/p&gt;

&lt;p&gt;As a matter of fact, &lt;a href=&quot;http://figures.spec.whatwg.org/&quot;&gt;the CSS Figures specification&lt;/a&gt; introduces some really cool and useful features, like vertical floating, where we get to float an element using &lt;code&gt;top&lt;/code&gt; and &lt;code&gt;bottom&lt;/code&gt; values, in addition to the &lt;code&gt;left&lt;/code&gt; and &lt;code&gt;right&lt;/code&gt; values that we have today. I can imagine how these values can be useful already.&lt;/p&gt;

&lt;p&gt;We also get a &lt;a href=&quot;http://figures.spec.whatwg.org/#the-float-offset-property&quot;&gt;float offset property&lt;/a&gt; which allows us to offset a floated element, which is super useful considering how easy it makes to wrap content around a figure such as an image or a blockquote. We are now able to offset any element by applying a &lt;code&gt;position:relative&lt;/code&gt; to it and then using the offset properties (&lt;code&gt;left&lt;/code&gt;, &lt;code&gt;right&lt;/code&gt;, &lt;code&gt;top&lt;/code&gt;, &lt;code&gt;bottom&lt;/code&gt;) to “push” it in any direction we want, but that will not affect the flow of content around it, as the position it occupies on the page will be preserved and won’t be occupied by any surrounding element. Using float offsets introduced in the CSS Figures spec, we will be able to wrap content around an offsetted float with so much ease, and this is just brilliant!&lt;/p&gt;

&lt;p&gt;When new features such as CSS Figures are implemented, we will definitely use those for a lot of use cases where they would fit in better than CSS Regions, but CSS Regions also have other use cases that neither the Multi-Column specification, nor the Figures specification can do. So I believe that it would be great for the future of the web for all of these tools and features to coexist and offer us, designers and developers, a wider range of options that give us more control over our layouts.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;final-words&quot;&gt;Final Words&lt;/h2&gt;

&lt;p&gt;CSS Regions give us the ability to do a lot of things that are otherwise not possible without them. They can be a very useful and powerful tool in our responsive web design toolset, and when used in conjunction with other features, can provide us with great solutions to common design problems we are faced with.&lt;/p&gt;

&lt;p&gt;Just like any other feature, CSS Regions should be used in the right place, and it is our responsibility to use them that way. I believe that we are smart enough to know when and where to use a feature.&lt;/p&gt;

&lt;p&gt;Do you have any great use cases for CSS Regions? Do you think it will be a valuable tool in our web design toolset?&lt;/p&gt;

&lt;p class=&quot;note&quot;&gt;
  I published this article originally on &lt;a href=&quot;http://flippinawesome.org/2014/01/27/css-regions-matter/&quot;&gt;flippinawesome.org&lt;/a&gt; on January 27&lt;sup&gt;th&lt;/sup&gt;, 2014.
&lt;/p&gt;

    
  </content>
 </entry>
 
 <entry>
   <title>Animating CSS Shapes with CSS Animations and Transitions</title>
   <link href="http://sarasoueidan.com//blog/animating-css-shapes/"/>
   <id>https://sarasoueidan.com/blog/animating-css-shapes</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;
  Today we're going to be talking about animating CSS shapes with CSS animations. 
  We'll be creating very basic &quot;shape-shifting&quot; layouts of sort. 
  There are also many considerations to take when animating CSS shapes, so we'll go over all points 
  in this article.
&lt;/p&gt;

&lt;p&gt;This is the third article in a series of articles I’m writing about CSS shapes, 
  so in this article I’m assuming you have a basic understanding of how CSS shapes are created. 
  You may want to check the first article: &lt;a href=&quot;http://sarasoueidan.com/blog/css-shapes/&quot;&gt;Creating Non-Rectangular Layouts with CSS Shapes&lt;/a&gt;, which covers all the basics to get you started creating CSS shapes in no time. The second article was about &lt;a href=&quot;http://sarasoueidan.com/blog/css-regions-with-shapes-for-readability/&quot;&gt;combining CSS shapes with CSS regions to create more readable layouts&lt;/a&gt;, tackling one face of the accessibility of non-rectangular shaped layouts.&lt;/p&gt;

&lt;div class=&quot;note warning&quot;&gt;
Notes: 
&lt;ul&gt;
&lt;li&gt;Most of this article's demos use the `shape-inside` property, which has been temporarily &lt;a href=&quot;https://bugs.webkit.org/show_bug.cgi?id=130698&quot;&gt;removed from Webkit&lt;/a&gt; and &lt;a href=&quot;https://codereview.chromium.org/209443007/&quot;&gt;Blink&lt;/a&gt;.&lt;/li&gt;

&lt;li&gt;The principles of animating shapes is applicable to both CSS Shapes *and* CSS Clipping masks. All the
demos in this article use clipping masks to visualize CSS Shapes. Hence, the shapes are animated as clipping paths as well. So, you will be able to see the shapes animate in webkit-based browsers at this time, but the content inside the shapes won't be affected by the shapes because of the current state of support for CSS Shapes.&lt;/li&gt;
              
&lt;li&gt;For a detailed suppprt table for CSS Clipping and Masking, see the &lt;a href=&quot;http://caniuse.com/css-masks&quot;&gt;CanIUse.com Support table&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
  Also, check the &lt;a href=&quot;http://caniuse.com/#feat=css-shapes&quot;&gt;current state of browser support for CSS Shapes&lt;/a&gt; out.
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;animatable-shapes&quot;&gt;Animatable CSS Shapes&lt;/h2&gt;

&lt;p&gt;There are two ways we can create a shape with CSS shapes: using an image URI which the browser uses to extract the shape from, and using one of the available CSS shapes functions, such as &lt;code class=&quot;highlighter-rouge&quot;&gt;polygon()&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;circle()&lt;/code&gt;, among others.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Shapes defined using an image are not animatable&lt;/strong&gt;. So, if you define a shape using an image, specify a transition, and then define another shape using another image when the element is hovered, for example, the shape applied to the shape will change but it will not animate or transition into the new shape; it will just “jump” from one shape into another in an abrupt manner.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The only way a shape can be animated is when it is defined using a &lt;u&gt;shape function&lt;/u&gt;.&lt;/strong&gt; Even so, there is one condition: &lt;strong&gt;the number of points defining the final shape 
must be the same as the number of points defining the initial shape&lt;/strong&gt;. When you think about it, makes perfect sense: you have a set of points that make up a shape, and then you move those points around and have them form another shape.&lt;/p&gt;

&lt;p&gt;So the &lt;strong&gt;transitions on CSS shapes are really transitions on the individual points making up a shape&lt;/strong&gt;. More specifically, &lt;strong&gt;transitions on the coordinates of each point&lt;/strong&gt;, where the coordinates are interpolated as real numbers to allow animations and transitions, just like any other animatable CSS values.&lt;/p&gt;

&lt;p&gt;When I first learned about this condition, I thought it would not be possible to change a simple shape into a more complex one that has more points defining it, but that’s actually not true, it is very doable: start by definining the points needed for the more complex shape, and then rearrange 
those points to form the simple shape.&lt;/p&gt;

&lt;p&gt;For example, even if you have 20 points making up the final shape, and you want to start with a simple rectangle defined by 4 points, you can still place those 16 extra points on the rectangular shape by simply placing them on one of the rectangle’s sides, without them affecting the shape of the rectangle. We’ll get into the examples shortly.&lt;/p&gt;

&lt;p&gt;Now, thinking about the complex shape first does not at all mean that the initial shape must be more complex than the final shape, it just means that you will think ahead and prepare all the requirements before you start coding. You can easily animate a simple shape into a more complex one if you have all the necessary points defined.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;considerations&quot;&gt;Things To Consider Before Animating Your Shaped Layouts&lt;/h2&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;content-fitting&quot;&gt;Content Fitting Inside Different Shapes&lt;/h3&gt;

&lt;p&gt;One thing to consider when animating CSS shapes in general is the fact that the content that fits into one shape may not perfectly fit into another. You may start out with a simple shape, and when you animate that shape into a new one, the content inside that shape may overflow the new shape, or may be too little for the new shape. So you may need to resize the element as you change its shape as well, so, again, the design process has to be thought through in detail. This also means that it wouldn’t be very simple to depend on animated CSS shapes for dynamic content, unless you create the shapes dynamically with Javascript. But that’s a topic for another blog post ;)&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;readability&quot;&gt;Animated Text Becomes Temporarily Unreadable&lt;/h3&gt;

&lt;p&gt;Animating text composition in general, whether by animating Shapes or even a simple animation of width, will make the text temporarily unreadable. &lt;em&gt;(Thanks &lt;a href=&quot;https://twitter.com/alanstearns&quot;&gt;Alan&lt;/a&gt; for bringing that to my attention)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;While it might be an interesting graphical effect for smooth layout changes, the process of text changing its layout blocks the ability of the reader to read your content, and it is only after the animation is over that they will again be able to read the text. That is, of course, assuming that the Shapes are readable to begin with, as we noted before.&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;accessibility&quot;&gt;General Accessibility&lt;/h3&gt;

&lt;p&gt;Be careful what shapes you choose, and consider how they affect the readability of your content. And think twice before integrating CSS animations with CSS Shapes.&lt;/p&gt;

&lt;p&gt;Also, shapes that are fine on big screens may not be fine on small screens. Personally, I would
stick with rectangular layouts on small screens, and progress to more complex (but still readable)
shaped layouts on big screens. But then again, this depends on the shapes you choose, and some
simple shapes are actually OK on small screens as well.&lt;/p&gt;

&lt;p&gt;The aim of this article is to discuss and &lt;strong&gt;experiment&lt;/strong&gt; with the different ways and options we have to animate CSS Shapes. &lt;strong&gt;Shapes animations should only be used in real projects when there is a practical use-case for these animations&lt;/strong&gt; (and of course when there is more acceptable browser support ^^). I’m sure someone will think of a nice use-case. So, for now, let’s dig into the actual animation process and have some CSS Shapes fun!&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;example-1&quot;&gt;1. Initial and Final Shapes Have Same Number Of Points&lt;/h2&gt;

&lt;p&gt;We’ll start off with very simple examples and then progess to more complex ones. The examples in this section won’t deal with the number of points defining a shape. We’ll leave that for the second section.&lt;/p&gt;

&lt;p&gt;The best way to visualize a shape animation it to use the shape properties in conjunction with the &lt;code class=&quot;highlighter-rouge&quot;&gt;clip-path&lt;/code&gt; property, which will clip any excess parts of the element that are outside the defined shape. I’ve talked about using CSS clip paths with CSS Shapes in detail in my &lt;a href=&quot;http://sarasoueidan.com/blog/css-shapes/&quot;&gt; first article&lt;/a&gt;, so check that out if you still don’t know how they can relate.&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;rearranging-points&quot;&gt;Rearranging Points To Create A New Shape&lt;/h3&gt;

&lt;p&gt;So for our first example, we’re going to do just that. And for the sake of simplicity and demonstration purposes, we’re not going to be doing anything fancy. We’ll create a simple shape and animate it on hover using a simple CSS transition. The initial and final shapes will have the same number of points so we won’t have to worry about that for now. All we have to do, is move the points around (rearrange them) and see how the shape transitions smoothly.&lt;/p&gt;

&lt;p&gt;We’ll start with a simple inverted trapezoid shape, and when you hover over the element, its shape will animate but the final shape will also be a trapezoid. The only thing we’re going here is move the points defining the shape around by changing their coordinate values.&lt;/p&gt;

&lt;p&gt;We’ll be using the &lt;code class=&quot;highlighter-rouge&quot;&gt;clip-path&lt;/code&gt; property to make the shape and the animation clearer and more obvious.&lt;/p&gt;

&lt;pre class=&quot;brush:css&quot;&gt;
.element {   
  shape-inside: polygon(0 0,  500px 0, 350px 300px, 150px 300px);
  shape-padding: 20px;
  transition: all 2s ease; 
  -webkit-clip-path: polygon(0 0,  500px 0, 350px 300px, 150px 300px);
} 
.element:hover {
  shape-inside: polygon(150px 0, 350px 0, 500px 300px, 0px 300px);
  -webkit-clip-path: polygon(150px 0, 350px 0, 500px 300px, 0px 300px);  
} 
&lt;/pre&gt;

&lt;p&gt;The shape-* properties are supported unprefixed in Canary, but the &lt;code class=&quot;highlighter-rouge&quot;&gt;clip-path&lt;/code&gt; property still needs its prefix to work.&lt;/p&gt;

&lt;p data-height=&quot;500&quot; data-theme-id=&quot;3617&quot; data-slug-hash=&quot;6aa071b2202052b5fed38b1312f3878d&quot; data-default-tab=&quot;result&quot; class=&quot;codepen&quot;&gt;See the Pen &lt;a href=&quot;http://codepen.io/SaraSoueidan/pen/6aa071b2202052b5fed38b1312f3878d&quot;&gt;animating a css shape&lt;/a&gt; by Sara Soueidan (&lt;a href=&quot;http://codepen.io/SaraSoueidan&quot;&gt;@SaraSoueidan&lt;/a&gt;) on &lt;a href=&quot;http://codepen.io&quot;&gt;CodePen&lt;/a&gt;.&lt;/p&gt;
&lt;script async=&quot;&quot; src=&quot;//codepen.io/assets/embed/ei.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;And it’s as simple as that! Just change the coordinates of the points defining the shape define a transition on the element, and you’ve got yourself a smooth animating shape.&lt;/p&gt;

&lt;p&gt;You will be able to see how the text inside the shape is animated into the new shape as well. And you can also see that, during the animation process, that text is very unreadable.&lt;/p&gt;

&lt;p&gt;Now, if you change the shape on hover to another shape by using another shape function for example, or changing the number of points defining the shape, the shape &lt;em&gt;does&lt;/em&gt; change, but it will not transition or animate; it will jump from the initial shape to the final one abruptly. For example, the following code:&lt;/p&gt;

&lt;pre class=&quot;brush:css&quot;&gt;
.demo:hover {
  /* blink needs old syntax at this time */
  shape-inside: circle(50%, 50%, 50%);
  -webkit-clip-path: circle(50%, 50%, 50%);
  /* new syntax for webkit */
  shape-inside: circle(50% at 50% 50%);
  -webkit-clip-path: circle(50% at 50% 50%);
} 
&lt;/pre&gt;

&lt;p&gt;I’ve added this snippet to the above demo. Click the “Edit on Codepen” link on the demo, and uncomment the snippet in the &lt;code class=&quot;highlighter-rouge&quot;&gt;.element:hover&lt;/code&gt; rule to see how it affects the animation.&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;resizing-shapes-with-no-points&quot;&gt;Resizing Shapes With No Specific Number Of Points&lt;/h3&gt;

&lt;p&gt;In our second example, we’re going to animate a circular shape by increasing its diameter. The number of points on a circle is undefined, and the only thing defining the circle in the &lt;code class=&quot;highlighter-rouge&quot;&gt;circle()&lt;/code&gt; function is its center and radius.&lt;/p&gt;

&lt;p&gt;We’re going to have the circle increase in size when we hover over it. This sounds a lot like resizing an element in CSS using CSS transforms and the &lt;code class=&quot;highlighter-rouge&quot;&gt;scale()&lt;/code&gt; function, but it’s not really the same. When you scale an element using CSS transforms, the entire element including its content will increase in size, and by increasing its size you’re not creating any extra room inside it for more content. But when you resize an element’s defined Shape, you &lt;em&gt;are&lt;/em&gt; making more room or taking away from it. So, you could, for example, increase the size of the element’s shape and then dynamically add some content to it and have that content fit inside it, but that’s not possible when you’re scaling the element up with CSS transforms.&lt;/p&gt;

&lt;p&gt;In this example we’ll first define a circular shape on our element using the &lt;code class=&quot;highlighter-rouge&quot;&gt;circle()&lt;/code&gt; shape function, and then animate the circle’s size when the element is hovered. You will be able to see how that will affect the room inside the element.&lt;/p&gt;

&lt;p&gt;First, we’re going to define the shape on the element and fire an animation on hover. We’ll define a circle of radius &lt;code class=&quot;highlighter-rouge&quot;&gt;210px&lt;/code&gt; whose center is positioned at the center of the element.&lt;/p&gt;

&lt;pre class=&quot;brush:css&quot;&gt;
.element {   
  /* ... */
  shape-padding: 15px;
  /* blink */
   shape-inside: circle(250px, 250px, 210px);
  -webkit-clip-path: circle(250px, 250px, 210px);
  /* webkit */
  shape-inside: circle(210px at 250px 250px);
  -webkit-clip-path: circle(210px at 250px 250px);
} 
.element:hover{ 
   -webkit-animation: scale 3s ease infinite;
} 
&lt;/pre&gt;

&lt;p&gt;Then we’re going to define the animation keyframes that will control the scaling of the shape. The final effect will be similar to a beating effect: the radius of the circle is going to increase and then decrease back to its initial size at the end of the animation. And we have set the animation to repeat infinitely as long as you’re hovering over the element.&lt;/p&gt;

&lt;pre class=&quot;brush:css&quot;&gt;
@keyframes scale {
  0% {
    shape-inside: circle(250px, 250px, 210px);
    -webkit-clip-path: circle(250px, 250px, 210px);

    shape-inside: circle(250px, 250px, 210px);
    -webkit-clip-path: circle(210px at 250px 250px);
  }
  50% {
    shape-inside: circle(250px, 250px, 250px);
    -webkit-clip-path: circle(250px, 250px, 250px);

    shape-inside: circle(250px, 250px, 250px);
    -webkit-clip-path: circle(250px at 250px 250px);
  }
  0% {
    shape-inside: circle(250px, 250px, 210px);
    -webkit-clip-path: circle(250px, 250px, 210px); 

    shape-inside: circle(250px, 250px, 210px);
    -webkit-clip-path: circle(210px at 250px 250px);
  }
}
&lt;/pre&gt;

&lt;p&gt;And here is the resulting effect:&lt;/p&gt;

&lt;p data-height=&quot;600&quot; data-theme-id=&quot;3617&quot; data-slug-hash=&quot;acb88a202823fd107d1327f4b1a9c3c2&quot; data-default-tab=&quot;result&quot; class=&quot;codepen&quot;&gt;See the Pen &lt;a href=&quot;http://codepen.io/SaraSoueidan/pen/acb88a202823fd107d1327f4b1a9c3c2&quot;&gt;animating a css shape&lt;/a&gt; by Sara Soueidan (&lt;a href=&quot;http://codepen.io/SaraSoueidan&quot;&gt;@SaraSoueidan&lt;/a&gt;) on &lt;a href=&quot;http://codepen.io&quot;&gt;CodePen&lt;/a&gt;.&lt;/p&gt;
&lt;script async=&quot;&quot; src=&quot;//codepen.io/assets/embed/ei.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;You can see that the room for content inside the circle increases, and more text can fit inside the shape when it grows. So, you could, for example, use a growing CSS Shape to show more content information when an element is hovered.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;example-2&quot;&gt;2. Initial and Final Shape Have Different Number Of Points&lt;/h2&gt;

&lt;p&gt;In this section we’re to animate from one shape to another, where the initial and final shapes are &lt;em&gt;visually&lt;/em&gt; defined by a different number of points. We’re going to use the &lt;code class=&quot;highlighter-rouge&quot;&gt;polygon()&lt;/code&gt; function to define our shapes.&lt;/p&gt;

&lt;p&gt;As I mentioned at the beginning of this article, only shapes with the same number of points can be animated into one another. So, in order to animate from a shape to another, we have to make sure that the number of points in the definition of these shapes is the same, even if they don’t visually appear to have the same number.&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;animating-complex-to-simple&quot;&gt;Animating A Complex Shape Into a Simple Shape&lt;/h3&gt;

&lt;p&gt;We’re going to start with a very simple demo, where we animate a star shape into a simple rectangle shape. (Not that the star shape is really complex, but it is more complex than the final shape it will be animating into.)&lt;/p&gt;

&lt;figure&gt;
  &lt;img src=&quot;https://sarasoueidan.com/images/star-to-rect.jpg&quot; alt=&quot;The two shapes&quot; /&gt;
    &lt;figcaption&gt;
      The two shapes we'll be animating. Blue discs show the number of points needed to define the shape.
    &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;The star is defined by 10 points (shown in blue), and the rectangle only needs 4 points to define it. It is true that the rectangle only &lt;strong&gt;needs&lt;/strong&gt; four points to define it, but it can also be defined by as many points as we want it to.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;polygon()&lt;/code&gt; function we’re going to use to define these two shapes &lt;strong&gt;needs to take in the same number of points for the two shapes&lt;/strong&gt; in order for the animation to be possible. This means that we need to use 10 points to draw the rectangle just like we will use 10 points to draw the star, and there is no problem with this. Why? Because we can simply place the extra points in a way that does not affect the resulting shape we want.&lt;/p&gt;

&lt;p&gt;So, the rectangle will be defined by 10 points, which we can place as shown in the image below:&lt;/p&gt;

&lt;figure&gt;
  &lt;img src=&quot;https://sarasoueidan.com/images/rect-points.jpg&quot; alt=&quot;points placed on the rectangle&quot; /&gt;
    &lt;figcaption&gt;
      The rectangle can be defined by as many points as the star. This is needed to make the animation work.
    &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Of course, we could have placed the extra 6 points anywhere on the rectangle’s edges, but that would change the animating effect. We’ll get to this in a minute.&lt;/p&gt;

&lt;p&gt;Now that we have the same number of points in both shapes, we can easily transition from one shape to another.&lt;/p&gt;

&lt;p&gt;We’ll first position the points so that they make up a star. The &lt;code class=&quot;highlighter-rouge&quot;&gt;polygon()&lt;/code&gt; function for the star shape looks like following; of course, we’re also going to clip the element to the shape using the &lt;code class=&quot;highlighter-rouge&quot;&gt;clip-path&lt;/code&gt; property as we did before.&lt;/p&gt;

&lt;pre class=&quot;brush:css&quot;&gt;
.element {
  /* ... */
  shape-inside: polygon(250px 0, 350px 170px, 500px 180px, 380px 320px, 450px 500px, 250px 420px, 50px 500px, 120px 320px, 0px 180px, 150px 170px );
  shape-padding: 10px;
  transition: all 3s ease; 
  -webkit-clip-path: polygon(250px 0, 350px 170px, 500px 180px, 380px 320px, 450px 500px, 250px 420px, 50px 500px, 120px 320px, 0px 180px, 150px 170px );  
}
&lt;/pre&gt;

&lt;p&gt;And when the element is hovered, the points will be rearranged to form a rectangle defined as follows:&lt;/p&gt;

&lt;pre class=&quot;brush:css&quot;&gt;
.element:hover {
shape-inside: polygon(250px 0, 500px 0, 500px 180px, 500px 320px, 500px 500px, 250px 500px, 0 500px, 0 320px, 0 180px, 0 0);
  -webkit-clip-path: polygon(250px 0, 500px 0, 500px 180px, 500px 320px, 500px 500px, 250px 500px, 0 500px, 0 320px, 0 180px, 0 0);  
}
&lt;/pre&gt;

&lt;p&gt;You can see the points being rearranged to form a rectangle in this live demo:&lt;/p&gt;

&lt;p data-height=&quot;700&quot; data-theme-id=&quot;3617&quot; data-slug-hash=&quot;17dd591f451f4757366faf3c9246504b&quot; data-default-tab=&quot;result&quot; class=&quot;codepen&quot;&gt;See the Pen &lt;a href=&quot;http://codepen.io/SaraSoueidan/pen/17dd591f451f4757366faf3c9246504b&quot;&gt;animating a css shape&lt;/a&gt; by Sara Soueidan (&lt;a href=&quot;http://codepen.io/SaraSoueidan&quot;&gt;@SaraSoueidan&lt;/a&gt;) on &lt;a href=&quot;http://codepen.io&quot;&gt;CodePen&lt;/a&gt;.&lt;/p&gt;
&lt;script async=&quot;&quot; src=&quot;//codepen.io/assets/embed/ei.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;Rearranging the points has a very big effect on the transition. The order of points in the initial shape is preserved when they are rearranged to form the second one, and you need to keep that in mind, otherwise you may end up with a not-so-beautiful transition/animation effect.&lt;/p&gt;

&lt;p&gt;The following is an example of what could happen if you randomly rearrange the points. You can see where each point will be placed, and you can tell that that is not the best way to do it.&lt;/p&gt;

&lt;p data-height=&quot;700&quot; data-theme-id=&quot;3617&quot; data-slug-hash=&quot;b49058b2d80de2c2463a42daf4d1c9aa&quot; data-default-tab=&quot;result&quot; class=&quot;codepen&quot;&gt;See the Pen &lt;a href=&quot;http://codepen.io/SaraSoueidan/pen/b49058b2d80de2c2463a42daf4d1c9aa&quot;&gt;animating a css shape&lt;/a&gt; by Sara Soueidan (&lt;a href=&quot;http://codepen.io/SaraSoueidan&quot;&gt;@SaraSoueidan&lt;/a&gt;) on &lt;a href=&quot;http://codepen.io&quot;&gt;CodePen&lt;/a&gt;.&lt;/p&gt;
&lt;script async=&quot;&quot; src=&quot;//codepen.io/assets/embed/ei.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;So be careful and always make sure you rearrange the points the best possible way that ensures a nice transitioning effect.&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;animating-simple-to-complex&quot;&gt;Animating A Simple Shape Into a Complex Shape&lt;/h3&gt;

&lt;p&gt;Our last example is the most complex one, but you’ll find that it’s not really complex at all.&lt;/p&gt;

&lt;p&gt;We’ll be applying the same concept we applied in the previous example, but instead of starting out with a higher number of points we’re going to start with a simple rectangular shape and have it animate into an irregular shape.&lt;/p&gt;

&lt;p&gt;We’ll have a look at what the final shape looks like, how many points it needs to define it, and then well define the initial rectangle shape using the same number of points. The points will be placed on the edges of the rectangle, and we’ll make sure we place them in a suitable way so that they animate to the final shape the way we’d expect them to.&lt;/p&gt;

&lt;p&gt;We want to start with a simple two-column layout. Each column is a separate rectangular element. And then when we hover over the columns’ container, a shape, a tree in our example, will show in the middle between the two columns, and the two columns will animate their shapes so that they kind of wrap the tree in the middle. This example is inspired by &lt;a href=&quot;http://codepen.io/adobe/pen/Cnvuf&quot;&gt;this pen&lt;/a&gt; by the Adobe Web Platform Team.&lt;/p&gt;

&lt;p&gt;So this is how the shapes in our our final demo will look like:&lt;/p&gt;

&lt;figure&gt;
  &lt;img src=&quot;https://sarasoueidan.com/images/demo-shapes.png&quot; alt=&quot;The shapes of the columns in the final demo&quot; /&gt;
		&lt;figcaption&gt;
      The image shows the shapes that the two text columns will animate to when their container is hovered. The shape on the left shows the points needed to define it. The shape on the right will have a similar point structure.
    &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;I drew this in image Photoshop, even the example tree is actually a shape defined in Photoshop. Again, we’re not going to be doing anything fancy in this article, we’ll leave that to another one!&lt;/p&gt;

&lt;p&gt;The shapes in the image are &lt;em&gt;similar&lt;/em&gt; to the shapes in the final demo. Of course, there are no coordinates in the image above to the shapes will probably differ a bit when we plot their points on the elements’ coordinate systems. So, let’s get to it!&lt;/p&gt;

&lt;p&gt;We’ll start by creating two columns of text inside a container. We’ll use the tree shape as a background to the entire container. At first, the background will be invisible, and then when the container is hovered, the background image will scale up, making it appear as if the tree is scaling up in the middle between the two columns. And as the tree appears, the two columns will animate into their respective shapes shown in the above image.&lt;/p&gt;

&lt;p&gt;In order to know how many points are exactly needed, so that we can define them on the rectangle, I’m going to start by defining the &lt;strong&gt;final&lt;/strong&gt; shape of the columns, and then move backwards and use the same number of points to define the rectangle.&lt;/p&gt;

&lt;p&gt;The right column’s final shape can be defined by the following shape function:&lt;/p&gt;

&lt;pre class=&quot;brush:css&quot;&gt;
shape-inside: polygon(0 0, 300px 0, 300px 550px, 30px 550px, 30px 450px, 80px 400px, 100px 400px, 120px 400px, 160px 350px, 120px 250px, 100px 200px,  100px 170px, 100px 160px, 60px 130px, 60px 110px, 0 60px);  
  -webkit-clip-path: polygon(0 0, 300px 0, 300px 550px, 30px 550px, 30px 450px, 80px 400px, 100px 400px, 120px 400px, 160px 350px, 120px 250px, 100px 200px,  100px 170px, 100px 160px, 60px 130px, 60px 110px, 0 60px); 
&lt;/pre&gt;

&lt;p&gt;From the above shape, we can now define the initial rectangular shape, by using the same number of points but placing them on the rectangle’s edges. Because our shape changes only on the left side, we can place all the animating points on the left edge of the recangle, and then have them move horizontally into their places on hover. This means that it’s enough to use the same &lt;code class=&quot;highlighter-rouge&quot;&gt;polygon()&lt;/code&gt; function as above, but move those points on the left of the shape to the left edge of the rectangle, by giving them all zero abscissa.&lt;/p&gt;

&lt;pre class=&quot;brush:css&quot;&gt;
shape-inside: polygon(0 0, 300px 0, 300px 550px, 0 550px, 0 450px, 0 400px, 0 400px, 0 400px, 0 350px, 0 250px, 0 200px, 0 170px, 0 160px, 0 130px, 0 110px, 0 60px);  
  -webkit-clip-path: polygon(0 0, 300px 0, 300px 550px, 0 550px, 0 450px, 0 400px, 0 400px, 0 400px, 0 350px, 0 250px, 0 200px, 0 170px, 0 160px, 0 130px, 0 110px, 0 60px);              
&lt;/pre&gt;

&lt;p&gt;So the final code to animate the shape of the right column when its container is hovered looks like the following:&lt;/p&gt;

&lt;pre class=&quot;brush:css&quot;&gt;
.column-right {
  float: right;
  shape-inside: polygon(0 0, 300px 0, 300px 550px, 0 550px, 0 450px, 0 400px, 0 400px, 0 400px, 0 350px, 0 250px, 0 200px, 0 170px, 0 160px, 0 130px, 0 110px, 0 60px);  
  -webkit-clip-path: polygon(0 0, 300px 0, 300px 550px, 0 550px, 0 450px, 0 400px, 0 400px, 0 400px, 0 350px, 0 250px, 0 200px, 0 170px, 0 160px, 0 130px, 0 110px, 0 60px);
}
.container:hover .column-right {
  shape-inside: polygon(0 0, 300px 0, 300px 550px, 30px 550px, 30px 450px, 80px 400px, 100px 400px, 120px 400px, 160px 350px, 120px 250px, 100px 200px,  100px 170px, 100px 160px, 60px 130px, 60px 110px, 0 60px);  
  -webkit-clip-path: polygon(0 0, 300px 0, 300px 550px, 30px 550px, 30px 450px, 80px 400px, 100px 400px, 120px 400px, 160px 350px, 120px 250px, 100px 200px,  100px 170px, 100px 160px, 60px 130px, 60px 110px, 0 60px); 
}
&lt;/pre&gt;

&lt;p&gt;Similarly, we can get the shape functions for the left column. First define the final (more complex) shape, to get the necssary number of points. Then rearragne those points into a rectangle.&lt;/p&gt;

&lt;pre class=&quot;brush:css&quot;&gt;
.column-left {
  shape-inside: polygon(0 0, 300px 0, 300px 60px, 300px 80px, 300px 100px, 300px 150px, 300px 180px, 300px 275px, 300px 375px, 300px 420px, 300px 410px, 300px 440px, 300px 450px, 300px 550px, 0 550px );
  -webkit-clip-path: polygon(0 0, 300px 0, 300px 60px, 300px 80px, 300px 100px, 300px 150px, 300px 180px, 300px 275px, 300px 375px, 300px 420px, 300px 410px, 300px 440px, 300px 450px, 300px 550px, 0 550px );
}
.container:hover .column-left {
  shape-inside: polygon(0 0, 300px 0, 300px 60px, 280px 80px, 240px 100px, 230px 150px, 200px 180px, 160px 275px, 130px 375px, 160px 420px, 240px 410px, 270px 440px, 290px 450px, 290px 550px, 0 550px );
  -webkit-clip-path: polygon(0 0, 300px 0, 300px 60px, 280px 80px, 240px 100px, 230px 150px, 200px 180px, 160px 275px, 130px 375px, 160px 420px, 240px 410px, 270px 440px, 290px 450px, 290px 550px, 0 550px );
}
&lt;/pre&gt;

&lt;p&gt;And of course in order to get a smooth shape transition, we need to define a transition on the two columns, and a transition on the container with the same speed and timing function, so that the background and column shapes can animate simultaneously.&lt;/p&gt;

&lt;pre class=&quot;brush:css&quot;&gt;
.container {
  width: 700px;
  height: 600px;
  margin: 30px auto;
  padding: 25px;
  background: #eee url(http://sarasoueidan.com/blog/animating-css-shapes/images/tree.png) 50% 50% no-repeat;
  background-size: 0;
  transition: all 1s linear;
}
/* scale the background up on hover */
.container:hover {
  background-size: 50% auto;
}

.column {
  /* height and width must be explicitly set otherwise Shapes won't work */
  width: 300px;
  height: 550px;
  text-align: justify;
  background-color: #000;
  color: #ddd;
  /* define same transition duration and timing function as the container's */
  transition: all 1s linear; 
  shape-padding: 10px;
}
&lt;/pre&gt;

&lt;p&gt;And this is the final working demo:&lt;/p&gt;

&lt;p data-height=&quot;750&quot; data-theme-id=&quot;3617&quot; data-slug-hash=&quot;94e3c9210c418770206487ef8700a1c2&quot; data-default-tab=&quot;result&quot; class=&quot;codepen&quot;&gt;See the Pen &lt;a href=&quot;http://codepen.io/SaraSoueidan/pen/94e3c9210c418770206487ef8700a1c2&quot;&gt;94e3c9210c418770206487ef8700a1c2&lt;/a&gt; by Sara Soueidan (&lt;a href=&quot;http://codepen.io/SaraSoueidan&quot;&gt;@SaraSoueidan&lt;/a&gt;) on &lt;a href=&quot;http://codepen.io&quot;&gt;CodePen&lt;/a&gt;.&lt;/p&gt;
&lt;script async=&quot;&quot; src=&quot;//codepen.io/assets/embed/ei.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;It is worth noting here that if the two columns were completely filled with text before they are animated, that text will overflow the shape that it will animate to. This is one of those cases where you would want to take into account the text and shapes before you decide to animate your element’s shape.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;final-words&quot;&gt;Final Words&lt;/h2&gt;

&lt;p&gt;In this article we went over the basics of animating CSS shapes. All of the shapes we animated here were static, i.e defined in the CSS and animated there. But sometimes, in order to achieve more compelling visual effects, you may want to dynamically create shapes while some element moves on the page. That kind of shape animations can be achieved by creating and animating CSS shapes using Javascript, and is, for now, outside the scope of this article.&lt;/p&gt;

&lt;p&gt;The examples and demos I showed in this article are all for demonstration purposes only, and may not make for a practical use-case for animated CSS shapes. But combined with CSS clipping paths, some creative shape-shifting layouts can be created that don’t compromise readability of the content.&lt;/p&gt;

&lt;p&gt;I hope this article, along with the previous two, helped you get up and running with CSS shapes. Of course, at this time, support for CSS Shapes is still limited, but I highly encourage you to start experimenting with them now, as you could help find and fix bugs, and of course when the time comes and Shapes are widely supported, you’ll be Shapes masters by then. :)&lt;/p&gt;

&lt;p&gt;I hope you found this article useful. Thank you very much for reading!&lt;/p&gt;


    
  </content>
 </entry>
 
 <entry>
   <title>Using CSS Regions With CSS Shapes For A Better Reading Experience</title>
   <link href="http://sarasoueidan.com//blog/css-regions-with-shapes-for-readability/"/>
   <id>https://sarasoueidan.com/blog/css-regions-with-shapes-for-readability</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;
Using &lt;a href=&quot;http://dev.w3.org/csswg/css-shapes/&quot;&gt;CSS shapes&lt;/a&gt; we can flow our content in non-rectangular areas. And sometimes we want to be able to flow our content into multiple custom-shaped areas inside an element. If you've read my &lt;a href=&quot;http://sarasoueidan.com/blog/css-shapes/index.html&quot;&gt;previous article&lt;/a&gt;, you already know that this can be done with CSS Shapes, by using an image with alpha transparency with multiple shapes defined in it, and letting the browser extract the content's float areas from it. As appealing as this may sound and as creative as we can get with our shapes, flowing the text into multiple areas can easily make our content almost completely unreadable.
&lt;/p&gt;

&lt;p&gt;&lt;small&gt;
    This article assumes that you’re already familiar with the basics of CSS Shapes and &lt;a href=&quot;http://dev.w3.org/csswg/css-regions/&quot;&gt;CSS Regions&lt;/a&gt;. I’ve recently written &lt;a href=&quot;http://sarasoueidan.com/blog/css-shapes/index.html&quot;&gt;an in-depth comprehensive article about creating non-rectangular layouts with CSS shapes&lt;/a&gt;, which is perfect for getting you up and running with CSS shapes.
&lt;/small&gt;&lt;/p&gt;
&lt;div class=&quot;note warning&quot;&gt;
  Notes: 
  &lt;ul&gt;
    &lt;li&gt;
      This article's demo uses the
      &lt;code&gt;shape-inside&lt;/code&gt; property, which will be temporarily &lt;a href=&quot;https://bugs.webkit.org/show_bug.cgi?id=130698&quot;&gt;removed from Webkit&lt;/a&gt; and &lt;a href=&quot;https://codereview.chromium.org/209443007/&quot;&gt;Blink&lt;/a&gt;. So, for the time being, this article will only show screenshots of how the demo works when &lt;code&gt;shape-inside&lt;/code&gt; is implemented again.
    &lt;/li&gt;
    &lt;li&gt;
      CSS Regions have also been &lt;a href=&quot;http://www.cnet.com/news/reversing-course-google-rejects-adobe-web-publishing-tech/&quot;&gt;dropped from Blink&lt;/a&gt;, so they currently don't work in Chrome.
    &lt;/li&gt;
    &lt;li&gt;
      Check the &lt;a href=&quot;http://caniuse.com/#feat=css-shapes&quot;&gt;current state of browser support for CSS Shapes&lt;/a&gt; out.
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;

&lt;p&gt;CSS shapes can introduce a serious accessibility problem when not used wisely. It’s great to achieve all kinds of creative layouts with custom shapes, but the first and most important thing to keep in mind is that the content is there to be read, so a designer must think accessibility and readability first, then appealing layout second.&lt;/p&gt;

&lt;p&gt;To explain this better, let’s get into an example of when CSS shapes can cause a really bad reading experience. Although, to be fair, it’s not CSS shapes that does that, it’s the decision the designer makes, but you know what I mean.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;the-problem&quot;&gt;The Problem: Multiple CSS Shapes Making Text Unreadable&lt;/h2&gt;

&lt;p&gt;A few days ago I &lt;a href=&quot;https://twitter.com/SaraSoueidan/status/407909116862943232&quot;&gt;tweeted&lt;/a&gt; about a &lt;a href=&quot;http://www.behance.net/gallery/Magazine-Feature-Magazine/9812813&quot;&gt;“fragmented” magazine layout&lt;/a&gt; I made using CSS Shapes and &lt;a href=&quot;http://www.w3.org/TR/css-masking/&quot;&gt;CSS Masks&lt;/a&gt;. The layout took literally less than 2 minutes to create after the mask images were ready (I made those using Photoshop). But after finishing the layout, I realized that, even though it looked quite interesting and creative, it was anything but readable.&lt;/p&gt;

&lt;p&gt;Here’s how the layout looked:&lt;/p&gt;

&lt;figure&gt;
   &lt;img src=&quot;https://sarasoueidan.com/images/old-demo.png&quot; alt=&quot;Fragmented Magazine Layout Demo Screenshot&quot; /&gt;
   &lt;figcaption&gt;Fragmented Magazine Layout created with CSS Shapes and Masks&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;The left part of the layout, where the text is flowing in 3 custom-shaped areas, is where the problem occurred.&lt;/p&gt;

&lt;p&gt;As with content masking, you can imagine your content as one layer and the mask as another; the content will be “painted” only where the mask is opaque, assuming that we’re working with an alpha mask which has only fully black and fully transparent areas. In this case, the fully black areas determine the areas where the content will be visible. And in terms of CSS shapes, the black areas determine the flow areas where our text will flow.&lt;/p&gt;

&lt;p&gt;This is the mask I used to create the 3 flow areas for the text:&lt;/p&gt;

&lt;figure&gt;
  &lt;img src=&quot;https://sarasoueidan.com/images/fragments-text-mask.png&quot; alt=&quot;Fragmented Magazine Layout Mask Defining Flow Areas&quot; /&gt;
  &lt;figcaption&gt;The alpha channel mask used to define the flow areas for the text container&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Now the problem occurs from the way the browser’s layout engine works to fill these areas with text. When the browser flows the text into the shapes it moves down an element line by line, and starts writing the text in the text’s flow areas, which, when you’re using a mask image, are the areas of the element covered by the black areas of the mask. For regular rectangular elements, i.e. those that don’t have any CSS shape defined on them, that is perfectly fine: just move down the element one line after another, and type the text on those lines. But when an element’s flow area changes, things can easily get messy.&lt;/p&gt;

&lt;p&gt;The browser starts with the first line on an element, and prints the text on that line &lt;em&gt;&lt;strong&gt;only where the line passes through a defined flow area (one of the black areas)&lt;/strong&gt;&lt;/em&gt;. So, what happens is that words will end up “scattered” on every line, and the lines divided into several “fragments”  depending on the number of flow areas passing through the line. A reader’s eye will have to “jump” from area to area to read the fragments that make up a line.&lt;/p&gt;

&lt;figure&gt;
  &lt;img src=&quot;https://sarasoueidan.com/images/fragments-text-mask-shapes-flow.png&quot; alt=&quot;Content flow into defined flow areas with CSS Shapes&quot; /&gt;
  &lt;figcaption&gt;The horizontal &quot;fragmented&quot; flow of text inside multiple flow areas defined by CSS shapes&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;As you can see in the image above, if you start reading a line, &lt;strong&gt;your eyes have to jump from area to area for every line&lt;/strong&gt;, which, after a few of lines, becomes impossible to do, at least for me.&lt;/p&gt;

&lt;p&gt;When I first started experimenting with CSS shapes I expected the text to flow in each of these custom-shaped areas the same way it does in CSS regions: I expected the browser to treat CSS Shapes’ flow areas the same way it treats CSS regions, but it does not. What we would normally want to do, is have the text flow in the individual areas, moving to one area after another one has been filled with content.&lt;/p&gt;

&lt;p&gt;One way to achieve this (filling areas one after another) is by using CSS regions to create any number of flow areas we want, and then giving each region a custom shape inside it using the &lt;code&gt;shape-inside&lt;/code&gt; property. So, for the above mask for example, which has 3 shapes inside it which define 3 flow areas, we will define each of these areas as a region, and then give each region a custom shape so that text flows inside it the way we want it to. This way, the browser will fill the first area (first region) with text, and then when it’s full, it will move into the second region after the first one’s been filled, and so on. This way, the text can be easily read inside each region (as long as you’re not using some crazy shape that will make text unreadable in all cases), as your eyes won’t have to jump between regions for every line.&lt;/p&gt;

&lt;figure&gt;
  &lt;img src=&quot;https://sarasoueidan.com/images/fragments-text-mask-regions-flow.png&quot; alt=&quot;Content flow into flow areas defined with CSS Regions and shapes&quot; /&gt;
  &lt;figcaption&gt;Using CSS regions to define the three areas, text will fill each region before flowing into the next one, making reading a lot easier&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Now let’s change the above demo I made by introducing CSS regions into it and see how that affects the readability of the layout.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;the-solution&quot;&gt;The Solution: Introducing CSS Regions Into The Layout&lt;/h2&gt;

&lt;p&gt;What we’re going to do, is create three regions inside our text container, and then give each region a shape using &lt;code&gt;shape-inside&lt;/code&gt; so that the text flows inside it in a non-rectangular manner.&lt;/p&gt;

&lt;p&gt;The right side of the demo, which is a “fragmented” image, is created using CSS masks. A mask is applied to the element, which will “erase” parts of the image where the mask is transparent. We’ll get to this part at the end of this section.&lt;/p&gt;

&lt;p&gt;First, we’re going to go over the markup for the two “pages”. The page with a class name &lt;code&gt;.text&lt;/code&gt; is the left page with the text inside it. Inside this page, we’re going to define 3 regions, and a &lt;code&gt;.content&lt;/code&gt; container which will contain the text that we want to flow into these regions.&lt;/p&gt;

&lt;p&gt;So, the 3 regions are initially empty, and via CSS, we’re going to fill them with the text contained in the &lt;code&gt;.content&lt;/code&gt; element.&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;div class=&quot;magazine&quot;&amp;gt;
    &amp;lt;div class=&quot;page text&quot;&amp;gt;
        &amp;lt;div class=&quot;content&quot;&amp;gt;
            Good design should be used solely as information that acknowledges its very difficult to leave room for the consumer with promises that part of design is only lousy design emphasises the intellectual side .... 
            &amp;lt;!--...--&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div class=&quot;region region-1&quot;&amp;gt;&amp;lt;/div&amp;gt;
        &amp;lt;div class=&quot;region region-2&quot;&amp;gt;&amp;lt;/div&amp;gt;
        &amp;lt;div class=&quot;region region-3&quot;&amp;gt;&amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
	&amp;lt;div class=&quot;page photo&quot;&amp;gt;
	    &amp;lt;img src=&quot;images/bg.jpg&quot; alt=&quot;Photo of a Sea&quot;&amp;gt;
	&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/pre&gt;

&lt;p&gt;Now that we have our markup ready, we’ll start by defining some general styles before we get into the relevant CSS. We’ll be giving all the elements fixed dimensions; both CSS shapes and CSS regions require an element to have fixed dimensions to work.&lt;/p&gt;

&lt;pre class=&quot;brush:css&quot;&gt;
.magazine {
    height: 700px;
    width: 1300px;
    font-size: .8em;
    margin: 1em auto;
    background-color: #fff;
    font-family: &quot;Nunito&quot;, sans-serif;
}
.page {
    width: 710px;
    height: 700px;
    float: left;
    overflow: hidden; 
    font-size: .9em;
}
.photo {
    float: right;
    width:590px;
}
.photo img{
    width: 100%;
    height: 100%;
    /* .. */
}
.text {
    /* We're justifying the text so that it fills the entire line so that the shapes are more &quot;defined&quot; by their content */
    text-align: justify;
    padding: 1em;
}
.region {
    /* must specify dimensions on regions otherwise they won't be filled with text */
    width: 220px;
    height: 670px;
    float: left;
    padding: .5em;
}
&lt;/pre&gt;

&lt;p&gt;With these styles set, we can now flow our text content into our regions using CSS’s &lt;code&gt;flow-from&lt;/code&gt; and &lt;code&gt;flow-into&lt;/code&gt; properties. I’ll be using the &lt;code&gt;-webkit-&lt;/code&gt; prefix as the demo will only work in webkit browsers at this time.&lt;/p&gt;

&lt;pre class=&quot;brush:css&quot;&gt;
.content {
    -webkit-flow-into: pocket;
}
.region {
    -webkit-flow-from: pocket;
}
&lt;/pre&gt;

&lt;p&gt;That’s all you need to flow the text into the regions. The demo would look like the following screenshot. I’ve colored the regions’ background so that they are more visible. And for the time being, you can ignore the right side of the page with the fragmented image effect. Like I mentioned earlier, we’ll get to that by the end of this section.&lt;/p&gt;

&lt;figure&gt;
  &lt;img src=&quot;https://sarasoueidan.com/images/fragmented-magazine-regions-show.jpg&quot; alt=&quot;Screenshot of Demo with Regions and Mask applied&quot; /&gt;
  &lt;figcaption&gt;Screenshot of the demo when text flows inside the regions before applying the shapes to them&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;The text flows from one region to another starting from the left column to the right, which is exactly what we set out to achieve. Now what we want to do next is apply the same non-rectangular shapes to our 3 columns (regions) as those we saw in the mask used above, so that the end result looks like the demo we want to improve.&lt;/p&gt;

&lt;p&gt;Because we want to change the flow of text &lt;em&gt;inside&lt;/em&gt; the regions, we’re going to use CSS’s &lt;code&gt;shape-inside&lt;/code&gt; property to change the shape of the flow area inside the regions. There are two ways we could define our shapes: using an image with an alpha channel like we did in the initial demo before we introduced regions, or by creating these shapes using the &lt;code&gt;polygon()&lt;/code&gt; shape function.&lt;/p&gt;

&lt;p&gt;Because each region will have only one flow area inside it which is a simple polygonal shape, we’re going to use the &lt;code&gt;polygon()&lt;/code&gt; function instead of creating 3 images for the 3 regions (the image previously used in the demo to define the 3 flow areas can be divided into 3 masks using Photoshop, each mask containing one of the shapes inside the image).&lt;/p&gt;

&lt;pre class=&quot;brush:css&quot;&gt;
.region-1 {
    shape-inside: polygon(80px 0px, 150px 0, 190px 200px, 130px 400px, 200px 550px, 200px 670px, 30px 670px, 0px 100px); 
}
.region-2 {
    shape-inside: polygon(150px 0px, 210px 170px, 150px 300px, 220px 670px, 50px 670px, 5px 430px, 40px 270px, 0px 60px); 
}
.region-3 {
    shape-inside: polygon(190px 0px, 210px 350px, 150px 610px, 140px 670px, 0px 370px, 40px 160px, 10px 40px);
}
&lt;/pre&gt;

&lt;p&gt;And now the text flows inside our regions in non-rectangular shapes:&lt;/p&gt;

&lt;figure&gt;
   &lt;img src=&quot;https://sarasoueidan.com/images/fragmented-magazine-regions-shaped.jpg&quot; alt=&quot;Screenshot of Demo with Shapes applied to Regions&quot; /&gt;
   &lt;figcaption&gt;Screenshot of the demo when shapes are applied to the regions&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;With this result, it’s a lot easier to read the text than it was in the intial demo. Your eyes can move down a column and then move to the next once it’s done with the first.&lt;/p&gt;

&lt;p&gt;Nonetheless, it’s absolutely necessary that a designer always design shapes that don’t strain the eyes. There’s probably a good reason why rectangular reading areas are the most comfortable to read in, and when designers decide to think outside the box (pun intended), it’s important to remember not to compromise readability and sacrifice a good user experience for beautiful comps.&lt;/p&gt;

&lt;p&gt;With that said, this is how the demo looks:&lt;/p&gt;

&lt;figure&gt;
   &lt;img src=&quot;https://sarasoueidan.com/images/css-regions-with-shapes-for-readability-header.png&quot; alt=&quot;Screenshot of the final demo&quot; /&gt;
   &lt;figcaption&gt;Screenshot of the final demo&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;And this is how CSS regions can help create a fairly better reading experience when used with CSS shapes. It would be great if browsers filled the CSS shapes the same way it filled CSS regions, but because it’s not a simple task to just change the layout algorithm, we can always use CSS regions to get that result.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;regions-with-masks&quot;&gt;Creating The &quot;Fragmented&quot; Photo Effect with CSS Masks&lt;/h2&gt;

&lt;p&gt;Last but not least, we’re going to use CSS masks to mask parts of the image in the &lt;code&gt;.photo&lt;/code&gt; page, to give it that “fragmented” effect. The mask used to create this effect on the image looks like so:&lt;/p&gt;

&lt;figure&gt;
  &lt;img src=&quot;https://sarasoueidan.com/images/fragments-mask.png&quot; alt=&quot;Mask used to create the fragmented effect on the image&quot; /&gt;
  &lt;figcaption&gt;Mask with alpha channel used to create the &quot;fragmented&quot; effect on the image&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;The above mask is an image with an alpha channel, where the opaque areas define where the content will be visible, and the transparent areas are the areas where the content will be “erased”. You can notice that it’s obviously a messy mask :P because that’s what happens when I try to “draw” with a mouse! But then again, it kind of emphasizes that “torn” and “fragmented” effect on the image, which is nice. =)&lt;/p&gt;

&lt;pre class=&quot;brush:css&quot;&gt;
.photo img{
    /* ... */

    /* mask applied to the image. At this time, only webkit browsers support CSS masks with a webkit prefix */
    -webkit-mask-image: url(../images/fragments-mask.png);
}
&lt;/pre&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;final-words&quot;&gt;Final Words&lt;/h2&gt;

&lt;p&gt;CSS Shapes will be one of the best things to happen to design on the web, but as we’ve always learned: with great power comes great repsonsibility. It’s &lt;a href=&quot;http://blogs.adobe.com/webplatform/2013/12/04/css-shapes-in-last-call/&quot;&gt;estimated&lt;/a&gt; that in a year from now many  browsers will have implemented CSS shapes, so it’s great to start experimenting with them from now. The more you experiment the more you can help find bugs to fix them before all browsers implement them.&lt;/p&gt;

&lt;p&gt;And finally, I hope you enjoyed reading this article and found it useful. Thank you for reading.&lt;/p&gt;


    
  </content>
 </entry>
 
 <entry>
   <title>Techniques For Creating Textured Text</title>
   <link href="http://sarasoueidan.com//blog/textured-text-techniques/"/>
   <id>https://sarasoueidan.com/blog/textured-text-techniques</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;
	For too long, we've resorted to graphics editors to create &lt;em&gt;images&lt;/em&gt; of text that has nice effects such as creative fills or that blends with its background in a nice subtle way. We used those images as a replacement for text on our pages, which made that text unaccessible and un-selectable.. But with all the advances in web design today, we can now create textured text effects using CSS, using SVG, and using HTML5 Canvas. This article introduces and shows you how to do that using all of those techniques.
&lt;/p&gt;

    

     <div>
        <p>
          <em>This article is <a href="http://tympanus.net/codrops/2013/12/02/techniques-for-creating-textured-text/">published @ <strong>Codrops</strong>.</a></em>
        </p>
    </div>
    
  </content>
 </entry>
 
 <entry>
   <title>Techniques For Responsive Typography</title>
   <link href="http://sarasoueidan.com//blog/responsive-typography-techniques/"/>
   <id>https://sarasoueidan.com/blog/responsive-typography-techniques</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;
	Text is the most important part of a website. Did you know that are several ways to make text responsive? Be it big headlines or body copy, the article will cover all those techniques, serving as an ultimate reference for making text responsive. We will cover accessibility, media queries, viewport units, and much more.
&lt;/p&gt;

    

     <div>
        <p>
          <em>This article is <a href="http://tympanus.net/codrops/2013/11/19/techniques-for-responsive-typography/">published @ <strong>Codrops</strong>.</a></em>
        </p>
    </div>
    
  </content>
 </entry>
 
 <entry>
   <title>CSS Overlay Techniques</title>
   <link href="http://sarasoueidan.com//blog/css-overlay-techniques/"/>
   <id>https://sarasoueidan.com/blog/css-overlay-techniques</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;
	Overlays can sometimes be annoying, but also undoubtedly have their useful use cases. There are different approaches to creating overlays, some of them work better than others, and some of them come with gotchas that you need to be aware of, including performance implications. In HTML5, we also get a native way to create modals with less hassle and less code. In this article, we will cover all of that.
&lt;/p&gt;

    

     <div>
        <p>
          <em>This article is <a href="http://tympanus.net/codrops/2013/11/07/css-overlay-techniques/">published @ <strong>Codrops</strong>.</a></em>
        </p>
    </div>
    
  </content>
 </entry>
 
 <entry>
   <title>Creating Non-Rectangular Layouts With CSS Shapes</title>
   <link href="http://sarasoueidan.com//blog/css-shapes/"/>
   <id>https://sarasoueidan.com/blog/css-shapes</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;Today we can create all kinds of &lt;a href=&quot;http://css-tricks.com/examples/ShapesOfCSS/&quot;&gt;shapes with CSS&lt;/a&gt; using CSS transforms, but all these shapes do not affect the flow of the content inside or around them. That is, if you create a triangle or a trapezoid with CSS, for example, the shape created does not define or affect the way the text inside it flows, or the way inline text around it does.&lt;/p&gt;

&lt;p&gt;With the introduction of CSS Shapes into the web, wrapping content in custom non-rectangular shapes, and recreating print designs and layouts on the web becomes a piece of cake!&lt;/p&gt;

&lt;p&gt;In this article we're going to go over the basics of declaring shapes, and creating some simple layouts using these new CSS technologies. When more CSS Shapes features are implemented, more complex and awesome layouts will be possible, but even with what we have at hand now, &lt;a href=&quot;http://blogs.adobe.com/webplatform/2013/10/23/css-shapes-visual-storytelling/&quot;&gt;some interesting and very creative layouts&lt;/a&gt; can be created with a little extra experimentation.&lt;/p&gt;

&lt;p&gt; &lt;strong&gt;The CSS technologies we’ll be covering in this article may not work in your browser. If you want to see the working live demos you need to make sure you’re viewing them in a browser that supports these technologies. Check the &lt;a href=&quot;http://caniuse.com/#feat=css-shapes&quot;&gt;current state of browser support for CSS Shapes&lt;/a&gt; out. You &lt;em&gt;don't need&lt;/em&gt; a supporting browser to understand the features and demos, though. I've included screenshots of the demos so you can see how the final result looks like&lt;/strong&gt;.&lt;/p&gt;

&lt;p class=&quot;note warning&quot;&gt;
Most of this article's demos use the &lt;code&gt;shape-inside&lt;/code&gt; property, which has been temporarily &lt;a href=&quot;https://bugs.webkit.org/show_bug.cgi?id=130698&quot;&gt;removed from Webkit&lt;/a&gt; and &lt;a href=&quot;https://codereview.chromium.org/209443007/&quot;&gt;Blink&lt;/a&gt;. So, for the time being, this article will only show screenshots of how the demos work when &lt;code&gt;shape-inside&lt;/code&gt; is implemented again.
&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;declaring-shapes&quot;&gt;Declaring Shapes&lt;/h2&gt;

&lt;p&gt;All HTML elements have a rectangular box model which governs the flow of content inside and around it. In order to give an element a custom non-rectangular shape, the &lt;code&gt;shape-inside&lt;/code&gt; and &lt;code&gt;shape-outside&lt;/code&gt; properties are used. At the time of writing of this article, the &lt;code&gt;shape-outside&lt;/code&gt; property can be applied to floating elements only, and the &lt;code&gt;shape-inside&lt;/code&gt; property isn't completely implemented, so you may still find bugs when u use it. The shape-* properties can also only be applied to block-level elements. Non-block-level elements should be forced to block if you want to use a shape property on them.&lt;/p&gt;

&lt;p&gt;Shape-* properties take one of three values: auto, a basic shape, or an image URI. If the value is set to auto, the element’s float area uses the margin box as normal. (If you’re not familiar with the &lt;a href=&quot;http://www.w3.org/TR/2007/WD-css3-box-20070809/&quot;&gt;CSS box model&lt;/a&gt;, make sure you read up on it because you should know how it works). &lt;/p&gt;

&lt;p&gt;If the value is set to a shape function, then the shape is computed based on the values of one of ‘&lt;code&gt;inset&lt;/code&gt;’, ‘&lt;code&gt;circle&lt;/code&gt;’, ‘&lt;code&gt;ellipse&lt;/code&gt;’ or ‘&lt;code&gt;polygon&lt;/code&gt;’. You can learn more about each of these functions in &lt;a href=&quot;http://blogs.adobe.com/webplatform/2014/02/11/new-css-shapes-syntax/&quot;&gt;this article&lt;/a&gt; by the Adobe Platform team.&lt;/p&gt;

&lt;p&gt; And finally, if the value is set to an image URI, the browser will use the image to extract and compute the shape based on the image’s alpha channel. The shape is computed to be the path that encloses the area where the opacity of the specified image is greater than the &lt;code&gt;shape-image-threshold&lt;/code&gt; value. If the &lt;code&gt;shape-image-threshold&lt;/code&gt; is not specified, the initial value to be considered is 0.5. The image should be CORS-same-origin, otherwise, it won't work, and the default value &lt;code&gt;auto&lt;/code&gt; will be the value of the computed shape.&lt;/p&gt;

&lt;p&gt;Shapes defined using the &lt;code&gt;shape-outside&lt;/code&gt; property define the &lt;em&gt;exclusion area&lt;/em&gt; on an element, while those defined using the &lt;code&gt;shape-inside&lt;/code&gt; property define the &lt;em&gt;float area&lt;/em&gt; of an element. We'll learn what each of these means in the examples below.&lt;/p&gt;

&lt;p&gt;The shapes defined by the shape-* properties can be modified by using the &lt;code&gt;shape-margin&lt;/code&gt; and &lt;code&gt;shape-padding&lt;/code&gt; properties. The margin and padding shape properties are self-explanatory.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;establishing-a-coordinate-system&quot;&gt;Establishing a coordinate system on an element&lt;/h2&gt;

&lt;p&gt;For the CSS shape declared to actually be applied on an element, we need to first start with establishing a coordinate system which we’ll be using to draw the shape.&lt;/p&gt;

&lt;p&gt;A coordinate system is necessary because the shapes you declare will be defined by a set of points (and radii if you’re drawing circles or ellipses for example), and these points have x and y coordinates which will be placed on this coordinate system.&lt;/p&gt;

&lt;p&gt;The shape-* properties use the content box of the element they’re applied to for their coordinate system, so in order to make them work, &lt;strong&gt;you need to specify a fixed width and height for the element&lt;/strong&gt; which defines its bounding box, which in turn will be used to establish the coordinate system for the shapes you draw. &lt;strong&gt;If no explicit width and height are specified, the shape-* properties don’t work&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The origin of the coordinate system defined on the element's bounding box is positioned at the top left corner.&lt;/p&gt;

&lt;p&gt;So, to declare a shape an element you have to start with:&lt;/p&gt;

&lt;ol&gt;
    &lt;li&gt;Specifying the dimensions of the element getting the shape (remember: the element should be floated when using &lt;code&gt;shape-outside&lt;/code&gt; on it).&lt;/li&gt; 
    &lt;li&gt;Declaring the shape on that element using the shape-* properties.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;applying-a-custom-shape&quot;&gt;Applying a background to a custom shape&lt;/h2&gt;

&lt;blockquote class=&quot;quotes-left&quot;&gt;
    &lt;p&gt;While the boundaries used for wrapping inline flow content outside a float can be defined using shapes, &lt;strong&gt;the actual box model does not change&lt;/strong&gt;. If the element has specified margins, borders or padding they will be computed and rendered according to the &lt;a href=&quot;http://www.w3.org/TR/css-shapes/#CSS3BOX&quot;&gt;CSS3BOX&lt;/a&gt; module. 
    &lt;cite&gt;&amp;#8212;&lt;a href=&quot;http://www.w3.org/TR/css-shapes/&quot;&gt;W3C CSS Shapes Module Level 1&lt;/a&gt;&lt;/cite&gt;
    &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In other words, the shape you define on an element using shape-* properties &lt;em&gt;only&lt;/em&gt; affects the element’s float area, i.e. the flow of the content inside/outside this element, but all the element’s other properties won’t be affected.&lt;/p&gt;

&lt;p&gt;For example, suppose you only want to draw a circular shape and have content float on its side like the shape in the image below, you’d first have to declare the circular shape on the element (again, remember to float the element and give it a height and width). Then, say you want to apply a background color to the circular shape to look like the one in the image..&lt;/p&gt;

&lt;figure&gt;
    &lt;img src=&quot;https://sarasoueidan.com/images/shape-background.png&quot; alt=&quot;Background applied to CSS shape&quot; /&gt;
    &lt;figcaption&gt;Background applied to a custom declared shape&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;You’d be tempted to just add a background color to the containing element and then end up with the above result (that’s what I did the first time), but doing that won’t do the job. The reason for that is that all properties of the element, other than the flow of content outside it, won’t be affected by the shape you defined inside it, and they will be rendered normally according to the element’s box model (its rectangular shape), as we’ve seen in the spec. So if you apply a background color to it, you’ll end up with this.&lt;/p&gt;

&lt;figure&gt;
    &lt;img src=&quot;https://sarasoueidan.com/images/box-model-background.png&quot; alt=&quot;Background applied to rectangular box model&quot; /&gt;
    &lt;figcaption&gt;Background applied to the element's rectangular box shape&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;So, &lt;em&gt;how can we apply the color to the shape only and not the whole element?&lt;/em&gt; This is where the &lt;code&gt;clip-path&lt;/code&gt; property from &lt;a href=&quot;http://www.w3.org/TR/2014/WD-css-masking-1-20140213/&quot;&gt;the CSS Masking specification&lt;/a&gt; can help.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;clip-path&lt;/code&gt; property will be used to &lt;em&gt;clip&lt;/em&gt; parts of the element that we don’t need and keep only the parts inside the shape we defined. That obviously means that we’re not actually applying the color &lt;em&gt;to&lt;/em&gt; the shape, we’re just &lt;em&gt;trimming&lt;/em&gt; the element and leaving only the shape intact. With this, you’ll end up with a floating circle wrapping text outside it.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;How, exactly? what value does the clip-path property get to do this?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The user coordinate system for the shapes defined by the &lt;code&gt;clip-path&lt;/code&gt; property is established using the bounding box of the element to which the clipping path is applied, so the coordinate system is the same one as that of the shape-* properties.&lt;/p&gt;

&lt;p&gt;Because of this, we can use the same shape defined in the shape-* property for the clip path, which will cut out, or &lt;em&gt;clip&lt;/em&gt;, everything inside the containing element that’s outside the boundaries of the shape, and we’ll end up with a custom shape with a background.&lt;/p&gt;

&lt;p&gt;You can test this concept live in &lt;a href=&quot;http://codepen.io/SaraSoueidan/pen/ad12e1280e4b1c481faa3b82bd9a3263&quot;&gt;this pen&lt;/a&gt;, just make sure you test it in a supporting browser.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;reminder&quot;&gt;Quick Reminder&lt;/h2&gt;

&lt;p&gt;At the time of writing of this article, the &lt;code&gt;shape-outside&lt;/code&gt; property only works on floats, and both &lt;code&gt;shape-outside&lt;/code&gt; and &lt;code&gt;shape-inside&lt;/code&gt; properties are applied only to block-level elements, or inline elements &lt;strong&gt;forced to block&lt;/strong&gt;. A shape defined on a float will cause inline content to wrap around the defined shape instead of the float's bounding box. Future levels of CSS Shapes will allow use of shapes on elements other than floats, and when that happens we’ll be able to wrap content on both sides of a shape (as in the image below). So for now, we can only float an element and have content flow  on either side of it.&lt;/p&gt;

&lt;figure&gt;
    &lt;img src=&quot;http://dev.w3.org/csswg/css-shapes-2/images/shapes_CSS2.1_MBP.png&quot; alt=&quot;Example rendering of circle shape and box model.&quot; /&gt;
    &lt;figcaption&gt;Flowing content on both sides of a CSS shape&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;You could also fake wrapping content on both sides using the &lt;a href=&quot;http://betravis.github.io/shape-tools/exclusion-punch/&quot;&gt;Exclusion Punch plugin&lt;/a&gt; by &lt;a href=&quot; https://twitter.com/bear_travis&quot;&gt;Bear Travis&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now let’s get our hands dirty drawing some shapes and creating some fun layouts!&lt;/p&gt;

&lt;p&gt;Each of the following examples will introduce a new tip/idea/technique that are used to define and use CSS shapes and exclusions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;You can view the live demo for each example by clicking on the demo's screenshot.&lt;/strong&gt;&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;example-1&quot;&gt;Example #1: Floating text around a custom shape with &lt;code&gt;shape-outside&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;We’ll start with a simple example. In this example we’re going to define a custom shape and have content flow on its side. The end result will look like the image below:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://sarasoueidan.com/demos/css-shapes-layouts/demo-1/index.html&quot;&gt;
    &lt;figure&gt;
        &lt;img src=&quot;https://sarasoueidan.com/demos/css-shapes-layouts/demo-1/images/demo-screenshot.jpg&quot; alt=&quot;Screenshot of Demo #1&quot; /&gt;
        &amp;lt;figcaption&amp;gt;Screenshot of Demo #1. Click on the screenshot to see the working demo.&amp;lt;/figcaption&amp;gt;
    &lt;/figure&gt;
&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the demo we have a container which contains two elements: a &lt;code&gt;.content&lt;/code&gt; container with text on the left, and another element with a class&lt;code&gt;.shaped&lt;/code&gt; floated to the right, which will get the custom shape and have the text flow on its left side.&lt;/p&gt;

&lt;p&gt;The heading in the &lt;code&gt;.content&lt;/code&gt; area is also getting a similar treatment to the one we're giving the floated div on the right, so I'll skip its explanation and only talk about what we're doing on the &lt;code&gt;.shaped&lt;/code&gt; area on the right.&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;div class=&quot;container&quot;&amp;gt;
    &amp;lt;div class=&quot;shaped&quot;&amp;gt;&amp;lt;/div&amp;gt;
    &amp;lt;div class=&quot;content&quot;&amp;gt;
         &amp;lt;h1&amp;gt;&amp;lt;span&amp;gt;La&amp;lt;/span&amp;gt; Tour &amp;lt;br/&amp;gt;Eiffel&amp;lt;/h1&amp;gt;
         &amp;lt;p&amp;gt;Lorem Ipsum......&amp;lt;/p&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/pre&gt;

&lt;p&gt;We will first start by giving the floated &lt;code&gt;div&lt;/code&gt; on the right a specific height and width to establish a coordinate system. We’ll set its height to be the same as its container, which for this demo I’ve set to be the same height as the viewport, using CSS’s &lt;code&gt;vh&lt;/code&gt; unit.&lt;/p&gt;

&lt;pre class=&quot;brush:css&quot;&gt;
.container{
    overflow:hidden;
    height: 100vh;
    width: 100vw;
}
.shaped{
    float:left;
    height:100vh;
    width:40vw;
    float:right;
    background: black url(../images/eiffel.jpg) center top no-repeat;
    background-size:cover;
}
&lt;/pre&gt;

&lt;p&gt;Now that the coordinate system is ready, we’re going to draw the shape, to define the float and exclusion areas of the element. There are two ways to go about declaring a shape for this demo:&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;using-polygon&quot;&gt;Using &lt;code&gt;polygon()&lt;/code&gt;&lt;/h3&gt;

&lt;p&gt;For the first method, we’ll be using the polygon() function. This function takes in a set of points that form the polygon, each point defined by x and y coordinates. We're going to define a very simple polygonal shape, with 4 vertices, as shown in the image below (blue and orange discs):&lt;/p&gt;

&lt;figure&gt;
    &lt;img src=&quot;https://sarasoueidan.com/demos/css-shapes-layouts/demo-1/images/demo-shape.jpg&quot; alt=&quot;Vertices of the Polygon&quot; /&gt;
    &lt;figcaption&gt;Screenshot showing the vertices making up the polygonal shape&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;The coordinates of the points can have either specific values (px or em), or percentage values. In this example we're going to provide percentage values for the vertices visible in the above screenshot. Now all we have to do is just declare this shape on the floated element so that the text flows on its side.&lt;/p&gt;

&lt;pre class=&quot;brush:css&quot;&gt;
.shaped{
    /*...*/
    shape-outside: polygon(0 0, 100% 0, 100% 100%, 30% 100%);
    shape-margin: 20px;
}
&lt;/pre&gt;

&lt;p&gt;And that’s it! the text can now flow in the &lt;em&gt;float area&lt;/em&gt; of the element, defined by the custom shape we declared on it.&lt;/p&gt;

&lt;p&gt;You can also see that I've added a margin to the shape, to push the content away from the shape a little and create a gap.&lt;/p&gt;

&lt;p&gt;But we have one more thing to add here. Like I mentioned in a previous section, the background of the floated element is applied to its original rectangular shape, not just to the shape we declared on it, because the background property is not affected by the shape declared on the element. So far, the demo looks like this:&lt;/p&gt;

&lt;figure&gt;
    &lt;img src=&quot;https://sarasoueidan.com/demos/css-shapes-layouts/demo-1/images/demo-screenshot-incomplete.jpg&quot; alt=&quot;Screenshot of background applied to rectangular shape of the element&quot; /&gt;
    &lt;figcaption&gt;Screenshot showing the background applied to the element covering its rectangular shape&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;So in order to clip out the excess areas that we don't need, we're going to use the &lt;code&gt;clip-path&lt;/code&gt; property, and give it the same value/shape that we gave to the &lt;code&gt;shape-outside&lt;/code&gt; property above. So we add this rule to the rule set:&lt;/p&gt;

&lt;pre class=&quot;brush:css&quot;&gt;
.shaped{
    /*...*/
    clip-path: polygon(0 0, 100% 0, 100% 100%, 30% 100%);
}
&lt;/pre&gt;

&lt;p&gt;And we're done! Simple, right? &lt;/p&gt;

&lt;p&gt;The page title on the left gets the same treatment as the &lt;code&gt;.floated&lt;/code&gt; &lt;code&gt;div&lt;/code&gt; on the right. The heading is floated inside its container &lt;code&gt;.content&lt;/code&gt;, it is given a specific height and width to establish a coordinate system, and then a shape is declared on it using the &lt;code&gt;shape-outside&lt;/code&gt; property just like we did on the &lt;code&gt;.floated&lt;/code&gt; element.&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;using-an-image-uri&quot;&gt;Using an image URI&lt;/h3&gt;

&lt;p&gt;Another way we could define the shape on our element is by using an image with an alpha channel, that is, any image with transparent areas.&lt;/p&gt;

&lt;p&gt;For our example here, instead of using the polygon() function to define the shape, we’ll give the &lt;code&gt;shape-outside&lt;/code&gt; property an image URI, and the browser will extract the shape from the image, and use it.&lt;/p&gt;

&lt;p&gt;The image that would define the exclusion area for this example is the one shown below. You can see that the image shows the same shape defined by the polygon() points in the previous method.&lt;/p&gt;

&lt;figure&gt;
    &lt;img src=&quot;https://sarasoueidan.com/demos/css-shapes-layouts/demo-1/images/mask.png&quot; alt=&quot;Image with Alpha Channel showing the polygonal shape&quot; /&gt;
    &lt;figcaption&gt;Image with Alpha Channel whose URI will be used to extract and compute the value of the shape&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;When you’re using an image with alpha channels to define a shape &lt;strong&gt;for the shape-outside property&lt;/strong&gt;, the &lt;em&gt;transparent area&lt;/em&gt; of the image will define the area where the inline text flows, this is the area called the &lt;em&gt;float area&lt;/em&gt; of the element. The black portion defines the exclusion area of the element.&lt;/p&gt;

&lt;p&gt;To use this image we write the following:&lt;/p&gt;

&lt;pre class=&quot;brush:css&quot;&gt;
.shaped{
    /*...*/
    shape-outside: url(../images/mm.png);
    shape-image-threshold: 0.5;/* this property is used to set the threshold used for extracting a shape from an image. 0.0 = fully transparent and 1.0 = fully opaque */
}
&lt;/pre&gt;

&lt;p&gt;Each of the two methods mentioned has its advantages. You might want to use an image URI for complex shapes that may be cumbersome to define the points for manually, in this case creating an alpha channel image in Photoshop would be much easier and faster than manually adding the points.&lt;/p&gt;

&lt;p&gt;Another situation where you might want to use an image URI instead of a shape function is when you have multiple float or exclusion areas inside an element, in that case using this method is necessary because you can’t, for now, declare multiple shapes on an element, but if the image contains multiple areas, the browser will extract these areas from the image and use them. Pretty neat, right? :) we’ll see an example of this in the last demo.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;example-2&quot;&gt;Exmaple #2: wrapping/flowing text inside a custom shape with &lt;code&gt;shape-inside&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;For the second example we’ll create a simple demo where the end result will look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://sarasoueidan.com/demos/css-shapes-layouts/demo-2/index.html&quot; class=&quot;image-link&quot;&gt;
    &lt;figure&gt;
        &lt;img src=&quot;https://sarasoueidan.com/demos/css-shapes-layouts/demo-2/images/demo-screenshot.png&quot; alt=&quot;Screenshot of demo #2&quot; /&gt;
        &amp;lt;figcaption&amp;gt;Screenshot of demo #2&amp;lt;/figcaption&amp;gt;
    &lt;/figure&gt;
&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The goal of this example is to demonstrate the &lt;code&gt;shape-inside&lt;/code&gt; property used to float text inside a non-rectangular shape. We have a container element with some placeholder text inside it, and we applied the photo as a background image to this container.&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;div class=&quot;container&quot;&amp;gt;            
    &amp;lt;div class=&quot;content&quot;&amp;gt;
        &amp;lt;p&amp;gt;...&amp;lt;/p&amp;gt;
    &amp;lt;/div&amp;gt;
    &amp;lt;h2&amp;gt;Corn Bread&amp;lt;/h2&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/pre&gt;

&lt;p&gt;As you can see from the demo screenshot above, the text is wrapped inside a circular shape at the top. So, we know that we’re going to have to declare a circle on our container. Now, like in the previous example, there are two ways we can do that..&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;using-circle&quot;&gt;Using &lt;code&gt;circle()&lt;/code&gt;&lt;/h3&gt;

&lt;p&gt;Using the &lt;code&gt;circle()&lt;/code&gt; function we're going to define a circle and position it on our element.&lt;/p&gt;

&lt;p&gt;The image below shows the coordinate system established on the element, and the position of the circle inside the element. We’re making sure the circle is positioned on top of the pan image inside the photo we’re using as a background, so that it appears as if the text is contained inside that pan. On the image the position of the center of the circle with respect to the coordinate system established on the element is also visible.&lt;/p&gt;

&lt;figure&gt;
    &lt;img src=&quot;https://sarasoueidan.com/demos/css-shapes-layouts/demo-2/images/demo-shape.png&quot; alt=&quot;Coordinate system and shape defined on the container&quot; /&gt;
    &lt;figcaption&gt;Coordinate system and shape defined on the container&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Because we want to wrap text &lt;em&gt;inside&lt;/em&gt; a custom shape, and not flow it around it, we’re going to use the &lt;code&gt;shape-inside&lt;/code&gt; property on the element containing this text. When you're applying the &lt;code&gt; shape-inside&lt;/code&gt; property to an element, you have to remember that this element would have the text content inside it, unlike the previous example, where the content was outside the element we declared the shape on.&lt;/p&gt;

&lt;p&gt; We’ll specify the coordinates of the center of the circle and we'll set the value of its radius, and apply those to the container:&lt;/p&gt;

&lt;pre class=&quot;brush:css&quot;&gt;
.container{
    float:left;
    width:600px;
    height:900px;
    overflow:hidden;
    margin:0 50px;
    color:white;
    font-size:13px;
    padding:10px;
    background: url(../images/pan.jpg) top left no-repeat;
    background-size:100% 100%;
    /*declare shape using the shape function circle()*/
    shape-inside: circle(160px at 400px 60px);
}
&lt;/pre&gt;

&lt;p&gt;Of course, unless you're attempting to create a perfect circular shape, you can also define the shape using &lt;code&gt;polygon()&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;Using an image URI&lt;/h4&gt;

&lt;p&gt;We can also use the URI of an image with an alpha channel to extract the shape of the circle from it. The image would look like the following: &lt;/p&gt;

&lt;figure&gt;
    &lt;img src=&quot;https://sarasoueidan.com/demos/css-shapes-layouts/demo-2/images/mask.png&quot; alt=&quot;Image with Alpha Channel showing the circular shape
              &quot; /&gt;
    &lt;figcaption&gt;Image with Alpha Channel defining the circular shape&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt; It’s important to note here that when you’re using an image with an alpha channel to define a shape &lt;strong&gt;for the &lt;code&gt;shape-inside&lt;/code&gt; property&lt;/strong&gt;, the &lt;em&gt;black (or opaque) area&lt;/em&gt; of the image will define the area where the text flows. In the previous example, the opaque area defined the &lt;em&gt;exclusion area&lt;/em&gt; of the element we applied the shape to, i.e the area where &lt;strong&gt;no&lt;/strong&gt; text flows.&lt;/p&gt;

&lt;p&gt;So declare the shape using an image URI instead of the shape function &lt;code&gt;circle()&lt;/code&gt;, you'll have to set the value of the &lt;code&gt;shape-inside&lt;/code&gt; property to point to the URI of the image:&lt;/p&gt;

&lt;pre class=&quot;brush:css&quot;&gt;
.container{
    /*...*/
     shape-inside: url(mask.png) top left;
}
&lt;/pre&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;example-3&quot;&gt;Example #3 : wrapping/flowing text inside a custom shape with &lt;code&gt;shape-inside&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;In this example we're also going to declare a polygonal shape on a container and have its content flow inside this shape. The end result will look like the image below:&lt;/p&gt;

&lt;!-- &lt;a href=&quot;https://sarasoueidan.com/demos/css-shapes-layouts/demo-3/index.html&quot; class=&quot;image-link&quot;&gt; --&gt;
&lt;figure&gt;
    &lt;img src=&quot;https://sarasoueidan.com/demos/css-shapes-layouts/demo-3/images/demo-screenshot.png&quot; alt=&quot;Screenshot of Demo #3&quot; /&gt;
    &lt;figcaption&gt;Screenshot of Demo #3&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;!-- &lt;/a&gt; --&gt;

&lt;p&gt;Here, too, we can use either a shape function or an image URI to declare the shape on the element.&lt;/p&gt;

&lt;p&gt;The shape declared on this container is clearly a &quot;random&quot; polygonal shape, not a geometric shape that we could declare using a shape function like &lt;code&gt;circle()&lt;/code&gt;, &lt;code&gt;ellipse()&lt;/code&gt;, or &lt;code&gt;inset()&lt;/code&gt;, so we're going to use the &lt;code&gt;polygon()&lt;/code&gt; function to declare it.&lt;/p&gt;

&lt;p&gt;The shape defined by a set of points is visible in the image below.&lt;/p&gt;

&lt;figure&gt;
    &lt;img src=&quot;https://sarasoueidan.com/demos/css-shapes-layouts/demo-3/images/demo-shape.png&quot; alt=&quot;The polygonal shape defined by a set of points&quot; /&gt;
    &lt;figcaption&gt;The polygonal shape defined by a set of points&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Because there's a fairly large number of points making this shape up, it would be cumbersome to calculate the coordinates of these points, so it would be helpful if there was a &lt;strong&gt; visual&lt;/strong&gt; tool available to help us &lt;em&gt;plot&lt;/em&gt; these points on the image, right? Well, there is a tool created by Adobe's &lt;a href=&quot; https://twitter.com/bear_travis&quot;&gt;Bear Travis&lt;/a&gt;, which is actually a collection of tools that can help you when working with CSS shapes. Make sure you &lt;a href=&quot;http://betravis.github.io/shape-tools&quot;&gt; check the Shape Tools out&lt;/a&gt; because they are very valuable.&lt;/p&gt;

&lt;p&gt;One of the Shape tools mentioned is called &lt;a href=&quot;http://betravis.github.io/shape-tools/polygon-drawing/&quot;&gt;Poly Draw&lt;/a&gt;, and it allows you to manually &quot;draw&quot; a shape, a polygon in particular, and then it generates the coordinates of the shape for you to copy and paste into your CSS to declare the shape on your element.&lt;/p&gt;

&lt;p&gt;I have used the Poly Draw tool to draw the above shape on the image. Now, the tool does not take an image and sets it as a background for the element you define the shape on, so I had to git clone the repo of the tool and fiddle with the tool’s code a bit in the dev tools, and I applied the image to it and plotted the points on it.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://twitter.com/razvancaliman&quot;&gt;Razvan Caliman&lt;/a&gt; suggested this idea when I asked him about the availability of a tool that allows us to define shapes on top of images right in the browser, just like the one he showed and used in &lt;a href=&quot;https://www.youtube.com/watch?v=zsLwZhTSuQk&amp;amp;list=PL8rji95IPUUDu3puqqxWMKFXf-NQ4z7WE&amp;amp;index=11&quot;&gt;his talk at this year's CSS Conf EU&lt;/a&gt;. If you haven't watched his talk yet, make sure you do. The tool he used will some day, soon I hope, be open-sourced by Adobe, and then it'll be an indispensible tool when working with CSS shapes. But until then, you could do with the Poly Draw tool.&lt;/p&gt;

&lt;p&gt;After drawing the shape with the Poly Draw tool, all you have to do is declare the resulting shape on your element and you're good to go.&lt;/p&gt;

&lt;pre class=&quot;brush:css&quot;&gt;
.container{
    width:445px;
    height:670px;
    overflow:hidden;
    margin:30px auto;
    /*shape generated by the Poly Draw tool*/
    shape-outside: polygon(170.67px 291.00px,126.23px 347.56px,139.79px 417.11px,208.92px 466.22px,302.50px 482.97px,343.67px 474.47px,446.33px 452.00px,443.63px 246.82px,389.92px 245.63px,336.50px 235.26px,299.67px 196.53px,259.33px 209.53px,217.00px 254.76px);
}
&lt;/pre&gt;

&lt;p&gt;We could also define the shape above using an image with an alpha channel. The image below shows what that image would look like. Again, the black areas define the float area when using &lt;code&gt;shape-inside&lt;/code&gt;, and they're where the text is going to flow.&lt;/p&gt;

&lt;figure&gt;
    &lt;img src=&quot;https://sarasoueidan.com/demos/css-shapes-layouts/demo-3/images/mask.png&quot; alt=&quot;Image with alpha channel defining the shape for demo #2&quot; /&gt;
     &lt;figcaption&gt;Image with alpha channel defining the shape for demo #2&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;If you want to go with the image URI instead of the shape function, youd replace the above shape outside value with the following:&lt;/p&gt;

&lt;pre class=&quot;brush:css&quot;&gt;
.container{
    /*...*/
    shape-inside: url(mask.png) top left;
}
&lt;/pre&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;example-4&quot;&gt;Example #4 : Multiple float areas with &lt;code&gt;shape-inside&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;In this example we're going to create multiple float areas inside an element to wrap content inside. The result of this demo is shown in the following image:&lt;/p&gt;

&lt;!-- &lt;a href=&quot;https://sarasoueidan.com/demos/css-shapes-layouts/demo-4/index.html&quot; class=&quot;image-link&quot;&gt; --&gt;
&lt;figure&gt;
    &lt;img src=&quot;https://sarasoueidan.com/demos/css-shapes-layouts/demo-4/images/demo-screenshot.jpg&quot; alt=&quot;Screenshot of Demo #3&quot; /&gt;
    &lt;figcaption&gt;Screenshot of Demo #4&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;!-- &lt;/a&gt; --&gt;

&lt;p&gt;We have a &lt;code&gt;div&lt;/code&gt; with a background image, and we want the text inside this &lt;code&gt;div&lt;/code&gt; to flow inside specific areas inside it, all of which have custom shapes.&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
&amp;lt;div class=&quot;container&quot;&amp;gt;
    &amp;lt;div class=&quot;content&quot;&amp;gt;
        &amp;lt;h2&amp;gt;Rosemary Sandwich&amp;lt;/h2&amp;gt;
        &amp;lt;p&amp;gt;...&amp;lt;/p&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/pre&gt;

&lt;p&gt;Now, since we can't declare multiple shapes on an element, we're going to use an image with an alpha channel. An image can contain as many shapes and areas as you want, so it's perfect to define multiple shapes on an element, and the browser will extract all the shapes from this image and use them on the element.&lt;/p&gt;

&lt;p&gt;We'll use the following image to define the shapes. The black areas in the image will define the float area of for the content inside the &lt;code&gt;.container&lt;/code&gt; where the text will flow.&lt;/p&gt;

&lt;figure&gt;
    &lt;img src=&quot;https://sarasoueidan.com/demos/css-shapes-layouts/demo-4/images/mask.png&quot; alt=&quot;Image with Alpha Channel defining shapes for demo #3&quot; /&gt;
    &lt;figcaption&gt;Image with Alpha Channel defining shapes for demo #3&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;We'll use the URI of this image as a value for the &lt;code&gt;shape-inside&lt;/code&gt; property that we're going to declare on the &lt;code&gt;.container&lt;/code&gt;, all the while remembering to set height and width values for the &lt;code&gt;div&lt;/code&gt;:&lt;/p&gt;

&lt;pre class=&quot;brush:css&quot;&gt;
.container{
    width:556px;
    height:835px;
    overflow:hidden;
    margin:0 50px;
    color:white;
    position:relative;
    background: url(../images/bread.jpg) top left no-repeat;
    background-size: 100% 100%;
    shape-inside: url(mask.png) top left;
    font-size:13px;
}
&lt;/pre&gt;

&lt;p&gt;And we're done. The browser does the rest of the work for us by extracting the shapes from the image we gave it, and our text flows nicely inside those areas!&lt;/p&gt;

&lt;p&gt;Using an image to define the shapes is the logical way to go when you have separate areas that are not connected to eachother, i.e that don't form a singe polygonal shape. For this demo, we could have used the &lt;code&gt;polygon()&lt;/code&gt; function to define the shape, by defining a polygon that looks like the one in the image below:&lt;/p&gt;

&lt;figure&gt;
    &lt;img src=&quot;https://sarasoueidan.com/demos/css-shapes-layouts/demo-4/images/demo-shape.png&quot; alt=&quot;Image of shape defined using polygon()&quot; /&gt;
    &lt;figcaption&gt;Image representing the points used to define a single polygon&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;But, as you can notice, this isn't the best way to do this, I just added this to show the difference between using an image and defining the shape with &lt;code&gt;polygon()&lt;/code&gt;, and to show that sometimes the best practice or the one that seems more proper and makes more sense is to use an image, even if you can use a shape function to define your shapes.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;shapes-with-regions-and-flexbox&quot;&gt;Combining CSS Shapes with Regions and Flexbox to create magazine layouts&lt;/h2&gt;

&lt;p&gt;Typical print magazines usually combine multi-column text layouts with non-rectangular shapes to create creative and appealing designs. The columns are usually equal in height unless needed otherwise.&lt;/p&gt;

&lt;p&gt;Once future CSS Shapes features are implemented, and wrapping content on both sides of a shape is possible, creating print-like digital magazine designs becomes very much possible when combining Shapes and Exclusions with Regions and Flexbox.&lt;/p&gt;

&lt;figure&gt;
    &lt;img src=&quot;https://sarasoueidan.com/images/multicolumn-shapes.png&quot; alt=&quot;Multi-column layouts with shapes&quot; /&gt;
    &lt;figcaption&gt; Travel Magazine by Bartosz Kwiecień on Behance. Layout like this could be replicated using future CSS Shapes technologies and Regions (&lt;a href=&quot;http://www.behance.net/gallery/Travel-Magazine/2159303&quot;&gt;&lt;/a&gt;) &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Flexbox provides us with the equal-height columns, Regions allows us to flow text into different areas on the page and separate the page content from its layout, and Shapes and Exclusions will allow us to add that final creative touch that takes our magazine layouts to the next level.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;final-words&quot;&gt;Final Words&lt;/h2&gt;

&lt;p&gt;I don’t think I’ve been excited about a new CSS feature as I am about CSS shapes and exclusions. The power, flexibility, and creativity that these features combined regions and flexbox can provide is just fantastic!&lt;/p&gt;

&lt;p&gt;Widespread support for CSS Shapes should be coming soon, as the web platform team at Adobe is constantly working on improving and implementing these features, and providing tools to make working with them easier.&lt;/p&gt;

&lt;p&gt;The future of web layout is looking brighter and more captivating every day. It's a wonderful time to be a web developer!&lt;/p&gt;

&lt;p&gt;I hope this article helped introduce you more to the technical part of getting started with CSS Shapes. This will not be my last article on this topic. Combining CSS Shapes with other cutting edge CSS technologies like Regions opens the door to a new world of creativity, and lots of new tutorials! ;)&lt;/p&gt;

&lt;p&gt;You should subscribe to my blog's &lt;a href=&quot;http://feeds.feedburner.com/sarasoueidan&quot;&gt;RSS feed&lt;/a&gt; and &lt;a href=&quot;http://twitter.com/SaraSoueidan&quot;&gt;follow me on Twitter&lt;/a&gt; to stay in the loop for upcoming new articles.&lt;/p&gt;

&lt;p&gt;Thank you for reading!&lt;/p&gt;

&lt;h3&gt;Resources &amp;amp; Further Learning&lt;/h3&gt;

&lt;ul class=&quot;resources&quot;&gt;
    &lt;li&gt;Bear Travis’s &lt;a href=&quot;http://betravis.github.io/shape-tools/&quot;&gt;CSS Shape tools&lt;/a&gt; &lt;/li&gt;
    &lt;li&gt;W3C's &lt;a href=&quot;http://www.w3.org/TR/css-shapes/&quot;&gt;CSS Shapes Working Draft&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;CSSWG Wiki on &lt;a href=&quot;http://wiki.csswg.org/ideas/css3-exclusions-use-cases&quot;&gt;CSS Shapes and Exclusions use cases examples&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;Adobe’s &lt;a href=&quot;http://html.adobe.com/webplatform/layout/shapes/browser-support/&quot;&gt;CSS shapes support matrix&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;Adobe Web Platform's &lt;a href=&quot;http://html.adobe.com/webplatform/layout/shapes/&quot;&gt;resources for CSS Layout&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://github.com/betravis/web-layout-lab&quot;&gt;This project&lt;/a&gt; by Bear Travis contains a series of exercises demonstrating new web platform layout features including an combining CSS Flexbox, Grid, Regions, Shapes, and Exclusions.&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;http://galjot.si/css-exclusions&quot;&gt;CSS Exculsions article&lt;/a&gt; by&lt;a href=&quot;http://galjot.si/&quot;&gt; Robert Sedovše&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;This article wouldn’t have been possible without the great help from &lt;a href=&quot;http://razvancaliman.com/&quot;&gt;Razvan Caliman&lt;/a&gt;, so a big thanks goes to him.&lt;/em&gt;&lt;/p&gt;


    
  </content>
 </entry>
 
 <entry>
   <title>Navicon Transformicons: Animated Navigation Icons with CSS Transforms</title>
   <link href="http://sarasoueidan.com//blog/navicon-transformicons/"/>
   <id>https://sarasoueidan.com/blog/navicon-transformicons</id>
   <content type="html">
    &lt;p class=&quot;size-2x note&quot;&gt;The following is a collaboration post between &lt;a href=&quot;http://bennettfeely.com/&quot;&gt;Bennett Feely&lt;/a&gt; and I. After seeing Bennett's impressive animated navigation icon transformations (or &quot;Navicon Transformicons&quot;) &lt;a href=&quot;http://codepen.io/bennettfeely/pen/twbyA&quot;&gt;pens&lt;/a&gt; &lt;a href=&quot;http://codepen.io/bennettfeely/pen/eClzu&quot;&gt;on Codepen&lt;/a&gt;, I asked him if he would like to write a tutorial on how he did them as a guest post on my blog. He kindly approved. And as he doesn't have a lot of free time to work the article, we decided to collaborate on it. We'll be covering a few of the icons he created in his pen, and a couple more.&lt;/p&gt;

&lt;p&gt;If you were to ask me what my most favorite CSS property is I might just answer the transition property. It has proven to be a perfect use case for progressive enhancement and it’s adoption has made countless websites feel smoother.  By the way, &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_animated_properties&quot;&gt;a heck of a lot of properties&lt;/a&gt; are also transitionable. &lt;/p&gt;

&lt;p&gt;While the prefixed transition property has been supported by the major browsers for a long time now (web speaking), there was quite a dilemma with browsers and their ability to transition and animate pseudo-elements (&lt;code&gt;:before&lt;/code&gt; and &lt;code&gt;:after&lt;/code&gt;). While Firefox has been doing things right since version 4.0, it wasn’t until March of this year when Chrome finally &lt;a href=&quot;http://css-tricks.com/pseudo-element-animationstransitions-bug-fixed-in-webkit/&quot;&gt;fixed things&lt;/a&gt;. Now, even IE10 supports transitions and animations on pseudo-elements.&lt;/p&gt;

&lt;p&gt;So what shall we call these transforming CSS icons? How about transformicons?&lt;/p&gt;

&lt;p class=&quot;note warning&quot;&gt;
              These code snippets are intended to work only in &lt;a href=&quot;http://caniuse.com/#feat=transforms2d&quot;&gt;browsers that support&lt;/a&gt; the properties used.
            &lt;/p&gt;

&lt;p class=&quot;note&quot;&gt;
              We will only cover the styles/transforms in SCSS, and add some explanation in the form of comments for those of you who aren't very familiar with SCSS. You'll find the complete compiled CSS code in the source code on Github.&lt;br /&gt; As the Javascript is very simple (just toggling a class name)  we won't be going over it, and you'll also find it in the downloadable source code on Github.
            &lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;demo-1&quot;&gt;Three-line to arrow (arrow left and arrow up)&lt;/h2&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/lines-to-arrows.png&quot; alt=&quot;Three-lines to Arrows Transformations&quot; /&gt;
&lt;/figure&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;demo-1-markup&quot;&gt;The Markup&lt;/h3&gt;

&lt;p&gt;The three-line menu icon, aka navicon, aka hamburger icon can be accomplished quite a few different ways, but in this case we will use a wrapper element and a child with two psuedo elements to form the three lines. The markup really isn’t heavy.&lt;/p&gt;

&lt;pre class=&quot;brush:html;&quot;&gt;
              &amp;lt;button class=&quot;lines-button arrow arrow-left&quot; type=&quot;button&quot; role=&quot;button&quot; aria-label=&quot;Toggle Navigation&quot;&amp;gt;
                &amp;lt;span class=&quot;lines&quot;&amp;gt;&amp;lt;/span&amp;gt;
              &amp;lt;/button&amp;gt;
            &lt;/pre&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;demo-1-scss&quot;&gt;The SCSS&lt;/h3&gt;

&lt;p&gt;First we’ll set up the wrapper around the actual navicon to trigger the transition. &lt;code&gt;$button-size&lt;/code&gt; is the width of the lines of the navicon, not the entire target area.&lt;/p&gt;

&lt;pre class=&quot;brush:scss;&quot;&gt;
              $button-size : 3.5rem; 
              $transition: .3s; // increase this to see the transformations in slow-motion

              .lines-button {
                display: inline-block;
                padding: $button-size/2 $button-size/4;
                transition: .3s;
                cursor: pointer;
                user-select: none;
                border-radius: $button-size/7;
              
                &amp;amp;:hover {
                  opacity: 1;
                }
              
                &amp;amp;:active {
                  transition: 0;
                  background: rgba(0,0,0,.1);
                }
              }
            &lt;/pre&gt;

&lt;p&gt;And now a mixin that we will use to make a single line.&lt;/p&gt;

&lt;pre class=&quot;brush:scss&quot;&gt;
              @mixin line {
                display: inline-block;
                width: $button-size;
                height: $button-size/7;
                background: $color;
                border-radius: $button-size/14; 
                transition: $transition;
              }
            &lt;/pre&gt;

&lt;p&gt;We are using the mixin in the &lt;code&gt;.lines&lt;/code&gt; element and its absolutely positioned pseudo elements to create the navicon. &lt;/p&gt;

&lt;pre class=&quot;brush:scss&quot;&gt;
              .lines {
                
                //create middle line
                @include line;
                position: relative; 

                /*create the upper and lower lines as pseudo-elements of the middle line*/
                &amp;amp;:before, &amp;amp;:after {
                
                 @include line;
                  position: absolute;
                    left:0;
                  content: '';
                  transform-origin: $button-size/14 center;
                }
                &amp;amp;:before { top: $button-size/4; }
                &amp;amp;:after { top: -$button-size/4; }
              }
            &lt;/pre&gt;

&lt;p&gt;We need to line up the transform origin of the pseudo elements (upper and lower lines) carefully if we want everything to line up perfectly.&lt;/p&gt;

&lt;p&gt;I created a simple pen to show where the transform origin goes and how the pseudo-elements are transformed.&lt;code&gt;:before&lt;/code&gt; is red, &lt;code&gt;:after&lt;/code&gt; is blue, and &lt;code&gt;.lines&lt;/code&gt; is green.&lt;/p&gt;

&lt;p&gt;Check the pen out &lt;a href=&quot;http://codepen.io/bennettfeely/pen/mhwDt&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;And here's a simple image to show the transform origins and how the pseudo-elements should align.&lt;/p&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/transform-origin.png&quot; alt=&quot;Image showing the transform origins of the pseudo-elements&quot; /&gt;
&lt;/figure&gt;

&lt;p&gt;When we hover over the three-line menu button in it’s original state, we’ll have it expand a little.&lt;/p&gt;

&lt;pre class=&quot;brush:scss&quot;&gt;
              .lines-button:hover {
                opacity: 1;
                  
                .lines {
                  &amp;amp;:before { top: $button-size/3.5; }
                  &amp;amp;:after { top: -$button-size/3.5; }
                }
              }
            &lt;/pre&gt;

&lt;p&gt;Finally, let’s transform this three-line menu into a left arrow icon. For this demo, when the &lt;code&gt;.lines-button&lt;/code&gt; wrapper clicked, we will add a &lt;code&gt;.close&lt;/code&gt; class to it. The arrow looks better when it is scaled down a bit so we will do so using &lt;code&gt;scale3d()&lt;/code&gt; rather than just &lt;code&gt;scale()&lt;/code&gt;, which will trigger hardware acceleration and should make things run a bit smoother.&lt;/p&gt;

&lt;pre class=&quot;brush:scss&quot;&gt;
              .lines-button.arrow.close {
                transform: scale3d(.8,.8,.8);
              }

              .lines-button.arrow.close .lines{
                  &amp;amp;:before,
                  &amp;amp;:after {
                    top: 0;
                    width: $button-size/1.8;
                  }
                
                  &amp;amp;:before { transform: rotate3d(0,0,1,40deg); }
                  &amp;amp;:after { transform: rotate3d(0,0,1,-40deg); }
              }
            &lt;/pre&gt;

&lt;p&gt;For the &lt;code&gt;:before&lt;/code&gt; and &lt;code&gt;:after&lt;/code&gt; lines, we will shorten them a bit and overlay them all on top of each other. Finally, we rotate them 40° in opposite directions to each other. We have made an arrow!&lt;/p&gt;

&lt;p&gt;For the &lt;strong&gt;second navicon transformation into an arrow pointing upwards&lt;/strong&gt;, the markup remains the same, we’ll just add a class of &lt;code&gt;.arrow-up&lt;/code&gt; to the button. &lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
              &amp;lt;button class=&quot;lines-button arrow arrow-up&quot; type=&quot;button&quot; role=&quot;button&quot; aria-label=&quot;Toggle Navigation&quot;&amp;gt;
                &amp;lt;span class=&quot;lines&quot;&amp;gt;&amp;lt;/span&amp;gt;
              &amp;lt;/button&amp;gt;
            &lt;/pre&gt;

&lt;p&gt;This icon will get the exact same styles and transformations as the previous one, but we'll rotate the icon in it's &lt;code&gt;.close&lt;/code&gt; state by 90 degrees so the arrow points upwards.&lt;/p&gt;

&lt;pre class=&quot;brush:scss&quot;&gt;
              .lines-button.arrow-up.close {
                transform: scale3d(.8,.8,.8) rotate3d(0,0,1,90deg); // Rotate around the z-axis
              }
            &lt;/pre&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;demo-2&quot;&gt;Three-line to &amp;#8212;&lt;/h2&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/lines-to-minus.png&quot; alt=&quot;Three-lines to Minus Transformation&quot; /&gt;
&lt;/figure&gt;

&lt;p&gt;The markup for this one is, of course, the same as the markup in the previous section. The button in this example gets a &lt;code&gt;.minus&lt;/code&gt; class, which defines the styles that will be applied to it.&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;demo-2-scss&quot;&gt;The SCSS&lt;/h3&gt;

&lt;p&gt;To style this icon we’ll apply the same styles as above too down until the hover state. But where this icon will differ from the previous one is in the styles applied to it when it’s clicked, i.e in the &lt;code&gt;.close&lt;/code&gt; state. This icon will transform into a “&amp;#8212;” (like a minus sign), which can resemble a “collapse menu” icon, or “show less”, if you’re using it for a mobile navigation.  The pseudo-elements (top and bottom lines) won’t be rotated so we’ll reset the transforms to none, and we’ll keep the width of the icon instead of shrinking it, and then we'll just overlay them on top of the &lt;code&gt;.lines&lt;/code&gt; element to form one single line instead of three.&lt;/p&gt;

&lt;pre class=&quot;brush:scss&quot;&gt;
              .lines-button.minus.close .lines{
                  &amp;amp;:before, &amp;amp;:after{
                    transform: none;
                    width: $button-size;
                    top:0;
                  }
              } 
            &lt;/pre&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;demo-3&quot;&gt;Three-line to &amp;#10005; (#1)&lt;/h2&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/lines-to-x.png&quot; alt=&quot;Three-lines to x Transformation&quot; /&gt;
&lt;/figure&gt;

&lt;p&gt;This icon will start out the exact same way the previous ones have. The markup structure is the same as the previous three-lines icons, with the same hover state expanding effect. For this transformation, the icon will get a &lt;code&gt;.x&lt;/code&gt; class (resembles a transformation to an x shape).&lt;/p&gt;

&lt;p&gt;When the button is clicked, an &lt;code&gt;.close&lt;/code&gt; class is added to it just like in the previous examples. But this is where the new transformation will be defined.&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;demo-3-scss&quot;&gt;The SCSS&lt;/h3&gt;

&lt;p&gt;In order to transform the three lines into an &amp;#10005; shape, we're going to change the icon's background into a transparent one (the middle line will disappear), and the upper and lower lines (the pseudo-elements) will be rotated by 45 degrees in opposite directions and overlayed to create the shape.&lt;/p&gt;

&lt;pre class=&quot;brush:scss&quot;&gt;
              .lines-button.x.close .lines{

                  /*hide the middle line*/
                  background: transparent;

                  /*overlay the lines by setting both their top values to 0*/
                  &amp;amp;:before, &amp;amp;:after{
                    transform-origin: 50% 50%;
                    top:0;
                    width: $button-size;
                  }

                  // rotate the lines to form the x shape
                  &amp;amp;:before{
                    transform: rotate3d(0,0,1,45deg); 
                  }
                  &amp;amp;:after{
                    transform: rotate3d(0,0,1,-45deg); 
                  }
              }
            &lt;/pre&gt;

&lt;p&gt;This transformation is very similar to the arrow transformation, but the key notes which make it different is keeping the width of the lines here instead of shrinking them like we did for the arrows, and keeping the transform origin at the center.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;demo-4&quot;&gt;Three-line to &amp;#10005; (#2)&lt;/h2&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/lines-to-x.png&quot; alt=&quot;Three-lines to x Transformation&quot; /&gt;
&lt;/figure&gt;

&lt;p&gt;This transformation is inspired by the fifth transformation style from Pedro Campos’s &lt;a href=&quot;http://codepen.io/pedrocampos/details/gufrx&quot;&gt;pen&lt;/a&gt; on Codepen.  We’ll make the markup for this one, of course, the same as the markup for all our buttons, with a specific class, in this case .x2.&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;demo-4-scss&quot;&gt;The SCSS&lt;/h3&gt;

&lt;p&gt;This icon will start out with the same transformation as the three-line-to-minus icon, and when the first transformation is finished, the pseudo-elements will rotate and form the &amp;#10005; shape. We’ll apply the second transformation when the first one is finished, so for that we’ll need to set a delay for the transitions so that they don’t happen simultaneously.&lt;/p&gt;

&lt;p&gt;Where this transformation differs from the previous &amp;#10005; effect is the order of transformations and the added delays. For the previous effect we rotated and overlayed simultaneously, while in this case we're going to overlay, and delay the rotation till the overlaying is done, and then we'll rotate.&lt;/p&gt;

&lt;pre class=&quot;brush:scss&quot;&gt;
              .lines-button.x2 .lines{
                  transition: background .3s .6s ease;

                  &amp;amp;:before, &amp;amp;:after{
                    //set transform origin back to center
                    transform-origin: 50% 50%;
                    transition: top .3s .6s ease, transform .3s ease;
                  }
              }
            &lt;/pre&gt;

&lt;p&gt;We have added a delay on the transition for the lines so that the transformations happen in a row.&lt;/p&gt;

&lt;p&gt;Next, we’ll define the transition delays and transformations for the pseudo-elements. When the button is clicked, the upper and lower lines will first be translated to overlay on top of each other, the middle line’s background will be set to transparent to hide it, because we don’t want it to be there when the x is formed, and then each of two remaining lines  will be rotated by 45deg (and -45deg for the opposite line) to form an &amp;#10005; shape.&lt;/p&gt;

&lt;pre class=&quot;brush:scss&quot;&gt;
              .lines-button.x2.close .lines{

                  transition: background .3s 0s ease;
                  background: transparent;

                  &amp;amp;:before, &amp;amp;:after{
                    transition: top .3s ease, transform .3s .5s ease;      
                    top:0;
                    width: $button-size;
                  }
                  &amp;amp;:before{
                    transform: rotate3d(0,0,1,45deg); 
                  }
                  &amp;amp;:after{
                    transform: rotate3d(0,0,1,-45deg); 
                  }
              }
            &lt;/pre&gt;

&lt;p&gt;The trick here that’s different from the previous transformations is just to set the transform origin of the pseudo-elements to be their center, and add the proper transition delays.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;demo-5&quot;&gt;Grid to &amp;#10006; (#1)&lt;/h2&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/grid-to-x.png&quot; alt=&quot;Grid to x Transformation&quot; /&gt;
&lt;/figure&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;demo-5-markup&quot;&gt;The Markup&lt;/h3&gt;

&lt;p&gt;Similar to the previous markup, we have a &lt;code&gt;.grid-button&lt;/code&gt; wrapping a &lt;code&gt;.grid&lt;/code&gt; icon.&lt;/p&gt;

&lt;pre class=&quot;brush:html&quot;&gt;
              &amp;lt;button class=&quot;grid-button rearrange&quot; type=&quot;button&quot; role=&quot;button&quot; aria-label=&quot;Toggle Navigation&quot; &amp;gt;
                &amp;lt;span class=&quot;grid&quot;&amp;gt;&amp;lt;/span&amp;gt;
              &amp;lt;/button&amp;gt;
            &lt;/pre&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;demo-5-scss&quot;&gt;The SCSS&lt;/h3&gt;

&lt;p&gt;For this icon, instead of using psuedo elements we will instead leverage the power of the mighty &lt;code&gt;box-shadow&lt;/code&gt; property. To make the code cleaner and easier to modify, we will create a &lt;code&gt;$base&lt;/code&gt; and a &lt;code&gt;$space&lt;/code&gt; variables. First we will style the &lt;code&gt;.grid-button&lt;/code&gt;, wrapper.&lt;/p&gt;

&lt;pre class=&quot;brush:scss&quot;&gt;
              //variables are used to make the buttons more flexible and easier to customize
              //these variables are replaced with their values in the compiled CSS

              $base : 1rem;
              $space : $base/4;
              $color : #c0392b;

              .grid-button {
                padding: $base*2; //2rem
                cursor: pointer;
                user-select: none;
              }

            &lt;/pre&gt;

&lt;p&gt;Now let’s get to the &lt;code&gt;.grid&lt;/code&gt; icon itself and the crazy &lt;code&gt;box-shadow&lt;/code&gt; property. Think of each comma-separated shadow as a it’s own sort of pseudo- element. It is very important to keep track of the order of each shadow in the box-shadow property or the animation will not look right.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;box-shadow&lt;/code&gt; property is nice that when a color is not specified, the property simply inherits whatever the color property may be. In a situation like ours, it’s very helpful with an element with a ton of shadows that are the same color to simply leave out the colors and set it once with the color property.&lt;/p&gt;

&lt;pre class=&quot;brush:scss&quot;&gt;
              .grid-button .grid{
                  width: $base;
                  height: $base;
                  background: $color;
                  color: $color; /* Not in use when the colors are specified below */
                  transition: $transition;
              }

            &lt;/pre&gt;

&lt;p&gt;When we click on the button, we add the &lt;code&gt;.close&lt;/code&gt; class to &lt;code&gt;.grid-button&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Because we'll be using two techniques to create an &amp;#10006; out of the grid icon, we'll be using two different class names for two two transformations. For the first one we'll use a &lt;code&gt;.rearrange&lt;/code&gt; class name, as we'll be rearranging the box shadows.&lt;/p&gt;

&lt;p&gt;First we’ll spread the box shadows for the icon to form a grid.&lt;/p&gt;

&lt;pre class=&quot;brush:scss&quot;&gt;
              .grid-button.rearrange .grid{
                  box-shadow:
                    -($base+$space) 0 -($base+$space),
                    0 0 -($base+$space),
                    ($base+$space) (-($base + $space)),
                    -($base+$space) 0,
                    $base+$space 0,
                    -($base+$space) ($base + $space),
                    0 ($base+$space),
                    ($base+$space) ($base + $space);
                
              }
              /* The CSS equivalent to the above box-shadow result is:
              
                box-shadow: 
                  -1.25rem -1.25rem, 
                  0 -1.25rem, 
                  1.25rem -1.25rem, 
                  -1.25rem 0, 
                  1.25rem 0, 
                  -1.25rem 1.25rem, 
                  0 1.25rem, 
                  1.25rem 1.25rem;

              */
            &lt;/pre&gt;

&lt;p&gt;And when the icon gets the &lt;code&gt;.close&lt;/code&gt; class on click, we’ll rearrange the shadows.&lt;/p&gt;

&lt;pre class=&quot;brush:scss&quot;&gt;
              .grid-button.rearrange.close .grid{ 
                    box-shadow:
                      0 0 -$base,
                      0 0 -$base*2,
                      $base 0,
                      -$base*2 0,
                      $base*2 0,
                      -$base 0,
                      0 $base*2,
                      0 $base;
                    transform: rotate3d(0,0,1,-45deg)  scale3d(.8,.8,.8);
              }

              /* The CSS equivalent to the box-shadow is:

                    box-shadow: 
                      0 -1rem, 
                      0 -2rem, 
                      1rem 0, 
                      -2rem 0, 
                      2rem 0, 
                      -1rem 0, 
                      0 2rem, 
                      0 1rem;

              */
            &lt;/pre&gt;

&lt;p&gt;We have removed all the spaces between the individual shadows (removed all the &lt;code&gt;$space&lt;/code&gt; variables), and moved the four corner shadows inward and four side shadows outward by rearranging them. Last but not least, we rotate the whole icon by -45° and scale it, all using hardware acceleration to make the animation run smoothly. And with that we've achieved the first effect.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;demo-6&quot;&gt;Grid to &amp;#10006; (#2)&lt;/h2&gt;

&lt;figure&gt;
	&lt;img src=&quot;https://sarasoueidan.com/images/grid-to-x-2.png&quot; alt=&quot;Grid to x Transformation #2&quot; /&gt;
&lt;/figure&gt;

&lt;p&gt;For the second grid to &amp;#10006; transformation, we’ll be doing something very similar to what we did previously, but instead of rearranging the shadows we’re going to “collapse” four of them into the main element and bring the other four closer by removing the spaces and thus forming an &amp;#10006;. We'll give the button with this effect a class &lt;code&gt;.collapse&lt;/code&gt;.&lt;/p&gt;

&lt;pre class=&quot;brush:scss&quot;&gt;
              .grid-button.collapse .grid{
                //the order of box shadows here differs a little from the above one order-wise
                  box-shadow:
                    -($base+$space) 0,
                    -($base+$space) ($base+$space),
                    $base+$space 0,
                    ($base+$space) (-($base+$space)),
                    0 0 -($base+$space),
                    -($base+$space) 0 -($base+$space),
                    0 ($base+$space),
                    ($base+$space) ($base+$space);
              }

              /*The CSS equivalent to the box-shadow specified here is:
                
                  box-shadow: 
                    -1.25rem 0, 
                    -1.25rem 1.25rem, 
                    1.25rem 0, 
                    1.25rem -1.25rem, 
                    0 -1.25rem, 
                    -1.25rem -1.25rem, 
                    0 1.25rem, 
                    1.25rem 1.25rem;

              */
            &lt;/pre&gt;

&lt;p&gt;And when the button is clicked the &lt;code&gt;.close&lt;/code&gt; class is added, and the shadows “collapse”.&lt;/p&gt;

&lt;pre class=&quot;brush:scss&quot;&gt;
              .grid-button.collapse.close .grid{
                  box-shadow:
                  -$base 0,
                  0 0 transparent,
                  $base 0,
                  0 0 transparent,
                  0 0 -$base,
                  0 0 transparent,
                  0 $base,
                  0 0 transparent;
              }

              /*The CSS equivalent to the box-shadow result is:
                
                  box-shadow: 
                    -1rem 0, 
                    0 0 transparent, 
                    1rem 0, 
                    0 0 transparent, 
                    0 -1rem, 
                    0 0 transparent, 
                    0 1rem, 
                    0 0 transparent;

              */
            &lt;/pre&gt;

&lt;p&gt;And we're done! I hope you liked these effects and found the tutorial useful!&lt;/p&gt;


    
  </content>
 </entry>
 
 <entry>
   <title>Draggable Metro App Showcase</title>
   <link href="http://sarasoueidan.com//blog/draggable-metro-app-showcase/"/>
   <id>https://sarasoueidan.com/blog/draggable-metro-app-showcase</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;Today I'd like to share with you an interactive and touch-optimized metro app showcase concept for showcasing a metro (probably a Windows Phone) app screenshot. The screenshot will be draggable and swipable, and you'll have a couple of extra options to view how the app would look like in a mobile phone frame.&lt;/p&gt;

&lt;p class=&quot;note warning&quot;&gt;Please note that this demo works only in browsers that support the Javascript objects and APIs used. I provided a couple of polyfills but the demo will only work in browsers that these polyfills provide fallback for. See the &lt;a href=&quot;#javascript&quot;&gt;Javascript section&lt;/a&gt; for details.&lt;/p&gt;

&lt;p&gt;This demo is inspired by the big number of &lt;a href=&quot;http://dribbble.com/search?page=2&amp;amp;q=windows+phone+app&quot;&gt;dribbble shots&lt;/a&gt; showcasing Windows phone app concepts, so I thought I'd recreate this showcasing concept and add some interactivity to it.&lt;/p&gt;

&lt;p&gt;The &lt;a href=&quot;http://dribbble.com/shots/1006346-Flat-Lumia-920-Mockup-Free-PSD&quot;&gt;Flat Lumia Phone PSD Mockup&lt;/a&gt; used in the demo is by Corey Ginnivan from Dribbble. I provided two colors in the demo resources, a red and a white frame.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;markup&quot;&gt;The Markup&lt;/h2&gt;

&lt;p&gt;We'll wrap our showcase in a &lt;code&gt;as-wrapper&lt;/code&gt; wrapper, which will contain a container for the mobile frame + app screenshot, and a section for the app description which will appear at some point during the interaction (we'll get to that in a moment).&lt;/p&gt;

&lt;p&gt;The mobile frame and the screenshot will be positioned absolutely. The frame needs to be positioned absolutely to overlap the screenshot, and the screenshot will be positioned this way too so that we can change its position via Javascript.&lt;/p&gt;

&lt;p&gt;The phone frame we're using has 3 buttons in its lower section, &lt;strong&gt;what we're going to do is we're actually doing to add 3 buttons &lt;em&gt;on top&lt;/em&gt; of these buttons with a transparent background, so that it seems like these built-in buttons are clickable&lt;/strong&gt;. And then we're also going to add two &lt;stront&gt;navigation arrows to the right of the frame to scroll the screenshot left and right&amp;lt;/strong&amp;gt;.&amp;lt;/p&amp;gt;

&lt;p&gt;&lt;strong&gt;The left-most arrow on the phone frame will scroll the app screen to the left to get it completely inside the boundaries of the phone frame. The windows button will scroll it back out to its initial position. The magnifier will launch the &quot;focus&quot; mode of the showcase, and the left and right navigation arrows will scroll the screenshot left and right respectively&lt;/strong&gt;.&lt;/p&gt;

&lt;pre class=&quot;brush:html;&quot;&gt;
&amp;lt;div class=&quot;as-wrapper&quot;&amp;gt;
    &amp;lt;div class=&quot;as-container&quot;&amp;gt;
        &amp;lt;div class=&quot;as-frame preventSelect&quot; id=&quot;as-frame&quot;&amp;gt;
            &amp;lt;img src=&quot;images/lumia-red.png&quot; alt=&quot;Omnia Phone Frame&quot; /&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div class=&quot;as-instructions&quot; id=&quot;as-instructions&quot;&amp;gt;
             &amp;lt;p&amp;gt;Drag or swipe app screenshot left and right with your mouse or finger.&amp;lt;/p&amp;gt;
            &amp;lt;p&amp;gt;Use buttons at the bottom of the frame to scroll screen and focus mobile frame.&amp;lt;/p&amp;gt;
            &amp;lt;button&amp;gt;Got it!&amp;lt;/button&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div class=&quot;as-nav-buttons&quot; id=&quot;as-nav-buttons&quot;&amp;gt;
            &amp;lt;button class=&quot;as-button as-nav-button as-left-nav-button preventSelect&quot; id=&quot;as-left-nav-button&quot;&amp;gt;&amp;lt;img src=&quot;images/nav-arrow-left.png&quot; alt=&quot;Left&quot;&amp;gt;&amp;lt;/button&amp;gt;
            &amp;lt;button class=&quot;as-button as-nav-button as-right-nav-button preventSelect&quot; id=&quot;as-right-nav-button&quot;&amp;gt;&amp;lt;img src=&quot;images/nav-arrow-right.png&quot; alt=&quot;Right&quot;&amp;gt;&amp;lt;/button&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;button class=&quot;as-button as-slide-button preventSelect&quot; id=&quot;as-slide-button&quot;&amp;gt;&amp;lt;/button&amp;gt;
        &amp;lt;button class=&quot;as-button as-reset-button preventSelect&quot; id=&quot;as-reset-button&quot;&amp;gt;&amp;lt;/button&amp;gt;
        &amp;lt;button class=&quot;as-button as-focus-button preventSelect&quot; id=&quot;as-focus-button&quot;&amp;gt;&amp;lt;/button&amp;gt;

        &amp;lt;div id=&quot;draggable&quot; class=&quot;as-screen preventSelect&quot;&amp;gt;
            &amp;lt;img src=&quot;images/app-screen.jpg&quot;&amp;gt;
        &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
    &amp;lt;div class=&quot;as-app-description preventSelect&quot; id=&quot;as-app-description&quot;&amp;gt;
        &amp;lt;h2&amp;gt;Your awesome app features and upsell&amp;lt;/h2&amp;gt;
                
        &amp;lt;p&amp;gt;Minima vero quibusdam error accusamus explicabo commodi deleniti ipsa debitis enim quae tempore molestias veritatis. Quo saepe voluptatibus officiis debitis necessitatibus magnam id possimus maxime atque amet. Officiis cupiditate deserunt!&amp;lt;/p&amp;gt;
        &amp;lt;p&amp;gt;Minima vero quibusdam error accusamus explicabo commodi deleniti ipsa debitis enim quae tempore molestias veritatis.&amp;lt;/p&amp;gt;
        &amp;lt;p&amp;gt;Minima vero quibusdam error accusamus explicabo commodi deleniti ipsa debitis enim quae tempore molestias veritatis.&amp;lt;/p&amp;gt;
        &amp;lt;a href=&quot;#&quot;&amp;gt;
            &amp;lt;img src=&quot;images/ws-button.png&quot; alt=&quot;Download App from Windows Store&quot; class=&quot;download-button&quot; id=&quot;download-button&quot;/&amp;gt;
        &amp;lt;/a&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/pre&gt;

&lt;p&gt;You have probably noticed the class &lt;code&gt;preventSelect&lt;/code&gt; that I adde to almost all elements, especially those inside the &lt;code&gt;as-container&lt;/code&gt;. What this class does is that it prevents these elements (via CSS) from being selected, otherwise selected elements will get in the way of the drag action and things will get messy!&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;css&quot;&gt;The CSS&lt;/h2&gt; 

&lt;p&gt;I'll go over the styles quickly. All styles are basic and easy to understand so I won't be getting into too much detail. The &quot;heart&quot; of this demo is the Javascript part, the CSS is simple and pretty straightforward. I added comments to the CSS code where necessary. We'll start with the general styles relevant to the demo.&lt;/p&gt;

&lt;pre class=&quot;brush:css;&quot;&gt;
              /* lazy reset :) */
              *{
                /*box sizing should be border box on .as-frame and .as-screen otherwise js calculations will need to change to include padding*/
                -moz-box-sizing: border-box;
                box-sizing: border-box;
                margin: 0;
                padding: 0;
                list-style: none;
              }
              body {
                background: #F0E9DD url(&quot;../images/02.jpg&quot;) repeat;
                color:#eee;
                font: 300 1.2em &quot;Source Sans Pro&quot;, sans-serif;
                overflow-x: hidden;
              }
              /* cross-browser prevent user select: http://stackoverflow.com/a/4358620 */
              .preventSelect {
                -moz-user-select: -moz-none;
                -khtml-user-select: none;
                -webkit-user-select: none;
                -ms-user-select: none;
                user-select: none;
              }
              .as-wrapper{
                width:95%;
                margin:0 auto;
                min-height:550px;
                position:relative;
              }
              .as-container {
                position: relative;
                height: 550px;
                width: 1300px;
                overflow: hidden;
                margin:20px auto;
                transition: width .6s ease;
              }
              /*class will be added via Javascript to shrink the frame + screenshot container and center it*/
              .shrink{
                width:297px;
              }
            &lt;/pre&gt;

&lt;p&gt;So far all styles are obvious and straight forward. The frame and screenshot container is given a height equal to the height of the phone frame (simply because we don't need it to be bigger than that), and now we'll move on to the frame and screenshot styles.&lt;/p&gt;

&lt;pre class=&quot;brush:css;&quot;&gt;
              /*div containing the app screenshot*/
              .as-screen {
                height: 75.6%;
                width: 1190px;
                top: 8.5%;
                left:0;
                position: absolute;
                cursor: move;
                cursor:grab;
                cursor:-moz-grab;
                cursor:-webkit-grab;
                z-index: 1;
                overflow: hidden;
                transition: all .5s ease-out;
              }
               /*app screenshot*/
              .as-screen img{
                pointer-events:none;/*to prevent image being dragged and interfering with the screen drag*/
              }
              /*div containing the phone frame*/
              .as-frame {
                position: absolute;
                z-index: 1;
                left: 0;
                width: 300px;
                height:550px;
                z-index: 2;
                pointer-events:none;
              }
              /*the phone frame*/
              .as-frame img{
                width:100%;
                pointer-events:none;
              }
              .as-instructions{
                position: absolute;
                top:100px;
                left:50px;
                width:200px;
                padding:20px;
                color:white;
                background:rgba(0,0,0,0.75);
                z-index:20;
                pointer-events:none;
              }
              .as-instructions button{
                background: none; 
                border:none;
                background-color: #B33E41;
                color:white;
                padding:5px 10px;
                margin-top:15px;
              }
            &lt;/pre&gt;

&lt;p&gt;Note that we need to set the pointer events on the frame to none to make sure it doesn't block the events on the screenshot.&lt;/p&gt;

&lt;p&gt;Now we'll style and position the control and navigation buttons.&lt;/p&gt;

&lt;pre class=&quot;brush:css;&quot;&gt;
              .as-slide-button, .as-reset-button, .as-focus-button{
                width:40px;
                height:40px;
                position:absolute;
                bottom:48px;  
                left:30px;
                background:none;
                border:none;
                cursor:pointer;
                z-index:20;
              }
              .as-reset-button{
                left:130px;
              }
              .as-focus-button{
                left:225px;
              }
              .as-nav-buttons{
                height:30px;
                position:absolute;
                bottom:30px;
                left:320px;
                z-index:20;
              }
              .as-nav-button{
                width:40px;
                height:40px;
                background:none;
                border:none;
                color:black;
                text-align:center;
                cursor:pointer;
              }
            &lt;/pre&gt;

&lt;p&gt;Last thing we're going to style is the description section which will appear when the screenshot has been dragged fully into the inside of the phone frame.&lt;/p&gt;

&lt;pre class=&quot;brush:css;&quot;&gt;
              /*initially the description will be hidden with opacity set to 0*/
              .as-app-description {
                opacity: 0;
                width:100%;
                position:absolute;
                right:0;
                top:0;
                bottom:0;
                margin: 5% 0;
                padding: 0 50px 0 450px;
                transition: opacity .3s ease-out;
              }
              .as-app-description h2{
                margin-bottom:1em;
              }
              /*this class will be added via Javascript to show the description*/
              .visible-description {
                opacity: 1;
              }
              .download-button {
                margin: 30px 0;
              }
            &lt;/pre&gt;

&lt;p&gt;We'll make the demo as responsive as possible. I'm saying &quot;as responsive as possible&quot; because a draggable showcase like this will look best on big/desktop screens, because of the width of the screenshot, but we'll make it work for all screen sizes. :)&lt;/p&gt;

&lt;pre class=&quot;brush:css;&quot;&gt;
              @media screen and (max-width: 64em){
                .as-app-description{
                  padding-left:380px;
                }
              }
              @media screen and (max-width: 50em){
                .as-wrapper{
                  padding:1%;
                }
                .as-app-description{
                  position:static;
                  margin-top:100px;
                  padding:0;
                  opacity:1;
                }
              }

              @media screen and (max-width: 30em){
                .as-container{
                  width:297px;
                  margin:30px auto;
                }
              }
            &lt;/pre&gt;

&lt;p&gt;On small screens we'll let the screenshot remain inside the phone frame with overflow set to hidden on the container.&lt;/p&gt;

&lt;p&gt;That's pretty much it for the styles. Now let's move on to the interactive part!&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;javascript&quot;&gt;The Javascript&lt;/h2&gt;

&lt;p&gt;OK, first things first: polyfills and plugins. For starters, I won't be using any JS framework, we'll be going vanilla.&lt;/p&gt;

&lt;p&gt;I'll be using the awesome Javascript &lt;a href=&quot;http://html5doctor.com/the-classlist-api/&quot;&gt;classList API&lt;/a&gt;, which is &lt;a href=&quot;http://caniuse.com/classlist&quot;&gt;not fully supported in all browsers&lt;/a&gt;, but it's awesome so I'll be using it anyway, and I'll add &lt;a href=&quot;https://github.com/eligrey/classList.js&quot;&gt;Eli Grey's classList polyfill&lt;/a&gt; which works in IE8, and provides basic classList.add(), classList.remove(), and classList.toggle() support (which is more than enough for this demo) to at least as far back as Android 2.1.&lt;/p&gt;

&lt;p&gt;For browsers that don't support &lt;code&gt;addEventListener&lt;/code&gt;, I'll be using &lt;a href=&quot;https://github.com/jonathantneal/EventListener/&quot;&gt;Jonathan Neal's eventListener polyfill&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Finally, I'll be using &lt;a href=&quot;http://eightmedia.github.io/hammer.js/&quot;&gt;Hammer.js&lt;/a&gt; to add touch swipe support for the draggable screenshot.&lt;/p&gt;

&lt;p&gt;We'll start by caching some variables and initializing others with some basic calculations which we'll need throughout the code.&lt;/p&gt;

&lt;pre class=&quot;brush:js;&quot;&gt;
              (function(){
                  var el = document.getElementById('draggable'),
                    //get screen width and offset..
                      elWidth = parseInt(window.getComputedStyle(el,null)['width']),
                      elLeft = el.offsetLeft,
                    //..use those to calculate right offset
                      elRight = elLeft + elWidth,
                    //do the same for the phone frame
                      frame = document.getElementById('frame'),
                      frameLeft = frame.offsetLeft, 
                      frameWidth = parseInt(window.getComputedStyle(frame,null)['width']),
                      frameRight = frameLeft + frameWidth,
                    //cache app description and control and navigation buttons 
                      desc = document.getElementById('as-app-description'),
                      scrollInButton = document.getElementById('as-slide-button'),
                      resetButton = document.getElementById('as-reset-button'),
                      focusButton = document.getElementById('as-focus-button'),
                      leftNavButton = document.getElementById('as-left-nav-button'),
                      rightNavButton = document.getElementById('as-right-nav-button'),
                    //instruction that appears at beginning of demo
                      tip = document.getElementById('as-instructions'),
                    //cache container
                      container = el.parentNode;
            &lt;/pre&gt;

&lt;p&gt;wow that's a lot! so what exactly are all those needed for?&lt;/p&gt;

&lt;p&gt;First, I cached all DOM elements that we're going to listen for events on so we can attach event handlers to them next. Then, I determined the left and right offsets for each of the draggable screen and the mobile frame, because we'll be needing these for the scrolling and dragging functions. The right offset is calculated by adding the left offset to the width of the element.&lt;/p&gt;

&lt;p&gt;Next, we'll attach event listeners to the control and navigation buttons, and we'll also add the swipe support with Hammer.js.&lt;/p&gt;

&lt;pre class=&quot;brush:js;&quot;&gt;
              //call the scrollScreen function when the screen is swiped left or right
              var scrollLeftOnSwipe = Hammer(el).on(&quot;swipeleft&quot;, function(event) {
                  scrollScreen(220, 'left');
                  hideTip();
              });
              var scrollRightOnSwipe = Hammer(el).on(&quot;swiperight&quot;, function(event) {
                  scrollScreen(220, 'right');
                  hideTip();
              });

              scrollInButton.addEventListener('click', function(){
                  scrollScreen(elWidth, 'left');
              }, false);
              leftNavButton.addEventListener('click', function(){
                  scrollScreen(220, 'left');
              }, false);
              rightNavButton.addEventListener('click', function(){
                  scrollScreen(220, 'right');
              }, false);
              resetButton.addEventListener('click', resetScreen, false);
              focusButton.addEventListener('click', focusFrame, false);
            &lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;scrollScreen(val, dir)&lt;/code&gt; function takes in two arguments: a &lt;code&gt;val&lt;/code&gt; which is the amount (in px) by which we want to scroll the screen, and a &lt;code&gt;dir&lt;/code&gt; which determines the direction in which we want to scroll it.&lt;/p&gt;

&lt;pre class=&quot;brush:js;&quot;&gt;
              function scrollScreen(val, dir){
                hideTip();
                var left = el.offsetLeft;

                if(dir == 'left'){
                    var deltaRight = elRight - frameRight;
                    if(deltaRight &amp;gt;= val){
                        left -= val;
                    }
                    else{
                        left -= deltaRight + 5;
                    } 
                }
                else if(dir == 'right'){
                    var deltaLeft = frameLeft - left;
                    if(deltaLeft &amp;gt;= val){
                        left += val;
                    }
                    else{
                        left += deltaLeft;
                    }
                }

                if(left &amp;lt;= frameLeft &amp;amp;&amp;amp; elRight &amp;gt;= frameRight - 5){
                    el.style.left = left + 'px';
                    elRight = left + elWidth;// in case elRight = frameRight the desc shows
                    showHideDesc();
                }    
            }

            function showHideDesc(){
                if( elRight &amp;lt;= frameRight + 30 &amp;amp;&amp;amp; !focus){
                    desc.classList.add('visible-description');
                }
                else{
                    desc.classList.remove('visible-description');
                }
             }

             function hideTip(){
                tip.style.display= &quot;none&quot;;
            }

             //when the reset button is clicked the screen is returned to its start position
             function resetScreen(e){
                el.style.left = 0;
                elLeft = 0;
                elRight = elWidth;
                showHideDesc();
            }
            &lt;/pre&gt;

&lt;p&gt;The function calculates the difference between the screenshot offsets and that of the frame offsets, and scrolls the screen by the value passed to it as long as the difference is bigger than this value. If it's smaller, it scrolls it by the value of the difference. At the end of the function, another function &lt;code&gt;showHideDesc()&lt;/code&gt; is called, which shows and hides the app description section based on the position of the screenshot with respect to the frame: if the screenshot's right offset = that of the frame's right offset, i.e the screenshot is fully inside the frame, then the description is shown, else, it's hidden.&lt;/p&gt;

&lt;p&gt;When the left arrow button (the one on the phone frame) is clicked, the scroll function is called with a value equals to the width of the screenshot, which basically means: scroll the screen to the max until it's fully inside the frame.&lt;/p&gt;

&lt;p&gt;The focus button (the magnifier) will cause a mode change for the demo. When it is clicked, the container containing the phone frame and the app screenshot will shrink (by adding the &lt;code&gt;.shrink&lt;/code&gt; class to it) to fit the size of the frame, and it's overflow is hidden, and it's centered in the screen, this way the frame will contain the app screenshot and you can drag/swipe left and right to view the app inside of it. &lt;em&gt;(see image below)&lt;/em&gt;&lt;/p&gt;

&lt;figure&gt;
    &lt;img src=&quot;https://sarasoueidan.com/images/new-focus-mode.png&quot; /&gt;
    &lt;figcaption&gt;
        The app showcase in 'focus' mode.
    &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;pre class=&quot;brush:js;&quot;&gt;
              var focus = false;

              function focusFrame(){
                  hideTip();
                  if(focus == false){
                      container.classList.add('shrink');
                      focus = true;
                      //show/hide description based on whether we're in the 'focus' state or not
                      desc.classList.remove('visible-description');
                  }
                  else{
                      focus = false;
                      container.classList.remove('shrink');
                      el.style.left = '0';
                      elRight = elWidth;//so that the description remains hidden
                  }
              }
            &lt;/pre&gt;

&lt;p&gt;The last thing we're going to do is add the drag functionality to the app screen. We'll be attaching event handlers for &lt;code&gt;mousedown&lt;/code&gt;, &lt;code&gt;mousemove&lt;/code&gt;, and &lt;code&gt;mouseup&lt;/code&gt; events, and their equivalent &lt;code&gt;touchstart&lt;/code&gt;, &lt;code&gt;touchmove&lt;/code&gt;, and &lt;code&gt;touchend&lt;/code&gt; events to support touch devices.&lt;/p&gt;

&lt;p&gt;What will happen is that every time the mouse is down (i.e the drag starts), the position of the mouse/finger is saved, and the current left offset of the screen is calculated, and a value &lt;code&gt;delta&lt;/code&gt; is also calculated, which determines the difference between the mouse position on drag start and the left offset of the draggable element (app screen).&lt;/p&gt;

&lt;p&gt;After that, as the mouse moves, its position is updated, and as its position changes so will the left offset of the draggable screen, as long as the boundaries of the screen don't exceed the boundaries of the frame from the left and right respectively: the right offset of the screen should not go below the right offset of the frame, and the left offset of the screen should not go above the left offset of the frame.&lt;/p&gt;

&lt;p&gt;Now that we've cleared up the logic behind the dragging function, here's the code for that function.&lt;/p&gt;

&lt;pre class=&quot;brush:js;&quot;&gt;
              //these values are reset on every mousedown event
              var mouseDownStartPosition, delta, mouseFrameDiff; 

              el.addEventListener(&quot;mousedown&quot;, startDrag, false);
              el.addEventListener(&quot;touchstart&quot;, startDrag, false);

              function startDrag( event ) {
                  hideTip();
                  //prevent contents of the screen from being selected in Opera and IE &amp;lt;= 10 by adding the unselectable attribute
                  el.setAttribute('unselectable', 'on');

                  elLeft = el.offsetLeft,
                  mouseDownStartPosition = event.pageX,
                  delta = mouseDownStartPosition - elLeft;
                  
                  document.addEventListener(&quot;mousemove&quot;, moveEl, true);
                  document.addEventListener(&quot;mouseup&quot;, quitDrag, false);
                  document.addEventListener(&quot;touchmove&quot;, moveEl, true);
                  document.addEventListener(&quot;touchend&quot;, quitDrag, false);
              }

              function moveEl(e){
                var moveX = e.pageX,
                    newPos = moveX - delta;
                    elLeft = newPos;
                    elRight = newPos + elWidth;
                    
                //-5 is a magic number because the phone frame has extra 5 px on the right side with a transparent bg
                //if you're using a different phone frame img u may not need this, but keeping it won't do any harm :)
                if(elRight &amp;gt;= frameRight - 5 &amp;amp;&amp;amp; elLeft &amp;lt;= frameLeft){
                    el.style.left = newPos + 'px';
                    showHideDesc();
                }
             }

             function quitDrag(){
                 document.removeEventListener('mousemove', moveEl, true);
                 el.setAttribute('unselectable', 'off');
             }
            &lt;/pre&gt;

&lt;p&gt;To make sure the screen doesn't keep moving when the dragging stops, we attached an event handler to the &lt;code&gt;mouseup&lt;/code&gt; (and &lt;code&gt;touchend&lt;/code&gt;) event, that will call a function which in turn will remove the corresponding event handlers from the &lt;code&gt;mousedown&lt;/code&gt; and &lt;code&gt;mousemove&lt;/code&gt; events.&lt;/p&gt;

&lt;p&gt;And that's it, I hope you like this showcase and find it useful! :)&lt;/p&gt;




&lt;/stront&gt;&lt;/p&gt;

    
  </content>
 </entry>
 
 <entry>
   <title>Building A Circular Navigation With CSS Transforms</title>
   <link href="http://sarasoueidan.com//blog/circular-navigation/"/>
   <id>https://sarasoueidan.com/blog/circular-navigation</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;
	In this article, we will learn how to apply CSS transforms to fake a &quot;slice&quot; shape, creating a circular navigation using nothing but CSS (and some maths!). The article includes an interactive demo that explains visually and step by step how the technique works and the shapes are created.
&lt;/p&gt;

    

     <div>
        <p>
          <em>This article is <a href="http://tympanus.net/codrops/2013/08/09/building-a-circular-navigation-with-css-transforms/">published @ <strong>Codrops</strong>.</a></em>
        </p>
    </div>
    
  </content>
 </entry>
 
 <entry>
   <title>S Gallery: A Responsive jQuery Gallery Plugin with CSS3 Animations</title>
   <link href="http://sarasoueidan.com//blog/s-gallery/"/>
   <id>https://sarasoueidan.com/blog/s-gallery</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;Today I'm going to share with you a gallery plugin I built (yeah, like the world needs another gallery plugin, right?) after having stumbled upon &lt;a href=&quot;http://store.sony.com/webapp/wcs/stores/servlet/ProductDisplay?catalogId=10551&amp;amp;storeId=10151&amp;amp;langId=-1&amp;amp;productId=8198552921666556433#gallery&quot; title=&quot;Sony's Product Gallery Page&quot;&gt;SONY's products gallery&lt;/a&gt; while I was browsing their website a while ago. &lt;/p&gt;

&lt;p&gt;Their products' image gallery is a simple one, but two things grabbed my attention about the gallery: &lt;/p&gt;

&lt;ol style=&quot;padding-left:50px&quot;&gt;
&lt;li&gt;It's made with Flash when it can totally be created with HTML, CSS3 and Javascript.&lt;/li&gt;
&lt;li&gt;It has a neat feature: exiting the slideshow mode back to the grid view mode, the last image which was active in the slideshow mode &quot;returns back&quot; to its position in the grid view, thus the user knows where they have stopped and what images are left in the gallery that they haven't maybe browsed. This is a neat feature which serves as a brain cue and thus is a nice and positive UX-aware touch.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Not to mention that the gallery is accessible by keyboard and you can navigate through the images via keyboard shortcuts, and enter into fullscreen mode with only the gallery being in fullscreen, therefore removing all distractions so that you can focus only on the products gallery.&lt;/p&gt;

&lt;p class=&quot;note warning&quot;&gt;The plugin uses &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/Guide/DOM/Using_full_screen_mode&quot;&gt;HTML5's FullScreen API&lt;/a&gt;, and relies heavily on CSS3 animations transforms, so it will work only in &lt;a href=&quot;http://caniuse.com/#search=animations&quot;&gt;browsers that support&lt;/a&gt; &lt;a href=&quot;http://caniuse.com/#search=transforms&quot;&gt;these features&lt;/a&gt;.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;markup&quot;&gt;The Markup&lt;/h2&gt;

&lt;p&gt;The markup needed for the plugin is simple: two unordered lists, the first one for the small versions of the images, and the second one for the big versions, wrapped in a container which I'll be giving an id of &lt;code&gt; #gallery-container&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;The small images should be scaled versions of the big images, i.e they should all have the same aspect ratio for best results.&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;One more thing is needed in the markup: the controls bar. The controls are used to (duh) control the slideshow and navigate through the images..&lt;/p&gt;

&lt;pre class=&quot;brush:html;&quot;&gt;
            &amp;lt;div id=&quot;gallery-container&quot;&amp;gt;
              &amp;lt;ul class=&quot;items--small&quot;&amp;gt;
                &amp;lt;li class=&quot;item&quot;&amp;gt;&amp;lt;a href=&quot;#&quot;&amp;gt;&amp;lt;img src=&quot;images/small-1.png&quot; alt=&quot;&quot; /&amp;gt;&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
                &amp;lt;li class=&quot;item&quot;&amp;gt;&amp;lt;a href=&quot;#&quot;&amp;gt;&amp;lt;img src=&quot;images/small-2.png&quot; alt=&quot;&quot; /&amp;gt;&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
                &amp;lt;!--.....--&amp;gt;
              &amp;lt;/ul&amp;gt;
              &amp;lt;ul class=&quot;items--big&quot;&amp;gt;
                &amp;lt;li class=&quot;item--big&quot;&amp;gt;
                  &amp;lt;a href=&quot;#&quot;&amp;gt;
                    &amp;lt;figure&amp;gt;
                      &amp;lt;img src=&quot;images/big-1.jpg&quot; alt=&quot;&quot; /&amp;gt;
                      &amp;lt;figcaption class=&quot;img-caption&quot;&amp;gt;
                        Caption
                      &amp;lt;/figcaption&amp;gt;
                    &amp;lt;/figure&amp;gt;
                    &amp;lt;/a&amp;gt;
                &amp;lt;/li&amp;gt;
                &amp;lt;li class=&quot;item--big&quot;&amp;gt;
                  &amp;lt;a href=&quot;#&quot;&amp;gt;
                    &amp;lt;figure&amp;gt;
                      &amp;lt;img src=&quot;images/big-2.jpg&quot; alt=&quot;&quot; /&amp;gt;
                      &amp;lt;figcaption class=&quot;img-caption&quot;&amp;gt;
                        Caption
                      &amp;lt;/figcaption&amp;gt;
                    &amp;lt;/figure&amp;gt;
                    &amp;lt;/a&amp;gt;
                &amp;lt;/li&amp;gt;
                &amp;lt;!--...--&amp;gt;
              &amp;lt;/ul&amp;gt;
              &amp;lt;div class=&quot;controls&quot;&amp;gt;
                &amp;lt;span class=&quot;control icon-arrow-left&quot; data-direction=&quot;previous&quot;&amp;gt;&amp;lt;/span&amp;gt; 
                &amp;lt;span class=&quot;control icon-arrow-right&quot; data-direction=&quot;next&quot;&amp;gt;&amp;lt;/span&amp;gt; 
                &amp;lt;span class=&quot;grid icon-grid&quot;&amp;gt;&amp;lt;/span&amp;gt;
                &amp;lt;span class=&quot;fs-toggle icon-fullscreen&quot;&amp;gt;&amp;lt;/span&amp;gt;
              &amp;lt;/div&amp;gt;
            &amp;lt;/div&amp;gt;
            &lt;/pre&gt;

&lt;p&gt;The class names are only CSS hooks, so you can change them, but make sure you change them in the stylesheet as well if you do.&lt;/p&gt;

&lt;p&gt;The control buttons use an image sprite for the icons, which will be included among the plugin assets.&lt;/p&gt;

&lt;p&gt;And that's all you need in the markup.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;dependencies&quot;&gt;Dependencies&lt;/h2&gt;

&lt;p&gt;The plugin has two dependencies: the stylesheet for the gallery and jQuery. Also, if you decide to use the same icons for the gallery controls as the ones I'm using, don't forget to include them in your directory as well.&lt;/p&gt;

&lt;p&gt;Link to the stylesheet in the head of your page (or import the stylesheet to your main stylesheet and concatenate them if you use Sass and Compass).&lt;/p&gt;

&lt;pre class=&quot;brush:html;&quot;&gt;
    &amp;lt;link rel=&quot;stylesheet&quot; href=&quot;path-to-stylesheets/styles.css&quot; /&amp;gt;
&lt;/pre&gt;

&lt;p&gt;Link to jQuery from a CDN and the plugin script at the bottom of the page right before the ending &lt;code&gt;body&lt;/code&gt; tag:&lt;/p&gt;

&lt;pre class=&quot;brush:html;&quot;&gt;
    &amp;lt;script src=&quot;//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js&quot;&amp;gt;&amp;lt;/script&amp;gt;
    &amp;lt;script src=&quot;path-to-your-js-scripts/scripts.js&quot;&amp;gt;&amp;lt;/script&amp;gt;
&lt;/pre&gt;

&lt;p&gt;In order to optimize this gallery for touch, I added &lt;a href=&quot;http://eightmedia.github.io/hammer.js/&quot;&gt;hammer.js&lt;/a&gt; into a javascript file called plugins.js, which also includes &lt;a href=&quot;https://github.com/sindresorhus/screenfull.js&quot;&gt;Screenfull.js&lt;/a&gt; by &lt;a href=&quot;https://twitter.com/sindresorhus&quot;&gt;Sindre Sorhus&lt;/a&gt;, which is a &quot;Simple wrapper for cross-browser usage of the JavaScript Fullscreen API&quot;. &lt;/p&gt;

&lt;p&gt;You have an option to add full-screen support to your gallery, which you can specify in the options when you call the plugin by setting the value for the option to &lt;code&gt;true&lt;/code&gt; (more on this next).&lt;/p&gt;

&lt;p&gt;Link to the script before you link to the plugin script: &lt;/p&gt;

&lt;pre class=&quot;brush:html;&quot;&gt;
    &amp;lt;script src=&quot;//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js&quot;&amp;gt;&amp;lt;/script&amp;gt;
    &amp;lt;script src=&quot;path-to-your-js-scripts/plugins.js&quot;&amp;gt;&amp;lt;/script&amp;gt;
    &amp;lt;script src=&quot;path-to-your-js-scripts/scripts.js&quot;&amp;gt;&amp;lt;/script&amp;gt;
&lt;/pre&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;using-the-plugin&quot;&gt;Using the Plugin&lt;/h2&gt;

&lt;p&gt;Calling the plugin is very straightforward. The fullScreenEnabled option is set to &lt;code&gt;false&lt;/code&gt; by default, you can enable it to add full-screen support by setting it to true:&lt;/p&gt;

&lt;pre class=&quot;brush: js;&quot;&gt;
              $(document).ready(function(){
                $('#gallery-container').sGallery({
                  fullScreenEnabled: true //default is false
                });
              });
            &lt;/pre&gt;

&lt;p&gt;And that's it. I hope you like this plugin and find it useful!&lt;/p&gt;


    
  </content>
 </entry>
 
 <entry>
   <title>Creative Add/Remove Effects for List Items with CSS3 Animations</title>
   <link href="http://sarasoueidan.com//blog/creative-list-effects/"/>
   <id>https://sarasoueidan.com/blog/creative-list-effects</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;
	It's not enough to bring animations and visual transitions to an interface, they should serve a purpose and goal, and this goal should be improving the user's experience.
&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Transitions […] provide the grease that smoothes out what happens in the interface.&lt;b&gt; Without transitional effects the user can be left to wonder what just occurred&lt;/b&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In today’s tutorial, we’ll be creating some creative animations and transitions for adding and removing items from a list, inspired by the concept from &lt;a href=&quot;https://medium.com/design-ux/926eb80d64e3&quot;&gt;Pasquale D’Silva’s article on Medium&lt;/a&gt;. &lt;a href=&quot;https://twitter.com/chriscoyier&quot;&gt;Chris Coyier&lt;/a&gt; &lt;a href=&quot;http://css-tricks.com/transitional-interfaces-coded/&quot;&gt;coded the transitions&lt;/a&gt; from Pasquale’s article.&lt;/p&gt;

&lt;p&gt;In this tutorial I’m extending Pasquale’s example, adding more animations and transitional effects, and I’ll also be using a small snippet from Chris’s article to add an extra step in each animation, which “makes room” for the added items before they are actually added.&lt;/p&gt;

&lt;p class=&quot;note&quot;&gt;
	For the sake of brevity in the example code, I am using the un-prefixed CSS properties, but you'll find them in the project's source code on Github. These code snippets are intended to work only in &lt;a href=&quot;http://caniuse.com/#feat=transforms2d&quot;&gt; browsers that support&lt;/a&gt; the properties used.&lt;br /&gt; 
&lt;/p&gt;

&lt;p&gt;So, let’s dig in!&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;markup&quot;&gt;The Markup&lt;/h2&gt;

&lt;p&gt;For demonstration purposes, I’ve built a simple reminders app. The app uses HTML5’s localStorage API to save the items to your browser’s local storage. So, you can actually use it to take in-browser notes if you want, it’s actually why I built it in the first place for my own notes. I’m not going to get into details of how to build this app because it’s not the purpose of this tutotial.&lt;/p&gt;

&lt;p&gt;The markup for the app is just a simple form with a text field and submit button, and an empty unordered list. The items will be added to the list dynamically. There’s also a couple of divs for the notifications which appear after saving or removing an item, and a counter and a button to delete all items at once. So here’s all the markup needed:&lt;/p&gt;

&lt;pre class=&quot;brush:html;&quot;&gt;
&amp;lt;div class=&quot;notification undo-button&quot;&amp;gt;Item Deleted. Undo?&amp;lt;/div&amp;gt;
&amp;lt;div class=&quot;notification save-notification&quot;&amp;gt;Item Saved&amp;lt;/div&amp;gt;
&amp;lt;div class=&quot;reminder-container&quot;&amp;gt;
    &amp;lt;header&amp;gt;
      &amp;lt;h1&amp;gt;mini reminders list&amp;lt;/h1&amp;gt;
    &amp;lt;/header&amp;gt;
	&amp;lt;form id=&quot;input-form&quot;&amp;gt;
        &amp;lt;input type=&quot;text&quot; id=&quot;text&quot; placeholder=&quot;Remind me to..&quot;/&amp;gt;
        &amp;lt;input type=&quot;submit&quot; value=&quot;Add&quot; /&amp;gt;
    &amp;lt;/form&amp;gt;
    &amp;lt;ul class=&quot;reminders&quot;&amp;gt;

    &amp;lt;/ul&amp;gt;
    &amp;lt;footer&amp;gt;
        &amp;lt;span class=&quot;count&quot;&amp;gt;&amp;lt;/span&amp;gt;
        &amp;lt;button class=&quot;clear-all&quot;&amp;gt;Delete All&amp;lt;/button&amp;gt;
    &amp;lt;/footer&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/pre&gt;

&lt;p&gt;You can add, edit, remove items, and restore them, and it’s actually the removing and restoring where the animations come in place the most. Adding the items is pretty simple, and not much of an animation happens there, except a fading in and falling down animation which we’ll get into as we start with the CSS, so let’s do that.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;css&quot;&gt;The CSS&lt;/h2&gt;

&lt;p&gt;New items added via javascript get a class of &lt;code&gt;.new-item&lt;/code&gt;. Items removed get a &lt;code&gt;.removed-item&lt;/code&gt; class, and restored items get a &lt;code&gt;.restored-item&lt;/code&gt; class. Each of these classes fires its own animation. The class names are going to be the same for all demos, and it’s the animation &lt;code&gt;@keyframes&lt;/code&gt; that will be different for each one.&lt;/p&gt;

&lt;p&gt;Let’s start with the first demo.&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;demo-1&quot;&gt;Demo #1&lt;/h3&gt;

&lt;figure&gt;
    &lt;img src=&quot;https://sarasoueidan.com/images/demo-1.png&quot; /&gt;
    &lt;figcaption&gt;Demo 1: Removed items &quot;fall down&quot;. Restored items come back in a reverse animation.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Newly added items are going to “fall down” from above. This is a very simple but nice effect. Each item starts at a position -400px above their final position, and fall down from that position. Don’t forget that the &lt;code&gt;animation-fill-mode&lt;/code&gt; should have a value of &lt;code&gt;forwards&lt;/code&gt; to make sure the items stays in their final position inside the list, otherwise it’ll just disappear up again as soon as the animation is finished.&lt;/p&gt;

&lt;pre class=&quot;brush:css;&quot;&gt;
/*newly added items start faded out and translated 400px upwards on the y-axis*/
li.new-item {
    opacity: 0;
    animation: new-item-animation .3s linear forwards;
}

@keyframes new-item-animation {
    from {
        opacity: 0;
        transform: translateY(-400px);
}

    to {
        opacity: 1;
        transform : translateY(0);
    }
}
&lt;/pre&gt;

&lt;p&gt;Removed items will “fall down” and fade out as they do. The falling down animation is also quite simple: the item is translated downwards along the y-axis, and is rotated as it is falling and faded out until it finally disappears (the Javascript makes sure the item is completely removed from the DOM at the end of this animation).&lt;/p&gt;

&lt;pre class=&quot;brush:css;&quot;&gt;
li.removed-item {
    animation: removed-item-animation 1s cubic-bezier(0.55, -0.04, 0.91, 0.94) forwards;
    /*transform origin is moved to the bottom left corner*/
    transform-origin: 0% 100%;
}

@keyframes removed-item-animation {
    0% {
        opacity: 1;
        transform: rotateZ(0);
}

    100% {
        opacity: 0;
        transform: translateY(600px) rotateZ(90deg);
    }
}
&lt;/pre&gt;

&lt;p&gt;Restoring the item fires an animation which visually reverses the above animation, so the keyframes defined are the exact opposite of the above ones:&lt;/p&gt;

&lt;pre class=&quot;brush:css;&quot;&gt;
/*the initial state in this animation is same as the final state of the above one*/
li.restored-item {
    animation: 
        openspace 0.3s ease forwards, 
        restored-item-animation 0.3s 0.3s cubic-bezier(0, 0.8, 0.32, 1.07) forwards;
}
/*
    Snippet @keyframe openspace source: http://css-tricks.com/transitional-interfaces-coded/
*/
@keyframes openspace {
    to {
        height: auto;
    }
}

@keyframes restored-item-animation {
    0% {
        opacity: 0;
        transform: translateY(600px) rotateZ(90deg);
    }

    10% {
        opacity: 1;
        transform: translateY(600px) rotateZ(90deg);
    }

    100% {
        opacity: 1;
        transform: rotateZ(0);
    }
}
&lt;/pre&gt;

&lt;p&gt;You can see that I used a keyframe called &lt;code&gt;openspace&lt;/code&gt; which I borrowed from Chris Coyier’s article. This makes sure that the items blow the restored item will slide down and make room for the restored item to come back into place. Now, when the items “slide down” to open space for the restored item, they should actually transition down in a smooth manner, but because the items in my app don’t have a fixed height, the keyframes are animating the height to a value of &lt;code&gt;auto&lt;/code&gt;, which unfortunately the browser doesn’t really “transition” to, so the items don’t actually “slide” down, they kinda jump down.&lt;/p&gt;

&lt;p&gt;There’s a way to make items change positions smoothly, it’s a technique by Steve Sanderson which he &lt;a href=&quot;http://blog.stevensanderson.com/2013/03/15/animating-lists-with-css-3-transitions/&quot;&gt; wrote about&lt;/a&gt;, but it uses absolute positioning, and a not-so-little amount of javascript. You can check his article out if you’re interested in knowing more about his technique, and the end result is pretty impressive!&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;demo-2&quot;&gt;Demo #2&lt;/h3&gt;

&lt;figure&gt;
    &lt;img src=&quot;https://sarasoueidan.com/images/demo-2.png&quot; alt=&quot;&quot; /&gt;
    &lt;figcaption&gt;Demo 2: Items scale and fade out into the user's face, and are restored in a reverse manner.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p class=&quot;note&quot;&gt;Credit for this idea goes to &lt;a href=&quot;https://twitter.com/TimPietrusky&quot;&gt;Tim Pietrusky&lt;/a&gt; who came up with this idea when I told him I was almost out of ideas after making the other 5 demos. :)&lt;/p&gt;

&lt;p&gt;Newly added items (i.e items that have not been removed and then restored) are faded in into position.&lt;/p&gt;

&lt;pre class=&quot;brush:css;&quot;&gt;
    li.new-item {
        opacity: 0;
        animation: fadeIn .3s linear forwards;
    }

@keyframes fadeIn {
    to {
        opacity: 1;
    }
}
&lt;/pre&gt;

&lt;p&gt;When items are removed, they scale up and fade out into the user’s face, and are restored in the opposite way, the animation for restoring items is the exact same as the removing animation but only reversed.&lt;/p&gt;

&lt;pre class=&quot;brush:css;&quot;&gt;
    li.removed-item {
        animation: removed-item-animation .6s ease-out forwards;
        transform-origin: 50% 50%;
    }

@keyframes removed-item-animation {
    0% {
        opacity: 1;
        transform: scale(1);
    }

    100% {
        opacity: 0;
        transform: scale(4);
    }
}

li.restored-item {
    animation: 
        openspace .3s ease forwards, 
        restored-item-animation .3s .3s ease-out forwards;
}

@keyframes openspace {
    to {
        height: auto;
    }
}

@keyframes restored-item-animation {
    0% {
        opacity: 0;
        transform: scale(4);
	}

	100% {
    	opacity: 1;
    	transform: scale(1);
	}
}
&lt;/pre&gt;

&lt;p&gt;And that’s it for demo 2, now let’s move on to demo 3.&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;demo-3&quot;&gt;Demo #3&lt;/h3&gt;

&lt;figure&gt;
    &lt;img src=&quot;https://sarasoueidan.com/images/demo-3.png&quot; alt=&quot;&quot; /&gt;
    &lt;figcaption&gt;Demo 3: Restored items slide in the from the right, and removed ones slide out to the left.&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;The third demo is visually simpler than the previous demos. Newly added items will have the same fading in effect as in the previous demo, so we’ll be skipping the animation for these items.&lt;/p&gt;

&lt;p&gt;Items deleted will slide out to the left, with a nice touch at the beginning of the sliding achieved by using a cubic bezier timing function. You should check the live demo to see how the animation works.&lt;/p&gt;

&lt;pre class=&quot;brush:css;&quot;&gt;
li.removed-item {
    animation: removed-item-animation .8s cubic-bezier(.65,-0.02,.72,.29);
}

@keyframes removed-item-animation {
   0% {
        opacity: 1;
        transform: translateX(0);
    }

    30% {
        opacity: 1;
        transform: translateX(50px);
    }

    80% {
        opacity: 1;
        transform: translateX(-800px);
    }

    100% {
        opacity: 0;
        transform: translateX(-800px);
    }
}
&lt;/pre&gt;

&lt;p&gt;Restored items will slide in from the right, with a timing function similar to the above one, but it’s not really the reverse version of it (also check out the demo to see the final result).&lt;/p&gt;

&lt;pre class=&quot;brush:css;&quot;&gt;
li.restored-item {
    animation: 
        openspace .3s ease forwards, 
        restored-item-animation .5s .3s cubic-bezier(.14,.25,.52,1.56) forwards;
}

@keyframes openspace {
    to {
        height: auto;
    }
}

@keyframes restored-item-animation {
    0% {
        opacity: 0;
        transform: translateX(300px);
    }

    70% {
        opacity: 1;
        transform: translateX(-50px);
    }

    100% {
        opacity: 1;
        transform: translateX(0);
    }
}
&lt;/pre&gt;

&lt;p&gt;And that’s it for the third demo, now moving on to the fourth one.&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;demo-4&quot;&gt;Demo #4&lt;/h3&gt;

&lt;figure&gt;
    &lt;img src=&quot;https://sarasoueidan.com/images/demo-4.png&quot; alt=&quot;&quot; /&gt;
    &lt;figcaption&gt;Demo 4: Restored items scale and fade in to position, and removed ones scale and fade out of view.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;This demo is also a simple one. Both new and restored items will be scaling in and fade in into their position, and the removed items will fade and scale out of view. Here are the two keyframes for these animations:&lt;/p&gt;

&lt;pre class=&quot;brush:css;&quot;&gt;
li.removed-item {
    animation: removed-item-animation .6s cubic-bezier(.55,-0.04,.91,.94) forwards;
}

@keyframes removed-item-animation {
    from {
        opacity: 1;
        transform: scale(1);
    }

    to {
        opacity: 0;
        transform: scale(0);
    }
}

li.restored-item {
    animation: 
        /*make sure to open space up before bringing the item into position*/
        openspace .3s ease forwards, 
        restored-item-animation .3s .3s cubic-bezier(0,.8,.32,1.07) forwards;
}

@keyframes openspace {
    to {
        height: auto;
    }
}

@keyframes restored-item-animation {
    from {
        opacity: 0;
        transform: scale(0);
    }

    to {
        opacity: 1;
        transform: scale(1);
    }
}
&lt;/pre&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;demo-5&quot;&gt;Demo #5&lt;/h3&gt;

&lt;figure&gt;
    &lt;img src=&quot;https://sarasoueidan.com/images/demo-5.png&quot; alt=&quot;&quot; /&gt;
    &lt;figcaption&gt;
                Demo 5: New items &quot;fall down&quot;. Removed items &quot;hang&quot; and &quot;fall down&quot;. Restored items slide in from right.
    &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;In this demo, when an item is deleted, it “hangs” down before it actually “falls down” and fades out. This is kind of the best part of this demo, because the newly added items fall down as in demo 1, and restored items slide in from the right as in demo 3, but with a slightly different timing function, so the animation for removing an item is the only new effect in this one.&lt;/p&gt;

&lt;pre class=&quot;brush:css;&quot;&gt;
li.restored-item {
    /*we'll initially translate the item to the right*/
    transform: translateX(300px);
    animation: 
        openspace .3s ease forwards, 
        restored-item-animation .3s .3s cubic-bezier(0,.8,.32,1.07) forwards;
}

@keyframes openspace {
    to {
        height: auto;
    }
}

@keyframes restored-item-animation {
    to {
        opacity: 1;
        transform: translateX(0);
    }
}

li.removed-item {
    animation: removed-item-animation 2s cubic-bezier(.55,-0.04,.91,.94) forwards;
    transform-origin: 0% 100%;
}
&lt;/pre&gt;

&lt;p&gt;changing rotation angle for the item at different frames gives the effect of the item “swinging” when it’s hanging, and then falls down.&lt;/p&gt;

&lt;pre class=&quot;brush:css;&quot;&gt;
@keyframes removed-item-animation {
    0% {
        opacity: 1;
        transform: rotateZ(0);
    }

    20% {
        opacity: 1;
        transform: rotateZ(140deg);
    }

     40% {
     	opacity: 1;
        transform: rotateZ(60deg);
    }

    60% {
        opacity: 1;
        transform: rotateZ(110deg);
    }

    70% {
        opacity: 1;
        transform: rotateZ(90deg) translateX(0);
    }

    90% {
        opacity: 1;
        transform: rotateZ(90deg) translateX(600px);
    }

    100% {
        opacity: 0;
        transform: rotateZ(90deg) translateX(600px);
    }
}
&lt;/pre&gt;

&lt;p&gt;That’s all for demo 5, now to the last demo.&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;demo-6&quot;&gt;Demo #6&lt;/h3&gt;

&lt;figure&gt;
    &lt;img src=&quot;https://sarasoueidan.com/images/demo-6.png&quot; alt=&quot;&quot; /&gt;
    &lt;figcaption&gt;Demo 6: Removed items slide out and fall down to the left. Restored and new items slide in from right.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;In this demo both newly added items and restored ones will get the same animation, where the items slide in from the right, “almost fall out” from the left, and get back into position.&lt;/p&gt;

&lt;pre class=&quot;brush:css;&quot;&gt;
li.restored-item {
    transform: translateX(300px);
    animation: 
        openspace .3s ease forwards, 
        restored-item-animation .5s .3s cubic-bezier(.14,.25,.52,1.56) forwards;
}

@keyframes openspace {
    to {
        height: auto;
    }
}

@keyframes restored-item-animation {
    0% {
        opacity: 0;
         transform: translateX(300px);
    }

    70% {
        opacity: 1;
        transform: translateX(-50px);
    }

    100% {
        opacity: 1;
        transform: translateX(0);
    }
}
&lt;/pre&gt;

&lt;p&gt;Items removed will slide slowly to the left, and then fall down to the left and fade out.&lt;/p&gt;

&lt;p&gt;Now, an important thing to do here is set an appropriate transform origin position, so that the falling down effect looks more realistic. I set the transform origin to the last point of contact between the item and the row is belongs to, right before it starts rotating and falling down, that way it looks that the item fell down due to its own “weight”.&lt;/p&gt;

&lt;pre class=&quot;brush:css;&quot;&gt;
li.removed-item {
    animation: removed-item-animation 1s linear;
    transform-origin: 390px 100%;
}

@keyframes removed-item-animation {
    0% {
        opacity: 1;
        transform: translateX(0) rotateZ(0);
    }

    50% {
        opacity: 1;
        transform: translateX(-400px) rotateZ(0);
    }

    75% {
        opacity: 1;
        transform: translateX(-420px) rotateZ(-30deg);
    }

    100% {
        opacity: 0;
        transform: translateX(-800px) rotateZ(-60deg)  translateY(400px);
    }
}
&lt;/pre&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;final-words&quot;&gt;Final Words&lt;/h2&gt;

&lt;p&gt;The possibilities are almost endless, there are lots of other more creative ways for adding/removing items into lists, I’m sure you can also think of your own effects, and I hope you find this tutorial inspiring!&lt;/p&gt;

&lt;p&gt;I didn’t get into the Javascript part because that’s not the focus of this tutorial.&lt;/p&gt;

&lt;p&gt;I should also note that there’s kind of a bug in Firefox which causes the page to “flash” or repaint whenever an item is focused on or out (i.e when you click the edit/save button). I don’t know if there’s a way around this, please let me know if you know what causes this flashing and if there’s a way to prevent it. For now, I can say that the &lt;b&gt;demos are best experienced in Webkit browsers&lt;/b&gt;.&lt;/p&gt;

&lt;p&gt;Thank you for reading, I hope you enjoyed this tutorial, and if you did make sure you subscribe to the RSS feed to stay updated!&lt;/p&gt;


    
  </content>
 </entry>
 
 <entry>
   <title>Horizontal Portfolio Layout with CSS3 Animations and jQuery</title>
   <link href="http://sarasoueidan.com//blog/horizontal-portfolio-layout/"/>
   <id>https://sarasoueidan.com/blog/horizontal-portfolio-layout</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;In this tutorial today we're going to create a horizontal portfolio layout with cool hover effects inspired by those on &lt;a href=&quot;http://www.guitouxx.com/#/en/home&quot;&gt;Guillaume Tomasi's personal website&lt;/a&gt;. The website is made in Flash, so I thought it would be nice to recreate the flash hover effect of the portfolio items using CSS3 animations and transitions, and some jQuery to replicate the image pan effect on hover.&lt;/p&gt;

&lt;p&gt;I've also added a simple falling down effect on scroll, where the portfolio items fall down as soon as they enter the visible area of the viewport.&lt;/p&gt;

&lt;p&gt;The artwork used in this demo is used with the permission of their owner &lt;a href=&quot;https://twitter.com/vladstudio&quot;&gt;Vlad Gerasimov&lt;/a&gt;. You can find the original images/wallpapers and more on his website &lt;a href=&quot;http://www.vladstudio.com/home/&quot;&gt;VladStudio.com&lt;/a&gt;.&lt;/p&gt;

&lt;p class=&quot;note&quot;&gt;Please note that this demo will work only in &lt;a href=&quot;http://caniuse.com/#feat=transforms3d&quot;&gt;browsers that support&lt;/a&gt; the CSS3 properties used.&lt;/p&gt;

&lt;p class=&quot;note&quot;&gt;For the sake of brevity in the example code, I am using the un-prefixed CSS properties, but you will find the prefixes in the downloadeable source code on Github.&lt;br /&gt; 

&lt;p&gt;So, let's get started!&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;markup&quot;&gt;The Markup&lt;/h2&gt; 

&lt;p&gt;Our list of items is literally a list of items each one with a class &lt;code&gt;item&lt;/code&gt;. Each item contains a &lt;code&gt;figure&lt;/code&gt; which in turn contains a &lt;code&gt;.view&lt;/code&gt; container which wraps an image inside it, and a footer with two paragraph tags that contain the meta information for each image, and a small date tag with its own animation.&lt;/p&gt;

&lt;pre class=&quot;brush:html;&quot;&gt;
              &amp;lt;ul class=&quot;portfolio-items&quot;&amp;gt;
                &amp;lt;li class=&quot;item&quot;&amp;gt;
                  &amp;lt;figure&amp;gt;
                    &amp;lt;div class=&quot;view&quot;&amp;gt;
                     &amp;lt;img src=&quot;images/1.jpg&quot; /&amp;gt;
                    &amp;lt;/div&amp;gt;
                    &amp;lt;figcaption&amp;gt;
                      &amp;lt;p&amp;gt;&amp;lt;span&amp;gt;&amp;lt;a href=&quot;http://www.vladstudio.com/wallpaper/?thetwoandthebubbles&quot;&amp;gt;The Two and The Bubbles&amp;lt;/a&amp;gt;&amp;lt;/span&amp;gt;&amp;lt;/p&amp;gt;
                      &amp;lt;p&amp;gt;&amp;lt;span&amp;gt;By Vlad Gerasimov&amp;lt;/span&amp;gt;&amp;lt;/p&amp;gt;
                    &amp;lt;/figcaption&amp;gt;
                  &amp;lt;/figure&amp;gt;
                  &amp;lt;div class=&quot;date&quot;&amp;gt;2005&amp;lt;/div&amp;gt;
                &amp;lt;/li&amp;gt;
                &amp;lt;li&amp;gt;
                  &amp;lt;!-- second item --&amp;gt;
                &amp;lt;/li&amp;gt;
                &amp;lt;li&amp;gt;
                  &amp;lt;!-- third item --&amp;gt;
                &amp;lt;/li&amp;gt;
                &amp;lt;!-- and so forth --&amp;gt;
              &amp;lt;/ul&amp;gt;
            &lt;/pre&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;css&quot;&gt;The CSS&lt;/h2&gt; 

&lt;p&gt;Let's start with basic styles for the items before we get into the animations and hover effects.&lt;/p&gt;

&lt;pre class=&quot;brush:css;&quot;&gt;
              .portfolio-items {
                height: 400px;
                overflow-x: scroll;
                overflow-y: hidden;
                white-space: nowrap;
                margin-bottom: 30px;
                position: relative;
              }
              .portfolio-items &amp;gt; li {
                display: inline-block;
                /*aligning items by top baseline makes sure the baseline doesn't change once the hover
                effect is fired and therefore the other items stay put*/
                vertical-align: top;
              }
              .item {
                width: 300px;
                height: 202px;
                margin: 150px 20px 0;
                padding: 5px;
                border-radius:2px;
                box-shadow: 0px 10px 10px -5px rgba(0,0,0,0.5);
                background-color: white;
                font-size: 14px;
                /*initially all items are moved 300px up and faded out and rotated, they will fade 
                into view and back to position later via javascript*/
                opacity: 0;
                position: relative;
                top: -300px;
                transform: rotate(-135deg);
                transition: all .3s ease, opacity 2s ease,  top 1s ease;
              }
              /*even items will be 100px lower than their siblings*/
              .item:nth-child(even) {
                margin-top: 100px;
              }
             
            &lt;/pre&gt;

&lt;p&gt;Now that all items have been styled and placed, we'll define the styles for the inner components of each item.&lt;/p&gt;

&lt;p&gt;The figure will take up the full width of the parent. The image will get both a height and a width, and we'll apply a transition to the items so that they change smoothly on hover.&lt;/p&gt;

&lt;p&gt;The figcaption with the metadata will be positioned absolutely, and will be invisible at first so it gets a 0 opacity value.&lt;/p&gt;

&lt;pre class=&quot;brush:css;&quot;&gt;
              figure{
                width:100%;
                height:100%;
              }
              .view {
                overflow: hidden;
                width: 100%;
                height: 190px;
                position: relative;
              }
              .view img {
                width: 300px;
                height: 190px;
                transition: width .3s ease;
                position: absolute;
              }

              figcaption {
                height: 60px;
                width: 100%;
                padding: 0;
                position: absolute;
                bottom: 0;
                overflow: hidden;
                opacity: 0;
              }
              figcaption p {
                font: bold 12px/18px &quot;Arial&quot;, sans-serif;
                text-transform: uppercase;
                padding: 0 10px;
                margin:  5px 0;
                width:100%;
                background-color: #f0f0f0;
                color:#333;
              }
              /*the text of the paragraph tags in the footer(figcaption) is initially hidden to the left*/
              figcaption span {
                left: -100%;
                opacity: 0;
              }
              figcaption a{
                 color: #CC320F; 
              }

              .date {
                z-index: 1;
                width: 50px;
                height: 30px;
                line-height: 30px;
                color: #fff;
                font-weight: bold;
                text-align: center;
                border-radius: 1px;
                background-color: #CC320F;
                position: absolute;
                bottom: 30px;
                left: 15px;
                transition: transform 0.5s cubic-bezier(0.12, 1.6, 0.91, 0.92);
              }

            &lt;/pre&gt;

&lt;p&gt;Now that we have all the items styled, we'll define what happens when each item is hovered.&lt;/p&gt;

&lt;pre class=&quot;brush:css;&quot;&gt;
              .item:hover {
                height: 270px;
                padding: 15px;
                transform: translateY(-68px);
              }
              .item:hover .date {
                transform: translate3d(0, 61px, 0);
              }
              .item:hover figcaption {
                animation: show .25s ease-in .120s forwards;
              }
              .item:hover p:nth-of-type(1) span{
                animation: slideOut .25s ease-out .15s forwards;
              }
              .item:hover p:nth-of-type(2) span{
                animation: slideOut .2s  ease-out .3s forwards;
              }
              .item:hover .view {
                height: 170px;
              }
              .item:hover .view img {
                top: -20px;
                left: -20px;
              }
            &lt;/pre&gt;

&lt;p&gt;When the item is hovered, it increases in height, its padding is increased, thus decreasing the view or &quot;viewport&quot; for each image, while the image keeps its original size. We'll later add a panning effect to the image which makes it possible to view the whole image despite the fact that its viewport got smaller, by changing its position as the mouse moves over it; this is why the image is  moved 20px to the left and upwards when its field of view decreases. We'll manipulate these positions with Javascript later.&lt;/p&gt;

&lt;p&gt;Also on hover, the date tag slides down, the footer is shown and the metadata slides in.&lt;/p&gt;

&lt;p&gt;Here are the keyframes defined for the above animations:&lt;/p&gt;

&lt;pre class=&quot;brush:css;&quot;&gt;
              /*animation to show the metadata*/
              @keyframes slideOut {
                0% {
                  left: -100%;
                  opacity: 0;
                }
                95% {
                  left: 0;
                  opacity: 0.2;
                }
                100% {
                  opacity: 1;
                  left: 0;
                }
              }
              /*animation to show the footer, which will simply up its opacity to 1*/
              @keyframes show {
                to {
                  opacity: 1;
                }
              }
            &lt;/pre&gt;

&lt;p&gt;When we defined the initial styles for the items, we defined their position and opacity so that they are &lt;i&gt;not&lt;/i&gt; visible at first, but once they are within the viewport's visible area, they get a class (via Javascript) which makes them &quot;fall down&quot; into position. Here is the class added to the items on scroll:&lt;/p&gt;

&lt;pre class=&quot;brush:css;&quot;&gt;
              .falldown {
                top: 0;
                opacity: 1;
                /*they are also initially rotated, and are rotated back to their normal position*/
                transform: rotate(0);
              }
            &lt;/pre&gt;

&lt;p&gt;For extra styling purposes, we're gonna style the scrollbar of the items' list. But bear in mind that these styles are supported only in Webkit browsers. You can, of course, use &lt;a href=&quot;http://jscrollpane.kelvinluck.com/&quot;&gt;one of&lt;/a&gt; &lt;a href=&quot;http://designhuntr.com/custom-jquery-scrollers/&quot;&gt;several&lt;/a&gt; javascript &lt;a href=&quot;http://slodive.com/web-development/jquery-scroll/&quot;&gt;plugins&lt;/a&gt; available to provide cross-browser scrollbar styles if it's necessary to your overall design.&lt;/p&gt;

&lt;pre class=&quot;brush:css;&quot;&gt;
              ::-webkit-scrollbar {
                width: 7px;
                height: 7px;
                cursor: pointer;
              }
              ::-webkit-scrollbar-track {
                background-color: #ddd;
                border-radius: 10px;
              }
              ::-webkit-scrollbar-thumb {
                border-radius: 10px;
                background-color: #C4290D;
              }
            &lt;/pre&gt;

&lt;p&gt;That's all the styling we need and all animations needed for the hover effect. Now we'll start defining the panning effect with Javascript and handling the list scroll function.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;javascript&quot;&gt;The Javascript&lt;/h2&gt; 

&lt;p&gt;We'll start by defining the scrolling function which will first check for the position of an item on the screen, and return true if the item is in the visible area of the viewport, but it only checks horizontally.&lt;/p&gt;

&lt;pre class=&quot;brush: js;&quot;&gt;
              //checks if element it is called on is visible (only checks horizontally)
              (function($) {
                var $window = $(window);
                
                $.fn.isVisible = function(){
                  var $this = $(this),
                    Left = $this.offset().left,
                    visibleWidth = $window .width();

                  return Left &amp;lt; visibleWidth;  
                }
              })(jQuery);
            &lt;/pre&gt;

&lt;p&gt;Now we're going to define the function what will call this function on the portfolio items to check for their visibility.&lt;/p&gt;

&lt;pre class=&quot;brush: js;&quot;&gt;
              (function($){
                var list = $('.portfolio-items'),
                    showVisibleItems = function(){
                    list.children('.item:not(.falldown)').each(function(el, i){
                        var $this = $(this);
                        if($this.isVisible()){
                          $this.addClass('falldown');
                        }
                      });
                    };
                    //....
            &lt;/pre&gt;

&lt;p&gt;We'll want to call this function as soon as the page has loaded to check for visible items and add the &lt;code&gt;.falldown&lt;/code&gt; class to all items that should be visible in the beginning. Then, we'll want to call this function whenever the list is scrolled as well.&lt;/p&gt;

&lt;pre class=&quot;brush:js;&quot;&gt;
                  //....
                  //initially show all visible items before any scroll starts
                  showVisibleItems();
                  
                  //then on scroll check for visible items and show them
                  list.scroll(function(){
                    showVisibleItems();
                  });
            &lt;/pre&gt;

&lt;p&gt;The last thing we're going to do is add the panning effect for the images on hover. What this function does is that it checks the position of the mouse cursor when it moves over each image, and moves the image along with the movement of the cursor. It measures the distance between the cursor and the image's &lt;code&gt;view&lt;/code&gt; boundaries, and then divides that by the part of the image that's hidden beyond the borders of the &lt;code&gt;view&lt;/code&gt;, thus making sure the image does not move any extra than it should. The function calculations should make it clearer:&lt;/p&gt;

&lt;pre class=&quot;brush:js;&quot;&gt;
              list.on('mousemove','img', function(ev){
                  var $this = $(this),
                      posX = ev.pageX, 
                      posY = ev.pageY,
                      data = $this.data('cache');
                //cache necessary variables
                    if(!data){
                      data = {};
                      data.marginTop = - parseInt($this.css('top')),
                      data.marginLeft = - parseInt($this.css('left')),
                      data.parent = $this.parent('.view'),
                      $this.data('cache', data); 
                    }

                var originX = data.parent.offset().left,
                    originY =  data.parent.offset().top;
                
                   //move image
                   $this.css({
                      'left': -( posX - originX ) / data.marginLeft,
                      'top' : -( posY - originY ) / data.marginTop
                   }); 
              });
            &lt;/pre&gt;

&lt;p&gt;One thing remaining is making sure the image returns to its initial position when the mouse leaves the item so that everything goes back to its initial state:&lt;/p&gt;

&lt;pre class=&quot;brush:js;&quot;&gt;
              list.on('mouseleave','.item', function(e){
                $(this).find('img').css({
                  'left': '0', 
                  'top' : '0'
                });
              });
            &lt;/pre&gt;

&lt;p&gt;To finish up, we're going to add mouse wheel support using &lt;a href=&quot;https://github.com/brandonaaron/jquery-mousewheel&quot;&gt; jQuery Mouse Wheel plugin by Brandon Aaron&lt;/a&gt;:&lt;/p&gt;

&lt;pre class=&quot;brush:js;&quot;&gt;
              //add mouse wheel support with the jquery.mousewheel plugin
              list.mousewheel(function(event, delta) {

                  this.scrollLeft -= (delta * 60);
                
                  event.preventDefault();

               });
            &lt;/pre&gt;

&lt;p&gt;Aaand we're done! :) I hope you liked this simple hover effect and found it useful.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thanks a lot &lt;a href=&quot;http://twitter.com/FWeinb&quot;&gt;Fabrice &lt;/a&gt; &lt;a href=&quot;http://blog.weinberg.me/&quot;&gt;Weinberg&lt;/a&gt; for helping me optimize and organize my Javascript code. :)&lt;/em&gt;&lt;/p&gt;


&lt;/p&gt;

    
  </content>
 </entry>
 
 <entry>
   <title>Lessons from the “Seductive Interaction Design” Book</title>
   <link href="http://sarasoueidan.com//blog/lessons-from-seductive-interaction-design-book/"/>
   <id>https://sarasoueidan.com/blog/lessons-from-seductive-interaction-design-book</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;
In this article today, I’m going to share with you some of the lessons I learned from one of the best books I’ve read: &lt;a href=&quot;http://www.peachpit.com/store/seductive-interaction-design-creating-playful-fun-and-9780321725523&quot;&gt;Seductive Interaction Design: Creating Playful, Fun, and Effective User Experiences&lt;/a&gt;.
&lt;/p&gt;

&lt;p&gt;I believe that you should read this book yourselves to fully appreciate all the information and insight the writer has to share. I will cover only the first half of the book. I will be pasting some excerpts, because there are many things that the writer describes a lot better than I could ever do. I will also be skipping a lot of the content for sake of brevity and of course because you should learn the rest from the book directly, not from me.&lt;/p&gt;

&lt;p&gt;
So think of this article as a review, preview, or just a simple article written by someone who is so excited about a book she read that she just needs to share some of what she learned with you.
&lt;/p&gt;

&lt;p class=&quot;note&quot;&gt;
&lt;strong&gt;Disclaimer:&lt;/strong&gt; Images, blockquotes, and quotes are excerpted from Seductive Interaction Design: Creating Playful, Fun, and Effective User Experiences by Stephen P. Anderson. Copyright © 2011. Used with permission of Pearson Education, Inc. and New Riders.&lt;br /&gt;
Link to book: &lt;a href=&quot;http://www.peachpit.com/store/seductive-interaction-design-creating-playful-fun-and-9780321725523&quot;&gt;Seductive Interaction Design: Creating Playful, Fun, and Effective User Experiences.&lt;/a&gt;&lt;br /&gt;
&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;overview&quot;&gt;OVERVIEW&lt;/h2&gt;

&lt;p&gt;
Writer, designer, and speaker &lt;a href=&quot;http://www.poetpainter.com/&quot;&gt;Stephen Anderson&lt;/a&gt; shows you in this book how the same tactics humans use to attract a mate can apply to the interactions between humans and interactive devices, to make people “fall in love” with your websites and/or applications.
&lt;/p&gt;
&lt;p&gt;
The book focuses on human behavior, in both physical and digital contexts, and talks about what actually drives people and influences their behavior and “seduces” them into taking certain kinds of actions. It also studies a lot of examples of existing web applications, and explores the underlying psychological principles applied to the user experience of these applications, that make them as effective and successful as they are.
&lt;/p&gt;
&lt;p&gt;One of the first examples mentioned in the book is LinkedIn and the effectiveness of the Profile Completeness process, specifically how LinkedIn manages to pull quite a bit of information out of millions of users through a series of prompts that are simple enough and yet very effective. By understanding what motivates people, they were able to get a lot of information out of them. And this is what the book is concerned with: why people do the things they do.&lt;/p&gt;
&lt;p&gt;The concept of level completeness used in LinkedIn can also be found in games as a “progress dynamic”, with points and levels. It can also be found in other contexts, one of which is martial arts, where each “level” is represented by a colored belt, one you earn while advancing towards the black belt. &lt;q&gt;By having different colored belts [..] you get rewarded and recognized along the path to mastery. These belts are a tangible, achievable goal to work toward&lt;/q&gt;.But why do we do the things we do when we have this kind of progress dynamic?&lt;/p&gt;
&lt;p&gt;We could look at several ideas from psychology:&lt;/p&gt;

&lt;blockquote&gt;
    &lt;ul style=&quot;list-style-type:none;&quot;&gt;
        &lt;li&gt;&lt;strong&gt;Sequencing:&lt;/strong&gt; We are more likely to take action when complex tasks are broken down into smaller tasks.&lt;/li&gt;
        &lt;li&gt;&lt;strong&gt;Appropriate challenges:&lt;/strong&gt; We delight in challenges, especially ones that strike a balance between being overwhelming and being boring.&lt;/li&gt;
        &lt;li&gt;&lt;strong&gt;Status:&lt;/strong&gt; We constantly assess how interactions enhance or diminish our standing relative to others and our personal best.&lt;/li&gt;
        &lt;li&gt;&lt;strong&gt;Achievements:&lt;/strong&gt; We are more likely to engage in activities in which meaningful achievements are recognized.&lt;/li&gt;
    &lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;So always offer your users some kind of reward after every step they make throughout a sign up form, or any other kind of forms that require several steps along the way to completeness, like surveys, for example, and be clear about why you’re asking for the information you ask for, and help them understand that whatever information you ask from them will actually benefit &lt;em&gt;them&lt;/em&gt;, even if you have other reasons you’re asking them for this information. &lt;b&gt;State your reasons in terms of how they would benefit &lt;em&gt; the user&lt;/em&gt; to provide this information to you&lt;/b&gt;, because &lt;q&gt;we’ve known that we are more interested in people who are interested in us. no one wants to sit and hear someone talk about themselves all night. The same is true in many online interactions&lt;/q&gt;.&lt;/p&gt;
&lt;p&gt;The writer then goes on to mention another example of great UX, which benefits both the user and the owners of the application, which is iTunes. He explains the process of signing up for iTunes in detail, and reveals the psychological part of the process, that makes users &lt;em&gt;want&lt;/em&gt; to continue the process:&lt;/p&gt;
&lt;blockquote&gt;
    &lt;ul style=&quot;list-style-type:none;&quot;&gt;
        &lt;li&gt;&lt;strong&gt;Feedback loops: &lt;/strong&gt; We’re engaged by situations in which we see our actions modify subsequent results.&lt;/li&gt;
        &lt;li&gt;&lt;strong&gt;Curiosity:&lt;/strong&gt; When teased with a small bit of interesting information, people want to know more.&lt;/li&gt;
        &lt;li&gt;&lt;strong&gt;Visual imagery:&lt;/strong&gt; Vision trumps all other senses and is the most direct way to perception.&lt;/li&gt;
        &lt;li&gt;&lt;strong&gt;Recognition over recall:&lt;/strong&gt; It’s easier to recognize things we have previously experienced than it is to recall them from memory.&lt;/li&gt;
    &lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;This kind of perspective offers you a new way to look at user experiences. For example, one example of UX that is &lt;em&gt;different&lt;/em&gt; and very attractive and more enjoyable, in my opinion, is the new form and survey experience that &lt;a href=&quot;http://www.typeform.com/&quot;&gt;Typeform&lt;/a&gt; offers, because it uses several concepts including sequencing and visual imagery, and they use images for a lot of their questions and offer multiple choices which are easier to use than having to recall stuff from memory. All these elements make filling up forms and taking surveys easier and more enjoyable, or at the very least, a lot less boring.&lt;/p&gt;

&lt;figure&gt;
            &lt;img src=&quot;https://sarasoueidan.com/images/typeform1.png&quot; /&gt;
            &lt;figcaption&gt;Screenshot from the preview video on the Typeform website. The application offers users a set of images to choose from.&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;figure&gt;
            &lt;img src=&quot;https://sarasoueidan.com/images/typeform2.png&quot; /&gt;
            &lt;figcaption&gt;Screenshot from the preview video on the Typeform website. The application offers users an image with the question. &quot;Vision trumps all other senses and is the most direct way to perception&quot;.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;usability-psychology&quot;&gt;Usability and Psychology&lt;/h2&gt;

&lt;p&gt;A simple and straightforward differentiation between the roles of usability and psychology in user experience design is the following:&lt;/p&gt;

&lt;blockquote&gt;
    Usability clears the way for a good experience by eliminating troublesome interface distractions, but a great experience stems from something more—an awareness of why people could or do care. &lt;b&gt;The danger is in confusing “ease of use” with actually desiring to use something.&lt;/b&gt; These are two entirely different things. Both are essential, but simply making something more usable won’t guarantee any more clicks or conversions. in this case, it was psychology that made this so engaging.
&lt;/blockquote&gt;
&lt;p&gt;Here is an image showing the difference in roles between usability and psychology in user experience design:&lt;/p&gt;
&lt;figure class=&quot;floated&quot;&gt;
    &lt;img src=&quot;https://sarasoueidan.com/images/usability-psychology.png&quot; alt=&quot;Usability vs Psychology&quot; /&gt;
    &lt;figcaption&gt;An image representing the role of each of usability and psychology in user experience design.&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;By using psychology to make your website/application move from being functional, reliable, usable, and convenient to being pleasurable and meaningful to the user. With this in mind, the writer then introduces a “user experience hierarchy of needs model” (shown in the image below)&lt;/p&gt;

&lt;figure&gt;
    &lt;img src=&quot;https://sarasoueidan.com/images/hierarchy.png&quot; alt=&quot;Hierarchy of Needs Model&quot; /&gt;
    &lt;figcaption&gt;User Experience Hierarchy of Needs model. From bottom to top is a basic product maturity continuum: a top to bottom focus starts with the experience you want people to have.&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;So if you want to create a revolutionary product, you have to think beyond basic functionality, usability, and convenience, and think about what kind of experience you want the user to have when using your product/website/application, but without forgetting the basics of usability.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;aesthetics&quot;&gt;Aesthetics, Beauty, and Behavior&lt;/h2&gt;

&lt;p&gt;The first “Weapon” of seduction is Aesthetics. This section explores the relation between aesthetics and human cognition, affect, and how aesthetics help our brains make certain associations between a product, and other real-life objects, and how these associations will end up affecting how we feel about it, and consequently how we behave. &lt;/p&gt;
&lt;blockquote&gt;
    As user experience designers, we must consider every stimulus that might influence user interaction. […] “aesthetics examines our response to an object or phenomenon” (according to Wikipedia). In other words, aesthetics aren’t just about the artistic merit of Web buttons or other visual effects, but about how people respond to these elements. The question becomes: How do aesthetic design choices influence understanding and emotions, and how do understanding and emotions influence behavior?
    […] we’ll look at how aesthetics influences cognition, affect, and associations.
&lt;/blockquote&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;aesthetics-cognition&quot;&gt;Aesthetics and Cognition&lt;/h3&gt;

&lt;p&gt;
&lt;q&gt;Cognition is the process of knowing. Based on patterns and experiences, we learn how to understand the world around us [..] and aesthetics play a critical role in cognitive processing&lt;/q&gt;, that is, in the way we perceive elements in the digital context.
&lt;/p&gt;
&lt;p&gt;The human brain has its own way of interpreting color, shadows, shading, and other natural occurrences, and the role of aesthetics is to communicate the functionality of the elements we see to our brains. They provide the brain with cues that communicate how we should interact with these elements. So, when designing, designers should think about what each color means, in addition to the role and meaning of shadows and shading. &lt;/p&gt;
&lt;p&gt;For example, think about a simple button (image below). The shadows, gradients, and beveled edges of the upper right button help the brain understand that this is in fact a button, and therefore it can be pressed, and that we can expect something to happen if we do. In this case, aesthetics communicate function. These shadows, gradients, and beveled edges &lt;q&gt;are perceived affordances—cues that communicate how a user can, and should, interact with an object. translation: if it looks like a button, it must be a button&lt;/q&gt;.&lt;/p&gt;
&lt;figure&gt;
    &lt;img src=&quot;https://sarasoueidan.com/images/buttons.png&quot; alt=&quot;Buttons and Meaning of Gradients&quot; style=&quot;margin-top:50px;&quot; /&gt;
    &lt;img src=&quot;https://sarasoueidan.com/images/alert.png&quot; alt=&quot;Alert Message and Meaning of color&quot; style=&quot;float:right&quot; /&gt;
&lt;/figure&gt;
&lt;p style=&quot;clear:both;&quot;&gt;The second image shows how wrong usage of colors can confuse the brain about the actual meaning and functionality of an element, in this case an alerted message.&lt;/p&gt;
&lt;p&gt;The use of shadows also plays an important role in determining how elements look on a page, and which elements lay on top of others (stacking context), so special attention should be paid to these kinds of details. One of my favorite paragraphs in this book is this one in which the writer gives a golden tip:&lt;/p&gt;
&lt;blockquote&gt;
    Whatever the natural reference is— shadows, reflections, lighting, bevels—i like to ask designers, “Could you build a physical model of this page?” if you can’t, then the viewer   will likely be disturbed, as something feels not   quite right.
&lt;/blockquote&gt;
&lt;p&gt;So, if you can build a physical model out of a digital design, then it can be interpreted correctly by the brain, otherwise it’s just not right!&lt;/p&gt;
&lt;p&gt;In addition to cuing the brain to understand the functionality and shape of objects, aesthetics play in a role in determining the relationship between these objects.&lt;q&gt;For example,  the law of proximity explains that if i place two or more items in a cluster together, you’ ll assume they are related&lt;/q&gt;.&lt;/p&gt;
&lt;figure&gt; 
    &lt;img src=&quot;https://sarasoueidan.com/images/proximity.png&quot; alt=&quot;&quot; /&gt;
&lt;/figure&gt;
&lt;p&gt;Then there is also contrast and connectedness.&lt;q&gt;if one object has different characteristics from other objects, we perceive it as being different. This is known as contrast.” Additionally, elements connected by uniform visual properties are perceived as being more related than elements that are not connected. This is known as uniform connectedness&lt;/q&gt;.
&lt;/p&gt;
&lt;figure&gt; 
    &lt;img src=&quot;https://sarasoueidan.com/images/connectedness.png&quot; alt=&quot;&quot; /&gt;
&lt;/figure&gt;
&lt;figure class=&quot;floated&quot;&gt; 
    &lt;img src=&quot;https://sarasoueidan.com/images/mac-genie.png&quot; alt=&quot;&quot; /&gt;
&lt;/figure&gt;
&lt;p&gt;Additionally, aesthetics help us understand the “space” in which we interact, for example, placing elements behind each other in a 3D space to communicate distance. Another example is the famous “genie effect” animation in Mac OS X, which communicates where a file is being stored/minimized for easy retrieval later.&lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;aesthetics-emotion&quot;&gt;Aesthetics and Emotion&lt;/h3&gt;

&lt;p&gt;Some UX designers believe that by making a product easy to use and convenient, this will directly lead to creating a better, enjoyable user experience. But in this chapter, the writer proves that things that are enjoyable will be perceived as easy to use and convenient. Again to relate digital experiences to real-life interactions between people, &lt;q&gt;Think of how quickly we form judgments about people in the first few moments after we meet them. Conversely, think about how our personal appearance (our personal aesthetic) affects the way people perceive us; or how product packaging influences our perception of the product inside. We may know better, but we continue to judge a book by its cover&lt;/q&gt;.&lt;/p&gt;
&lt;p&gt;The way products and interfaces look says a lot about them. Take for example an application with a lot of attention to details. When you see this kind of attention, you subconsciously trust the application you’re interacting with more. On the other hand, imagine a UI with inconsistent fonts, odd paddings, line heights, and such details that can butcher even the greatest designs, “how might these sloppy UI details affect our perception of the application?” How can you trust an application whose owner wasn’t attentive enough to care about these small details of their own product? How will they be able to pay attention to our needs if they can’t even pay attention to the small details in their product?&lt;/p&gt;
&lt;p&gt;There are also a lot of studies mentioned in the book that prove that &lt;q&gt;not only do aesthetics affect perceived usability, they also influence actual performance&lt;/q&gt;. I’m going to mention only one short experiment for the sake of brevity:&lt;/p&gt;
&lt;blockquote&gt;
    One study, “The Influence of Design aesthetics in usability testing: effects on user Performance and Perceived usability,” (Sonderegger and Sauer, 2009), presented adolescents with one of two mobile phones, an attractive one, and one less so. The conclusion? “The visual appearance of the phone had a positive effect on performance, leading to reduced task completion times for the attractive model.
&lt;/blockquote&gt;
&lt;p&gt;All the experiments the writer mentions lead us to one firm conclusion:&lt;/p&gt;
&lt;blockquote&gt;
    The more we learn about people, and how our brains process information, the more we see the truth of that phrase: form and function aren’t separate. If form exists independently of function, and we can treat aesthetics and function as two separate elements, then we ignore the evidence that beauty is much more than decoration. Our brains can’t help but agree.
&lt;/blockquote&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;aesthetics-associations&quot;&gt;Aesthetics and Associations&lt;/h3&gt;

&lt;p&gt;Aesthetics play a major role in associations our brains make between objects. Our brains tend to try to connect objects to other objects, and when that is done it shares the characteristics of one of these objects with the other.&lt;/p&gt;
&lt;p&gt;The best way to explain this point is to just paste one of the (interesting) examples the writer mentions in the book: Apple products!&lt;/p&gt;
&lt;blockquote&gt;
    &lt;p&gt;In a 2005 essay on design and perceptions, Luke Williams recounts how another designer discovered why so many people think of the iPod as a “clean” device. Apparently, this designer had been sitting on the toilet (where all great ideas happen!) when it occurred to him that the iPod references the same materials used in a bathroom, “the shiny white porcelain of the bathtub and the reflective chrome of the faucet on the wash basin.” This might sound laughable, until you factor in that Jonathan ives, apple’s senior vice president of design, once worked for an agency that designed—you guessed it— bathroom appliances. Coincidence? Perhaps. What’s important is that “consciously or unconsciously, the iPod materials reference a convention of ‘cleanliness’ that everybody interacts with every day—a bathroom.
    &lt;/p&gt;
    &lt;p&gt;We’re talking about human perception, and the system of conventions that shapes our perceptions. Perception is essential to the process of design.
            These aesthetic associations are evident in other apple products. If you own an apple laptop, you may have noticed the soothing sleep-light indicator that’s visible when your computer is “sleeping.” The rate at which this light fades in and out is comparable to that of the average respiratory rate for adults, about 12 to 20 breaths per minute. Coincidence? apple owns the patent for a Breathing status Led indicator (Us 6,658,577 B2), which “mimics the rhythm of breathing which is psychologically appealing&quot;.&lt;/p&gt;
        &lt;img src=&quot;https://sarasoueidan.com/images/ipod.png&quot; alt=&quot;&quot; width=&quot;250&quot; height=&quot;230&quot; /&gt;
    &lt;p&gt;One final example: when apple launched the original iPod shuffle, they compared it directly to a pack of gum, due to the equivalent sizes of the two products. This is a great example of a conceptual metaphor, in which we make sense of new information by associating it with something we’re already familiar with. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;power-of-faces&quot;&gt;The Power of Faces&lt;/h3&gt;

&lt;p&gt;Including faces in our online interactions also affects the associations our brains make, because faces carry with them some kind of associations, and can help build trust because of a higher fidelity of information that the application or website presents.&lt;/p&gt;
&lt;figure&gt;
    &lt;img src=&quot;https://sarasoueidan.com/images/faces.png&quot; alt=&quot;&quot; /&gt;
&lt;/figure&gt;
&lt;p&gt;One example to best explain this notion is Facebook's logout screen. &lt;/p&gt;
&lt;blockquote&gt;
    The original Facebook deactivation page was pretty boring. It simply stated: “We’re sorry you’re leaving. tell us why Facebook was not useful.” One of the designers suggested that closing your account be “more like leaving summer camp (you know, a place that has all your friends and you don’t want to leave.)”
        &lt;img src=&quot;https://sarasoueidan.com/images/facebook.png&quot; alt=&quot;&quot; /&gt;
    Inspired by this concept, the design team created a new deactivation page that pulls faces from a few of your friends’ profiles, along with the message that asks, “are you sure you want to deactivate your account? Your 498 friends will no longer be able to keep in touch with you.” has this made a difference? according to Julie Zhuo, design manager at Facebook, this has reduced the deactivation rate by 7 percent. at least a million fewer users have deactivated their accounts!
&lt;/blockquote&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;playful-seduction-techniques&quot;&gt;Playful Seduction Techniques&lt;/h2&gt;

&lt;blockquote&gt;
    In dating terms, it’s easy to think, “People will like me for who I am.” The truth is people have to be interested just enough to get to know you (your app) in the first place. What we’re talking about in this chapter are ways to design interactions that are more interesting and playful—interactions that engage people in both intellectually and emotionally. This leads to experiences that do more than merely work, they delight people.
&lt;/blockquote&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;fun&quot;&gt;Be Fun&lt;/h3&gt;

&lt;p&gt;We all like funny people. And by funny I don’t mean people who are always making jokes and trying to make you laugh, because those can become really annoying! By funny people I mean people who are fun to be around, who always have their way to make you smile, and who are great talkers, and can get a message to you in a fun way that sticks into your brains and that you’re more likely to remember later, and smile about it! &lt;/p&gt;
&lt;p&gt;By including humor in digital contexts, you engage people in a meaningful and memorable way. One of the examples that show the kind of fun you can use is the Southwest Airlines company. 
&lt;/p&gt;
&lt;figure&gt;
    &lt;img src=&quot;https://sarasoueidan.com/images/southwest.png&quot; alt=&quot;Southwest&quot; /&gt;
    &lt;figcaption&gt;
        Some messages that the Southwest Airlines company uses that include fun phrases.
     &lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Some may argue that humor is not always appropriate, so the writer has another golden advice to give:&lt;/p&gt;
&lt;blockquote&gt;
    &lt;p&gt;If it’s appropriate in a real-world interaction, why not online as well? Are we suddenly transformed into emotionless automatons when we sit in front of a screen? No.&lt;/p&gt;
                [...]
    &lt;p&gt;Humor is appropriate (or inappropriate) based on the situation, not the industry.”&lt;/p&gt;
&lt;/blockquote&gt;
&lt;figure&gt;
    &lt;img src=&quot;https://sarasoueidan.com/images/popup.png&quot; alt=&quot;Fun Popup&quot; /&gt;
     &lt;figcaption&gt;
        An alert message that uses humor to convey a message.
    &lt;/figcaption&gt;
&lt;/figure&gt;
&lt;h3 class=&quot;deeplink&quot; id=&quot;unpredictable&quot;&gt;Be Unpredictable&lt;/h3&gt;
&lt;blockquote&gt;
    Our brains are aroused by the unexpected. While stability and a sense of control are no doubt critical user interface principles, there’s something exciting about the unexpected. not knowing what to expect heightens our anxiety, and our curiosity. Our brains are aroused by new and unexpected discoveries within our normal routines.
&lt;/blockquote&gt;
&lt;p&gt;One of the simplest way to make an experience more enjoyable after repeated visits, is by breaking the routine. One of the simplest and most obvious examples of offering a slight surprise and breaking the routine is the Google Search homepage. The Google logo changes depending on the occasion. This little change adds a new flavor to our daily visits, that would otherwise be all boring and the same, especially that the Google homepage design is already too simple to start with. So add these little design touches help break the routine and keep our brains expecting something new at different occasions.&lt;/p&gt;
&lt;p&gt;Another example of breaking routines is for example changing the content of a confirmation message every time it pops up so that it’s different every time, or changing an image on the homepage after repeated visits, or delightful messages that pop up in unexpected places. These messages don’t have to be necessary, yet they could be pleasant enough to stick in the head of your users or readers.&lt;/p&gt;
&lt;p&gt;One perfect example of a delightful surprise the writer mentions is a note he saw while he was going up the stairs in a hotel, where he was surprised by a “Everything is going to be alright” written in uppercase on one of the stairs (image below), that made him grin, and was so pleasant that he still remembers it and thought it was worthy of mentioning as a great example in his book.&lt;/p&gt;
&lt;figure&gt;
    &lt;img src=&quot;https://sarasoueidan.com/images/stairs.png&quot; alt=&quot;Stairs&quot; /&gt;
&lt;/figure&gt;
&lt;blockquote&gt;
    A good date is full of delightful moments— some planned, some not—that make the overall experience memorable and pleasant. What does my wife remember from our first date? among other fond memories, it rained and we got our shoes stuck in the mud!
&lt;/blockquote&gt;
&lt;p&gt;These kinds of small delightful surprises make the interaction with the digital world seem more human, which is a very important aspect if you’re trying to sell yourself as a trustworthy person.&lt;/p&gt;
&lt;p&gt;So what makes a good present or surprise? Another golden advice from the writer:&lt;/p&gt;
&lt;blockquote&gt;
    A good gift is one that pumps up the recipient. 
&lt;/blockquote&gt;
&lt;p&gt;Think of the word pump:  P is for pleasurable, U is for unexpected, M is for meaningful (useful, not generic), and the last P is for pleasantly packaged.&lt;/p&gt;
&lt;h3 class=&quot;deeplink&quot; id=&quot;mysterious&quot;&gt;Be Mysterious&lt;/h3&gt;
&lt;p&gt;
&lt;q&gt;In new relationships, flirtation often involves some element of playful teasing&lt;/q&gt;, and a similar kind of teasing can be applied to the relationship between users and a website or application.
&lt;/p&gt;
&lt;p&gt;Curiosity is a powerful human drive that pushes us to do a lot of the things we do. It’s probably the reason why I read the book in the first place. I was curious to know how a website or application can be “Seductive”, I was even more curious after I started reading to understand how people think, and understand how my brain works, and why &lt;em&gt;I&lt;/em&gt; do the things I do.&lt;/p&gt;

&lt;p&gt;Part of the role of usability is make things clear to the user, and remove all roadblocks and ambiguous elements that make a user experience become a rather frustrating one.&lt;/p&gt;
&lt;p&gt;Just like we have to care about the basics and make an application or product usable and functional, and after that we can cross that line to make it pleasurable and memorable,&lt;b&gt; we can also start thinking about adding some kind of &lt;em&gt;controlled&lt;/em&gt; uncertainty to the experience after having provided the user with the clarity he needs, thus introducing a level of thrill and suspense to the experience&lt;/b&gt;.&lt;/p&gt;
&lt;p&gt;Sometimes giving the user all the information they want from the first visit rids them of their interest and leaves no more room for curiosity to drive them forward to further explore the application at hand.&lt;/p&gt;
&lt;figure&gt;
    &lt;img src=&quot;https://sarasoueidan.com/images/curiosity1.png&quot; alt=&quot;Curiosity Zone&quot; /&gt;
&lt;/figure&gt;
&lt;p&gt;You want to make sure that you &lt;em&gt;tease&lt;/em&gt; them with as little information as necessary, enough to drive them into the “curiosity zone”.&lt;/p&gt;
&lt;figure&gt;
    &lt;img src=&quot;https://sarasoueidan.com/images/curiosity2.png&quot; alt=&quot;Curiosity Zone&quot; /&gt;
&lt;/figure&gt;
&lt;blockquote&gt;
    &lt;p&gt;Information can be presented in a manner that is straightforward or curious. If we opt for the latter, we are guaranteed not only attention, but probably higher engagement as well— curiosity demands that we know more!&lt;/p&gt;
              
    [..]
              
    &lt;p&gt;When we become aware that  information is missing—when something changes from being known (or so we thought) to an unknown state—we become curious. This is the explanation of curiosity posed by behavioral economist George Loewenstein in his information gap theory. Loewenstein says, “curiosity happens when we feel a gap in our knowledge.”&lt;/p&gt;
              
    [..]
              
    &lt;p&gt;The feeling we get from these information gaps is best described as deprivation, which is critical to understanding why we are motivated by curiosity. to “eliminate the feeling of deprivation,” we seek out the missing information.&lt;/p&gt;
              
    [..]
              
    &lt;p&gt;Simply stated: I’m curious because there’s a gap between “what I know and what I want to know.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;It’s human to be curious. And it’s part of our nature to seek to solve mysteries around us. The kind of curiosity the writer refers to in his book is akin to teasing. Teasing people by making them aware that there is something they &lt;em&gt;don’t&lt;/em&gt; know. But there are a few ways to make this teasing effective, and I believe the most important one is to let them know that the information you’re hiding away from them will benefit &lt;em&gt;them&lt;/em&gt; the most. Also, make sure that once they get to the point where they uncover the information you withheld from them at the beginning, they find that this information meets their expectations, the expectations they built because of &lt;em&gt;you&lt;/em&gt;. Also, &lt;q&gt;don’t lure users with something that is given away freely elsewhere&lt;/q&gt;. &lt;/p&gt;

&lt;h3 class=&quot;deeplink&quot; id=&quot;self-expression&quot;&gt;Let others express themselves around you&lt;/h3&gt;

&lt;p&gt;The final principle in the playful seduction section is &lt;em&gt;self-expression&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Giving the users the ability to express themselves in your application, by giving them, for example, the ability to customize the application and make it more personal, is very important when the users expect this kind of option to be available to them, and even when they don’t!&lt;/p&gt;
&lt;blockquote&gt;
    The need for self-expression shows up in just about any area where people are allowed to control something, especially where this control is tied to an identity.
&lt;/blockquote&gt;
&lt;p&gt;Some examples of allowing users to express and apply their personal identity to an application would  be to allow them to change the theme, or use custom emoticons, or change the content and add/remove what they want, change the layout and theme, and so on.&lt;/p&gt;
&lt;p&gt;If you’re one of the people who use Windows’ MSN chat application, then you definitely know that for the past couple of months or more, the company was preparing MSN users to switch, or “upgrade”, to Skype. I am one of the individuals who sincerely hated this “upgrade” for one simple reason: Skype doesn’t allow you to use custom emoticons. And after reading a lot of comments on the Skype blog, I realized that I am only one of so many people who hated this “upgrade” as well, and my intention was very clear: to &lt;em&gt; not&lt;/em&gt; make the switch. Why? Because my MSN is customized to my needs, and Skype doesn’t allow you to add custom emoticons. I love custom emoticons. I can’t chat without them. And I really believe this should not be called an upgrade because IMO to upgrade means to make better, and when your newer version lacks a lot of the best features of your previous one, then this, IMO, is more of a downgrade. Microsoft should’ve given this move a lot of thought before actually deciding to move forward with it.&lt;/p&gt;
&lt;p&gt;The point is, giving your users the ability to customize your application or express themselves in any way is a great way to make the application feel more personal and thus enjoyable for them. And if you ever decide to do that, please don’t take that option away from them later. If you add it to your app, just make it stick. &lt;/p&gt;

&lt;h4 class=&quot;deeplink&quot; id=&quot;final-words&quot;&gt;Final Words&lt;/h4&gt;

&lt;p&gt;I only covered the first two sections of the book in this article. The amount of information, tips, and insight I wrote here is just a drop from the sea of what you can find in the book. The next two sections are titled “The Subtle Art of Seduction” and “The Game of Seduction”, with a &lt;em&gt;lot&lt;/em&gt; more insight and tips in them.&lt;/p&gt;
&lt;p&gt;If you’re a UX designer, a designer, a web developer, or any person interested in learning more about human behavior, or interested in making your products more popular or trying to build a stronger online existence, then this is definitely a must-read for you. I cannot recommend this book enough.&lt;/p&gt;
&lt;p&gt;After reading this book, I’m definitely never going to look at applications and UX like I used to, because it gave me a lot of insight, and helped me see and understand the reasons behind successful user experiences, and taught me how I could apply these principles even to a small website, not just a huge application.&lt;/p&gt;
&lt;p&gt;I hope you enjoyed this article and found it useful. Thank you for reading! &lt;/p&gt;


    
  </content>
 </entry>
 
 <entry>
   <title>How to Create Windows-8-like animations with CSS3 and jQuery</title>
   <link href="http://sarasoueidan.com//blog/windows8-animations/"/>
   <id>https://sarasoueidan.com/blog/windows8-animations</id>
   <content type="html">
    &lt;p class=&quot;size-2x&quot;&gt;I have recently realized that CSS3 3D transforms have been out there for quite a long time now and yet I haven't experimented with them yet. I have also been using Windows 8 for a while now, and the first thing that struck me as impressive about it was the transitions and animations built into the dashboard, so I thought it would be really cool if  my first experiment with CSS 3D transforms would be to recreate those animations and effects. So, here goes the tutorial on how I did that.
&lt;/p&gt;

&lt;p class=&quot;note warning&quot;&gt;Please note that this demo works only in &lt;a href=&quot;http://caniuse.com/#feat=transforms3d&quot;&gt;browsers that support&lt;/a&gt; the CSS3 properties used. &lt;/p&gt;

&lt;p class=&quot;note&quot;&gt;For the sake of brevity in the example code, I am using the un-prefixed CSS properties, but you will find the prefixes in the downloadeable source code on Github.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;markup&quot;&gt;The Markup&lt;/h2&gt;

&lt;p&gt;The demo's structure is pretty simple: The dashboard is a list of tiles, of three sizes, small, big, and 2xbig, floated inside 3 columns. And then there are  their corresponding &quot;pages&quot;. A page is an overlay which opens when u click on one of the boxes on the dashboard. It represents an app on desktop, with each of the tiles being the shortcut to that app.&lt;/p&gt;

&lt;p&gt;Each tile will open up a corresponding page. There are two kinds of page transitions included in the Windows 8 dashboard: one that opens the page in a 3D rotating effect from the right of the screen, and one that slides the page in and back to the left. We will define a class name for each type of page as &lt;code&gt; s-page&lt;/code&gt; for the pages that slide from and to the left, and &lt;code&gt; r-page&lt;/code&gt; for pages that rotate open from the right.&lt;/p&gt;

&lt;p&gt;Now, for each tile, we need to specify what type of page it opens (depending on the effect you want for that page). We will define the type of the page for each tile in a custom data attribute called &lt;code&gt; data-page-type&lt;/code&gt;, this will take care of applying the right class names triggering the right animations later on.&lt;/p&gt;

&lt;p&gt;Each page should also have a name. The page name for a certain app will be different from that of another app, so the &quot;Skype&quot; tile opens up a page called &quot;skype-app&quot; for example. I've used only two page names in this example, which are repeated for all tiles, and used a &lt;code&gt; custom-page&lt;/code&gt; name for the last tile for sake of example. You'll probably have to add a different page for each tile, hence a different page name specified in every tile.&lt;/p&gt;

&lt;p&gt;Here's the markup for the whole dashboard (tiles and pages):&lt;/p&gt;

&lt;pre class=&quot;brush:html;&quot; style=&quot;max-height:600px; overflow-y: scroll;&quot;&gt;
            &amp;lt;div class=&quot;demo-wrapper&quot;&amp;gt;
              &amp;lt;!-- classnames for the pages should include: 1) type of page 2) page name--&amp;gt;
               &amp;lt;div class=&quot;s-page random-restored-page&quot;&amp;gt;
                  &amp;lt;div class=&quot;page-content&quot;&amp;gt;
                    &amp;lt;h2 class=&quot;page-title&quot;&amp;gt;Some minimized App&amp;lt;/h2&amp;gt;
                    &amp;lt;div class=&quot;close-button s-close-button&quot;&amp;gt;x&amp;lt;/div&amp;gt;
                  &amp;lt;/div&amp;gt;
                &amp;lt;/div&amp;gt;
                &amp;lt;div class=&quot;s-page custom-page&quot;&amp;gt;
                  &amp;lt;div class=&quot;page-content&quot;&amp;gt;
                    &amp;lt;h2 class=&quot;page-title&quot;&amp;gt;Thank You!&amp;lt;/h2&amp;gt;
                    &amp;lt;div class=&quot;close-button s-close-button&quot;&amp;gt;x&amp;lt;/div&amp;gt;
                  &amp;lt;/div&amp;gt;
                &amp;lt;/div&amp;gt;
                &amp;lt;div class=&quot;r-page random-r-page&quot;&amp;gt;
                  &amp;lt;div class=&quot;page-content&quot;&amp;gt;
                    &amp;lt;h2 class=&quot;page-title&quot;&amp;gt;App Screen&amp;lt;/h2&amp;gt;
                    &amp;lt;p&amp;gt;Chew iPad power cord chew iPad power cord attack feet chase mice leave dead animals as gifts and stick butt in face chew iPad power cord. Chase mice. Run in circles use lap as chair why must they do that. Intrigued by the shower destroy couch leave hair everywhere sleep on keyboard chew iPad power cord. Use lap as chair. Missing until dinner time stand in front of the computer screen, intently sniff hand. Find something else more interesting. Destroy couch play time so inspect anything brought into the house hate dog burrow under covers. Sleep on keyboard destroy couch so hate dog so hide when guests come over. Chase mice destroy couch lick butt throwup on your pillow use lap as chair yet intrigued by the shower but climb leg. Stare at ceiling make muffins or hunt anything that moves claw drapes. Intently sniff hand intrigued by the shower. Why must they do that. Cat snacks leave dead animals as gifts or inspect anything brought into the house sweet beast so stare at ceiling give attitude. Flop over claw drapes but sun bathe lick butt, and chase mice. Rub face on everything lick butt leave hair everywhere lick butt, missing until dinner time for use lap as chair lick butt. Make muffins leave dead animals as gifts play time. Chew foot intrigued by the shower stare at ceiling inspect anything brought into the house yet hopped up on goofballs. 

                    Hunt anything that moves intently sniff hand for hunt anything that moves play time. Chew foot climb leg throwup on your pillow so lick butt yet make muffins hate dog. Intrigued by the shower. Intently sniff hand shake treat bag. Cat snacks burrow under covers make muffins but all of a sudden go crazy find something else more interesting. Flop over chase mice. Give attitude. Inspect anything brought into the house. Stick butt in face sun bathe so find something else more interesting and intrigued by the shower. Rub face on everything use lap as chair. 

                    Under the bed claw drapes chase mice but leave hair everywhere yet make muffins yet claw drapes. Use lap as chair. Find something else more interesting stretch for under the bed. Nap all day intrigued by the shower, hate dog sweet beast intently sniff hand so hate dog nap all day. Swat at dog hide when guests come over and mark territory chase mice for cat snacks. Use lap as chair. Lick butt throwup on your pillow need to chase tail. 

                    Mark territory. Stick butt in face shake treat bag yet hunt anything that moves, yet hopped up on goofballs yet stare at ceiling under the bed. Give attitude chase imaginary bugs stretch so hunt anything that moves so hide when guests come over but intrigued by the shower find something else more interesting. Make muffins behind the couch for chew foot. Sweet beast flop over but throwup on your pillow. Intently sniff hand use lap as chair and missing until dinner time and chase imaginary bugs. 
                    &amp;lt;/p&amp;gt;
                  &amp;lt;/div&amp;gt;
                  
                  &amp;lt;div class=&quot;close-button r-close-button&quot;&amp;gt;x&amp;lt;/div&amp;gt;
                &amp;lt;/div&amp;gt;
              &amp;lt;!--each tile should specify what page type it opens (to determine which animation) and the corresponding page name it should open--&amp;gt;
                &amp;lt;div class=&quot;dashboard clearfix&quot;&amp;gt;
                  &amp;lt;ul class=&quot;tiles&quot;&amp;gt;
                    &amp;lt;div class=&quot;col1 clearfix&quot;&amp;gt;
                      &amp;lt;li class=&quot;tile tile-big tile-1 slideTextUp&quot; data-page-type=&quot;r-page&quot; data-page-name=&quot;random-r-page&quot;&amp;gt;
                        &amp;lt;div&amp;gt;&amp;lt;p&amp;gt;This tile's content slides up&amp;lt;/p&amp;gt;&amp;lt;/div&amp;gt;
                        &amp;lt;div&amp;gt;&amp;lt;p&amp;gt;View all tasks&amp;lt;/p&amp;gt;&amp;lt;/div&amp;gt;
                      &amp;lt;/li&amp;gt;
                      &amp;lt;li class=&quot;tile tile-small tile tile-2 slideTextRight&quot; data-page-type=&quot;s-page&quot; data-page-name =&quot;random-restored-page&quot;&amp;gt;
                        &amp;lt;div&amp;gt;&amp;lt;p class=&quot;icon-arrow-right&quot;&amp;gt;&amp;lt;/p&amp;gt;&amp;lt;/div&amp;gt;
                        &amp;lt;div&amp;gt;&amp;lt;p&amp;gt;Tile's content slides right. Page opens from left&amp;lt;/p&amp;gt;&amp;lt;/div&amp;gt;
                      &amp;lt;/li&amp;gt;
                      &amp;lt;li class=&quot;tile tile-small last tile-3&quot; data-page-type=&quot;r-page&quot; data-page-name=&quot;random-r-page&quot;&amp;gt;
                        &amp;lt;p class=&quot;icon-calendar-alt-fill&quot;&amp;gt;&amp;lt;/p&amp;gt;
                      &amp;lt;/li&amp;gt;
                      &amp;lt;li class=&quot;tile tile-big tile-4&quot; data-page-type=&quot;r-page&quot; data-page-name=&quot;random-r-page&quot;&amp;gt;
                        &amp;lt;figure&amp;gt;
                          &amp;lt;img src=&quot;images/blue.jpg&quot; /&amp;gt;
                          &amp;lt;figcaption class=&quot;tile-caption caption-left&quot;&amp;gt;Slide-out Caption from left&amp;lt;/figcaption&amp;gt;
                        &amp;lt;/figure&amp;gt;
                      &amp;lt;/li&amp;gt;
                    &amp;lt;/div&amp;gt;

                    &amp;lt;div class=&quot;col2 clearfix&quot;&amp;gt;
                      &amp;lt;li class=&quot;tile tile-big tile-5&quot; data-page-type=&quot;r-page&quot; data-page-name=&quot;random-r-page&quot;&amp;gt;
                        &amp;lt;div&amp;gt;&amp;lt;p&amp;gt;&amp;lt;span class=&quot;icon-cloudy&quot;&amp;gt;&amp;lt;/span&amp;gt;Weather&amp;lt;/p&amp;gt;&amp;lt;/div&amp;gt;
                      &amp;lt;/li&amp;gt;
                      &amp;lt;li class=&quot;tile tile-big tile-6 slideTextLeft&quot; data-page-type=&quot;r-page&quot; data-page-name=&quot;random-r-page&quot;&amp;gt;
                        &amp;lt;div&amp;gt;&amp;lt;p&amp;gt;&amp;lt;span class=&quot;icon-skype&quot;&amp;gt;&amp;lt;/span&amp;gt;Skype&amp;lt;/p&amp;gt;&amp;lt;/div&amp;gt;
                        &amp;lt;div&amp;gt;&amp;lt;p&amp;gt;Make a Call&amp;lt;/p&amp;gt;&amp;lt;/div&amp;gt;
                      &amp;lt;/li&amp;gt;
                      &amp;lt;!--Tiles with a 3D effect should have the following structure:
                          1) a container inside the tile with class of .faces
                          2) 2 figure elements, one with class .front and the other with class .back--&amp;gt;
                      &amp;lt;li class=&quot;tile tile-small tile-7 rotate3d rotate3dX&quot; data-page-type=&quot;r-page&quot; data-page-name=&quot;random-r-page&quot;&amp;gt;
                        &amp;lt;div class=&quot;faces&quot;&amp;gt;
                          &amp;lt;div class=&quot;front&quot;&amp;gt;&amp;lt;span class=&quot;icon-picassa&quot;&amp;gt;&amp;lt;/span&amp;gt;&amp;lt;/div&amp;gt;
                          &amp;lt;div class=&quot;back&quot;&amp;gt;&amp;lt;p&amp;gt;Launch Picassa&amp;lt;/p&amp;gt;&amp;lt;/div&amp;gt;
                        &amp;lt;/div&amp;gt;
                      &amp;lt;/li&amp;gt;
                      &amp;lt;li class=&quot;tile tile-small last tile-8 rotate3d rotate3dY&quot; data-page-type=&quot;r-page&quot; data-page-name=&quot;random-r-page&quot;&amp;gt;
                        &amp;lt;div class=&quot;faces&quot;&amp;gt;
                          &amp;lt;div class=&quot;front&quot;&amp;gt;&amp;lt;span class=&quot;icon-instagram&quot;&amp;gt;&amp;lt;/span&amp;gt;&amp;lt;/div&amp;gt;
                          &amp;lt;div class=&quot;back&quot;&amp;gt;&amp;lt;p&amp;gt;Launch Instagram&amp;lt;/p&amp;gt;&amp;lt;/div&amp;gt;
                        &amp;lt;/div&amp;gt;
                      &amp;lt;/li&amp;gt;
                    &amp;lt;/div&amp;gt;

                    &amp;lt;div class=&quot;col3 clearfix&quot;&amp;gt;      
                      &amp;lt;li class=&quot;tile tile-2xbig tile-9&quot; data-page-type=&quot;custom-page&quot; data-page-name=&quot;random-r-page&quot;&amp;gt;
                        &amp;lt;figure&amp;gt;
                          &amp;lt;img src=&quot;images/summer.jpg&quot; /&amp;gt;
                          &amp;lt;figcaption class=&quot;tile-caption caption-bottom&quot;&amp;gt;Fixed Caption: Some Subtitle or Tile Description Goes Here with some kinda link or anything
                          &amp;lt;/figure&amp;gt;
                      &amp;lt;/li&amp;gt;
                      &amp;lt;li class=&quot;tile tile-big tile-10&quot; data-page-type=&quot;s-page&quot; data-page-name=&quot;custom-page&quot;&amp;gt;
                        &amp;lt;div&amp;gt;&amp;lt;p&amp;gt;Windows-8-like Animations with CSS3 &amp;amp; jQuery &amp;copy; Sara Soueidan. Licensed under MIT.&amp;lt;/p&amp;gt;&amp;lt;/div&amp;gt;
                      &amp;lt;/li&amp;gt;
                    &amp;lt;/div&amp;gt;
                  &amp;lt;/ul&amp;gt;
                &amp;lt;/div&amp;gt;&amp;lt;!--end dashboard--&amp;gt;
              &amp;lt;/div&amp;gt;
          &lt;/pre&gt;

&lt;p&gt;The icon font I'm using is from &lt;strong&gt;Icomoon&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;What will happen is that the Javascript will get the name and type of page to be opened when a tile is clicked, and then, according to the type of the page, it will apply specific class names to the page (whose name we have also retrieved from the &lt;code&gt;data-page-name&lt;/code&gt; attribute) to open it with the specified type of animation for each class applied.&lt;/p&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;css&quot;&gt;The CSS&lt;/h2&gt;

&lt;p class=&quot;note&quot;&gt;Please note that I'm taking a mobile-first approach to the styles, which we'll then make responsive in the media queries section.&lt;/p&gt;

&lt;p&gt;First, the styles for the demo wrapper, the container in which the whole demo will be contained. We'll define general styles, and make sure to set a &lt;code&gt;perspective&lt;/code&gt; to activate the 3D space, otherwise, the whole demo will look flat and two dimensional.&lt;/p&gt;

&lt;pre class=&quot;brush:css;&quot;&gt;
            .demo-wrapper {
              padding: 2em .5em;
              width: 100%;
              height:100%;
              perspective: 3300px;
              position: relative;
            }
          &lt;/pre&gt;

&lt;p&gt;Now let's start with the dashboard styles and animations.&lt;/p&gt;

&lt;p&gt;The first animation applied to the dashboard is fired when the page loads. The dashboard is initially hidden and translated to the right of the screen, and fades and translates in to position on page load.&lt;/p&gt;

&lt;pre class=&quot;brush:css&quot;&gt;
            .dashboard {
              margin: 0 auto;
              width: 100%;
              padding: 1em;
              transform: translateX(200px);
              opacity:0;
              animation: start 1s ease-out forwards;
            }
            @keyframes start{
              0%{
                transform: translateX(200px);
                opacity:0;
              }
              50%{
                opacity:1;
              }
              100%{
                transform: translateX(0);
                opacity:1;
              }
            }
          &lt;/pre&gt;
&lt;p&gt;The dashboard also fades into the view and fades back when a tile is clicked. Once a tile is clicked, the dashboard translates back along the z-axis, decreases in size, and fades its opacity gradually till it becomes 0. And when an opened page is closed, the dashboard fades back into the view.&lt;/p&gt;

&lt;p&gt;The three columns in the dashboard fade in one after the other, with a slight delay between them. When a page is closed, a class name is added to each column (via Javascript), and each of these classes calls the animation with a certain delay.&lt;/p&gt;

&lt;p&gt;Here are the classes and the animations applied to the dashboard upon clicking the tiles and closing the pages:&lt;/p&gt;

&lt;pre class=&quot;brush:css;&quot;&gt;
            .fadeOutback{
             animation: fadeOutBack 0.3s ease-out 1 normal forwards;
            }
            .fadeInForward-1, .fadeInForward-2, .fadeInForward-3 {
              /*remember: in the second animation u have to set the final values reached by the first one*/
              opacity:0;
              transform: translateZ(-5em) scale(0.75);
              animation: fadeInForward .5s cubic-bezier(.03,.93,.43,.77) .4s normal forwards;
            }
        
            .fadeInForward-2{
              animation-delay: .55s;
            }
            .fadeInForward-3{
              animation-delay: .7s;
            }
        
            @keyframes fadeOutBack{
              0% {transform: translateX(-2em) scale(1); opacity:1;}
              70% {transform: translateZ(-5em) scale(0.6); opacity:0.5;}
              95% {transform: translateZ(-5em) scale(0.6); opacity:0.5;}
              100% {transform: translateZ(-5em) scale(0); opacity:0;}
            }
            @keyframes fadeInForward{
              0% {transform: translateZ(-5em) scale(0); opacity:0;}
              100% {transform: translateZ(0) scale(1); opacity:1;}
            }
          &lt;/pre&gt;
&lt;p&gt;Now we're going to style the pages.&lt;/p&gt;

&lt;pre class=&quot;brush:css&quot;&gt;
            .r-page {
              width: 100%;
              height: 100%;
              text-align: center;
              font-size: 2em;
              font-weight: 300;
              position: absolute;
              right: 0;
              top: 0;
              left:0;
              bottom:0;
              opacity: 0;
              color: white;
              z-index: 10;
              padding:10px;
              transform-origin: 100% 0%;
              transform: rotateY(-90deg) translateZ(5em)
            }

            .s-page {
              color: white;
              z-index: 10;
              text-align: center;
              font-size: 2em;
              font-weight: 300;
            }
            .page-content{
              overflow-y:auto;
              max-height:100%;
              font-size:.6em;
              padding:.6em;
              text-align:left;
            }

            .s-page, .r-page{
              background-color: white;
              color:black;
            }

            .page-title {
              margin: .25em 0;
              font-weight: 100;
              font-size: 3em;
              text-align:center;
            }

            .close-button {
              font-size: 1.5em;
              width: 1em;
              height: 1em;
              position: absolute;
              top: .75em;
              right: .75em;
              cursor: pointer;
              line-height: .8em;
              text-align: center
            }

          &lt;/pre&gt;
&lt;p&gt;I've set the original position of each &lt;code&gt;r-page&lt;/code&gt; in the 3D space by first rotating it about the y-axis (the vertical axis), then I moved the page 5em to the left of the screen by using &lt;code&gt;translateZ&lt;/code&gt;. Always remember: when u transform an element in 3D, you transform its coordinate system along with it. What I wanted to do is move the page 5em to the left of the screen, but instead of using &lt;code&gt;translateX&lt;/code&gt; I used &lt;code&gt;translateZ&lt;/code&gt;, because after the first tranformation (rotation about y axis), the coordinate system was also rotated, so now the z-axis points towards the left, and the x-axis is pointing towards you, the viewer.&lt;/p&gt;

&lt;p&gt;All the pages, except the &amp;lt;/code&amp;gt;s-page&amp;lt;/code&amp;gt; app page, have the same initial position in the 3D space. The &lt;code&gt;s-page&lt;/code&gt;s, on the other hand, are positioned -150% left of the screen off canvas, so that they slide into view when their animation is fired.&lt;/p&gt;

&lt;p&gt;
Once the tile for each page is clicked, a corresponding class is added (via javascript) to the page that will open, and each class calls for a certain animation. So, each page will get a class name that will define the 3D effect for the page.
&lt;/p&gt;

&lt;p&gt;These are the class names that trigger the opening and closing of the pages, along with the animations defined for each class.&lt;/p&gt;

&lt;pre class=&quot;brush:css;&quot;&gt;
            /* opens the r-page type pages*/
            .openpage{ 
              animation: rotatePageInFromRight 1s cubic-bezier(.66,.04,.36,1.03) 1 normal forwards;
            }
            /* closes the r-page type pages */
            .slidePageLeft{
              transform: rotateY(0) translateZ(0) ; opacity:1;
              animation:slidePageLeft .8s ease-out 1 normal forwards;
            }
            /* opens the s-page type pages */
            .slidePageInFromLeft{
              animation: slidePageInFromLeft .8s cubic-bezier(.01,1,.22,.99) 1 0.25s normal forwards;
            }
            /* closes the s-page type pages*/
            .slidePageBackLeft{
              opacity:1; 
              left:0;
              animation: slidePageBackLeft .8s ease-out 1 normal forwards;
            }
          &lt;/pre&gt;
&lt;p&gt;I'm using the &lt;code&gt;animation&lt;/code&gt; &lt;a href=&quot;http://www.w3.org/TR/css3-animations/#animation-shorthand-property&quot;&gt;shorthand property&lt;/a&gt; here. The last value &lt;code&gt;forwards&lt;/code&gt; corresponds to the &lt;code&gt;animation-fill-mode&lt;/code&gt; property, which must be set to &lt;code&gt;forwards&lt;/code&gt;, otherwise the page will get back to its initial &quot;closed&quot; position after the animation is over. So, in order to keep the page open, and be able to create sequential animations, the element has to stay in the final state defined by the first animation, and from there start the second animation.&lt;/p&gt;

&lt;p&gt;These are the animations for the classes applied to the pages:&lt;/p&gt;

&lt;pre class=&quot;brush:css;&quot;&gt;
          @keyframes rotatePageInFromRight{
            0% {transform:rotateY(-90deg) translateZ(5em);opacity:0}
            30% {opacity:1}
            100% {transform: rotateY(0deg) translateZ(0) ; opacity:1}
          }
        
          /*When the close-button is clicked, the page slides to the left*/
          /*note that the start of the second animation is the same state as the
          end of the previous one*/
        
          @keyframes slidePageLeft{
            0% {left:0; transform: rotateY(0deg) translateZ(0) ; opacity:1}
            70% {opacity:1;}
            100% {opacity:0; left:-150%; transform: rotateY(0deg)}
          }
          @keyframes slidePageInFromLeft{
            0% {opacity:0; }
            30% {opacity:1}
            100% {opacity:1; left:0;}
          }
          @keyframes slidePageBackLeft{
            0% {opacity:1; left:0; transform: scale(0.95);}
            10% {transform: scale(0.9);}
            70% {opacity:1;}
            100% {opacity:0; left:-150%;}
          }
          &lt;/pre&gt;
&lt;p&gt;Last but not least we'll style the dashboard tiles and define the transitions and animations applied to them when they are hovered.&lt;/p&gt;

&lt;p&gt;General styles defining the size of the tiles:&lt;/p&gt;

&lt;pre class=&quot;brush:css;&quot;&gt;
            .tile{
              float: left;
              margin: 0 auto 1%;
              color: white;
              font-size: 1.3em;
              text-align: center;
              height: 8em;
              font-weight: 300;
              overflow: hidden;
              cursor: pointer;
              position:relative;
              background-color: #fff;
              color: #333;
              position:relative;
              transition: background-color 0.2s ease-out
            }
            .tile-2xbig{
              height:16.15em;
              width:100%;
            }
            .tile-big {
              width: 100%
            }
            .tile-small {
              width: 49%;
              margin-right: 2%
            }
            .tile-small.last {
              margin-right: 0
            }
          &lt;/pre&gt;
&lt;p&gt;A couple of tiles contain an image along with an image caption. These tiles will get a class &lt;code&gt;fig-tile&lt;/code&gt; to determine their type in the Javascript code. The colors used for the text and background of the corresponding page will be retrieved from the colors of the caption, so don't forget to define them.The caption can be either fixed, or it can slide in when the tile is hovered:&lt;/p&gt;

&lt;pre class=&quot;brush:css;&quot;&gt;
            .tile-caption{
              position:absolute;
              z-index:1;
              background-color: #455962;
              color:#fff;
              font-size:1em;
              padding:1em;
              text-align: left;
            }
            .caption-bottom{
              left:0;
              bottom:0;
              right:0;
              height:40%;
            }
            .caption-left{
              left:-100%;
              top:0;
              bottom:0;
              width:40%;
              transition: left .3s linear;
            }
            .tile:hover .caption-left{
              left:0;
            }
          &lt;/pre&gt;
&lt;p&gt;Regular tiles, with no special kind of animation, will change their background and text color on hover. In order to make sure the text is vertically centered in each tile, each one will contain a &lt;code&gt;div&lt;/code&gt; with a paragraph containing the text. We'll use the table-cell display property to center this text vertically.&lt;/p&gt;

&lt;pre class=&quot;brush:css;&quot;&gt;
            .tile div{
              position:absolute;
              top:0; left:0; right:0; bottom:0;
              width:100%;
              height:100%;
              text-align:center;
              display:table;
              padding:0 1em;
              transition: all .3s ease;
            }
            .tile div p{
              display:table-cell;
              vertical-align:middle;
            }
          &lt;/pre&gt;
&lt;p&gt;I'll skip the general styles of the tiles for sake of brevity, but make sure you set a background and text color on all tiles, even the ones that will be covered by an image, because these colors will be retrieved via Javascript and set as the colors for the corresponding page of this tile. Let's move on to the animations and transitions on the tiles.&lt;/p&gt;

&lt;p&gt;Tiles with text sliding inside of them will contain two &lt;code&gt;div&lt;/code&gt;s, each div will be like a &quot;face&quot; or a block inside the tile. These &lt;code&gt;div&lt;/code&gt;s are positioned absolutely, and moved on hover according to the direction of slide we want.&lt;/p&gt;

&lt;p&gt;For a tile's text to slide up on hover, we'll apply a class &lt;code&gt;slideTextUp&lt;/code&gt;.&lt;/p&gt;

&lt;pre class=&quot;brush:css;&quot;&gt;
            /* slide text inside tile up */
            /* 2nd div will be positioned below the bottom of the tile*/
            .slideTextUp div:nth-child(2){
              top:100%;
            }
            /*both divs will be translated up on hover*/
            .slideTextUp:hover div{
              transform: translateY(-100%);
            }
            .tile-1 p{
              font-size:1.3em;
            }
          &lt;/pre&gt;

&lt;p&gt;Similarly, tiles with text sliding to the right and left, will get class names &lt;code&gt;slideTextLeft&lt;/code&gt; and &lt;code&gt;slideTextRight&lt;/code&gt;, with a similar structure as the above tile.&lt;/p&gt;

&lt;pre class=&quot;brush:css;&quot;&gt;
            /* slide text inside tile to the right*/
            .slideTextRight div:first-child{
              left:-100%;
            }
            .slideTextRight:hover div{
              transform: translateX(100%);
            } 

            /* slide text inside tile to the left */
            .slideTextLeft div:nth-child(2){
              left:100%;
            }
            .slideTextLeft:hover div{
              transform: translateX(-100%);
            } 
          &lt;/pre&gt;
&lt;p&gt;A couple tiles have a different hover effect, they rotate to reveal the back face of the tile. This effect is a very simple and basic &quot;card flip&quot; effect. I won't get into the details of this effect, but if you're new to this, you can read more about it in &lt;a href=&quot;http://desandro.github.io/3dtransforms/docs/card-flip.html&quot;&gt;this excellent tutorial&lt;/a&gt; by David De Sandro.&lt;/p&gt;

&lt;p&gt;For this flipping effect, apply a &lt;code&gt;rotate3d&lt;/code&gt; class to the tile you want to flip. For a card with a vertical flip we'll add a class &lt;code&gt;rotate3dY&lt;/code&gt;, and for a horizontal flip we'll apply a class &lt;code&gt;rotate3dX&lt;/code&gt; (in addition to the &lt;code&gt;rotate3d&lt;/code&gt; class), and we'll apply the following styles: &lt;/p&gt;

&lt;pre class=&quot;brush:css;&quot;&gt;
            /* rotate tile in 3D*/
            .rotate3d{
              /* add a perspective to the tile to activate 3d space inside it*/
              perspective: 800px;
              overflow: visible;
            }
            .faces{
              /* preserve the 3d space in the container wrapping the two &quot;faces&quot; and define a transition */
              transform-style: preserve-3d;
              transition: transform 1s;
            }
            .faces div {
              /* position faces on top of each other */
              display: block;
              position: absolute;
              top:0; left:0; right:0; bottom:0;
              width: 100%;
              height: 100%;
              /* hide backface visibility so that the back of the face is hidden when it's rotated */
              backface-visibility: hidden;
            }
          &lt;/pre&gt;
&lt;p&gt;We'll rotate one of the faces in the tiles so that the two faces are back to back.&lt;/p&gt;

&lt;pre class=&quot;brush:css;&quot;&gt;
            .rotate3dY .back{
              transform: rotateY( 180deg );
            }
            .rotate3dX .back{
              transform: rotateX( 180deg );
            }
          &lt;/pre&gt;
&lt;p&gt;And when the tile is hovered, the &lt;code&gt;.faces&lt;/code&gt; &lt;code&gt;div&lt;/code&gt; will be rotated to reveal the back face. &lt;/p&gt;

&lt;pre class=&quot;brush:css;&quot;&gt;
            .rotate3dY:hover .faces:hover{
                transform: rotateY( 180deg );
            }
            .rotate3dX:hover .faces:hover{
                transform: rotateX( 180deg );
            }
          &lt;/pre&gt;
&lt;p&gt;For styles rotating in 3D, remember to set a background and text color for the &lt;code&gt;.front&lt;/code&gt; face, so that these colors be retrieved and set to the tile's page when it is opened.&lt;p&gt;

&lt;p&gt;And that's all for the styles and animations!&lt;/p&gt;

&lt;p&gt;Now let's define responsive styles for the dashboard. Dashboard columns are initially full-width on small screens (remember we're starting mobile-first), and they will be floated next to each other on big screens.&lt;/p&gt;

&lt;pre class=&quot;brush:css;&quot;&gt;
            .col1,
            .col2,
            .col3 {
              width: 99%;
              margin: 1em auto
            }
            @media screen and (min-width: 43.75em) {
              .col1,
              .col2,
              .col3 {
                float: left;
                margin-right: 1%;
                width: 49%
              }
              .page-title{
                font-size:2.5em;
              }
              .page-content{
                font-size:1em;
              }
              .close-button{
                font-size:2em;
              }
            }

            @media screen and (min-width: 64em) {
              .col1,
              .col2,
              .col3 {
                float: left;
                margin-right: .5%;
                width: 31%
              }

              .col3 {
                margin-right: 0
              }

              .col1 {
                margin-left: 2em
              }
              .page-title{
                font-size:3.5em;
              }
            }
          &lt;/pre&gt;

&lt;h2 class=&quot;deeplink&quot; id=&quot;javascript&quot;&gt;The Javascript&lt;/h2&gt; 

&lt;p&gt;All click events will be handled wtih Javascript. I'll be using jQuery for this example. Event handlers are going to be set on each of the dashboard tiles, and when a click event is detected, we're going to retrieve the name and type of the corresponding page from the &lt;code&gt;data-page-type&lt;/code&gt; and &lt;code&gt;data-page-name&lt;/code&gt; attributes, and use these to open the page.&lt;/p&gt;

&lt;p&gt;Other click events will be handled when clicking on the close button in each page. The close button for each page type will apply the suitable class names to close this page type.&lt;/p&gt;

&lt;p&gt;Additionally, in order to give each page the same background color and text color as its corresponding tile, we will first loop through the tiles, retrieve its colors, and then applies it to its corresponsing page. If a tile has a &lt;code&gt;rotate3d&lt;/code&gt; class, it looks for the background-color of the front &quot;face&quot; of the tile, and applies that to the page.&lt;/p&gt;

&lt;pre class=&quot;brush:javascript;&quot;&gt;
            (function(){
                //get the background-color for each tile and apply it as background color for the cooresponding screen
                $('.tile').each(function(){
                    var $this= $(this),
                        page = $this.data('page-name'),
                        bgcolor = $this.css('background-color'),
                        textColor = $this.css('color');
                    //if the tile rotates, we'll use the colors of the front face
                        if($this.hasClass('rotate3d')) {
                          frontface = $this.find('.front');
                          bgcolor = frontface.css('background-color');
                          textColor = frontface.css('color');
                        }
                    //if the tile has an image and a caption, we'll use the caption styles
                        if($this.hasClass('fig-tile')) {
                          caption = $this.find('figcaption');
                          bgcolor = caption.css('background-color');
                          textColor = caption.css('color');
                        }

                    $this.on('click',function(){
                      $('.'+page).css({'background-color': bgcolor, 'color': textColor})
                                 .find('.close-button').css({'background-color': textColor, 'color': bgcolor});
                    });
                });

                function showDashBoard(){
                  for(var i = 1; i &amp;lt;= 3; i++) {
                    $('.col'+i).each(function(){
                        $(this).addClass('fadeInForward-'+i).removeClass('fadeOutback');
                    });
                  }
                }

                function fadeDashBoard(){
                  for(var i = 1; i &amp;lt;= 3; i++) {
                    $('.col'+i).addClass('fadeOutback').removeClass('fadeInForward-'+i);
                  }
                }
              
                
              //listen for when a tile is clicked
              //retrieve the type of page it opens from its data attribute
              //based on the type of page, add corresponding class to page and fade the dashboard
              $('.tile').each(function(){
                var $this= $(this),
                    pageType = $this.data('page-type'),
                    page = $this.data('page-name');
                    
                $this.on('click',function(){
                  if(pageType === &quot;s-page&quot;){
                      fadeDashBoard();
                      $('.'+page).addClass('slidePageInFromLeft').removeClass('slidePageBackLeft');
                    }
                    else{
                      $('.'+page).addClass('openpage');
                      fadeDashBoard();
                    }
                });
              });

              //when a close button is clicked:
              //close the page
              //wait till the page is closed and fade dashboard back in
              $('.r-close-button').click(function(){
                  $(this).parent().addClass('slidePageLeft')
                      .one('webkitAnimationEnd oanimationend msAnimationEnd animationend', function(e) {
                            $(this).removeClass('slidePageLeft').removeClass('openpage');
                          });
                  showDashBoard();
              });
              $('.s-close-button').click(function(){
                  $(this).parent().removeClass('slidePageInFromLeft').addClass('slidePageBackLeft');
                  showDashBoard();
              });

            })();
          &lt;/pre&gt;
&lt;p&gt;And that's it!&lt;br /&gt; I hope you enjoyed this tutorial and found it useful! :)&lt;/p&gt;




&lt;/p&gt;&lt;/p&gt;

    
  </content>
 </entry>
 

</feed>