Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display: table-cell on new row

Tags:

Here is an example code for what I mean:

HTML

<ul class="table">
  <li class="table-cell"></li>
  <li class="table-cell"></li>
  <li class="table-cell"></li>
  <li class="table-cell"></li>
</ul>

CSS:

.table {
  display: table;
}

.table-cell {
  display: table-cell;
  width: 25%;
}

Q: How can I make the third and forth <li> (table cells) display on new row with width: 50% each?

Q: Is there a way using only CSS and not jQuery or Javascript?

like image 309
codiac Avatar asked Feb 08 '13 12:02

codiac


People also ask

What does display table cell do?

Setting display to table makes the element behave like a table. So you can make a replica of an HTML table without using the table element and corresponding elements such as tr and td . For example, in HTML, you can make a table with the <table> element and also a <div> , or any container of your choice.

Which tag indicates a new row of a table?

<tr>: The Table Row element. The <tr> HTML element defines a row of cells in a table.

Which values of the display property allow the next element to sit beside it?

An inline element will accept margin and padding, but the element still sits inline as you might expect. Margin and padding will only push other elements horizontally away, not vertically.

How do you display a table?

Double-click the table name in the grid. Select names of tables in the grid, then right-click and select Display Rows from the shortcut menu, or select Display Rows from the File menu.


1 Answers

The simple answer is: you can't, at least not with lists. Adjacent elements with their display set to table-cell are treated as belonging to the same row (even if a row element is not provided, an anonymous one will be created for you). Since lists aren't allowed to contain any other tags between the ul and the li, this simply isn't possible with lists. You'll have to use either different markup or different styling.

Here are your options if you don't want to modify the markup.

Inline-block on the list items: http://jsfiddle.net/XNq74/1/

li {
    display: inline-block; /* could use floats here instead */
    width: 40%;
}

CSS columns would be a reasonable choice: http://jsfiddle.net/XNq74/

ul {
    columns: 2;
}

http://caniuse.com/#feat=multicolumn

Flexbox would also work and can be used in combination with inline-block: http://jsfiddle.net/XNq74/2/

ul {
    display: flex;
    flex-flow: row wrap;
}

li {
    flex: 1 1 40%;
}

http://caniuse.com/#search=flexbox

like image 95
cimmanon Avatar answered Sep 21 '22 14:09

cimmanon