Is it possible to align the width of an element like in a table with CSS without changing the HTML?

I put together a demo of the current menu. The HTML structure cannot be changed.

Html

<ul class="menu row1">
    <li>item</li>
    <li>varied item</li>
    <li>item</li>
    <li>item</li>
</ul>
<ul class="menu row2">
    <li>longer item</li>
    <li>item</li>
    <li>longer item</li>
</ul>

      

CSS

.menu li {
    display: inline-block; border-right: 1px solid; 
    padding: 0 10px; margin: 0; line-height: 32px;
}
.menu {border: 1px solid; margin: 10px 10px 0;}
.menu.row2 {margin-top: 0; border-top: 0;}
.menu li, .menu {border-color: #888;}

      

Js

var $row0 = $('.menu.row1 > li');
var $row1 = $('.menu.row2 > li');

for (x=0; x < $row0.length; x++) {
    if (typeof $row1[x] != 'undefined') {
        var w1 = $($row0[x]).width();
        var w2 = $($row1[x]).width();
        var w3 = Math.max(w1, w2);

        $($row0[x]).add($row1[x]).width(w3);
    }
}

      

Is it possible to get the same effect as the current JS with CSS? Thank.

Update:

Thanks to Big00d for the answer. I've updated the demo.

New CSS

.menu li {
    display: table-cell; border: 1px solid #888; 
    padding: 0 10px; margin: 0; line-height: 32px;
    border-left: 0;
}
.menu {margin: 10px 10px 0; display: table-row;}
.menu.row2 {margin-top: 0;}
.menu.row2 li {border-top: 0;}

      

Supported in IE8 and above.

+3


source to share


2 answers


Add display:table-row;

in .menu

and display:table-cell

in .menu > li

, that should do the trick



+3


source


You probably want to use display: table/table-row/table-cell

from vertical-align:bottom;

: http://www.quirksmode.org/css/display.html#table



+3


source







All Articles