-4

What I want to inquire about him, how to make 4 cells in each line automatically without the use of programming languages ​​such as php . In other words, I want the line is not likely more than 4 cells and any new cell to be in a new line automatically, without manually to put in the code <tr>

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
o6 qr
  • 95
  • 2
  • 7

3 Answers3

0
<table>
 <tr>
  <td> Cell 1 </td>
  <td> Cell 2 </td>
  <td> Cell 3 </td>
  <td> Cell 4 </td>
 </tr>
</table>

This is the HTML

Sam Creamer
  • 5,187
  • 13
  • 34
  • 49
0

Assuming you have a table markup like so:

<table>
    <tr>
        <td>Cell 1</td>
        <td>Cell 2</td>
        <td>Cell 3</td>
        <td>Cell 4</td>
        <td>Cell 5</td>
        <td>Cell 6</td>
        <td>Cell 7</td>
        <td>Cell 8</td>
        <td>Cell 9</td>
        <td>Cell 10</td>
    </tr>
</table>

...You can cause each row to have 4 cells with the following css:

FIDDLE

CSS

td {
    float: left;
}
td:nth-child(4n + 5) {
    clear:left;
}

Edit:

If you want the each td to take up 25% of the table width, then you'll need you set a width on your table and then set width: 25% on all td elements (also reset defalt margin/paddings to 0)

Like so:

Updated FIDDLE

body, td {
    margin: 0;padding:0;
}
table {
    width: 100%;
}
td {
    float: left;
    width: 25%;
}
Danield
  • 121,619
  • 37
  • 226
  • 255
0

Let's assume you want to create a layout for a page and you use <tr> as a synonym for line/row of blocks, not for creating layout tables made of table, tr and td (that's a bad practice) or worse here for data table (a data table with 4 cells per row also have 4 header cells th in the first row (in general) and do use tr to semantically indicate where the rows are and how much cells they each contain)

That said, you can use sibling elements in your HTML code and float them. Here's a Fiddle

Relevant code:

HTML

<div class="block">Some content 1</div>
<div class="block">Some content 2</div>
<!-- (...) -->

CSS

* {
    -webkit-box-sizing: border-box;
       -moz-box-sizing: border-box;
            box-sizing: border-box;
}
body {
    width: 400px;
}
.block {
    float: left;
    width: 24%;
    margin: 0 1% 1% 0;
    padding: 4px;
    border: 1px solid #aaa;
}

/* Improvements for last "row" http://slides.com/heydon/effortless-style#/45 and next slides */
.block:nth-child(4n+1):last-child {
    width: 99%;
}
.block:nth-child(4n+1):nth-last-child(2),
.block:nth-child(4n+2):last-child {
    width: 49%;
}
.block:nth-child(4n+1):nth-last-child(3),
.block:nth-child(4n+2):nth-last-child(2),
.block:nth-child(4n+3):last-child {
    width: 32%;
}
Community
  • 1
  • 1
FelipeAls
  • 21,711
  • 8
  • 54
  • 74