title: How to change the Tooltip position based on available space
body: <p>How do I make the tooltip appear on top if there's not enough space at the bottom, and vice-versa? Is there any CSS hack or I have to calculate it using JS?</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-css lang-css prettyprint-override"><code>.foo {
    position: relative;
  }
  
  .tooltip {
    display: none;
  }
  
  .foo a:focus+.tooltip {
    display: block;
    background: black;
    position: absolute;
    color: white;
    padding: 5px;
    z-index: 1;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;div class="foo"&gt;&lt;a href="#" class="foo"&gt; this is a link&lt;/a&gt;
    &lt;div class="tooltip"&gt;this is a tooltip&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="foo"&gt;&lt;a href="#" class="link"&gt;this is a link&lt;/a&gt;
    &lt;div class="tooltip"&gt;this is a tooltip&lt;/div&gt;
  &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>