Lecture 22: CSS Tables and Outlines

Overview:

  1. Progressive Enhancement and Graceful Degradation
  2. General Considerations with Tables
  3. table-layout Property
  4. empty-cells Property
  5. border-spacing Property
  6. border-collapse Property
  7. caption-side Property
  8. outline-related Properties

Progressive Enhancement and Graceful Degradation

General Considerations with Tables

table-layout Property

Property Usage and Effect Values Accepted Recommendations
table-layout Determines how a table is sized, either based on its content or using the specified widths.

Apply to <table> and elements displayed as inline tables (display: inline-table).
auto (the default) | fixed For consistent rendering cross-browser make sure that the <table> element has a defined width.

Mac IE 5.x has no support.

Always check to make sure that content is not being truncated. Narrow widths will pose difficulties.

Sample code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Embedded CSS: Table-Layout</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css"><!--
    body, h1, td {font: 12px verdana,geneva,lucida,arial,sans-serif;}
    h1 {font-size: 16px;}
    td {border: 1px solid #000; width: 50px;}
    #fixed {width: 200px; table-layout: fixed;}
    #automatic {width: 200px;}
    --></style>
</head>
<body>

<h1>Table using the fixed layout algorithm:</h1>
<table cellspacing="2" id="fixed">
<tr>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
</tr>
</table>

<h1>Table using the automatic layout algorithm:</h1>
<table cellspacing="2" id="automatic">
<tr>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
</tr>
</table>

</body>
</html>

See how this example renders

empty-cells Property

Property Usage and Effect Values Accepted Recommendations
empty-cells Determines whether an empty table cell displays or hides borders. show | hide Windows IE 5.x, 6, & 7 lack support.

Mac IE 5.x renders the empty cells too large.

Sample code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Embedded CSS: Empty-Cells</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css"><!--
    body, h1, td {font: 12px verdana,geneva,lucida,arial,sans-serif;}
    h1 {font-size: 16px;}
    td {border: 1px solid #000; width: 100px;}
    #show {empty-cells: show;}
    #hide {empty-cells: hide;}
    --></style>
</head>
<body>

<h1>Table with empty cells shown:</h1>
<table cellspacing="2" id="show" border="3">
<tr>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
    <td></td>
    <td></td>
</tr>
</table>

<h1>Table with empty cells hidden:</h1>
<table cellspacing="2" id="hide" border="3">
<tr>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
    <td></td>
    <td></td>
</tr>
</table>

</body>
</html>

See how this example renders

border-spacing Property

Property Usage and Effect Values Accepted Recommendations
border-spacing Determines the space between table cells.

Apply to <table> and elements displayed as inline tables (display: inline-table).
Number and unit (e.g., px, em, %)

If two values are supplied they are space separated and the first value is horizontal (left/right) while the second value is vertical (top/bottom) spacing.
IE browsers lack support, so the cellspacing attribute is necessary for them.

Sample code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Embedded CSS: Border-Spacing</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css"><!--
    body, h1, td {font: 12px verdana,geneva,lucida,arial,sans-serif;}
    h1 {font-size: 16px;}
    #equal {border-spacing: 8px;}
    #different {border-spacing: 10px 25px;}
    td {border: 1px solid #000; padding: 5px; width: 100px;}
    table {border: 1px solid #000;}
    --></style>
</head>
<body>

<h1>Table with equal border spacing around all cells:</h1>
<table id="equal">
<tr>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
</tr>
<tr>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
</tr>
</table>

<h1>Table with different horizontal and vertical border spacing:</h1>
<table id="different">
<tr>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
</tr>
<tr>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
</tr>
</table>

</body>
</html>

See how this example renders

border-collapse Property

Property Usage and Effect Values Accepted Recommendations
border-collapse Determines whether table cells share borders or have distinct borders.

Apply to <table> and elements displayed as inline tables (display: inline-table).
separate (the default) | collapse Windows IE browsers do support this, but avoid cellspacing values greater than 0, due to gapping.

Mac IE 5.x lacks support.

Opera and Gecko-based browsers ignore the cellspacing attribute and the border-spacing property when borders are collapsed.

Sample code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Embedded CSS: Border-Collapse</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css"><!--
    body, h1, td {font: 12px verdana,geneva,lucida,arial,sans-serif;}
    h1 {font-size: 16px;}
    .collapse {border-collapse: collapse;}
    td {border: 3px solid #000; padding: 5px; width: 100px;}
    --></style>
</head>
<body>

<h1>Table with collapsed borders and 3px cell border:</h1>
<table class="collapse">
<tr>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
</tr>
<tr>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
</tr>
</table>

<h1>Table with separate borders, no cellspacing, and 3px cell border:</h1>
<table cellspacing="0">
<tr>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
</tr>
<tr>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
</tr>
</table>

<h1>Table with collapsed borders and cellspacing more than 0:</h1>
<table class="collapse" cellspacing="15">
<tr>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
</tr>
<tr>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
</tr>
</table>

</body>
</html>

See how this example renders

caption-side Property

Property Usage and Effect Values Accepted Recommendations
caption-side Determines whether a caption renders above or below the table. top (the default) | bottom Windows IE 5.x, 6, & 7 lack support.

Sample code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Embedded CSS: Caption-Side</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css"><!--
    body, td, caption {font: 12px verdana,geneva,lucida,arial,sans-serif;}
    .bottomcaption {caption-side: bottom; text-align: left;}
    td {border: 1px solid #000; padding: 5px; width: 100px;}
    table {border-collapse: collapse;}
    --></style>
</head>
<body>

<table cellspacing="0">
<caption>A sample caption using the defaults</caption>
<tr>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
</tr>
<tr>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
</tr>
</table>

<p>&nbsp;</p>

<table cellspacing="0">
<caption class="bottomcaption">A left-aligned bottom caption</caption>
<tr>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
</tr>
<tr>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
</tr>
</table>

</body>
</html>

See how this example renders

outline-related Properties

Property Usage and Effect Values Accepted Recommendations
outline-color Determines the color of the outline. A pre-defined color name, hexadecimal value (e.g., #fff), an rgb value: rgb(255,0,0) or rgb(0%,50%,25%), or 'transparent' Only one color can be specified and it applies to all four sides.
outline-style Determines the style of the outline. dotted | dashed | solid | double | groove | inset | outset | none Only one style can be specified and it applies to all four sides.
outline-width Determines the width of the outline. Number and unit (e.g., px, em, %), as well as keywords (thin | medium | thick) Only one width can be specified and it applies to all four sides.
outline Combines the other outline-related properties into one sequence. Sequence of Values: outline-width outline-style outline-color Recommended because it is more efficient.

Sample code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Embedded CSS: Outline</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css"><!--
    body, h1, td {font: 12px verdana,geneva,lucida,arial,sans-serif;}
    h1 {font-size: 16px;}
    table {border-collapse: collapse;}
    td {border-bottom: 1px solid red;
        border-top: 1px solid red;
        padding: 5px;
        width: 100px;}
    td:hover {outline: 3px solid green;}
    p {background: #ccc;
       border: 1px solid #000;
       -moz-outline: 2px solid green;
       outline: 2px solid green;}
    --></style>
</head>
<body>

<h1>Outline hover effects for cells (works well in Firefox
and poorly in Opera):</h1>
<table cellspacing="0">
<tr>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
</tr>
<tr>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
</tr>
<tr>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
    <td>sampletext sampletext sampletext</td>
</tr>
</table>

<h1>Static outlines (works in all browsers supporting outline):</h1>
<p>Some sample text</p>
<p>And then even more</p>
</body>
</html>

See how this example renders