XML technologies / CSS / CSS Cookbook / Tables / Styling table content

(Excerpt from “CSS Kochbuch” by Christopher Schmitt)

Problem

Links inside table cells should look different from those in the rest of the document.

Solution

Use a selector for descendant elements (also called a contextual selector ) to define styles for cell content:

td a {
 display: block;
 background-color: #333;
 color: white;
 text-decoration: none;
 padding: 4px;
}

Discussion

Combining a type selector and a descendant selector in a CSS rule lets you achieve a good design with little effort spent structuring your document, while keeping file size small. Styles defined by td a apply only to elements a located inside table cells (td).

A class selector gives you greater control over the appearance of cell content:

<td class="navText"><a href="/">Home</a></td>

You can now assign styles using a combination of descendant and class selectors:

td.navText a {
 font-size: x-small;
}

If a table cell contains more content or markup than a single link, enclose its content in an element div and then use a class selector.

The following example demonstrates this technique with a cell containing an unordered list:

<td>
  <div class="tblcontent">
    <p>To-do list on your day off.</p>
    <ul>
      <li><a href="http://www.imdb.com/title/tt0120737">Watch <cite>The Fellowship of the Rings</cite>, Extended Version</a></li>
      <li><a href="http://www.imdb.com/title/tt0167261/">Watch <cite>The Two Towers</cite>, Extended Version</a></li>
      <li><a href="http://www.imdb.com/title/tt0167260/">Watch <cite>The Return of the King</cite>, Extended Version</a></li>
      <li>Start or join local Elvish society.</li>
    </ul>
  </div>
</td>

The CSS rules styling the table cell’s content are as follows:

.tblcontent p {
 margin: 0;
 padding: 0;
 font-weight: bold;
}
.tblcontent ul {
 margin: 0;
 padding: 0;
}
.tblcontent li {
 margin: 0;
 padding: 0;
 line-height: 1.5;
}
.tblcontent li a {
 padding-left: 15px;
 background-image: url(bullet.gif);
 background-repeat: no-repeat;
}

See also

  

 

 

 

back to top