title: Why does "position: absolute; left: 0; right: 0; width: XYpx; margin: 0 auto" actually center?
body: <p>I'm pretty proficient when it comes to CSS but today I stumbled over a snippet that got me head-scratching (<a href="https://stackoverflow.com/a/19341954/151509">here</a> and <a href="https://stackoverflow.com/a/29260884/151509">here</a>).</p>
  
  <p>I never thought that one can center an absolutely positioned element via <code>margin: 0 auto</code> but given the element in question has a set width and there's <code>left: 0</code> and <code>right: 0</code>, it actually seems to work:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-css lang-css prettyprint-override"><code>#parent
  {
      background: #999;
      height: 300px;
      position: relative;
      width: 300px;
  }
  
  #child
  {
      background: #333;
      height: 50px;
      left: 0;
      margin: 0 auto;
      position: absolute;
      right: 0;
      width: 50px;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;div id="parent"&gt;
      &lt;div id="child"&gt;&lt;/div&gt;
  &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>(<a href="http://jsfiddle.net/maryisdead/10hvkcr2/" rel="nofollow noreferrer">JSFiddle</a>)</p>
  
  <p>I always thought <code>left: 0</code> and <code>right: 0</code> will dictate the element's width (100% of it's first relatively positioned parent) but it seems <code>width</code> takes precedence here and therefore makes <code>margin: 0 auto</code> work.</p>
  
  <p>Is this defined behavior? Can I find something about it in any spec? I googled around a bit but came up with nothing useful.</p>