Reputation:
Given a list:
<ul>
<li>...</li>
<li>...</li>
<li>...</li>
<li>...</li>
<li>...</li>
<li>...</li>
<li>...</li>
<li>...</li>
<li>...</li>
</ul>
And this CSS:
ul { display: table; width: 600px; }
li { display: table-cell; width: 33%; }
You'd expect 3 rows of 3 columns, each 200px-ish wide. But it seems the li
width is ignored.
Is there any way to force rows with display: table-cell
without adding in extra markup?
Edit:
Also, I'm using table-cell
because I want then entire row to have the same height (despite varying content), as I want to position something in all three cells in each row, but at the bottom of the lowest cell. That is a confusing sentence I know.
Basically I want to turn a bunch of li
s into equal height columns, but with multiple rows only using CSS. All the equal height column solutions I've seen can't do this.
Upvotes: 14
Views: 37791
Reputation: 7680
I am using display: inline-block
and vertical-align: top
But if you want background that stretches with height as well, javascript is the only way (that I can think of)
Upvotes: 1
Reputation: 3561
I think there is no semantic solution. It should be wrapper container for every set of items to apply same height to them. But it's possible to solve design problem (kinda make a grid) here is an example how to make it: multiple rows with same height on a line
Upvotes: 1
Reputation:
This is the solution I came up with.
The markup is slightly modified, to remove the whitespace. This is because with the li
s set to display: inline
, the newlines between them will take up some of the width.
Upvotes: 3
Reputation: 46629
If you can't change the markup, it can be done without faking a table. You can make the list items display:block
instead of display:table-cell
.
By the way, 33% of 600px
is not 200 pixels. Use 200px
!
Edit:
If you know what the maximum height of the content is going to be, you can use that as the height for all list items of course, and use vertical-align or line-height to align the contents. (But as that's not an option either, the solutions that don't change the markup are out.)
Upvotes: 0
Reputation: 92863
You can give display:inline-table
instead of display:table-cell
to LI. Write like this:
ul { display: table; width: 600px; font-size:0;}
li { display: inline-table; width: 33%;background:red; font-size:15px;}
Check this http://jsfiddle.net/56FGT/1/
Upvotes: 14