<?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: MELVIN GEORGE</title>
    <description>The latest articles on DEV Community by MELVIN GEORGE (@melvin2016).</description>
    <link>https://dev.to/melvin2016</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%2F71181%2F4846ffba-fd4a-4d54-b8b1-bbd8dcb0b8c7.png</url>
      <title>DEV Community: MELVIN GEORGE</title>
      <link>https://dev.to/melvin2016</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/melvin2016"/>
    <language>en</language>
    <item>
      <title>Use margin-inline-start CSS property instead of margin-left for better internationalization</title>
      <dc:creator>MELVIN GEORGE</dc:creator>
      <pubDate>Mon, 12 Dec 2022 05:39:38 +0000</pubDate>
      <link>https://dev.to/melvin2016/use-margin-inline-start-css-property-instead-of-margin-left-for-better-internationalization-416p</link>
      <guid>https://dev.to/melvin2016/use-margin-inline-start-css-property-instead-of-margin-left-for-better-internationalization-416p</guid>
      <description>&lt;p&gt;&lt;a href="https://melvingeorge.me/blog/use-margin-inline-start-css-instead-of-margin-left-for-better-internationalization-support" rel="noopener noreferrer"&gt;Originally posted here!&lt;/a&gt;&lt;br&gt;
  On the world wide web (www) or the internet, we have our website visited by people from different countries and languages. Some of the languages are read from left to right (like English), and some from right to left (like Arabic). So when the direction of the language gets changed it is good for the website layout to change accordingly as well as a good user experience so that the website doesn't look bad.&lt;/p&gt;

&lt;p&gt;To accommodate for the language intricacies, we have to make the website layout responsive in every language and its reading direction.&lt;/p&gt;
&lt;h3&gt;
  
  
  TL;DR
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- Changing language direction to `rtl`  --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt; &lt;span class="na"&gt;dir=&lt;/span&gt;&lt;span class="s"&gt;"rtl"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&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;"box"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Hello World&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;span class="c"&gt;&amp;lt;!-- Style for the red color box --&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!--
    Adding a 30px margin to the start of the box 
    using an internationalization property
  --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
    &lt;span class="nc"&gt;.box&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;300px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;300px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;white&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="py"&gt;margin-inline-start&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;30px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;For example, suppose we have a layout with a &lt;code&gt;red&lt;/code&gt; background color box that has a text called &lt;code&gt;Hello World&lt;/code&gt; like this,&lt;/p&gt;

&lt;p&gt;&lt;a href="/uploads/Screenshot_2022_10_29_at_12_37_53_PM_4650f6827a.png" class="article-body-image-wrapper"&gt;&lt;img src="/uploads/Screenshot_2022_10_29_at_12_37_53_PM_4650f6827a.png" alt="red color box with hello world text shown on webpage"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The HTML code will look like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt; &lt;span class="na"&gt;dir=&lt;/span&gt;&lt;span class="s"&gt;"ltr"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&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;"box"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Hello World&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;span class="c"&gt;&amp;lt;!-- Style for the red color box --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
    &lt;span class="nc"&gt;.box&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;300px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;300px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;white&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's add a left margin of &lt;code&gt;30px&lt;/code&gt; like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt; &lt;span class="na"&gt;dir=&lt;/span&gt;&lt;span class="s"&gt;"ltr"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&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;"box"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Hello World&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;span class="c"&gt;&amp;lt;!-- Style for the red color box --&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!-- Adding a 30px left margin to the box --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
    &lt;span class="nc"&gt;.box&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;300px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;300px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;white&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;margin-left&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;30px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the webpage looks like this,&lt;/p&gt;

&lt;p&gt;&lt;a href="/uploads/red_color_box_margin_left_applied_1a82d77514.png" class="article-body-image-wrapper"&gt;&lt;img src="/uploads/red_color_box_margin_left_applied_1a82d77514.png" alt="red color box where 30px margin is applied to its left side"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Have you noticed the &lt;code&gt;dir="ltr"&lt;/code&gt; attribute on the main &lt;code&gt;html&lt;/code&gt; tag in our HTML code? This refers to the language direction of the content on the webpage. the &lt;code&gt;dir&lt;/code&gt; refers to the direction and the value &lt;code&gt;ltr&lt;/code&gt; refers that the direction of the language from left to right (&lt;code&gt;ltr&lt;/code&gt;). This is the default for languages like English. Now let's change the &lt;code&gt;dir&lt;/code&gt; attribute's value to &lt;code&gt;rtl&lt;/code&gt; which refers that the direction of the language from right to left.&lt;/p&gt;

&lt;p&gt;It can be done like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- Changing language direction to `rtl`  --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt; &lt;span class="na"&gt;dir=&lt;/span&gt;&lt;span class="s"&gt;"rtl"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&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;"box"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Hello World&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;span class="c"&gt;&amp;lt;!-- Style for the red color box --&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!-- Adding a 30px left margin to the box --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
    &lt;span class="nc"&gt;.box&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;300px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;300px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;white&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;margin-left&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;30px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="/uploads/red_color_box_shifted_to_right_ec892309b7.png" class="article-body-image-wrapper"&gt;&lt;img src="/uploads/red_color_box_shifted_to_right_ec892309b7.png" alt="red color box with text is shifted from left to right side of the webpage"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see that after changing the &lt;code&gt;dir&lt;/code&gt; attribute's value from &lt;code&gt;ltr&lt;/code&gt; to &lt;code&gt;rtl&lt;/code&gt;, the whole box with the text is now shifted to the right side since the language direction is now changed.&lt;/p&gt;

&lt;p&gt;Have you noticed something weird here?&lt;/p&gt;

&lt;p&gt;You may have guessed that the &lt;code&gt;margin-left&lt;/code&gt; of &lt;code&gt;30px&lt;/code&gt; is applied but is not taking effect to its right side after we changed the &lt;code&gt;dir&lt;/code&gt; attribute. Well, we can't blame the browser since we have defined it specifically to be applied to the left side of the box. We now have a weird-looking website. This is huge and will break the website layout if our layout is more complex and large.&lt;/p&gt;

&lt;p&gt;To accommodate for this problem of not switching the left margin when we change the language direction, we now have an internationalized version of the &lt;code&gt;margin-left&lt;/code&gt; property which is the &lt;code&gt;margin-inline-start&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's change replace the &lt;code&gt;margin-left&lt;/code&gt; property with the new property of &lt;code&gt;margin-inline-start&lt;/code&gt; in our HTML code like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- Changing language direction to `rtl`  --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt; &lt;span class="na"&gt;dir=&lt;/span&gt;&lt;span class="s"&gt;"rtl"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&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;"box"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Hello World&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;span class="c"&gt;&amp;lt;!-- Style for the red color box --&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!--
    Adding a 30px margin to the start of the box 
    using an internationalization property
  --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
    &lt;span class="nc"&gt;.box&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;300px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;300px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;white&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="py"&gt;margin-inline-start&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;30px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The webpage now looks like this,&lt;/p&gt;

&lt;p&gt;&lt;a href="/uploads/red_box_internatiolized_bafe669a23.png" class="article-body-image-wrapper"&gt;&lt;img src="/uploads/red_box_internatiolized_bafe669a23.png" alt="red box with better internationalization support"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see that the &lt;code&gt;30px&lt;/code&gt; margin is now applied to its right side and the layout is now correct automatically by changing the direction of the language.&lt;/p&gt;

&lt;p&gt;We have successfully made our webpage layout look good using internationalization standards.&lt;/p&gt;

&lt;p&gt;See the above code live in &lt;a href="https://codesandbox.io/s/use-margin-inline-start-instead-margin-left-for-better-internationalization-css-e22ovw?file=/index.html" rel="noopener noreferrer"&gt;codesandbox&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;That's all 😃!&lt;/p&gt;

</description>
      <category>css</category>
    </item>
    <item>
      <title>Use margin-inline-end CSS property instead of margin-right for better internationalization support</title>
      <dc:creator>MELVIN GEORGE</dc:creator>
      <pubDate>Mon, 12 Dec 2022 05:39:08 +0000</pubDate>
      <link>https://dev.to/melvin2016/use-margin-inline-end-css-property-instead-of-margin-right-for-better-internationalization-support-2nk3</link>
      <guid>https://dev.to/melvin2016/use-margin-inline-end-css-property-instead-of-margin-right-for-better-internationalization-support-2nk3</guid>
      <description>&lt;p&gt;&lt;a href="https://melvingeorge.me/blog/use-margin-inline-end-css-property-instead-of-margin-right-for-better-internationalization-support" rel="noopener noreferrer"&gt;Originally posted here!&lt;/a&gt;&lt;br&gt;
  On the world wide web (www) or the internet, we have our website visited by people from different countries and languages. Some of the languages are read from left to right (like English), and some from right to left (like Arabic). So when the direction of the language gets changed it is good for the website layout to change accordingly as well as a good user experience so that the website doesn't look bad.&lt;/p&gt;

&lt;p&gt;To accommodate for the language intricacies, we have to make the website layout responsive in every language and its reading direction.&lt;/p&gt;
&lt;h3&gt;
  
  
  TL;DR
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- Changing language direction to `rtl`  --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt; &lt;span class="na"&gt;dir=&lt;/span&gt;&lt;span class="s"&gt;"rtl"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&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;"box-1"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Hello World 1&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;"box-2"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Hello World 2&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;span class="c"&gt;&amp;lt;!-- Style for the red color box --&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!--
    Using `margin-inline-end` instead of `margin-right` 
    for better internationalization
  --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
    &lt;span class="nc"&gt;.box-1&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="n"&gt;inline-block&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;300px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;300px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;white&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="py"&gt;margin-inline-end&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;30px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nc"&gt;.box-2&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="n"&gt;inline-block&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;300px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;300px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;white&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;For example, suppose we have a layout with two &lt;code&gt;red&lt;/code&gt; background color boxes side by side that has the text called &lt;code&gt;Hello World 1&lt;/code&gt; on the first box and &lt;code&gt;Hello World 2&lt;/code&gt; on the second box like this,&lt;/p&gt;

&lt;p&gt;&lt;a href="/uploads/Screenshot_2022_11_06_at_12_05_36_AM_89993c004e.png" class="article-body-image-wrapper"&gt;&lt;img src="/uploads/Screenshot_2022_11_06_at_12_05_36_AM_89993c004e.png" alt="red color boxes with hello world 1 text shown on the first box and hello world 2 on the second box on the webpage"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The HTML code will look like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt; &lt;span class="na"&gt;dir=&lt;/span&gt;&lt;span class="s"&gt;"ltr"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&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;"box-1"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Hello World 1&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;"box-2"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Hello World 2&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;span class="c"&gt;&amp;lt;!-- Style for the red color box --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
    &lt;span class="nc"&gt;.box-1&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="n"&gt;inline-block&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;300px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;300px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;white&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nc"&gt;.box-2&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="n"&gt;inline-block&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;300px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;300px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;white&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's add a right margin of &lt;code&gt;30px&lt;/code&gt; to the first box like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt; &lt;span class="na"&gt;dir=&lt;/span&gt;&lt;span class="s"&gt;"ltr"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&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;"box-1"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Hello World 1&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;"box-2"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Hello World 2&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;span class="c"&gt;&amp;lt;!-- Style for the red color box --&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!-- Adding 30px right margin to the first box --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
    &lt;span class="nc"&gt;.box-1&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="n"&gt;inline-block&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;300px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;300px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;white&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;margin-right&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;30px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nc"&gt;.box-2&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="n"&gt;inline-block&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;300px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;300px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;white&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the webpage looks like this,&lt;/p&gt;

&lt;p&gt;&lt;a href="/uploads/Screenshot_2022_11_06_at_12_10_20_AM_e1b671c248.png" class="article-body-image-wrapper"&gt;&lt;img src="/uploads/Screenshot_2022_11_06_at_12_10_20_AM_e1b671c248.png" alt="red color boxes where 30px margin is applied to the first box's left side"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Have you noticed the &lt;code&gt;dir="ltr"&lt;/code&gt; attribute on the main &lt;code&gt;html&lt;/code&gt; tag in our HTML code? This refers to the language direction of the content on the webpage. The &lt;code&gt;dir&lt;/code&gt; refers to the direction and the value &lt;code&gt;ltr&lt;/code&gt; refers that the direction of the language from left to right (&lt;code&gt;ltr&lt;/code&gt;). This is the default for languages like English. Now let's change the &lt;code&gt;dir&lt;/code&gt; attribute's value to &lt;code&gt;rtl&lt;/code&gt; which refers that the direction of the language from right to left.&lt;/p&gt;

&lt;p&gt;It can be done like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- Changing language direction to `rtl`  --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt; &lt;span class="na"&gt;dir=&lt;/span&gt;&lt;span class="s"&gt;"rtl"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&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;"box-1"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Hello World 1&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;"box-2"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Hello World 2&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;span class="c"&gt;&amp;lt;!-- Style for the red color box --&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!-- Adding 30px right margin to the first box --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
    &lt;span class="nc"&gt;.box-1&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="n"&gt;inline-block&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;300px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;300px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;white&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;margin-right&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;30px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nc"&gt;.box-2&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="n"&gt;inline-block&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;300px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;300px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;white&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="/uploads/Screenshot_2022_11_06_at_12_14_41_AM_fcbdb62a18.png" class="article-body-image-wrapper"&gt;&lt;img src="/uploads/Screenshot_2022_11_06_at_12_14_41_AM_fcbdb62a18.png" alt="red color boxes with text is now shifted from left to right side of the webpage"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see that after changing the &lt;code&gt;dir&lt;/code&gt; attribute's value from &lt;code&gt;ltr&lt;/code&gt; to &lt;code&gt;rtl&lt;/code&gt;, the whole box with the text is now shifted to the right side since the language direction is now changed.&lt;/p&gt;

&lt;p&gt;Have you noticed something weird here?&lt;/p&gt;

&lt;p&gt;You may have guessed that the &lt;code&gt;margin-right&lt;/code&gt; of &lt;code&gt;30px&lt;/code&gt; is applied but is not taking effect between the boxes like we needed after we changed the &lt;code&gt;dir&lt;/code&gt; attribute.&lt;/p&gt;

&lt;p&gt;Well, we can't blame the browser since we have defined it specifically to be applied to the right side of the box, and it is getting applied to the right side of the last box. (Since the direction is now from right to left, the first box on the end of the screen) Because of this, we now have a weird-looking website. This is huge and will break the website layout if our layout is more complex and large.&lt;/p&gt;

&lt;p&gt;To accommodate for this problem of not switching the right margin when we change the language direction, we now have an internationalized version of the &lt;code&gt;margin-right&lt;/code&gt; property which is the &lt;code&gt;margin-inline-end&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's replace the &lt;code&gt;margin-right&lt;/code&gt; property in the &lt;code&gt;box-1&lt;/code&gt; box class name with the new property of &lt;code&gt;margin-inline-end&lt;/code&gt; in our HTML code like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- Changing language direction to `rtl`  --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt; &lt;span class="na"&gt;dir=&lt;/span&gt;&lt;span class="s"&gt;"rtl"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&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;"box-1"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Hello World 1&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;"box-2"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Hello World 2&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;span class="c"&gt;&amp;lt;!-- Style for the red color box --&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!--
    Using `margin-inline-end` instead of `margin-right` 
    for better internationalization
  --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
    &lt;span class="nc"&gt;.box-1&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="n"&gt;inline-block&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;300px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;300px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;white&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="py"&gt;margin-inline-end&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;30px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nc"&gt;.box-2&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="n"&gt;inline-block&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;300px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;300px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;white&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The webpage now looks like this,&lt;/p&gt;

&lt;p&gt;&lt;a href="/uploads/Screenshot_2022_11_06_at_12_33_23_AM_1574fad80c.png" class="article-body-image-wrapper"&gt;&lt;img src="/uploads/Screenshot_2022_11_06_at_12_33_23_AM_1574fad80c.png" alt="red boxes with better internationalization support"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see that the &lt;code&gt;30px&lt;/code&gt; margin is now applied to its left side and the layout is now automatically corrected by changing the direction of the language. This will make our website more responsive and internationalized when the &lt;code&gt;dir&lt;/code&gt; changes from &lt;code&gt;ltr&lt;/code&gt; or &lt;code&gt;rtl&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We have successfully made our webpage layout look good using internationalization standards.&lt;/p&gt;

&lt;p&gt;See the above code live in &lt;a href="https://codesandbox.io/s/use-margin-inline-end-css-property-instead-of-margin-right-for-better-internationalization-support-g7hbq3?file=/index.html" rel="noopener noreferrer"&gt;codesandbox&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;That's all 😃!&lt;/p&gt;

</description>
      <category>css</category>
    </item>
    <item>
      <title>How to make HTML elements in horizontal order that is responsive to the screens using CSS?</title>
      <dc:creator>MELVIN GEORGE</dc:creator>
      <pubDate>Mon, 12 Dec 2022 05:38:38 +0000</pubDate>
      <link>https://dev.to/melvin2016/how-to-make-html-elements-in-horizontal-order-that-is-responsive-to-the-screens-using-css-1h53</link>
      <guid>https://dev.to/melvin2016/how-to-make-html-elements-in-horizontal-order-that-is-responsive-to-the-screens-using-css-1h53</guid>
      <description>&lt;p&gt;&lt;a href="https://melvingeorge.me/blog/make-html-elements-horizontal-order-responsive-to-screen-using-css" rel="noopener noreferrer"&gt;Originally posted here!&lt;/a&gt;&lt;br&gt;
  To make HTML elements in horizontal order or row-wise that are responsive to the screens, we can use the CSS &lt;code&gt;display&lt;/code&gt; property and set its value to &lt;code&gt;flex&lt;/code&gt;, then we can use the property &lt;code&gt;flex-direction&lt;/code&gt; and set its value to &lt;code&gt;row&lt;/code&gt;, and finally, we can use the &lt;code&gt;flex-wrap&lt;/code&gt; property and set its value to &lt;code&gt;wrap&lt;/code&gt;. These values should be applied to the parent element of the children elements where you want this to take place.&lt;/p&gt;

&lt;p&gt;For example, let's say we have three red color boxes with &lt;code&gt;200 * 200&lt;/code&gt; dimensions (width * height).&lt;/p&gt;

&lt;p&gt;The HTML code would look something like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="c"&gt;&amp;lt;!-- Three `red` color boxes --&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;"box"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Box 1&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;"box"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Box 2&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;"box"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Box 3&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="c"&gt;&amp;lt;!-- Styles for the boxes --&amp;gt;&lt;/span&gt;
    &lt;span class="c"&gt;&amp;lt;!--
      `margin` of `2px` is applied to
      make some gap within the elements
    --&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
      &lt;span class="nc"&gt;.box&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;200px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;200px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The webpage now looks like this,&lt;/p&gt;

&lt;p&gt;&lt;a href="/uploads/Screenshot_2022_11_06_at_7_12_58_PM_e725a01fe6.png" class="article-body-image-wrapper"&gt;&lt;img src="/uploads/Screenshot_2022_11_06_at_7_12_58_PM_e725a01fe6.png" alt="three red color boxes arranged vertically"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see that we have defined three &lt;code&gt;red&lt;/code&gt; background colors &lt;code&gt;div&lt;/code&gt; and applied a width of &lt;code&gt;200px&lt;/code&gt; and a height of &lt;code&gt;200px&lt;/code&gt;. If you look closely, you can also see a parent &lt;code&gt;div&lt;/code&gt; that is enclosing all these boxes. This is where we have to apply the CSS properties to make it horizontal order or row-wise.&lt;/p&gt;

&lt;p&gt;Let's add a class of &lt;code&gt;parent&lt;/code&gt; to the parent &lt;code&gt;div&lt;/code&gt; element and apply the &lt;code&gt;display: flex;&lt;/code&gt;, &lt;code&gt;flex-direction: row;&lt;/code&gt; and the &lt;code&gt;flex-wrap: wrap;&lt;/code&gt; properties to it.&lt;/p&gt;

&lt;p&gt;It can be done like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="c"&gt;&amp;lt;!-- Three `red` color boxes --&amp;gt;&lt;/span&gt;
    &lt;span class="c"&gt;&amp;lt;!-- 
      add a class name of `parent` to the parent
      `div` to apply styles to it.
    --&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;"parent"&lt;/span&gt;&lt;span class="nt"&gt;&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;"box"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Box 1&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;"box"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Box 2&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;"box"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Box 3&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="c"&gt;&amp;lt;!-- Styles for the boxes --&amp;gt;&lt;/span&gt;
    &lt;span class="c"&gt;&amp;lt;!--
      `margin` of `2px` is applied to
      make some gap within the elements
    --&amp;gt;&lt;/span&gt;
    &lt;span class="c"&gt;&amp;lt;!--
      adding `display: flex`, `flex-direction: row`
      and `flex-wrap: wrap` property to make it in horizontal order.
    --&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
      &lt;span class="nc"&gt;.box&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;200px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;200px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="nc"&gt;.parent&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="n"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nl"&gt;flex-direction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nl"&gt;flex-wrap&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;wrap&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the webpage looks like this,&lt;/p&gt;

&lt;p&gt;&lt;a href="/uploads/Screenshot_2022_11_06_at_7_40_09_PM_62df17425b.png" class="article-body-image-wrapper"&gt;&lt;img src="/uploads/Screenshot_2022_11_06_at_7_40_09_PM_62df17425b.png" alt="three red color boxes on the webpage are now in horizontal order or row-wise"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let's understand what just happened. The &lt;code&gt;display: flex;&lt;/code&gt; property sets the parent &lt;code&gt;div&lt;/code&gt; element into a flexbox container, and the &lt;code&gt;flex-direction: row&lt;/code&gt; property made the div take up the entire available width of the screen by making the elements flow in the &lt;code&gt;row&lt;/code&gt; direction and finally the &lt;code&gt;flex-wrap: wrap;&lt;/code&gt; property allows the three red color boxes to go into the second line if the available width is too low.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;flex-wrap: wrap&lt;/code&gt; property made the three boxes to be responsive to the screen.&lt;/p&gt;

&lt;p&gt;A visual representation of the webpage is shown below,&lt;/p&gt;

&lt;p&gt;&lt;a href="/uploads/representation_448ce73bae.gif" class="article-body-image-wrapper"&gt;&lt;img src="/uploads/representation_448ce73bae.gif" alt="three red color boxes flows into the second line when the width of the screen is too low for the boxes"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have successfully made the 3 boxes flow in the horizontal direction as well as making them responsive. Yay 🥳!&lt;/p&gt;

&lt;p&gt;See the above code live in the &lt;a href="https://codesandbox.io/s/make-html-elements-horizontal-order-responsive-to-screen-using-css-x4hn64?file=/index.html" rel="noopener noreferrer"&gt;codesandbox&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;That's all 😃.&lt;/p&gt;

</description>
      <category>css</category>
    </item>
    <item>
      <title>How to make font size grow according to the size of the screen using CSS?</title>
      <dc:creator>MELVIN GEORGE</dc:creator>
      <pubDate>Mon, 12 Dec 2022 05:38:08 +0000</pubDate>
      <link>https://dev.to/melvin2016/how-to-make-font-size-grow-according-to-the-size-of-the-screen-using-css-4b8b</link>
      <guid>https://dev.to/melvin2016/how-to-make-font-size-grow-according-to-the-size-of-the-screen-using-css-4b8b</guid>
      <description>&lt;p&gt;&lt;a href="https://melvingeorge.me/blog/make-font-size-grow-according-size-of-screen-using-css" rel="noopener noreferrer"&gt;Originally posted here!&lt;/a&gt;&lt;br&gt;
  To make the font size grow according to the width of the screen, the better way is to use the &lt;code&gt;clamp()&lt;/code&gt; CSS function and pass the minimum font size value, the preferred font size value and finally the maximum font size value it should take according to the width of the screen.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;clamp()&lt;/code&gt; CSS function accepts three arguments such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the first argument is the minimum value to stop resizing the font size if it is equal to or below a supplied font-size value.&lt;/li&gt;
&lt;li&gt;the second argument is the rate at which the font size should grow according to the screen size.&lt;/li&gt;
&lt;li&gt;the third argument is the maximum value to stop resizing the font size if it is equal to or above a supplied font-size value.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;NOTE: &lt;code&gt;clamp()&lt;/code&gt; CSS function is not only reserved for the &lt;code&gt;font-size&lt;/code&gt; property and can be used in various other properties too. The above explanation is to understand its usage of it concerning the font-size CSS property&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  TL;DR
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Hello World&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!-- Style for fonts --&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!--
    Add `0.75rem` with `2vw` as the second 
    argument to the `clamp()` CSS function
    - add a minimum value as the first argument
    - add a maximum value as the third argument
  --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;clamp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1rem&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;2vw&lt;/span&gt; &lt;span class="err"&gt;+&lt;/span&gt; &lt;span class="m"&gt;0.75rem&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1.5rem&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;For example, let's say we have a text called &lt;code&gt;Hello World&lt;/code&gt; on the webpage. The HTML for that would look something like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Hello World&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To grow the font size of the &lt;code&gt;paragraph&lt;/code&gt; tag text at a rate of &lt;code&gt;1%&lt;/code&gt; to the width of the screen, we can use the &lt;code&gt;2vw&lt;/code&gt; unit in CSS.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE: &lt;code&gt;1vw&lt;/code&gt; unit refers to &lt;code&gt;1%&lt;/code&gt;of the width of the screen.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To do that let's define our &lt;code&gt;font-size&lt;/code&gt; CSS property and then use the &lt;code&gt;2vw&lt;/code&gt; the like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Hello World&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!-- Style for fonts --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2vw&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now there are 2 problems with this approach:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;one is the font size won't increase if the user tries to magnify the webpage, because the font size property is tied to the view width or the width of the screen only.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;the second one is if the width of the screen is too low, the font size becomes too small to read and if the screen becomes too big it becomes really large also.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Visually the webpage looks like this,&lt;/p&gt;

&lt;p&gt;&lt;a href="/uploads/01_d16ea8210e.gif" class="article-body-image-wrapper"&gt;&lt;img src="/uploads/01_d16ea8210e.gif" alt="font size not scaling correctly when the user increases or decreases mangification"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To avoid this problem, let's first tie the font size unit with the relative root font-size value of the webpage, in our case we will be using &lt;code&gt;0.75rem&lt;/code&gt;. The &lt;code&gt;0.75rem&lt;/code&gt; means &lt;code&gt;0.75 * 16px&lt;/code&gt; (In our case, &lt;code&gt;16px&lt;/code&gt; is the default font size of the root element).&lt;/p&gt;

&lt;p&gt;So let's add the &lt;code&gt;0.75rem&lt;/code&gt; unit with the &lt;code&gt;2vw&lt;/code&gt; using the &lt;code&gt;clamp()&lt;/code&gt; CSS function and pass this value as the second argument to the function.&lt;/p&gt;

&lt;p&gt;It can be done like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Hello World&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!-- Style for fonts --&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!--
    Add `0.75rem` with `2vw` as the second
    argument to the `clamp()` CSS function
  --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;clamp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;2vw&lt;/span&gt; &lt;span class="err"&gt;+&lt;/span&gt; &lt;span class="m"&gt;0.75rem&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now when the user magnifies the webpage, the font size increases.&lt;/p&gt;

&lt;p&gt;It looks like this,&lt;/p&gt;

&lt;p&gt;&lt;a href="/uploads/02_755ba15763.gif" class="article-body-image-wrapper"&gt;&lt;img src="/uploads/02_755ba15763.gif" alt="font size scales as user increases or decreases magnification"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now the only problem is that we need to set a minimum font size till it should decrease and a maximum value the font size should increase and then stops. Otherwise, it will keep on increasing or decreasing as the screen keeps on getting smaller or bigger.&lt;/p&gt;

&lt;p&gt;To do that we need to pass the minimum font size and the maximum font size as the first and third arguments to the &lt;code&gt;clamp()&lt;/code&gt; size CSS function.&lt;/p&gt;

&lt;p&gt;Let's pass the value of &lt;code&gt;1rem&lt;/code&gt; as the minimum value and &lt;code&gt;1.5rem&lt;/code&gt; as the maximum value.&lt;/p&gt;

&lt;p&gt;It can be done like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Hello World&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!-- Style for fonts --&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!--
    Add `0.75rem` with `2vw` as the second 
    argument to the `clamp()` CSS function
    - add a minimum value as the first argument
    - add a maximum value as the third argument
  --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;clamp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1rem&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;2vw&lt;/span&gt; &lt;span class="err"&gt;+&lt;/span&gt; &lt;span class="m"&gt;0.75rem&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1.5rem&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the webpage has a minimum and maximum font size it can scale to.&lt;/p&gt;

&lt;p&gt;The webpage text now scales like this when screen size increases or decreases,&lt;/p&gt;

&lt;p&gt;&lt;a href="/uploads/03_fd8b665254.gif" class="article-body-image-wrapper"&gt;&lt;img src="/uploads/03_fd8b665254.gif" alt="webpage now scales as user increases or decreases screen width"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have successfully made the font size grow according to screen size. Yay 🥳!&lt;/p&gt;

&lt;p&gt;See the above code live in &lt;a href="https://codesandbox.io/s/make-font-size-grow-according-size-of-screen-using-css-xzc38j?file=/index.html" rel="noopener noreferrer"&gt;codesandbox&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;That's all 😃.&lt;/p&gt;

</description>
      <category>css</category>
    </item>
    <item>
      <title>How to limit the number of characters on a single line in a webpage using CSS?</title>
      <dc:creator>MELVIN GEORGE</dc:creator>
      <pubDate>Mon, 12 Dec 2022 05:37:38 +0000</pubDate>
      <link>https://dev.to/melvin2016/how-to-limit-the-number-of-characters-on-a-single-line-in-a-webpage-using-css-37fj</link>
      <guid>https://dev.to/melvin2016/how-to-limit-the-number-of-characters-on-a-single-line-in-a-webpage-using-css-37fj</guid>
      <description>&lt;p&gt;&lt;a href="https://melvingeorge.me/blog/limit-number-of-text-characters-on-single-line-webpage-using-css" rel="noopener noreferrer"&gt;Originally posted here!&lt;/a&gt;&lt;br&gt;
  To limit the number of characters that should be on a line on the webpage, you can use the &lt;code&gt;max-inline-size&lt;/code&gt; CSS property on the HTML elements that wrap the texts and use a numeric value with the &lt;code&gt;ch&lt;/code&gt; unit (&lt;code&gt;ch&lt;/code&gt; refers to the number of characters).&lt;/p&gt;
&lt;h3&gt;
  
  
  TL;DR
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="c"&gt;&amp;lt;!-- Sample Paragraphs --&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;p&amp;gt;&lt;/span&gt;
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Similique
        dolorem iste accusantium, architecto labore nobis pariatur, ipsa, magni
        incidunt amet at? Ab nihil voluptas mollitia culpa quidem tempora
        explicabo temporibus non? Assumenda labore tempore rerum voluptatibus
        laudantium et facere sed exercitationem nisi culpa dicta perspiciatis
        atque dolorum, aperiam soluta. Consectetur optio inventore eius ullam,
        dignissimos officiis mollitia dicta, error possimus consequatur quasi
        laborum harum nulla.
      &lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;

      &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Suscipit,
        laboriosam voluptatibus. Modi id aliquid sit ullam cupiditate aperiam
        nihil dolorem quas fuga omnis eos obcaecati ipsam corrupti voluptatibus
        officiis labore, ipsa consequuntur consequatur numquam facere sequi eius
        quidem. Id accusantium, neque velit animi excepturi saepe explicabo
        consectetur debitis cum voluptate dolorem hic odio nostrum earum
        doloribus, ipsam voluptatem! Libero ipsa ullam officiis odit aliquam
        iusto facilis? Aut!
      &lt;span class="nt"&gt;&amp;lt;/p&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;/body&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!-- Styles for the webpage --&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!--
    Adding `max-inline-size: 66ch` CSS property 
    to limit the characters of a single line to 66.
  --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;article&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="py"&gt;max-inline-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;66ch&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;clamp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1rem&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1vw&lt;/span&gt; &lt;span class="err"&gt;+&lt;/span&gt; &lt;span class="m"&gt;0.75rem&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1.5rem&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;20px&lt;/span&gt; &lt;span class="nb"&gt;auto&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;For example, let's say we have a webpage with some paragraphs of text inside an &lt;code&gt;article&lt;/code&gt; HTML element.&lt;/p&gt;

&lt;p&gt;The HTML code would look something like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="c"&gt;&amp;lt;!-- Sample Paragraphs --&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;p&amp;gt;&lt;/span&gt;
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Similique
        dolorem iste accusantium, architecto labore nobis pariatur, ipsa, magni
        incidunt amet at? Ab nihil voluptas mollitia culpa quidem tempora
        explicabo temporibus non? Assumenda labore tempore rerum voluptatibus
        laudantium et facere sed exercitationem nisi culpa dicta perspiciatis
        atque dolorum, aperiam soluta. Consectetur optio inventore eius ullam,
        dignissimos officiis mollitia dicta, error possimus consequatur quasi
        laborum harum nulla.
      &lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;

      &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Suscipit,
        laboriosam voluptatibus. Modi id aliquid sit ullam cupiditate aperiam
        nihil dolorem quas fuga omnis eos obcaecati ipsam corrupti voluptatibus
        officiis labore, ipsa consequuntur consequatur numquam facere sequi eius
        quidem. Id accusantium, neque velit animi excepturi saepe explicabo
        consectetur debitis cum voluptate dolorem hic odio nostrum earum
        doloribus, ipsam voluptatem! Libero ipsa ullam officiis odit aliquam
        iusto facilis? Aut!
      &lt;span class="nt"&gt;&amp;lt;/p&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;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now if we see this without any styles, the text would go to the very edge of the screen like this,&lt;/p&gt;

&lt;p&gt;&lt;a href="/uploads/Screenshot_2022_11_10_at_7_22_31_PM_d7e93da568.png" class="article-body-image-wrapper"&gt;&lt;img src="/uploads/Screenshot_2022_11_10_at_7_22_31_PM_d7e93da568.png" alt="texts in the webpage are expanded till the very end of the screen"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is not looking any good and very hard for the users to read the texts.&lt;/p&gt;

&lt;p&gt;So to limit the paragraphs texts we can use the &lt;code&gt;max-inline-size&lt;/code&gt; property and give a value of &lt;code&gt;66ch&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;66ch&lt;/code&gt; value refers to limiting the characters in a single line to 66 characters.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;max-inline-size&lt;/code&gt; property is the new internationalized version of the &lt;code&gt;max-width&lt;/code&gt; property. The &lt;code&gt;max-inline-size&lt;/code&gt; property helps to automatically correct the width according to the writing direction of a particular language.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It can be done like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="c"&gt;&amp;lt;!-- Sample Paragraphs --&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;p&amp;gt;&lt;/span&gt;
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Similique
        dolorem iste accusantium, architecto labore nobis pariatur, ipsa, magni
        incidunt amet at? Ab nihil voluptas mollitia culpa quidem tempora
        explicabo temporibus non? Assumenda labore tempore rerum voluptatibus
        laudantium et facere sed exercitationem nisi culpa dicta perspiciatis
        atque dolorum, aperiam soluta. Consectetur optio inventore eius ullam,
        dignissimos officiis mollitia dicta, error possimus consequatur quasi
        laborum harum nulla.
      &lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;

      &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Suscipit,
        laboriosam voluptatibus. Modi id aliquid sit ullam cupiditate aperiam
        nihil dolorem quas fuga omnis eos obcaecati ipsam corrupti voluptatibus
        officiis labore, ipsa consequuntur consequatur numquam facere sequi eius
        quidem. Id accusantium, neque velit animi excepturi saepe explicabo
        consectetur debitis cum voluptate dolorem hic odio nostrum earum
        doloribus, ipsam voluptatem! Libero ipsa ullam officiis odit aliquam
        iusto facilis? Aut!
      &lt;span class="nt"&gt;&amp;lt;/p&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;/body&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!-- Styles for the webpage --&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!--
    Adding `max-inline-size: 66ch` CSS property 
    to limit the characters of a single line to 66.
  --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;article&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="py"&gt;max-inline-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;66ch&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After adding the &lt;code&gt;max-inline-size: 66ch&lt;/code&gt; CSS property, the webpage now looks like this,&lt;/p&gt;

&lt;p&gt;&lt;a href="/uploads/Screenshot_2022_11_10_at_7_35_41_PM_4420b55679.png" class="article-body-image-wrapper"&gt;&lt;img src="/uploads/Screenshot_2022_11_10_at_7_35_41_PM_4420b55679.png" alt="webpage text got limited to 60 characters per line"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's also add some CSS styles to center the texts and to make them grow according to the size of the screen.&lt;/p&gt;

&lt;p&gt;It can be done like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="c"&gt;&amp;lt;!-- Sample Paragraphs --&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;p&amp;gt;&lt;/span&gt;
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Similique
        dolorem iste accusantium, architecto labore nobis pariatur, ipsa, magni
        incidunt amet at? Ab nihil voluptas mollitia culpa quidem tempora
        explicabo temporibus non? Assumenda labore tempore rerum voluptatibus
        laudantium et facere sed exercitationem nisi culpa dicta perspiciatis
        atque dolorum, aperiam soluta. Consectetur optio inventore eius ullam,
        dignissimos officiis mollitia dicta, error possimus consequatur quasi
        laborum harum nulla.
      &lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;

      &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Suscipit,
        laboriosam voluptatibus. Modi id aliquid sit ullam cupiditate aperiam
        nihil dolorem quas fuga omnis eos obcaecati ipsam corrupti voluptatibus
        officiis labore, ipsa consequuntur consequatur numquam facere sequi eius
        quidem. Id accusantium, neque velit animi excepturi saepe explicabo
        consectetur debitis cum voluptate dolorem hic odio nostrum earum
        doloribus, ipsam voluptatem! Libero ipsa ullam officiis odit aliquam
        iusto facilis? Aut!
      &lt;span class="nt"&gt;&amp;lt;/p&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;/body&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!-- Styles for the webpage --&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!--
    Adding `max-inline-size: 66ch` CSS property 
    to limit the characters of a single line to 66.
  --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;article&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="py"&gt;max-inline-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;66ch&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;clamp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1rem&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1vw&lt;/span&gt; &lt;span class="err"&gt;+&lt;/span&gt; &lt;span class="m"&gt;0.75rem&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1.5rem&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;20px&lt;/span&gt; &lt;span class="nb"&gt;auto&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="/uploads/Screenshot_2022_11_10_at_7_42_53_PM_800ea43e54.png" class="article-body-image-wrapper"&gt;&lt;img src="/uploads/Screenshot_2022_11_10_at_7_42_53_PM_800ea43e54.png" alt="webpage texts is centered, responsive and with only 66 chracters in a single line"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE: To know more about making texts responsive see the blog on &lt;a href="https://melvingeorge.me/blog/make-font-size-grow-according-size-of-screen-using-css" rel="noopener noreferrer"&gt;How to make font size grow according to the size of the screen using CSS?&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We have successfully limited the characters to 66 in a single line using CSS. Yay 🥳!&lt;/p&gt;

&lt;p&gt;See the above code live in the &lt;a href="https://codesandbox.io/s/limit-number-of-text-characters-on-single-line-webpage-using-css-due3d5?file=/index.html" rel="noopener noreferrer"&gt;codesandbox&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;That's all 😃.&lt;/p&gt;

</description>
      <category>css</category>
    </item>
    <item>
      <title>How to load custom fonts into a webpage using CSS?</title>
      <dc:creator>MELVIN GEORGE</dc:creator>
      <pubDate>Mon, 12 Dec 2022 05:37:08 +0000</pubDate>
      <link>https://dev.to/melvin2016/how-to-load-custom-fonts-into-a-webpage-using-css-473g</link>
      <guid>https://dev.to/melvin2016/how-to-load-custom-fonts-into-a-webpage-using-css-473g</guid>
      <description>&lt;p&gt;&lt;a href="https://melvingeorge.me/blog/load-custom-fonts-webpage-using-css" rel="noopener noreferrer"&gt;Originally posted here!&lt;/a&gt;&lt;br&gt;
  To load custom fonts or fonts that are hosted by yourself in a server using CSS first, you need to use the &lt;code&gt;@font-face&lt;/code&gt; CSS rule block. Inside the block you can then define a CSS property called &lt;code&gt;src&lt;/code&gt; and the first value is a &lt;code&gt;url()&lt;/code&gt; function where the function takes the path to your font file. Then right after the &lt;code&gt;url()&lt;/code&gt; function you can add another function called &lt;code&gt;format()&lt;/code&gt; in which you can pass the value of &lt;code&gt;woff2&lt;/code&gt;. (&lt;code&gt;woff2&lt;/code&gt; is a well-supported font type format and has the best performance gain among the browsers).&lt;/p&gt;

&lt;p&gt;After the &lt;code&gt;src&lt;/code&gt; property, you need to add another property called &lt;code&gt;font-family&lt;/code&gt; and the value should be the name of your font. You can have a custom name for your font file.&lt;/p&gt;
&lt;h3&gt;
  
  
  TL;DR
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Hello World&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!-- Styles for the webpage --&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!--
    Using custom font file using the `@font-face`
    CSS rule and using the `src` css property.
  --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;@font-face&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;src&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sx"&gt;url("./mycustomfont.ttf")&lt;/span&gt; &lt;span class="n"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;"woff2"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nl"&gt;font-family&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;MyCustomFontName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nt"&gt;body&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;font-family&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;MyCustomFontName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;For example, let's say we have a webpage with the text &lt;code&gt;Hello World&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The HTML code would look something like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Hello World&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It may look like this visually,&lt;/p&gt;

&lt;p&gt;&lt;a href="/uploads/Screenshot_2022_11_10_at_11_11_12_PM_97f5a0a6f0.png" class="article-body-image-wrapper"&gt;&lt;img src="/uploads/Screenshot_2022_11_10_at_11_11_12_PM_97f5a0a6f0.png" alt="simple webpage with hello world text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let's say we need the texts in the webpage to use our custom font file that is hosted in the root directory of the server where the &lt;code&gt;index.html&lt;/code&gt; is also present.&lt;/p&gt;

&lt;p&gt;To do that let's use the &lt;code&gt;@font-face&lt;/code&gt; CSS rule block and use the &lt;code&gt;src&lt;/code&gt; CSS property and pass the path to our font file in the &lt;code&gt;url()&lt;/code&gt; function like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Hello World&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!-- Styles for the webpage --&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!--
    Using custom font file using the `@font-face`
    CSS rule and using the `src` css property.
  --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;@font-face&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;src&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sx"&gt;url("./mycustomfont.ttf")&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we have defined the path to our font file, we also need to set the format to &lt;code&gt;woff2&lt;/code&gt; as it is a better-performant and well-supported font format using the &lt;code&gt;format()&lt;/code&gt; function right after the &lt;code&gt;url()&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;It can be done like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Hello World&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!-- Styles for the webpage --&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!--
    Using custom font file using the `@font-face`
    CSS rule and using the `src` css property.
  --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;@font-face&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;src&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sx"&gt;url("./mycustomfont.ttf")&lt;/span&gt; &lt;span class="n"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;"woff2"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's name our custom font using the &lt;code&gt;font-family&lt;/code&gt; CSS property and use a custom font name of our choice like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Hello World&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!-- Styles for the webpage --&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!--
    Using custom font file using the `@font-face`
    CSS rule and using the `src` css property.
  --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;@font-face&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;src&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sx"&gt;url("./mycustomfont.ttf")&lt;/span&gt; &lt;span class="n"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;"woff2"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nl"&gt;font-family&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;MyCustomFontName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have successfully imported our custom font file and given it a name.&lt;/p&gt;

&lt;p&gt;Now we need to apply our custom font to all the texts on the webpage, to fo that we can target the &lt;code&gt;body&lt;/code&gt; HTML element in the CSS and use the property of &lt;code&gt;font-family&lt;/code&gt; and pass the name of the custom font which in our case is the value of &lt;code&gt;MyCustomFontName&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It can be done like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Hello World&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!-- Styles for the webpage --&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!--
    Using custom font file using the `@font-face`
    CSS rule and using the `src` css property.
  --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;@font-face&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;src&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sx"&gt;url("./mycustomfont.ttf")&lt;/span&gt; &lt;span class="n"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;"woff2"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nl"&gt;font-family&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;MyCustomFontName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nt"&gt;body&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;font-family&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;MyCustomFontName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the text font in our webpage is changed to our custom font.&lt;/p&gt;

&lt;p&gt;The webpage now looks like this,&lt;/p&gt;

&lt;p&gt;&lt;a href="/uploads/Screenshot_2022_11_10_at_10_53_41_PM_0953657a7f.png" class="article-body-image-wrapper"&gt;&lt;img src="/uploads/Screenshot_2022_11_10_at_10_53_41_PM_0953657a7f.png" alt="webpage texts shown in the new custom font"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have successfully loaded our custom font into the webpage using CSS. Yay 🥳!&lt;/p&gt;

&lt;p&gt;See the above code live in the &lt;a href="https://codesandbox.io/s/load-custom-fonts-webpage-using-css-5d5wq1?file=/index.html" rel="noopener noreferrer"&gt;codesandbox&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;That's all 😃.&lt;/p&gt;

</description>
      <category>css</category>
    </item>
    <item>
      <title>How to load a font from a url as soon as possible or preload a font in HTML?</title>
      <dc:creator>MELVIN GEORGE</dc:creator>
      <pubDate>Mon, 12 Dec 2022 05:36:38 +0000</pubDate>
      <link>https://dev.to/melvin2016/how-to-load-a-font-from-a-url-as-soon-as-possible-or-preload-a-font-in-html-3701</link>
      <guid>https://dev.to/melvin2016/how-to-load-a-font-from-a-url-as-soon-as-possible-or-preload-a-font-in-html-3701</guid>
      <description>&lt;p&gt;&lt;a href="https://melvingeorge.me/blog/load-font-from-url-as-soon-as-possible-or-preload-font-html" rel="noopener noreferrer"&gt;Originally posted here!&lt;/a&gt;&lt;br&gt;
  To load a font from a url as soon as the webpage loads is to use the &lt;code&gt;link&lt;/code&gt; tag. In the link tag you can use the &lt;code&gt;href&lt;/code&gt; attribute where the value should be the url or path to your font file, then the &lt;code&gt;rel&lt;/code&gt; attribute with the value of &lt;code&gt;preload&lt;/code&gt;. The value &lt;code&gt;preload&lt;/code&gt; will instruct the browser to load the fonts as soon as possible. The &lt;code&gt;link&lt;/code&gt; tag should be placed inside the &lt;code&gt;head&lt;/code&gt; tag of the HTML code.&lt;/p&gt;

&lt;p&gt;We also need to add three more attributes like the below:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the &lt;code&gt;type&lt;/code&gt; attribute with a value of &lt;code&gt;font/woff2&lt;/code&gt; to specify the type of font we are going to use. The &lt;code&gt;woff2&lt;/code&gt; is a better-performant and well-supported font type among browsers.&lt;/li&gt;
&lt;li&gt;the &lt;code&gt;as&lt;/code&gt; attribute with a value of &lt;code&gt;font&lt;/code&gt; to specify the kind of file we are preloading.&lt;/li&gt;
&lt;li&gt;and finally the &lt;code&gt;crossorigin&lt;/code&gt; attribute without any value. Using the &lt;code&gt;crossorigin&lt;/code&gt; attribute without any value will consider the browser to use the value of &lt;code&gt;anonymous&lt;/code&gt; so that request will use the &lt;code&gt;CORS&lt;/code&gt; headers and also the credentials flag is set to the value of &lt;code&gt;same-origin&lt;/code&gt; which is what we want.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  TL;DR
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="c"&gt;&amp;lt;!--
    Using the `link` tag and then the `rel="preload"` attribute
    to load the font as soon as possible.
  --&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt;
      &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://fonts.googleapis.com/css2?family=Xanh+Mono&amp;amp;display=swap"&lt;/span&gt;
      &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"preload"&lt;/span&gt;
      &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"font/woff2"&lt;/span&gt;
      &lt;span class="na"&gt;as=&lt;/span&gt;&lt;span class="s"&gt;"font"&lt;/span&gt;
      &lt;span class="na"&gt;crossorigin&lt;/span&gt;
    &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!-- A simple webpage --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Hello World&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;For example, let's say we have a webpage with the text &lt;code&gt;Hello World&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In HTML, it may look like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!-- A simple webpage --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Hello World&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We need to use a font that is hosted at this url: &lt;a href="https://fonts.googleapis.com/css2?family=Xanh+Mono&amp;amp;display=swap" rel="noopener noreferrer"&gt;https://fonts.googleapis.com/css2?family=Xanh+Mono&amp;amp;display=swap&lt;/a&gt;. The font name is &lt;code&gt;Xanh Mono&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To do that we can use the &lt;code&gt;link&lt;/code&gt; tag and then add the &lt;code&gt;href&lt;/code&gt; attribute with the value of the font url and then use the &lt;code&gt;rel&lt;/code&gt; attribute with the value of &lt;code&gt;preload&lt;/code&gt; to load the font as soon as the page loads.&lt;/p&gt;

&lt;p&gt;It can be done like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="c"&gt;&amp;lt;!--
    Using the `link` tag and then the `rel="preload"` attribute
    to load the font as soon as possible.
  --&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt;
      &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://fonts.googleapis.com/css2?family=Xanh+Mono&amp;amp;display=swap"&lt;/span&gt;
      &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"preload"&lt;/span&gt;
    &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!-- A simple webpage --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Hello World&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's also supply additional attributes to instruct the details about the font we are preloading from the url. The attributes are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;type&lt;/code&gt; with the value of &lt;code&gt;font/woff2&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;as&lt;/code&gt; with the value of &lt;code&gt;font&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;crossorigin&lt;/code&gt; without any value&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It can be done like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="c"&gt;&amp;lt;!--
    Using the `link` tag and then the `rel="preload"` attribute
    to load the font as soon as possible.
  --&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt;
      &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://fonts.googleapis.com/css2?family=Xanh+Mono&amp;amp;display=swap"&lt;/span&gt;
      &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"preload"&lt;/span&gt;
      &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"font/woff2"&lt;/span&gt;
      &lt;span class="na"&gt;as=&lt;/span&gt;&lt;span class="s"&gt;"font"&lt;/span&gt;
      &lt;span class="na"&gt;crossorigin&lt;/span&gt;
    &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!-- A simple webpage --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Hello World&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will preload all the font files and cache them so that we can use the font.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE: The above &lt;code&gt;link&lt;/code&gt; tag with font preloading will only load the font and adds it first in the request chain and caches it. We still need to tell the browser we are going to use this font. To know more about it see the next blog on &lt;a href="https://melvingeorge.me/blog/use-preloaded-fonts-in-html" rel="noopener noreferrer"&gt;How to use the preloaded fonts in HTML?&lt;/a&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;See the above code live in the &lt;a href="https://codesandbox.io/s/load-fonts-as-soon-as-possible-or-preload-fonts-html-4hqj23?file=/index.html" rel="noopener noreferrer"&gt;codesandbox&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;That's all 😃.&lt;/p&gt;

</description>
      <category>html</category>
    </item>
    <item>
      <title>How to use preloaded fonts in HTML?</title>
      <dc:creator>MELVIN GEORGE</dc:creator>
      <pubDate>Mon, 12 Dec 2022 05:36:08 +0000</pubDate>
      <link>https://dev.to/melvin2016/how-to-use-preloaded-fonts-in-html-1g8p</link>
      <guid>https://dev.to/melvin2016/how-to-use-preloaded-fonts-in-html-1g8p</guid>
      <description>&lt;p&gt;&lt;a href="https://melvingeorge.me/blog/use-preloaded-fonts-html" rel="noopener noreferrer"&gt;Originally posted here!&lt;/a&gt;&lt;br&gt;
  To use the &lt;a href="https://melvingeorge.me/blog/load-font-from-url-as-soon-as-possible-or-preload-font-html" rel="noopener noreferrer"&gt;preloaded fonts&lt;/a&gt; in HTML, you have to use another &lt;code&gt;link&lt;/code&gt; tag and then use the &lt;code&gt;href&lt;/code&gt; attribute where the value is the same url you used for preloading the fonts followed by the &lt;code&gt;rel&lt;/code&gt; attribute where the value of it should be &lt;code&gt;stylesheet&lt;/code&gt;. The new &lt;code&gt;link&lt;/code&gt; tag must be after the preloading &lt;code&gt;link&lt;/code&gt; tag and should be inside the &lt;code&gt;head&lt;/code&gt; tag in the HTML code.&lt;/p&gt;
&lt;h3&gt;
  
  
  TL;DR
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="c"&gt;&amp;lt;!--
    Using the `link` tag and then the `rel="preload"` attribute
    to load the font as soon as possible.
  --&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt;
      &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://fonts.googleapis.com/css2?family=Xanh+Mono&amp;amp;display=swap"&lt;/span&gt;
      &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"preload"&lt;/span&gt;
      &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"font/woff2"&lt;/span&gt;
      &lt;span class="na"&gt;as=&lt;/span&gt;&lt;span class="s"&gt;"font"&lt;/span&gt;
      &lt;span class="na"&gt;crossorigin&lt;/span&gt;
    &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;

    &lt;span class="c"&gt;&amp;lt;!-- Using another `link` tag to use the preloaded font --&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt;
      &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://fonts.googleapis.com/css2?family=Xanh+Mono&amp;amp;display=swap"&lt;/span&gt;
      &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt;
    &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!-- A simple webpage --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Hello World&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;

  &lt;span class="c"&gt;&amp;lt;!-- Styles for the webpage --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;body&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;font-family&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Xanh&lt;/span&gt; &lt;span class="n"&gt;Mono&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;For example, let's say we have a webpage with the text of &lt;code&gt;Hello World&lt;/code&gt; where we also have a font preloaded using the &lt;code&gt;link&lt;/code&gt; tag like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="c"&gt;&amp;lt;!--
    Using the `link` tag and then the `rel="preload"` attribute
    to load the font as soon as possible.
  --&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt;
      &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://fonts.googleapis.com/css2?family=Xanh+Mono&amp;amp;display=swap"&lt;/span&gt;
      &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"preload"&lt;/span&gt;
      &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"font/woff2"&lt;/span&gt;
      &lt;span class="na"&gt;as=&lt;/span&gt;&lt;span class="s"&gt;"font"&lt;/span&gt;
      &lt;span class="na"&gt;crossorigin&lt;/span&gt;
    &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!-- A simple webpage --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Hello World&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we need to use another &lt;code&gt;link&lt;/code&gt; tag right after the preloaded &lt;code&gt;link&lt;/code&gt; tag. The new &lt;code&gt;link&lt;/code&gt; tag should have an attribute of &lt;code&gt;href&lt;/code&gt; and the value of it should be the same font url as the preloaded font url followed by the &lt;code&gt;rel&lt;/code&gt; attribute having the value of &lt;code&gt;stylesheet&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It can be done like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="c"&gt;&amp;lt;!--
    Using the `link` tag and then the `rel="preload"` attribute
    to load the font as soon as possible.
  --&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt;
      &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://fonts.googleapis.com/css2?family=Xanh+Mono&amp;amp;display=swap"&lt;/span&gt;
      &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"preload"&lt;/span&gt;
      &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"font/woff2"&lt;/span&gt;
      &lt;span class="na"&gt;as=&lt;/span&gt;&lt;span class="s"&gt;"font"&lt;/span&gt;
      &lt;span class="na"&gt;crossorigin&lt;/span&gt;
    &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;

    &lt;span class="c"&gt;&amp;lt;!-- Using another `link` tag to use the preloaded font --&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt;
      &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://fonts.googleapis.com/css2?family=Xanh+Mono&amp;amp;display=swap"&lt;/span&gt;
      &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt;
    &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!-- A simple webpage --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Hello World&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have now the font ready to be used, let's apply it to all the texts in the &lt;code&gt;body&lt;/code&gt; tag using the &lt;code&gt;font-family&lt;/code&gt; CSS property and then using the font's name which is &lt;code&gt;Xanh Mono&lt;/code&gt; as its value.&lt;/p&gt;

&lt;p&gt;It can be done like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="c"&gt;&amp;lt;!--
    Using the `link` tag and then the `rel="preload"` attribute
    to load the font as soon as possible.
  --&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt;
      &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://fonts.googleapis.com/css2?family=Xanh+Mono&amp;amp;display=swap"&lt;/span&gt;
      &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"preload"&lt;/span&gt;
      &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"font/woff2"&lt;/span&gt;
      &lt;span class="na"&gt;as=&lt;/span&gt;&lt;span class="s"&gt;"font"&lt;/span&gt;
      &lt;span class="na"&gt;crossorigin&lt;/span&gt;
    &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;

    &lt;span class="c"&gt;&amp;lt;!-- Using another `link` tag to use the preloaded font --&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt;
      &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://fonts.googleapis.com/css2?family=Xanh+Mono&amp;amp;display=swap"&lt;/span&gt;
      &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt;
    &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!-- A simple webpage --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Hello World&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;

  &lt;span class="c"&gt;&amp;lt;!-- Styles for the webpage --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;body&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;font-family&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Xanh&lt;/span&gt; &lt;span class="n"&gt;Mono&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have successfully used our preloaded font in HTML. Yay 🥳.&lt;/p&gt;

&lt;p&gt;See the above code live in the &lt;a href="https://codesandbox.io/s/use-preloaded-fonts-html-bt6sqf?file=/index.html" rel="noopener noreferrer"&gt;codesandbox&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;That's all 😃.&lt;/p&gt;

</description>
      <category>html</category>
    </item>
    <item>
      <title>How to set the aspect ratio for images using CSS?</title>
      <dc:creator>MELVIN GEORGE</dc:creator>
      <pubDate>Mon, 12 Dec 2022 05:35:38 +0000</pubDate>
      <link>https://dev.to/melvin2016/how-to-set-the-aspect-ratio-for-images-using-css-1lip</link>
      <guid>https://dev.to/melvin2016/how-to-set-the-aspect-ratio-for-images-using-css-1lip</guid>
      <description>&lt;p&gt;&lt;a href="https://melvingeorge.me/blog/set-aspect-ratio-for-images-using-css" rel="noopener noreferrer"&gt;Originally posted here!&lt;/a&gt;&lt;br&gt;
  To set the aspect ratio for images in a webpage using CSS, you can use the &lt;code&gt;aspect-ratio&lt;/code&gt; property and set a preferred aspect ratio value on the image element selector.&lt;/p&gt;
&lt;h3&gt;
  
  
  TL;DR
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!-- Styles for the webpage --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;img&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="py"&gt;max-inline-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;30vw&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="py"&gt;aspect-ratio&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;16&lt;/span&gt; &lt;span class="p"&gt;/&lt;/span&gt; &lt;span class="m"&gt;9&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;object-fit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;cover&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;article&amp;gt;&lt;/span&gt;
      &lt;span class="c"&gt;&amp;lt;!-- 
        add a width of 100% to stop the elements on the
        webpage from moving around while the image is being loaded 
      --&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt;
        &lt;span class="na"&gt;width=&lt;/span&gt;&lt;span class="s"&gt;"100%"&lt;/span&gt;
        &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://images.unsplash.com/photo-1667723047494-b19cebb52706?ixlib=rb-4.0.3&amp;amp;ixid=MnwxMjA3fDB8MHx0b3BpYy1mZWVkfDV8UzRNS0xBc0JCNzR8fGVufDB8fHx8&amp;amp;auto=format&amp;amp;fit=crop&amp;amp;w=800&amp;amp;q=60"&lt;/span&gt;
      &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Photo by Aloha Risha on Unsplash&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
        Billions upon billions of laws of physics culture globular star cluster
        the sky calls to us light years. Encyclopedia Galactica colonies how far
        away a still more glorious dawn awaits are creatures of the cosmos. (I
        mean if you want to read it. Go on. You are reading a random meaningless
        paragraph or are you? :/) Great turbulent clouds with pretty stories for
        which there's little good evidence invent the universe that something
        incredible is waiting to be known emerged into consciousness not a
        sunrise but a galaxy rise. Vastness is bearable only through love
        courage of our questions network of wormholes astonishment emerged into
        consciousness permanence of the stars and billions upon billions upon
        billions upon billions upon billions upon billions upon billions.
      &lt;span class="nt"&gt;&amp;lt;/p&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;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;For example, let's say we have a webpage with an image and a paragraph like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&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;img&lt;/span&gt;
        &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://images.unsplash.com/photo-1667723047494-b19cebb52706?ixlib=rb-4.0.3&amp;amp;ixid=MnwxMjA3fDB8MHx0b3BpYy1mZWVkfDV8UzRNS0xBc0JCNzR8fGVufDB8fHx8&amp;amp;auto=format&amp;amp;fit=crop&amp;amp;w=800&amp;amp;q=60"&lt;/span&gt;
      &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Photo by Aloha Risha on Unsplash&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
        Billions upon billions of laws of physics culture globular star cluster
        the sky calls to us light years. Encyclopedia Galactica colonies how far
        away a still more glorious dawn awaits are creatures of the cosmos. (I
        mean if you want to read it. Go on. You are reading a random meaningless
        paragraph or are you? :/) Great turbulent clouds with pretty stories for
        which there's little good evidence invent the universe that something
        incredible is waiting to be known emerged into consciousness not a
        sunrise but a galaxy rise. Vastness is bearable only through love
        courage of our questions network of wormholes astonishment emerged into
        consciousness permanence of the stars and billions upon billions upon
        billions upon billions upon billions upon billions upon billions.
      &lt;span class="nt"&gt;&amp;lt;/p&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;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Visually the webpage looks like this,&lt;/p&gt;

&lt;p&gt;&lt;a href="/uploads/01_a6db36ff2e.gif" class="article-body-image-wrapper"&gt;&lt;img src="/uploads/01_a6db36ff2e.gif" alt="image not sized according to webpage screen size"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see from the above GIF that the image is not properly sized, to remove that we can use the &lt;code&gt;max-inline-size&lt;/code&gt; property (&lt;code&gt;max-inline-size&lt;/code&gt; is just like the &lt;code&gt;max-width&lt;/code&gt; CSS property since it takes into consideration of the various language writing directions) and use the value of &lt;code&gt;30vw&lt;/code&gt; also can be read as 20 view width on the &lt;code&gt;img&lt;/code&gt; tag using the CSS.&lt;/p&gt;

&lt;p&gt;It can be done like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!-- Styles for the webpage --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;img&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="py"&gt;max-inline-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;30vw&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&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;img&lt;/span&gt;
        &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://images.unsplash.com/photo-1667723047494-b19cebb52706?ixlib=rb-4.0.3&amp;amp;ixid=MnwxMjA3fDB8MHx0b3BpYy1mZWVkfDV8UzRNS0xBc0JCNzR8fGVufDB8fHx8&amp;amp;auto=format&amp;amp;fit=crop&amp;amp;w=800&amp;amp;q=60"&lt;/span&gt;
      &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Photo by Aloha Risha on Unsplash&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
        Billions upon billions of laws of physics culture globular star cluster
        the sky calls to us light years. Encyclopedia Galactica colonies how far
        away a still more glorious dawn awaits are creatures of the cosmos. (I
        mean if you want to read it. Go on. You are reading a random meaningless
        paragraph or are you? :/) Great turbulent clouds with pretty stories for
        which there's little good evidence invent the universe that something
        incredible is waiting to be known emerged into consciousness not a
        sunrise but a galaxy rise. Vastness is bearable only through love
        courage of our questions network of wormholes astonishment emerged into
        consciousness permanence of the stars and billions upon billions upon
        billions upon billions upon billions upon billions upon billions.
      &lt;span class="nt"&gt;&amp;lt;/p&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;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="/uploads/02_c6760d5a51.png" class="article-body-image-wrapper"&gt;&lt;img src="/uploads/02_c6760d5a51.png" alt="image is now resized to 30vw of the screen"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now the image is resized to &lt;code&gt;30vw&lt;/code&gt; of the screen width.&lt;/p&gt;

&lt;p&gt;We aim to set the aspect ratio of the image to be in the &lt;code&gt;16:9&lt;/code&gt; value which is the standard value for a widescreen image.&lt;/p&gt;

&lt;p&gt;To do that, we can use the &lt;code&gt;aspect-ratio&lt;/code&gt; CSS property on the &lt;code&gt;img&lt;/code&gt; selector and set its value to &lt;code&gt;16 / 9&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It can be done like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!-- Styles for the webpage --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;img&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="py"&gt;max-inline-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;30vw&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="py"&gt;aspect-ratio&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;16&lt;/span&gt; &lt;span class="p"&gt;/&lt;/span&gt; &lt;span class="m"&gt;9&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&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;img&lt;/span&gt;
        &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://images.unsplash.com/photo-1667723047494-b19cebb52706?ixlib=rb-4.0.3&amp;amp;ixid=MnwxMjA3fDB8MHx0b3BpYy1mZWVkfDV8UzRNS0xBc0JCNzR8fGVufDB8fHx8&amp;amp;auto=format&amp;amp;fit=crop&amp;amp;w=800&amp;amp;q=60"&lt;/span&gt;
      &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Photo by Aloha Risha on Unsplash&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
        Billions upon billions of laws of physics culture globular star cluster
        the sky calls to us light years. Encyclopedia Galactica colonies how far
        away a still more glorious dawn awaits are creatures of the cosmos. (I
        mean if you want to read it. Go on. You are reading a random meaningless
        paragraph or are you? :/) Great turbulent clouds with pretty stories for
        which there's little good evidence invent the universe that something
        incredible is waiting to be known emerged into consciousness not a
        sunrise but a galaxy rise. Vastness is bearable only through love
        courage of our questions network of wormholes astonishment emerged into
        consciousness permanence of the stars and billions upon billions upon
        billions upon billions upon billions upon billions upon billions.
      &lt;span class="nt"&gt;&amp;lt;/p&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;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After setting the aspect ratio, the webpage now looks like this,&lt;/p&gt;

&lt;p&gt;&lt;a href="/uploads/03_ab63b75fc9.png" class="article-body-image-wrapper"&gt;&lt;img src="/uploads/03_ab63b75fc9.png" alt="image squeezed after setting the aspect ratio on the image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As soon as we set the aspect ratio of &lt;code&gt;16/9&lt;/code&gt; for the image, it got squeezed to take the aspect ratio we defined.&lt;/p&gt;

&lt;p&gt;To remove this problem we can use the &lt;code&gt;object-fit&lt;/code&gt; CSS property and use a value of &lt;code&gt;cover&lt;/code&gt; or &lt;code&gt;contain&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the &lt;code&gt;cover&lt;/code&gt; value on the &lt;code&gt;object-fit&lt;/code&gt; property will crop the image to take up the entire aspect ratio dimension.&lt;/li&gt;
&lt;li&gt;the &lt;code&gt;contain&lt;/code&gt; value on the &lt;code&gt;object-fit&lt;/code&gt; property will resize the image into the aspect ratio dimension but it will not crop the image.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's use the value of &lt;code&gt;cover&lt;/code&gt; on the &lt;code&gt;object-fit&lt;/code&gt; CSS property on the &lt;code&gt;img&lt;/code&gt; selector.&lt;/p&gt;

&lt;p&gt;It can be done like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!-- Styles for the webpage --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;img&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="py"&gt;max-inline-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;30vw&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="py"&gt;aspect-ratio&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;16&lt;/span&gt; &lt;span class="p"&gt;/&lt;/span&gt; &lt;span class="m"&gt;9&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;object-fit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;cover&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&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;img&lt;/span&gt;
        &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://images.unsplash.com/photo-1667723047494-b19cebb52706?ixlib=rb-4.0.3&amp;amp;ixid=MnwxMjA3fDB8MHx0b3BpYy1mZWVkfDV8UzRNS0xBc0JCNzR8fGVufDB8fHx8&amp;amp;auto=format&amp;amp;fit=crop&amp;amp;w=800&amp;amp;q=60"&lt;/span&gt;
      &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Photo by Aloha Risha on Unsplash&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
        Billions upon billions of laws of physics culture globular star cluster
        the sky calls to us light years. Encyclopedia Galactica colonies how far
        away a still more glorious dawn awaits are creatures of the cosmos. (I
        mean if you want to read it. Go on. You are reading a random meaningless
        paragraph or are you? :/) Great turbulent clouds with pretty stories for
        which there's little good evidence invent the universe that something
        incredible is waiting to be known emerged into consciousness not a
        sunrise but a galaxy rise. Vastness is bearable only through love
        courage of our questions network of wormholes astonishment emerged into
        consciousness permanence of the stars and billions upon billions upon
        billions upon billions upon billions upon billions upon billions.
      &lt;span class="nt"&gt;&amp;lt;/p&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;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The webpage now looks like this,&lt;/p&gt;

&lt;p&gt;&lt;a href="/uploads/Screenshot_2022_11_12_at_5_57_48_AM_85115cbef9.png" class="article-body-image-wrapper"&gt;&lt;img src="/uploads/Screenshot_2022_11_12_at_5_57_48_AM_85115cbef9.png" alt="image cropped to fit the entire aspect ratio dimension"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now the image is cropped to take up the entire dimension of the aspect ratio we provided which is the &lt;code&gt;16:9&lt;/code&gt; (widescreen mode dimension).&lt;/p&gt;

&lt;p&gt;Now there is one more problem I would like to show you, which is the moving of text contents or the elements on the webpage while the image is loading. This will be a bad user experience if the user has a slow network connection.&lt;/p&gt;

&lt;p&gt;See the below GIF to see the problem,&lt;/p&gt;

&lt;p&gt;&lt;a href="/uploads/04_fe864d4f64.gif" class="article-body-image-wrapper"&gt;&lt;img src="/uploads/04_fe864d4f64.gif" alt="text contents and webpage elements moving while image is being loaded"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To avoid this problem, all we have to do this set the &lt;code&gt;width&lt;/code&gt; attribute on the &lt;code&gt;img&lt;/code&gt; HTML tag to have the value of &lt;code&gt;100%&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It can be done like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!-- Styles for the webpage --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;img&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="py"&gt;max-inline-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;30vw&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="py"&gt;aspect-ratio&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;16&lt;/span&gt; &lt;span class="p"&gt;/&lt;/span&gt; &lt;span class="m"&gt;9&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;object-fit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;cover&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;article&amp;gt;&lt;/span&gt;
      &lt;span class="c"&gt;&amp;lt;!-- 
        add a width of 100% to stop the elements on the
        webpage from moving around while the image is being loaded 
      --&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt;
        &lt;span class="na"&gt;width=&lt;/span&gt;&lt;span class="s"&gt;"100%"&lt;/span&gt;
        &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://images.unsplash.com/photo-1667723047494-b19cebb52706?ixlib=rb-4.0.3&amp;amp;ixid=MnwxMjA3fDB8MHx0b3BpYy1mZWVkfDV8UzRNS0xBc0JCNzR8fGVufDB8fHx8&amp;amp;auto=format&amp;amp;fit=crop&amp;amp;w=800&amp;amp;q=60"&lt;/span&gt;
      &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Photo by Aloha Risha on Unsplash&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
        Billions upon billions of laws of physics culture globular star cluster
        the sky calls to us light years. Encyclopedia Galactica colonies how far
        away a still more glorious dawn awaits are creatures of the cosmos. (I
        mean if you want to read it. Go on. You are reading a random meaningless
        paragraph or are you? :/) Great turbulent clouds with pretty stories for
        which there's little good evidence invent the universe that something
        incredible is waiting to be known emerged into consciousness not a
        sunrise but a galaxy rise. Vastness is bearable only through love
        courage of our questions network of wormholes astonishment emerged into
        consciousness permanence of the stars and billions upon billions upon
        billions upon billions upon billions upon billions upon billions.
      &lt;span class="nt"&gt;&amp;lt;/p&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;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="/uploads/05_e8195e0181.gif" class="article-body-image-wrapper"&gt;&lt;img src="/uploads/05_e8195e0181.gif" alt="texts content and elements stays in place while image is being loaded"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have successfully set an aspect ratio for the image using CSS. Yay 🥳!&lt;/p&gt;

&lt;p&gt;See the above code live in the &lt;a href="https://codesandbox.io/s/set-aspect-ratio-for-images-using-css-9j7274?file=/index.html" rel="noopener noreferrer"&gt;codesandbox&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;That's all 😃.&lt;/p&gt;

</description>
      <category>css</category>
    </item>
    <item>
      <title>How to fetch an image on priority using HTML?</title>
      <dc:creator>MELVIN GEORGE</dc:creator>
      <pubDate>Mon, 12 Dec 2022 05:35:09 +0000</pubDate>
      <link>https://dev.to/melvin2016/how-to-fetch-an-image-on-priority-using-html-1bl6</link>
      <guid>https://dev.to/melvin2016/how-to-fetch-an-image-on-priority-using-html-1bl6</guid>
      <description>&lt;p&gt;&lt;a href="https://melvingeorge.me/blog/fetch-image-on-priority-using-html" rel="noopener noreferrer"&gt;Originally posted here!&lt;/a&gt;&lt;br&gt;
  To fetch an image on priority on a webpage using HTML, you can use the &lt;code&gt;link&lt;/code&gt; HTML tag and use the attribute of &lt;code&gt;rel&lt;/code&gt; with the value of &lt;code&gt;prefetch&lt;/code&gt; which instructs the browser that the image needed to be downloaded on priority, then the &lt;code&gt;href&lt;/code&gt; attribute with the value of the image's url and finally the &lt;code&gt;as&lt;/code&gt; attribute having the value of &lt;code&gt;image&lt;/code&gt; to define the type of the asset you are prefetching.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;link&lt;/code&gt; tag should be placed inside the &lt;code&gt;head&lt;/code&gt; tag of the HTML code.&lt;/p&gt;
&lt;h3&gt;
  
  
  TL;DR
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!-- 
    The `link` tag for prefetching the image
    should be inside the `head` tag 
  --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt;
    &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"prefetch"&lt;/span&gt;
    &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://images.unsplash.com/photo-1667584412550-95241626c80a?ixlib=rb-4.0.3&amp;amp;ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;amp;auto=format&amp;amp;fit=crop&amp;amp;w=687&amp;amp;q=80"&lt;/span&gt;
    &lt;span class="na"&gt;as=&lt;/span&gt;&lt;span class="s"&gt;"image"&lt;/span&gt;
  &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;

  &lt;span class="c"&gt;&amp;lt;!-- Basic style for the image to make it responsive --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;img&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="py"&gt;max-inline-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;20vw&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="py"&gt;block-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;auto&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="py"&gt;aspect-ratio&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="p"&gt;/&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;object-fit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;contain&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt;
      &lt;span class="na"&gt;width=&lt;/span&gt;&lt;span class="s"&gt;"100%"&lt;/span&gt;
      &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://images.unsplash.com/photo-1667584412550-95241626c80a?ixlib=rb-4.0.3&amp;amp;ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;amp;auto=format&amp;amp;fit=crop&amp;amp;w=687&amp;amp;q=80"&lt;/span&gt;
    &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Photo by Nico Knaack on Unsplash&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
      Lorem ipsum dolor sit amet consectetur adipisicing elit. Provident quis
      sequi placeat quisquam doloremque dignissimos qui, asperiores maiores nam,
      repudiandae, recusandae distinctio libero odio nesciunt quidem repellat
      laudantium quo error itaque! Placeat corporis inventore saepe quae non
      sunt fugit omnis, architecto quibusdam incidunt ex voluptatum et itaque
      alias, nisi dolores molestias aliquam officia fuga aut nulla perferendis.
      Ea sapiente corrupti minima eum harum, voluptatibus molestiae.
    &lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;NOTE: Only use the prefetching of images using the &lt;code&gt;link&lt;/code&gt; tag if the image is of vital importance. Since prefetching, an image using the &lt;code&gt;link&lt;/code&gt; tag will stop all the other prefetches like the prefetching of scripts, fonts, etc.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For example, let's say we have a webpage with some text and an image.&lt;/p&gt;

&lt;p&gt;The HTML code would look something like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;img&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="py"&gt;max-inline-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;20vw&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="py"&gt;block-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;auto&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="py"&gt;aspect-ratio&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="p"&gt;/&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;object-fit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;contain&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt;
      &lt;span class="na"&gt;width=&lt;/span&gt;&lt;span class="s"&gt;"100%"&lt;/span&gt;
      &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://images.unsplash.com/photo-1667584412550-95241626c80a?ixlib=rb-4.0.3&amp;amp;ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;amp;auto=format&amp;amp;fit=crop&amp;amp;w=687&amp;amp;q=80"&lt;/span&gt;
    &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Photo by Nico Knaack on Unsplash&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
      Lorem ipsum dolor sit amet consectetur adipisicing elit. Provident quis
      sequi placeat quisquam doloremque dignissimos qui, asperiores maiores nam,
      repudiandae, recusandae distinctio libero odio nesciunt quidem repellat
      laudantium quo error itaque! Placeat corporis inventore saepe quae non
      sunt fugit omnis, architecto quibusdam incidunt ex voluptatum et itaque
      alias, nisi dolores molestias aliquam officia fuga aut nulla perferendis.
      Ea sapiente corrupti minima eum harum, voluptatibus molestiae.
    &lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see from the above code we have an &lt;code&gt;img&lt;/code&gt; tag where the &lt;code&gt;src&lt;/code&gt; attribute has the value of the image location. We have also added some basic style to the image to make it responsive to the screen to make it nicer to look at.&lt;/p&gt;

&lt;p&gt;The webpage would look like this,&lt;/p&gt;

&lt;p&gt;&lt;a href="/uploads/Screenshot_2022_11_12_at_7_53_10_PM_bcc507f5b1.png" class="article-body-image-wrapper"&gt;&lt;img src="/uploads/Screenshot_2022_11_12_at_7_53_10_PM_bcc507f5b1.png" alt="a webpage with some random text and an image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We aim to prefetch the same image and cache it in our browser, to do that we have to use the &lt;code&gt;link&lt;/code&gt; tag then use the &lt;code&gt;rel&lt;/code&gt; attribute with the value of &lt;code&gt;prefetch&lt;/code&gt;, then the &lt;code&gt;href&lt;/code&gt; attribute with the value of the image url which in our case is the &lt;code&gt;https://images.unsplash.com/photo-1667584412550-95241626c80a?ixlib=rb-4.0.3&amp;amp;ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;amp;auto=format&amp;amp;fit=crop&amp;amp;w=687&amp;amp;q=80&lt;/code&gt; url and finally using the &lt;code&gt;as&lt;/code&gt; attribute having the value of &lt;code&gt;image&lt;/code&gt; to set the type of the asset we are prefetching.&lt;/p&gt;

&lt;p&gt;It can be done like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!-- 
    The `link` tag for prefetching the image
    should be inside the `head` tag 
  --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt;
    &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"prefetch"&lt;/span&gt;
    &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://images.unsplash.com/photo-1667584412550-95241626c80a?ixlib=rb-4.0.3&amp;amp;ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;amp;auto=format&amp;amp;fit=crop&amp;amp;w=687&amp;amp;q=80"&lt;/span&gt;
    &lt;span class="na"&gt;as=&lt;/span&gt;&lt;span class="s"&gt;"image"&lt;/span&gt;
  &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;

  &lt;span class="c"&gt;&amp;lt;!-- Basic style for the image to make it responsive --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;img&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="py"&gt;max-inline-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;20vw&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="py"&gt;block-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;auto&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="py"&gt;aspect-ratio&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="p"&gt;/&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;object-fit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;contain&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt;
      &lt;span class="na"&gt;width=&lt;/span&gt;&lt;span class="s"&gt;"100%"&lt;/span&gt;
      &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://images.unsplash.com/photo-1667584412550-95241626c80a?ixlib=rb-4.0.3&amp;amp;ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;amp;auto=format&amp;amp;fit=crop&amp;amp;w=687&amp;amp;q=80"&lt;/span&gt;
    &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Photo by Nico Knaack on Unsplash&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
      Lorem ipsum dolor sit amet consectetur adipisicing elit. Provident quis
      sequi placeat quisquam doloremque dignissimos qui, asperiores maiores nam,
      repudiandae, recusandae distinctio libero odio nesciunt quidem repellat
      laudantium quo error itaque! Placeat corporis inventore saepe quae non
      sunt fugit omnis, architecto quibusdam incidunt ex voluptatum et itaque
      alias, nisi dolores molestias aliquam officia fuga aut nulla perferendis.
      Ea sapiente corrupti minima eum harum, voluptatibus molestiae.
    &lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the image will be prefetched before all other scripts, font files, etc., and will be cached by the browser for later use.&lt;/p&gt;

&lt;p&gt;We have successfully fetched an image on priority using the HTML. Yay 🥳!&lt;/p&gt;

&lt;p&gt;See the above code live in the &lt;a href="https://codesandbox.io/s/fetch-image-on-priority-using-html-7td7mv?file=/index.html" rel="noopener noreferrer"&gt;codesandbox&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;That's all 😃.&lt;/p&gt;

</description>
      <category>html</category>
    </item>
    <item>
      <title>How to parallelly process or decode the image to improve webpage performance?</title>
      <dc:creator>MELVIN GEORGE</dc:creator>
      <pubDate>Mon, 12 Dec 2022 05:33:04 +0000</pubDate>
      <link>https://dev.to/melvin2016/how-to-parallelly-process-or-decode-the-image-to-improve-webpage-performance-15ak</link>
      <guid>https://dev.to/melvin2016/how-to-parallelly-process-or-decode-the-image-to-improve-webpage-performance-15ak</guid>
      <description>&lt;p&gt;&lt;a href="https://melvingeorge.me/blog/parallelly-process-or-decode-the-image-to-improve-webpage-performance" rel="noopener noreferrer"&gt;Originally posted here!&lt;/a&gt;&lt;br&gt;
  To parallelly process or decode the image to improve webpage performance, you can use the &lt;code&gt;decoding&lt;/code&gt; attribute with the value of &lt;code&gt;async&lt;/code&gt; on the HTML &lt;code&gt;img&lt;/code&gt; tag.&lt;/p&gt;

&lt;p&gt;Using the value of &lt;code&gt;async&lt;/code&gt; on the &lt;code&gt;decoding&lt;/code&gt; attribute will still download the images but the image's process will be done parallelly and thus the browser can resume the processes for other kinds of data such as the texts on the webpage. This will eliminate blocking the browser rendering and improve the perceived performance of the website.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE: To know more about lazily loading the images as the user scrolls the webpage, see the blog on &lt;a href="https://melvingeorge.me/blog/native-image-lazy-loading-html" rel="noopener noreferrer"&gt;How to lazy load images in websites the easy way?&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  TL;DR
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!-- Basic styles for image --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;img&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="py"&gt;max-inline-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;30vw&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="py"&gt;block-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;auto&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="c"&gt;&amp;lt;!-- 
      Add the `decoding` attribute with the value of `async`
      to parallelly process or decode the image file.
    --&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt;
      &lt;span class="na"&gt;decoding=&lt;/span&gt;&lt;span class="s"&gt;"async"&lt;/span&gt;
      &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://images.unsplash.com/photo-1666797630713-f5a2e54d3d23?ixlib=rb-4.0.3&amp;amp;ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;amp;auto=format&amp;amp;fit=crop&amp;amp;w=1740&amp;amp;q=80"&lt;/span&gt;
    &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Photo by Dimitry Anikin on Unsplash&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
      Lorem ipsum dolor sit amet consectetur adipisicing elit. Recusandae
      expedita iusto cupiditate incidunt aliquid deserunt aut accusamus cum
      dignissimos dolore aliquam ullam rerum aperiam alias tenetur saepe
      laboriosam, tempora et illum voluptatum a! Adipisci, sapiente! Fuga ex
      esse magni, non labore architecto iusto? Sequi saepe, laboriosam
      reiciendis nisi vero aut eveniet amet labore voluptas ullam perspiciatis.
      Voluptate, sit veritatis explicabo ullam praesentium beatae saepe aperiam?
    &lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;For example, let's say we have a webpage with an image and a random paragraph like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!-- Basic styles for image --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;img&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="py"&gt;max-inline-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;30vw&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="py"&gt;block-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;auto&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&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;"https://images.unsplash.com/photo-1666797630713-f5a2e54d3d23?ixlib=rb-4.0.3&amp;amp;ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;amp;auto=format&amp;amp;fit=crop&amp;amp;w=1740&amp;amp;q=80"&lt;/span&gt;
    &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Photo by Dimitry Anikin on Unsplash&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
      Lorem ipsum dolor sit amet consectetur adipisicing elit. Recusandae
      expedita iusto cupiditate incidunt aliquid deserunt aut accusamus cum
      dignissimos dolore aliquam ullam rerum aperiam alias tenetur saepe
      laboriosam, tempora et illum voluptatum a! Adipisci, sapiente! Fuga ex
      esse magni, non labore architecto iusto? Sequi saepe, laboriosam
      reiciendis nisi vero aut eveniet amet labore voluptas ullam perspiciatis.
      Voluptate, sit veritatis explicabo ullam praesentium beatae saepe aperiam?
    &lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see from the above code we have given some basic styles to the image to make it responsive to the screen.&lt;/p&gt;

&lt;p&gt;Now let's add the &lt;code&gt;decoding&lt;/code&gt; attribute and use the value of &lt;code&gt;async&lt;/code&gt; to the &lt;code&gt;img&lt;/code&gt; HTML tag to instruct the browser to parallelly process or decode the image file.&lt;/p&gt;

&lt;p&gt;It can be done like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!-- Basic styles for image --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;img&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="py"&gt;max-inline-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;30vw&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="py"&gt;block-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;auto&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="c"&gt;&amp;lt;!-- 
      Add the `decoding` attribute with the value of `async`
      to parallelly process or decode the image file.
    --&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt;
      &lt;span class="na"&gt;decoding=&lt;/span&gt;&lt;span class="s"&gt;"async"&lt;/span&gt;
      &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://images.unsplash.com/photo-1666797630713-f5a2e54d3d23?ixlib=rb-4.0.3&amp;amp;ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;amp;auto=format&amp;amp;fit=crop&amp;amp;w=1740&amp;amp;q=80"&lt;/span&gt;
    &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Photo by Dimitry Anikin on Unsplash&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
      Lorem ipsum dolor sit amet consectetur adipisicing elit. Recusandae
      expedita iusto cupiditate incidunt aliquid deserunt aut accusamus cum
      dignissimos dolore aliquam ullam rerum aperiam alias tenetur saepe
      laboriosam, tempora et illum voluptatum a! Adipisci, sapiente! Fuga ex
      esse magni, non labore architecto iusto? Sequi saepe, laboriosam
      reiciendis nisi vero aut eveniet amet labore voluptas ullam perspiciatis.
      Voluptate, sit veritatis explicabo ullam praesentium beatae saepe aperiam?
    &lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have successfully decoded the image file parallelly and improved the webpage performance.&lt;/p&gt;

&lt;p&gt;See the above code live in the &lt;a href="https://codesandbox.io/s/parallelly-process-or-decode-the-image-to-improve-webpage-performance-using-html-nrxbc4?file=/index.html" rel="noopener noreferrer"&gt;codesandbox&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;That's all 😃.&lt;/p&gt;

</description>
      <category>html</category>
    </item>
    <item>
      <title>How to load images of different sizes on different screen widths using HTML?</title>
      <dc:creator>MELVIN GEORGE</dc:creator>
      <pubDate>Mon, 12 Dec 2022 05:32:34 +0000</pubDate>
      <link>https://dev.to/melvin2016/how-to-load-images-of-different-sizes-on-different-screen-widths-using-html-e9k</link>
      <guid>https://dev.to/melvin2016/how-to-load-images-of-different-sizes-on-different-screen-widths-using-html-e9k</guid>
      <description>&lt;p&gt;&lt;a href="https://melvingeorge.me/blog/load-images-of-different-sizes-on-different-screen-widths-using-html" rel="noopener noreferrer"&gt;Originally posted here!&lt;/a&gt;&lt;br&gt;
  To load images of different sizes on different screen widths or sizes using HTML, you can use the &lt;code&gt;srcset&lt;/code&gt; attribute on the &lt;code&gt;img&lt;/code&gt; HTML tag and the value should be the url of the image followed by a whitespace and the original width of the image. The width should end with the unit of &lt;code&gt;w&lt;/code&gt; (1w means 1px). If you have more image sizes then the &lt;code&gt;,&lt;/code&gt; symbol (comma) should be used to separate the values (image url and its original width).&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;srcset&lt;/code&gt; should be used along with the &lt;code&gt;src&lt;/code&gt; attribute. According to the user's device width, the browser will automatically choose the image url from the &lt;code&gt;srcset&lt;/code&gt; attribute values and replace it with the default value on the &lt;code&gt;src&lt;/code&gt; attribute.&lt;/p&gt;
&lt;h2&gt;
  
  
  Why is this used?
&lt;/h2&gt;

&lt;p&gt;Sometimes on smaller screen devices, downloading a big image is not needed and will also be affecting the network bandwidth of the device which in turn affects the performance of our website on smaller devices. To compact this problem the &lt;code&gt;srcset&lt;/code&gt; attribute is introduced in the &lt;code&gt;img&lt;/code&gt; HTML tag.&lt;/p&gt;
&lt;h3&gt;
  
  
  TL;DR
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
    &lt;span class="c"&gt;/* Basic styles for image */&lt;/span&gt;
    &lt;span class="nt"&gt;img&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="py"&gt;max-inline-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;30vw&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="py"&gt;block-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;auto&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="c"&gt;&amp;lt;!-- 
      using the `srcset` attribute on the `img` HTML tag to set
      different image URLs and the image widths to be chosen
      automatically by the browser according to the user's device-width  
    --&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;"/01_small.jpeg"&lt;/span&gt;
      &lt;span class="na"&gt;srcset=&lt;/span&gt;&lt;span class="s"&gt;"/01_small.jpeg 640w, /02_medium.jpeg 1920w, /03_large.jpeg 2400w"&lt;/span&gt;
    &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Photo by Neha Maheen Mahfin on Unsplash&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
      Lorem, ipsum dolor sit amet consectetur adipisicing elit. Repellat ipsam
      aspernatur totam consequatur labore? Vitae voluptas pariatur quos
      repellendus aliquam at magni asperiores nostrum, atque a vero odio. Odit
      dolores quae voluptas enim error quas, architecto minus voluptates. Illum,
      alias molestias ipsam quo accusantium magnam modi eveniet fugit
      perferendis officia dolor nemo eos dignissimos in ipsum neque autem
      voluptas obcaecati. Reiciendis doloremque sed et quo.
    &lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;For example, let's say we have a webpage with an image tag (&lt;code&gt;img&lt;/code&gt;) and a random paragraph (&lt;code&gt;p&lt;/code&gt;) like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
    &lt;span class="c"&gt;/* Basic styles for image */&lt;/span&gt;
    &lt;span class="nt"&gt;img&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="py"&gt;max-inline-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;30vw&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="py"&gt;block-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;auto&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&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;"/01_small.jpeg"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Photo by Neha Maheen Mahfin on Unsplash&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
      Lorem, ipsum dolor sit amet consectetur adipisicing elit. Repellat ipsam
      aspernatur totam consequatur labore? Vitae voluptas pariatur quos
      repellendus aliquam at magni asperiores nostrum, atque a vero odio. Odit
      dolores quae voluptas enim error quas, architecto minus voluptates. Illum,
      alias molestias ipsam quo accusantium magnam modi eveniet fugit
      perferendis officia dolor nemo eos dignissimos in ipsum neque autem
      voluptas obcaecati. Reiciendis doloremque sed et quo.
    &lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The webpage now looks like this,&lt;/p&gt;

&lt;p&gt;&lt;a href="/uploads/Screenshot_2022_11_14_at_9_17_18_PM_c53d848d20.png" class="article-body-image-wrapper"&gt;&lt;img src="/uploads/Screenshot_2022_11_14_at_9_17_18_PM_c53d848d20.png" alt="simple webpage with an image and a random paragraph"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see from the above code that in the &lt;code&gt;img&lt;/code&gt; HTML tag, we have used the &lt;code&gt;src&lt;/code&gt; tag and given an image url as the value. Currently, the browser downloads this image on every screen size.&lt;/p&gt;

&lt;p&gt;Now let's introduce the &lt;code&gt;srcset&lt;/code&gt; attribute on the &lt;code&gt;img&lt;/code&gt; tag and use different image URLs for different screen widths.&lt;/p&gt;

&lt;p&gt;These are the image URLs and their original widths which we are going to set as the values of the &lt;code&gt;srcset&lt;/code&gt; attribute:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;for the image url &lt;code&gt;01_small.jpeg&lt;/code&gt; it has an original width of &lt;code&gt;640w&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;for the image url &lt;code&gt;02_medium.jpeg&lt;/code&gt; it has an original width of &lt;code&gt;1920w&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;for the image url &lt;code&gt;03_large.jpeg&lt;/code&gt; it has an original width of &lt;code&gt;2400w&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The values for the &lt;code&gt;srcset&lt;/code&gt; attribute should be in the format where the image url is first followed by a whitespace and then the original width of the image with the unit of &lt;code&gt;w&lt;/code&gt;. Since we have three image URLs, we have to separate them using the &lt;code&gt;,&lt;/code&gt; symbol (comma).&lt;/p&gt;

&lt;p&gt;It can be done like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
    &lt;span class="c"&gt;/* Basic styles for image */&lt;/span&gt;
    &lt;span class="nt"&gt;img&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="py"&gt;max-inline-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;30vw&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="py"&gt;block-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;auto&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="c"&gt;&amp;lt;!-- 
      using the `srcset` attribute on the `img` HTML tag to set
      different image URLs and the image widths to be chosen
      automatically by the browser according to the user's device-width  
    --&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;"/01_small.jpeg"&lt;/span&gt;
      &lt;span class="na"&gt;srcset=&lt;/span&gt;&lt;span class="s"&gt;"/01_small.jpeg 640w, /02_medium.jpeg 1920w, /03_large.jpeg 2400w"&lt;/span&gt;
    &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Photo by Neha Maheen Mahfin on Unsplash&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
      Lorem, ipsum dolor sit amet consectetur adipisicing elit. Repellat ipsam
      aspernatur totam consequatur labore? Vitae voluptas pariatur quos
      repellendus aliquam at magni asperiores nostrum, atque a vero odio. Odit
      dolores quae voluptas enim error quas, architecto minus voluptates. Illum,
      alias molestias ipsam quo accusantium magnam modi eveniet fugit
      perferendis officia dolor nemo eos dignissimos in ipsum neque autem
      voluptas obcaecati. Reiciendis doloremque sed et quo.
    &lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's check the &lt;code&gt;Devtools&lt;/code&gt; (if you are in chrome, you can right-click on the webpage, and select the &lt;code&gt;Inspect&lt;/code&gt; option) and go to the &lt;code&gt;Network&lt;/code&gt; tab to see which size is getting downloaded for each screen size.&lt;/p&gt;

&lt;h3&gt;
  
  
  Screen size of &lt;code&gt;1440px&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="/uploads/Screenshot_2022_11_14_at_10_26_50_PM_aac4753593.png" class="article-body-image-wrapper"&gt;&lt;img src="/uploads/Screenshot_2022_11_14_at_10_26_50_PM_aac4753593.png" alt="large sized image is used in the webpage for screen size of 1440px"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Screen size of &lt;code&gt;1000px&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="/uploads/Screenshot_2022_11_14_at_10_30_45_PM_c2e6c076f6.png" class="article-body-image-wrapper"&gt;&lt;img src="/uploads/Screenshot_2022_11_14_at_10_30_45_PM_c2e6c076f6.png" alt="medium sized image is used in the webpage for screen size of 1000px"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Screen size of &lt;code&gt;500px&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="/uploads/Screenshot_2022_11_14_at_10_32_58_PM_3a9324976a.png" class="article-body-image-wrapper"&gt;&lt;img src="/uploads/Screenshot_2022_11_14_at_10_32_58_PM_3a9324976a.png" alt="small sized image is used in the webpage for screen size of 500px"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This will help a lot in improving the performance of the webpage.&lt;/p&gt;

&lt;p&gt;We have successfully loaded different image sizes on different screen sizes or widths using HTML. Yay 🥳.&lt;/p&gt;

&lt;p&gt;See the above code live in the &lt;a href="https://codesandbox.io/s/load-images-of-different-sizes-on-different-screen-widths-using-html-4xqbv2?file=/index.html" rel="noopener noreferrer"&gt;codesandbox&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;That's all 😃.&lt;/p&gt;

</description>
      <category>html</category>
    </item>
  </channel>
</rss>
