3

I am trying to figure out a certain layout with css.

[..tab1 random text..][..tab2 more random text..][..tab3 other random text..][..tab4..][..tab5..]

These are the requirements:

  • All tabs are inside a div with fixed width.
  • Tabs should not expand over that div, but should fill it out.
  • Number of tabs can vary.
  • Each Tab has unknown width (depends on text inside).
  • All Tabs should share the same padding (space from outer most letters to inner border of tab should be the same, represented by the ".." above). This padding does not have to be fixed but should be the same among the tabs.
  • No Calculations. All should work without modifying the css if one ore more tabs are added or removed.

I tried it with tables:

<table>
    <tr>
        <td>Tab1</td>
        <td>Tab 1 Space</td>
        <td>Tab 2 more Space</td>
        <td>Tab 3 even more Space</td>
        <td>Tab 4</td>
    </tr>
</table>

(fiddle)

The Problem I face is, that the padding varies based on width of the table and number of tabs.

Shog9
  • 156,901
  • 35
  • 231
  • 235
Björn
  • 12,587
  • 12
  • 51
  • 70
  • 1
    I think what you're asking might not be possible with css alone, you would probably need some js to this. Would be happy to be proven wrong, would be good to see someone do this. – wushudude Nov 07 '13 at 15:15

7 Answers7

5

IF I understand all of the constraints correctly, this is easily accomplished with the table and table-cell display properties.

Set your containing div width:

.container {
width: 800px;
}

then set your ul to have the table display:

ul {
display: table;
width: 100%;
}

finally, set your li's to be table-cells:

li {
display: table-cell;
}

See the full example: http://codepen.io/anon/pen/IfbHs

2507rkt3
  • 21,311
  • 1
  • 18
  • 15
  • close, but this doesnt meet the criteria: "All Tabs should share the same padding" – Björn Nov 16 '13 at 17:43
  • Hmm - when I look at the box-model in Chrome DevTools, it looks like each
  • gets 40px of padding. Are you seeing something different?
  • – 2507rkt3 Nov 17 '13 at 02:32
  • You are right, the values add up. But when I add a couple of more li tabs, the tabs expand over the div.container, which violates: "Tabs should not expand over that div, but should fill it out", see http://codepen.io/anon/pen/xJHeG. Still very close. – Björn Nov 17 '13 at 10:08
  • Well - like ScottS said, that's impossible. You can either have consistent padding, or the ability to add as many items as you want. If you want consistent padding, then add a padding declaration to the
  • 's. If you want to add infinite items, remove the padding declaration.
  • – 2507rkt3 Nov 17 '13 at 12:59