To do this, you need a monospace font. *
Basically I see two options: 1. use spaces 2. fields.
Option 1
Your text will look like
I•am•under•the•text•above ••am•under•••••text•above
where • denotes a space character. Pretty straight forward in terms of CSS, since you don't have to worry about distance. The browser does everything for you. Example: http://jsfiddle.net/PYXdr/
* Well, it is possible with any font using a lot of JS, but I think it is not worth it.
Option 2
Since you probably don't want spaces between your intervals, you might prefer this:
I•am•under•the•text•above am•under text•above
Now you need to reflect on the interval manually. Each range should receive margin-left , which pushes it to the desired position. But before we can do this, we need to know the width of one character (using JS, since CSS does not provide this). Ok, pretty easy:
var el = document.createElement('pre'); el.style.display = 'inline-block'; el.innerHTML = ' '; document.body.appendChild(el); var width = parseFloat(getComputedStyle(el).width); document.body.removeChild(el);
Now release and move the gaps:
span1.style.marginLeft = (2 * width) + 'px'; span2.style.marginLeft = (5 * width) + 'px';
Example: http://jsfiddle.net/JC3Sc/
Putting it all together
Now here is a basic example of how this might work:
var text = "I am under the text above me and there is lots more text to come.\nI am even moving onto a new line since I have more text" var highlightBorders = [[2, 3, 4, 6], [6, 7]];
This is not a complete solution, since a) I really don’t see which parts you want to highlight, and b) I do not want to spoil all the fun. But you have an idea.
Example: http://jsfiddle.net/tNyqL/