title: Prevent overlapping while positioning element at height of another
body: <p>Inside a long text document there are some &quot;special words&quot; to which I want to display notes/annotations on the left. Each note should be <strong>as close as possible</strong> to the level of the word it is refering to.</p>
  <p>The HTML for this is organised in a table. Each paragraph is one table row, consisting on annotations in the left and main text in the right table column. the notes/annotations go to the left. However, unfortunately, there are also some other elements/text nodes in there.</p>
  <pre><code>&lt;table&gt;
      &lt;tr&gt;
          &lt;td class&quot;comments&quot;&gt;
              &lt;span id=&quot;dog&quot; class=&quot;note&quot;&gt;Note for dog&lt;/span&gt;
              &lt;span id=&quot;cat&quot; class=&quot;note&quot;&gt;Note for cat&lt;/span&gt;
              &lt;span id=&quot;horse&quot; class=&quot;note&quot;&gt;Note for horse&lt;/span&gt;
              Somethin else than a note.
          &lt;/td&gt;
          &lt;td&gt;[Text...]
              &lt;span id=&quot;dog_anchor&quot; class=&quot;reference&quot;&gt;Dog&lt;/span&gt;
              &lt;span id=&quot;cat_anchor&quot; class=&quot;reference&quot;&gt;Cat&lt;/span&gt;
              &lt;span id=&quot;horse_anchor&quot; class=&quot;reference&quot;&gt;Horse&lt;/span&gt;
              [Text...]
          &lt;/td&gt;
      &lt;/tr&gt;
  &lt;/table&gt;
  </code></pre>
  <p>It's easy to change the &quot;note&quot;-<code>span</code>s to <code>absolute</code> and positioned them on the level of their reference:</p>
  <pre><code>$('span[class*=&quot;note&quot;]').each(function (index, value) {
      var my_id = $(this).attr('id');
      var element_ref = document.getElementById(my_id + &quot;_anchor&quot;); // get reference element
      var pos_of_ref = element_ref.offsetTop; // get position of reference element
      $(this).css('top', pos_of_ref); // set own position to position of reference element
  });
  </code></pre>
  <p>However, life is not so simple here. Since there could be a lot of reference words in one line (while on other there are none of them) I need a rather sophisticated way to distribute the notes so that they are as close as possible to their references without destroying anything in the layout (e.g. being placed outside of the table cell or overlapping with other elements).</p>
  <p>Furthermore,  the height of the table cells could not be changed.  Elements which are not notes must not be moved. (Note elements are always in the order they appear in the main text. That's not the problem.)
  So, I need an algorithm like this:</p>
  <ul>
  <li>Take all notes in a table cell.</li>
  <li>Analyse blank space in that table cell: Which areas are blank, which are blocked?</li>
  <li>Distribute the notes in the table cell so that each note is as close as possible to its reference word without any element colliding with any other item in the table cell.</li>
  </ul>
  <p>Is there any fast and elegant way to do this without having to write hundreds of lines of code?</p>
  <p>Here is a JSfiddle: <a href="https://jsfiddle.net/5vLsrLa7/7/" rel="noreferrer">https://jsfiddle.net/5vLsrLa7/7/</a></p>
  <p><strong>[Update on suggested solutions]</strong></p>
  <p>Simply setting the position of the side notes to <code>relative</code> or just moving notes down won't work, because in this case, the side notes will just go downwards relative to their desired position which results in side notes way to far from their reference words. After all, for a neat solution I need to side notes spread in both directions: up and down.</p>
  <p><strong>[Update]</strong>
  The expected result would be something like this:
  <a href="https://i.stack.imgur.com/hgDRJ.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/hgDRJ.jpg" alt="Expected result: note/reference placing" /></a></p>
  <p>As you see, it's never possible to place all the notes at the height of their reference. However, the free space is used to position them as close as possible, moving them up and down.</p>