<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Niels den Dekker</title>
    <description>The latest articles on DEV Community by Niels den Dekker (@nielsdendekker).</description>
    <link>https://dev.to/nielsdendekker</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F360790%2F895ca0f4-5a03-4976-aade-676c99a1c8f7.jpg</url>
      <title>DEV Community: Niels den Dekker</title>
      <link>https://dev.to/nielsdendekker</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nielsdendekker"/>
    <language>en</language>
    <item>
      <title>HTML5 Best Practices: Header Element</title>
      <dc:creator>Niels den Dekker</dc:creator>
      <pubDate>Mon, 18 May 2020 07:07:33 +0000</pubDate>
      <link>https://dev.to/nielsdendekker/html5-best-practices-header-element-4a45</link>
      <guid>https://dev.to/nielsdendekker/html5-best-practices-header-element-4a45</guid>
      <description>&lt;p&gt;Let's take a dive into one of the most commonly used and misunderstood elements introduced in HTML5 - the &lt;code&gt;&amp;lt;header&amp;gt;&lt;/code&gt; element. Although it appears a new simple element to use, there's much more behind the scenes that we need to uncover, before we start using &lt;br&gt;
the &lt;code&gt;&amp;lt;header&amp;gt;&lt;/code&gt; element.&lt;/p&gt;
&lt;h3&gt;
  
  
  Before the header element
&lt;/h3&gt;

&lt;p&gt;Here's what we used to do to create a header which might contain things such as logos and titles:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"header"&lt;/span&gt; &lt;span class="na"&gt;role=&lt;/span&gt;&lt;span class="s"&gt;"banner"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Ultimate Courses&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;HTML5 has introduced a new way to define a header, that doesn't involve a generic looking &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; that isn't semantic or accessible by default - you can see to make things accessible we would have to use a &lt;code&gt;role="banner"&lt;/code&gt; attribute to provide further description.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;🕵️‍♂️ Checkout the HTML5 &lt;a href="https://html.spec.whatwg.org/multipage/sections.html#the-header-element"&gt;Header element spec&lt;/a&gt; from WHATWG - who are a community that maintain and evolve the HTML specification!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;With the rise of accessible technologies and the need for correctly implementing standards - we turn to the &lt;code&gt;header&lt;/code&gt; element as our saviour, where we also find out that we no longer need to use the &lt;code&gt;role="banner"&lt;/code&gt; attribute (which we'll come on to shortly).&lt;/p&gt;

&lt;h3&gt;
  
  
  What is the Header Element?
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;&amp;lt;header&amp;gt;&lt;/code&gt; element is a new semantic element that we can use to create better meaning behind our code. &lt;/p&gt;

&lt;p&gt;By using &lt;code&gt;&amp;lt;header&amp;gt;&lt;/code&gt; you no longer need to use the &lt;code&gt;role="banner"&lt;/code&gt; as it's inferred under the hood.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;🕵️‍♂️ Find out more about the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/Banner_role"&gt;ARIA banner role&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Header was created to group together the introduction of a website or introduce specific content, for instance a company name or an article title.&lt;/p&gt;

&lt;p&gt;Lets make the move from a &lt;code&gt;div&lt;/code&gt; to a &lt;code&gt;header&lt;/code&gt; element:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;header&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Ultimate Courses&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/header&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Easy enough right? We've now transformed our div into a brand new header!&lt;/p&gt;

&lt;p&gt;Let's investigate further and uncover some best practices.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using the Header Element
&lt;/h3&gt;

&lt;p&gt;Every element created in HTML comes with a set of placement rules, which means that for validation and accessibility reasons the &lt;code&gt;&amp;lt;header&amp;gt;&lt;/code&gt; element can't be used anywhere we want - we must be careful not to create invalid code by using it incorrectly.&lt;/p&gt;

&lt;p&gt;For instance, we cannot put a &lt;code&gt;&amp;lt;header&amp;gt;&lt;/code&gt; element inside an &lt;code&gt;&amp;lt;address&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;footer&amp;gt;&lt;/code&gt; or another &lt;code&gt;&amp;lt;header&amp;gt;&lt;/code&gt; element.&lt;/p&gt;

&lt;p&gt;Here's how a typical real-world example of a header would look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"hero"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;header&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"hero-header"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"hero-logo"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Ultimate Courses&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;nav&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"hero-nav"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Home&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/nav&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/header&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;article&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"article"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;...&lt;span class="nt"&gt;&amp;lt;/article&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is a perfectly valid example and it reflects the purpose of the &lt;code&gt;&amp;lt;header&amp;gt;&lt;/code&gt; element. As you can see there's not really a huge change from using a &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; as we can still use our &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;nav&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt; elements inside.&lt;/p&gt;

&lt;h4&gt;
  
  
  Other use-cases for header
&lt;/h4&gt;

&lt;p&gt;In the above example we looked at a standalone &lt;code&gt;&amp;lt;header&amp;gt;&lt;/code&gt; element, you can also see an &lt;code&gt;&amp;lt;article&amp;gt;&lt;/code&gt; element which is another great use case for introducing a &lt;code&gt;&amp;lt;header&amp;gt;&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;article&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"article"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;header&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"article-header"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;My blog title&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Niels den Dekker, &lt;span class="nt"&gt;&amp;lt;time&amp;gt;&lt;/span&gt;May 15, 2020&lt;span class="nt"&gt;&amp;lt;/time&amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/header&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Your article text...&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;h2&amp;gt;&lt;/span&gt;The next title&lt;span class="nt"&gt;&amp;lt;/h2&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/article&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is why developers struggle with the &lt;code&gt;&amp;lt;header&amp;gt;&lt;/code&gt; element, as it can be used in multiple places in your website - and not just to wrap a logo as it's commonly assumed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Header element and accessibility
&lt;/h3&gt;

&lt;p&gt;Let's talk accessibility - which is often an afterthought. Here is how the &lt;code&gt;&amp;lt;header&amp;gt;&lt;/code&gt; element is interpreted:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;&amp;lt;header&amp;gt;&lt;/code&gt; element specified at the top of your HTML - the one closest to our &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt; element - is always interpreted as the introduction of the webpage.&lt;/li&gt;
&lt;li&gt;If the &lt;code&gt;&amp;lt;header&amp;gt;&lt;/code&gt; element is being used as a child of either a &lt;code&gt;&amp;lt;aside&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;article&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;main&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;nav&amp;gt;&lt;/code&gt;, or &lt;code&gt;&amp;lt;section&amp;gt;&lt;/code&gt; it becomes a more semantic content wrapper.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here's how we might use both examples together in a real-world use case:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"hero"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;header&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"hero-header"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt; &lt;span class="c"&gt;&amp;lt;!-- closest to body - page header --&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"hero-logo"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Ultimate Courses&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;nav&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"hero-nav"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Home&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;/nav&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/header&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;article&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"article"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;header&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"article-header"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;My blog title&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Niels den Dekker, &lt;span class="nt"&gt;&amp;lt;time&amp;gt;&lt;/span&gt;May 15, 2020&lt;span class="nt"&gt;&amp;lt;/time&amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;/header&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Your article text...&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;h2&amp;gt;&lt;/span&gt;The next title&lt;span class="nt"&gt;&amp;lt;/h2&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/article&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The first header represents the introduction to the webpage, whereas the second one simply represents the introduction to the article.&lt;/p&gt;

&lt;h3&gt;
  
  
  Legacy browser support
&lt;/h3&gt;

&lt;p&gt;Because &lt;code&gt;&amp;lt;header&amp;gt;&lt;/code&gt; was introduced in HTML5, there are older browsers that exist that have no idea what &lt;code&gt;&amp;lt;header&amp;gt;&lt;/code&gt; means.&lt;/p&gt;

&lt;p&gt;For supporting browsers every &lt;code&gt;&amp;lt;header&amp;gt;&lt;/code&gt; element is &lt;code&gt;display: block&lt;/code&gt;, which means older browsers won't have &lt;code&gt;display: block&lt;/code&gt; built-in - which leads to very concerning style bugs.&lt;/p&gt;

&lt;p&gt;We’d need to help older browsers out by adding our own &lt;code&gt;display: block&lt;/code&gt; to our stylesheet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="c1"&gt;// style.css&lt;/span&gt;
&lt;span class="nt"&gt;header&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;block&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now we can use our new HTML5 Header element in all browsers without causing any layout bugs.&lt;/p&gt;

&lt;p&gt;This is more of a hack due to the way unrecognized elements can still be styled with CSS.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;🕵️‍♂️ Find out more about the &lt;a href="https://caniuse.com/#search=%3Cheader%3E"&gt;browser compatibility&lt;/a&gt; with the header element!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;Now you're primed with essential knowledge on the &lt;code&gt;&amp;lt;header&amp;gt;&lt;/code&gt; element and when to use it correctly.&lt;/p&gt;

&lt;p&gt;We've also learned about semantic elements and the importance of accessibility, through our journey from &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; to &lt;code&gt;&amp;lt;header&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you are serious about your HTML and CSS skills, your next step is to take a look at our &lt;a href="https://dev.to/learn/html-css-basics"&gt;HTML + CSS Basics course&lt;/a&gt; that will teach you the full language basics in detail as well as many use cases you'll need in daily front-end development!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Thanks for reading and happy semantic coding!&lt;/p&gt;

</description>
      <category>html</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>semantic</category>
    </item>
    <item>
      <title>A guide to Responsive images with srcset</title>
      <dc:creator>Niels den Dekker</dc:creator>
      <pubDate>Sat, 04 Apr 2020 17:21:56 +0000</pubDate>
      <link>https://dev.to/nielsdendekker/a-guide-to-responsive-images-with-srcset-ka4</link>
      <guid>https://dev.to/nielsdendekker/a-guide-to-responsive-images-with-srcset-ka4</guid>
      <description>&lt;p&gt;High resolution images are something we all want. Whether it's on a mobile phone, a desktop or a laptop - no one wants to look at a blurry image. There are multiple ways to achieve this, but today we're going to look at a solution simple to achieve with HTML. Let's have a look at the &lt;code&gt;srcset&lt;/code&gt; attribute.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;srcset&lt;/code&gt; attribute gives you the option to specify multiple images and let the browser decide what the best image is.&lt;/p&gt;

&lt;p&gt;Let's start with something simple:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/photo-small.jpg"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is a normal image include which can be viewed on a normal laptop or for example a MacBook with a retina display. To get a high resolution version on a retina display we'd have to use media queries in CSS.&lt;/p&gt;

&lt;p&gt;This would be your HTML:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"low-res"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/photo-small.jpg"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"high-res"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/photo-large.jpg"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As we mentioned, we'd need some CSS magic to let the browser know - hey, this is what I want to use when a user is visiting the website on a retina display.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.high-res&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;none&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;@media&lt;/span&gt; 
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;-webkit-min-device-pixel-ratio&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; 
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;min-resolution&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;192dpi&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nc"&gt;.low-res&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;none&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nc"&gt;.high-res&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;block&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now this is all super fancy, but there's a much simpler way of doing this. And we can do it with pure HTML!&lt;/p&gt;

&lt;h4&gt;
  
  
  The srcset attribute
&lt;/h4&gt;

&lt;p&gt;This is where our new &lt;code&gt;srcset&lt;/code&gt; attribute comes in. We’re basically moving all our CSS rules over to just one simple line, which would look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/photo.jpg"&lt;/span&gt; &lt;span class="na"&gt;srcset=&lt;/span&gt;&lt;span class="s"&gt;"/photo@2x.jpg 2x"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let's start dissecting what I’ve just shown you to get a better understanding of what’s going on in our new &lt;code&gt;srcset&lt;/code&gt; attribute.&lt;/p&gt;

&lt;h4&gt;
  
  
  Conventions
&lt;/h4&gt;

&lt;p&gt;As you can see I’ve used a rather weird name for &lt;code&gt;photo.jpg&lt;/code&gt; which I changed to &lt;code&gt;photo@2x.jpg&lt;/code&gt;. This is simply our original file, but exported twice the size. That’s why I have postfixed it with &lt;code&gt;@2x&lt;/code&gt;. We’ve used an example with just 1 specific image size in &lt;code&gt;srcset&lt;/code&gt;, but there could be 4 for example - which we will get on to later!&lt;/p&gt;

&lt;p&gt;Here’s a quick example of how I would manage my files in &lt;code&gt;srcset&lt;/code&gt; and make sure they’re making sense for everyone else who’s also going to work with your images and HTML.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/photo.jpg"&lt;/span&gt; &lt;span class="na"&gt;srcset=&lt;/span&gt;&lt;span class="s"&gt;"/photo@2x.jpg 2x, /photo@1000w.jpg 1000w"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Note: we’re using widths here because srcset only works based on width or pixel density (1x, 2x, etc). You can’t specify an image based on a certain height of the screen.&lt;/p&gt;

&lt;h4&gt;
  
  
  Browser calculations
&lt;/h4&gt;

&lt;p&gt;Before we go into all the specifics about different ways to specify your image widths. Let's have a look at how your browser actually picks the image.&lt;/p&gt;

&lt;p&gt;Your browser attempts to calculate the best fit image based on the device width. For regular devices the ratio it is looking for is 1. If the device is considered “retina" then the browser will multiply the result by 2 to find the best image size.&lt;/p&gt;

&lt;p&gt;Multiply what you might ask? We’ve got a set of images of 500px, 1250px and 2500px.&lt;/p&gt;

&lt;p&gt;The device we’re using has a width of 1000px, now let’s look at the calculations the browser makes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;500 / 1000 &lt;span class="o"&gt;=&lt;/span&gt; 0.5
1250 / 1000 &lt;span class="o"&gt;=&lt;/span&gt; 1.25
2500 / 1000 &lt;span class="o"&gt;=&lt;/span&gt; 2.5
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As we mentioned earlier, on non-retina devices the browser will try and match a ratio of 1. This means for this device it will pick the image of 1250px as it’s the closest to 1.&lt;/p&gt;

&lt;p&gt;A retina display has a ratio of 2. This is where our multiply comes in. The browser will look at 1.25 and go, this image will be too small and will lose quality. It will then multiply 1.25 by 2 and see if there’s anything available.&lt;/p&gt;

&lt;p&gt;There’s a 2500px image with a ratio of 2.5 which matches what the browser is looking for so it will automatically load in that image for optimum result.&lt;/p&gt;

&lt;h4&gt;
  
  
  The srcset attribute
&lt;/h4&gt;

&lt;p&gt;Now we’ve talked about conventions and how the browser picks the image, let’s have a look at how the &lt;code&gt;srcset&lt;/code&gt; attribute is made up and how we can declare the images.&lt;/p&gt;

&lt;p&gt;Note: source set takes 2 values and is separated by a comma after each declaration.&lt;/p&gt;

&lt;p&gt;Let’s have a look:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/photo.jpg"&lt;/span&gt; &lt;span class="na"&gt;srcset=&lt;/span&gt;&lt;span class="s"&gt;"/photo@1000w.jpg 1000w, /photo@2x.jpg 2x, /photo@3x.jpg 3x"&lt;/span&gt; &lt;span class="na"&gt;alt=&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can see that every line is made up out of a file you wish to load in and after that a space with the width of the image’ real size. In this case, we’ve got an image with a width of 1000px. So we say when the width (w) of the screen is 1000px, load in that image.&lt;/p&gt;

&lt;p&gt;Now there’s another attribute which I’ve not discussed out there that works together with our sourceset. This is the &lt;code&gt;sizes&lt;/code&gt; attribute. Now when sizes has not been specified, it’s default value is always &lt;code&gt;100vw&lt;/code&gt; (100% of your device viewport).&lt;/p&gt;

&lt;p&gt;Next one in line is &lt;code&gt;2x&lt;/code&gt;. We’ve discussed this a bit already but it’s the device-pixel ratio we’re specifying here. When the device has 2x the amount of pixels than a regular 1x device, it will automatically grab the image specified and load it in.&lt;/p&gt;

&lt;p&gt;You all can guess what &lt;code&gt;3x&lt;/code&gt; is going to do! As you can see &lt;code&gt;srcset&lt;/code&gt; is quite a powerful tool to make your images responsive and you didn’t need a single line of CSS for it.&lt;/p&gt;

&lt;h4&gt;
  
  
  Conclusion
&lt;/h4&gt;

&lt;p&gt;If you’re a developer who’s looking for an easy way to specify your responsive images, source set is the way to go! There are loads of frameworks out there who would make the use of source sets an even bigger asset. Think about how dynamic it could be.&lt;/p&gt;

&lt;p&gt;CSS limits us as we can’t target something in a CSS file. The media queries we write will and remain the same. Our HTML can be be changed and manipulated, which opens a lot of doors when it comes to the &lt;code&gt;srcset&lt;/code&gt; attribute!&lt;/p&gt;

&lt;p&gt;Liked the post? For more expert blog articles or video courses, please visit &lt;a href="https://ultimatecourses.com"&gt;Ultimate Courses&lt;/a&gt;!&lt;/p&gt;

&lt;p&gt;Thanks for reading and happy coding!&lt;/p&gt;

</description>
      <category>html</category>
      <category>css</category>
      <category>srcset</category>
      <category>responsive</category>
    </item>
  </channel>
</rss>
