<?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: Jan Drewniak</title>
    <description>The latest articles on DEV Community by Jan Drewniak (@jandre3000).</description>
    <link>https://dev.to/jandre3000</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%2F339201%2Ff95902b0-1a9b-4121-a388-30547732254b.jpg</url>
      <title>DEV Community: Jan Drewniak</title>
      <link>https://dev.to/jandre3000</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jandre3000"/>
    <language>en</language>
    <item>
      <title>Why isn’t `0` a valid time value in CSS?</title>
      <dc:creator>Jan Drewniak</dc:creator>
      <pubDate>Sat, 22 Feb 2020 05:30:30 +0000</pubDate>
      <link>https://dev.to/jandre3000/why-isn-t-0-a-valid-time-value-in-css-2ce8</link>
      <guid>https://dev.to/jandre3000/why-isn-t-0-a-valid-time-value-in-css-2ce8</guid>
      <description>&lt;p&gt;If you’ve ever written a CSS transition property like the one below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.a&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;transition&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;translate&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="n"&gt;ease-out&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You’ll notice that Devtools informs you that the CSS is invalid.&lt;/p&gt;

&lt;p&gt;Rewrite that line adding the &lt;code&gt;ms&lt;/code&gt; unit after the zero:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.a&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;transition&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;translate&lt;/span&gt; &lt;span class="m"&gt;0ms&lt;/span&gt; &lt;span class="n"&gt;ease-out&lt;/span&gt; &lt;span class="m"&gt;0ms&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And the CSS is fine and dandy.&lt;/p&gt;

&lt;h2&gt;
  
  
  So what’s the difference between &lt;code&gt;0&lt;/code&gt; and &lt;code&gt;0ms&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;The first value is a plain, unitless number (and let’s agree that zero is in fact a &lt;a href="https://www.quora.com/Is-the-number-zero-considered-a-real-number"&gt;real&lt;/a&gt; number). In the CSS specification, numbers are referred to as &lt;strong&gt;numeric data types&lt;/strong&gt;. The &lt;a href="https://www.w3.org/TR/css-values-3/"&gt;numeric data types&lt;/a&gt; in CSS include the following: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;length&amp;gt;&lt;/code&gt; type - (&lt;code&gt;px&lt;/code&gt;, &lt;code&gt;em&lt;/code&gt;, &lt;code&gt;rem&lt;/code&gt; ...there’s a tonne)
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;angle&amp;gt;&lt;/code&gt; type - (&lt;code&gt;deg&lt;/code&gt;, &lt;code&gt;grad&lt;/code&gt;, &lt;code&gt;rad&lt;/code&gt;, &lt;code&gt;turn&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;frequency&amp;gt;&lt;/code&gt; type -  (&lt;code&gt;Hz&lt;/code&gt;, &lt;code&gt;kHz&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;resolution&amp;gt;&lt;/code&gt; type - &lt;code&gt;dpi&lt;/code&gt;, &lt;code&gt;dpcm&lt;/code&gt;, &lt;code&gt;dppx&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  On Zero
&lt;/h2&gt;

&lt;p&gt;But the unitless zero isn’t mentioned in any of those! The only mention of zero in the specification is a &lt;strong&gt;single line&lt;/strong&gt; regarding length units:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;For zero lengths the unit identifier is optional (i.e. can be syntactically represented as the &lt;a href="https://www.w3.org/TR/css-values-3/#number-value"&gt;&amp;lt;number&amp;gt;&lt;/a&gt; 0).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;— &lt;a href="https://www.w3.org/TR/css-values-3/#lengths"&gt;CSS Values 3, lengths&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So this implies that &lt;strong&gt;you can only use a unitless Zero as a &lt;a href="https://www.w3.org/TR/css-values-3/#lengths"&gt;&amp;lt;length&amp;gt;&lt;/a&gt; unit&lt;/strong&gt;. Ok... but why is that the case? &lt;/p&gt;

&lt;h2&gt;
  
  
  A bit of history
&lt;/h2&gt;

&lt;p&gt;To understand why the unitless zero is only valid for length values and not others, we have to go beyond the spec and read some of the conversation around it. &lt;/p&gt;

&lt;p&gt;Luckily, W3C conversations around the specification are all transcribed and recorded on public mailing lists. Here’s part of a meeting of the CSS Working Group from &lt;a href="https://lists.w3.org/Archives/Public/www-style/2016Mar/0205.html"&gt;March 2016&lt;/a&gt;: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;glazou&lt;/strong&gt;: From author's perspective, &lt;strong&gt;0 is 0 and you don't need a unit&lt;/strong&gt;.&lt;br&gt;
  &lt;strong&gt;glazou&lt;/strong&gt;: Did consider in the past.&lt;br&gt;
  &lt;strong&gt;glazou&lt;/strong&gt;: Same argument for 0 vs 0px.&lt;br&gt;
  &lt;strong&gt;glazou&lt;/strong&gt;: Should we make the same choice today?&lt;br&gt;
  &lt;strong&gt;glazou&lt;/strong&gt;: Isn't 0 the same as 0deg?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And here’s another one &lt;a href="https://lists.w3.org/Archives/Public/www-style/2017Apr/0027.html"&gt;from April 2017&lt;/a&gt;: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TabAtkins&lt;/strong&gt;: A while back in a meeting I missed we decided to allow&lt;br&gt;
             unitless 0 for all angles because there were places&lt;br&gt;
             where impl allowed it. I don't think we did for all&lt;br&gt;
             dimensions.&lt;br&gt;
  &lt;strong&gt;fantasai&lt;/strong&gt;: Correct.&lt;br&gt;
  &lt;strong&gt;TabAtkins&lt;/strong&gt;: Even just on length vs angle &lt;strong&gt;it causes grammar&lt;br&gt;
             ambiguity&lt;/strong&gt; in motion where the shorthand can combine a&lt;br&gt;
             length and angle. In general this will become a&lt;br&gt;
             problem. &lt;strong&gt;Unitless 0 are a legacy mistake&lt;/strong&gt;. This will&lt;br&gt;
             cause footguns in the future. I would like to revisit&lt;br&gt;
             the decision and make it unitless 0 angles in the&lt;br&gt;
             functions that allow them as a legacy thing and then&lt;br&gt;
             restrict to just lengths in grammars.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hmmm... “Unitless 0 are a legacy mistake”? That sounds interesting. Could it be that the unitless zero is just a bad idea, and it works for lengths but not other number types because lengths were implemented first? Let’s take the unitless zero for a spin and see if it causes any “ambiguity or confusion in the grammars”.&lt;/p&gt;

&lt;h2&gt;
  
  
  Is unitless zero just a mistake?
&lt;/h2&gt;

&lt;p&gt;Let’s take the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/animation"&gt;animation example&lt;/a&gt; from MDN:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.a&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c"&gt;/* @keyframes duration | timing-function | delay | 
    iteration-count | direction | fill-mode | play-state | name */&lt;/span&gt;
    &lt;span class="nl"&gt;animation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;3s&lt;/span&gt; &lt;span class="n"&gt;ease-in&lt;/span&gt; &lt;span class="m"&gt;0s&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="n"&gt;reverse&lt;/span&gt; &lt;span class="nb"&gt;both&lt;/span&gt; &lt;span class="n"&gt;paused&lt;/span&gt; &lt;span class="n"&gt;slidein&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The full animation shorthand contains an &lt;strong&gt;iteration-count&lt;/strong&gt; value, which is a &lt;strong&gt;unitless number&lt;/strong&gt;. If unitless zeros were allowed for time values, it would look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.a&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;animation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;ease-in&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="n"&gt;reverse&lt;/span&gt; &lt;span class="nb"&gt;both&lt;/span&gt; &lt;span class="n"&gt;paused&lt;/span&gt; &lt;span class="n"&gt;slidein&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Oh yeah, that does look confusing. See the &lt;code&gt;0 0&lt;/code&gt;? which is the time value and which is the iteration count?&lt;/p&gt;

&lt;p&gt;So I guess, as Tab Atkins put it, the unitless zero in most cases is just a &lt;strong&gt;footgun&lt;/strong&gt;. &lt;/p&gt;

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