4

Is there anyway to simulate Tab positions with CSS.

Like:

..........|.........|..........|.........
Text!     Next tab  |          |
Longer text!        Next tab   |
Even more longer text!         Next tab

How can I simulate this behavior?

PS: If it not obvious, I don't know the text length in advance.

Jerome
  • 2,350
  • 14
  • 25
Luiz Borges
  • 537
  • 1
  • 5
  • 19

4 Answers4

2

A pure CSS solution seems unlikely to me for this behaviour. So if you turn into using javascript, it might look like this:

HTML:

<div id="tabs">
    <p><span>Text!</span><span>Next tab</span></p>
    <p><span>Longer text!</span><span>Next tab</span></p>
    <p><span>Even more longer text !</span><span>Next tab</span></p>
</div>

Javascript:

var tabs = document.getElementById('tabs');
var ps = tabs.getElementsByTagName('p');
var p;
var spans;
var span;
var w;
var wTab = 70;

for (var i = 0; i < ps.length; i++)
{   p = ps.item(i);
    spans = p.getElementsByTagName('span');
    for (var j = 0; j < spans.length - 1; j++)
    {   span = spans.item(j);
        w = span.offsetWidth;
        w = Math.ceil(w / wTab) * wTab - w;
        span.style.paddingRight = w + 'px';
    }
}

See also Demo fiddle.

Disclaimer: I'm a noob at javascript, and surely don't know anything about javascript frameworks, so it's highly probable this routine can be optimized.

NGLN
  • 43,011
  • 8
  • 105
  • 200
  • This works as I need, but I still need a non-javascript solution. It doesn't need to be perfect. If I found a way to make a inline block width grow in steps (or something similar) it would solve the problem right way. I will wait a few days before marking this as Accepted Anwser. – Luiz Borges Jun 12 '11 at 12:05
1

If it doesn't have to be perfect (as the OP said Jun 12 '11 at 12:05), at least in ie7, a <pre> block already supports tabbing with tab-chars (fixed at 8 char-widths [ref] unless tab-size is supported [ref]), and, maybe useful here too, a style-attribute to override the default monospace font.

Community
  • 1
  • 1
e.c
  • 61
  • 1
  • 1
0

You should use table for showing tabular data: see this demonstration fiddle.

NGLN
  • 43,011
  • 8
  • 105
  • 200
  • i don't think "tabs" are "tabular" data, as similar as they sound. "tabular data" is information that needs to be laid out as in a spreadsheet. Tabs are just a convenient UI way of displaying navigation. – Thomas Shields Jun 10 '11 at 20:34
  • @Thomas Shields: Well, I don't understand what you mean by that last sentence, but you are completely right: I didn't got the question. I do now! ;-) – NGLN Jun 11 '11 at 00:21
  • i just meant that tabs are a nice way of displaying navigation to the user. Glad you figured it out for your other answer. :) – Thomas Shields Jun 11 '11 at 00:48
0

Here is a way that I got around a similar problem, but the success of this method will depend on your specific application.

Rather than thinking about it as a string with tabs, try using an HTML Table with a border of 0. Each column is perfectly aligned so it's like a tab.

<table border="0">
 <tr>
   <td>not tabbed</td>
   <td>1 tab over</td>
   <td> </td>
 </tr>
 <tr>
   <td>not tabbed</td>
   <td> </td>
   <td>2 tabs over</td>
 </tr>
</table>

will give you what you want - or you can modify it to do so