Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a "Table" where rows wrap horizontally in HTML5 /css3?

I have the following table using html table, tr, td tags. enter image description here

This is all great, but I am using angular, and the problem is if I have more rows, I don't have the space for it vertically. Instead I want to "overflow" to the right of it so that it looks like:

enter image description here

What is the best way using HTML5/CSS so that I can make it such that content spills over if it exceeds the table? Note that in some cases I may have 2 or 3 entries, so I am hoping I don't have to make the width double the size of normal at the start, and instead have the table be sized based on the number of entries I have in the table. No scrollbars.

I feel like the table tag in HTML is limiting and may not allow me to do this. What is the best way to do this? Using divs?

<table style="background:green;">
<tr><td>Name</td><td>Sport</td><td>Score</td></tr>
<tr><td>Jordan</td><td>Soccer</td><td>50</td></tr>
<tr><td>Jordan</td><td>Soccer</td><td>50</td></tr>
<tr><td>Jordan</td><td>Soccer</td><td>50</td></tr>
</table>

Attempting a flexbox approach, though it seems that if I put the flexbox in a div, then the background doesn't appear to fill properly once the entries go beyond the first column:

https://jsfiddle.net/t0h7w2hw/

like image 400
Rolando Avatar asked May 12 '17 22:05

Rolando


2 Answers

Please follow below code example

This will wrap rows in a list style in mobile. You need to data-label attribute with each content td so that it knows which column it is. In mobile you can show lable before td by using css content:"" property.

body {
  font-family: "Arial", sans-serif;
  line-height: 1.25;
}
table {
  border: 1px solid #ccc;
  border-collapse: collapse;
  margin: 0;
  padding: 0;
  width: 100%;
  table-layout: fixed;
}
table caption {
  font-size: 1.5em;
  margin: .5em 0 .75em;
}
table tr {
  background: #f8f8f8;
  border: 1px solid #ddd;
  padding: .35em;
}
table th,
table td {
  padding: .625em;
  text-align: center;
}
table th {
  font-size: .85em;
  letter-spacing: .1em;
  text-transform: uppercase;
}
@media screen and (max-width: 600px) {
  table {
    border: 0;
  }
  table caption {
    font-size: 1.3em;
  }
  table thead {
    border: none;
    clip: rect(0 0 0 0);
    height: 1px;
    margin: -1px;
    overflow: hidden;
    padding: 0;
    position: absolute;
    width: 1px;
  }
  table tr {
    border-bottom: 3px solid #ddd;
    display: block;
    margin-bottom: .625em;
  }
  table td {
    border-bottom: 1px solid #ddd;
    display: block;
    font-size: .8em;
    text-align: right;
  }
  table td:before {
    /*
    * aria-label has no advantage, it won't be read inside a table
    content: attr(aria-label);
    */
    content: attr(data-label);
    float: left;
    font-weight: bold;
    text-transform: uppercase;
  }
  table td:last-child {
    border-bottom: 0;
  }
}
<table>
  <caption>Statement Summary</caption>
  <thead>
    <tr>
      <th scope="col">Card</th>
      <th scope="col">Due Date</th>
      <th scope="col">Amount</th>
      <th scope="col">Period</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td data-label="Card">Visa - 3412</td>
      <td data-label="Due Date">04/01/2016</td>
      <td data-label="Amount">$1,190</td>
      <td data-label="Period">03/01/2016 - 03/31/2016</td>
    </tr>
    <tr>
      <td scope="row" data-label="Card">Visa - 6076</td>
      <td data-label="Due Date">03/01/2016</td>
      <td data-label="Amount">$2,443</td>
      <td data-label="Period">02/01/2016 - 02/29/2016</td>
    </tr>
    <tr>
      <td scope="row" data-label="Card">Corporate AMEX</td>
      <td data-label="Due Date">03/01/2016</td>
      <td data-label="Amount">$1,181</td>
      <td data-label="Period">02/01/2016 - 02/29/2016</td>
    </tr>
    <tr>
      <td scope="row" data-label="Card">Visa - 3412</td>
      <td data-label="Due Date">02/01/2016</td>
      <td data-label="Amount">$842</td>
      <td data-label="Period">01/01/2016 - 01/31/2016</td>
    </tr>
  </tbody>
</table>
like image 58
Optimum Creative Avatar answered Sep 26 '22 12:09

Optimum Creative


Actually there is no such facility is defined for HTML table element by using CSS. I have tries some by giving various positions for table and containing div, not worked. As in the fiddle example you have provided, you must use HTML div element for that.

like image 35
Biby Augustine Avatar answered Sep 22 '22 12:09

Biby Augustine