Sunday 1 September 2013

Organizing Data with Tables - Part 2

Border Collapse Property

Tables consist of a parent table element as well as subsequent td elements. Applying a border to both of these elements will begin to stack up borders all around. Take for example, putting a 2 pixel border around an entire table and then an additional 2 pixel border around each table cell. In return this would create a 4 pixel border around every cell within the table.
The border-collapse property determines a tables border model. There are three values for the border-collapse property, including collapseseparate, and inherit. By default the value is separate meaning that all of the different borders will stack up to one another accordingly, like mentioned above. The collapse property, on the other hand, will condense the borders down to one, choosing the table cell as the primary border.
  1. table {
  2. border: 2px solid blue;
  3. border-collapse: separate;
  4. }
  5. th, td {
  6. border: 2px solid red;
  7. }

Border Collapse Property Demo

  • LukeBrad
    8876
    separate
  • LukeBrad
    8876
    collapse

Border Spacing Property

Understanding that the border-collapse property with the separate values allows borders to be stacked up against one another the border-spacing property can determine how much space, if any, appears between the borders. Going back to our example, a table with a 2 pixel border and a 2 pixel border around each cell creates a 4 pixel border all around. Adding in a border-spacing value of 2 pixels would then separate the two borders by 2 pixels, creating a 6 pixel total border width.
  1. table {
  2. border: 2px solid blue;
  3. border-collapse: separate;
  4. border-spacing: 2px;
  5. }
  6. th, td {
  7. border: 2px solid red;
  8. }

Border Spacing Property Demo

LukeBrad
8876

Adding in Borders

Putting a border around a table is fairly simple, however putting borders around rows or cells can be more difficult. Below are a few examples of how to add these borders. For an additional reference and examples on how to add borders around a table check out the tables within Bootstrap, from Twitter.

Row Borders

Putting a border between each row is as simple as adding a bottom border to each table cell. To remove the bottom border of the cells within the last row of the table the pseudo-class last-child selector is used.
  1. table {
  2. border-collapse: collapse;
  3. border-spacing: 0;
  4. }
  5. th, td {
  6. border-bottom: 1px solid #c6c9cc;
  7. }
  8. tr:last-child td {
  9. border: 0;
  10. }

Row Borders Demo

TrackArtistLength
We Are YoungFun.4:10
Pumped Up KicksFoster the People3:59
Midnight CityM834:03

Cell Borders

On the other hand, putting a border around every individual cell is fairly easy. Simply put a border around each table header or table data cell and set the overall table to have a border-spacing value of collapse.
  1. table {
  2. border-collapse: collapse;
  3. border-spacing: 0;
  4. }
  5. th, td {
  6. border: 1px solid #c6c9cc;
  7. }

Cell Borders Demo

TrackArtistLength
We Are YoungFun.4:10
Pumped Up KicksFoster the People3:59
Midnight CityM834:03

Aligning Text

In addition to table borders, aligning text within cells, both horizontally and vertically, plays an integral part of table formatting. Commonly names, descriptions, and so forth are flush left while numbers and figures are flush right. Other information, depending on its context, should be centered. Moving text horizontally can be accomplished using thetext-align property within CSS, as covered in the typography lesson.
To align text vertically the vertical-align property is used. The vertical-align property only works withinline and table-cell display elements. That said, the vertical-align property will not work for block level or any other level elements.
The vertical-align property accepts a handful of different values, of which change depending on if the element is displayed as an inline and table-cell element. Within tables the most popular values include topmiddle, andbottom. These values then vertically position text within the cell in relation to their value.
HTML
  1. <table>
  2. <thead>
  3. <tr>
  4. <th colspan="2">Items</th>
  5. <th class="qty">Qty</th>
  6. <th class="price">Price</th>
  7. </tr>
  8. </thead>
  9. <tbody>
  10. <tr>
  11. <td class="item">Envisioning Information <span>By Edward R. Tufte – Hardcover</span></td>
  12. <td class="stock in">In Stock</td>
  13. <td class="qty">1</td>
  14. <td class="price">$33.32</td>
  15. </tr>
  16. <tr>
  17. <td class="item">Outliers <span>By Malcolm Gladwell – Hardcover</span></td>
  18. <td class="stock in">In Stock</td>
  19. <td class="qty">2</td>
  20. <td class="price">$33.58 <span>($16.79 × 2)</span></td>
  21. </tr>
  22. <tr>
  23. <td class="item">Don’t Make Me Think <span>By Steve Krug – Paperback</span></td>
  24. <td class="stock out">Out of Stock</td>
  25. <td class="qty">1</td>
  26. <td class="price">$22.80</td>
  27. </tr>
  28. <tr>
  29. <td class="item">Steve Jobs <span>By Walter Isaacson – Hardcover</span></td>
  30. <td class="stock in">In Stock</td>
  31. <td class="qty">1</td>
  32. <td class="price">$17.49</td>
  33. </tr>
  34. </tbody>
  35. <tfoot>
  36. <tr class="sub">
  37. <td colspan="3">Subtotal</td>
  38. <td>$107.19</td>
  39. </tr>
  40. <tr class="tax">
  41. <td colspan="3">Tax</td>
  42. <td>$10.71</td>
  43. </tr>
  44. <tr class="total">
  45. <td colspan="3">Total</td>
  46. <td>$117.90</td>
  47. </tr>
  48. </tfoot>
  49. </table>
CSS
  1. table {
  2. border-collapse: collapse;
  3. border-spacing: 0;
  4. }
  5. th, td {
  6. border: 1px solid #c6c9cc;
  7. vertical-align: top;
  8. }
  9. th {
  10. font-size: 11px;
  11. text-transform: uppercase;
  12. }
  13. th.qty, th.price {
  14. text-align: center;
  15. }
  16. tbody td.item {
  17. color: #404853;
  18. font-weight: bold;
  19. }
  20. tbody td.stock, tbody td.qty, tbody td.price {
  21. vertical-align: middle;
  22. }
  23. tbody td.stock, tbody td.qty {
  24. text-align: center;
  25. }
  26. tbody td.price {
  27. text-align: right;
  28. }
  29. tfoot td {
  30. text-align: right;
  31. }
  32. tfoot tr.sub td, tfoot tr.tax td {
  33. color: #8c8c8c;
  34. font-size: 12px;
  35. }
  36. tfoot tr.total td {
  37. color: #404853;
  38. font-size: 14px;
  39. font-weight: bold;
  40. }
  41. .in {
  42. color: #00b515;
  43. }
  44. .out {
  45. color: #b50000;
  46. }
  47. span {
  48. color: #8c8c8c;
  49. display: block;
  50. font-size: 12px;
  51. font-weight: normal;
  52. }

Aligning Text Demo

ITEMSQTYPRICE
Envisioning InformationBy Edward R. Tufte – HardcoverIn Stock1$33.32
OutliersBy Malcolm Gladwell – HardcoverIn Stock2$33.58($16.79 × 2)
Don’t Make Me ThinkBy Steve Krug – PaperbackOut of Stock1$22.80
Steve JobsBy Walter Isaacson – HardcoverIn Stock1$17.49
Subtotal$107.19
Tax$10.71
Total$117.90