CSSデザインカタログ » table » 背景色でセルを表す表 : table001

サンプル

アクセス解析月ごとの集計
年月 セッション ページビュー 新規セッション率 平均滞在時間 コンバージョン数
2007-01 5624 8021 75.2% 00:42 4
2007-02 5021 7533 70.1% 01:06 5
2007-03 5847 9105 81.3% 00:36 2
2007-04 6185 10230 84.9% 00:53 6
2007-05 5133 7688 76.0% 01:15 5

ソース

XHTML
<table class="table001">
  <caption>アクセス解析月ごとの集計</caption>
  <tr>
    <th scope="col">年月</th>
    <th scope="col">セッション</th>
    <th scope="col">ページビュー</th>
    <th scope="col">新規セッション率</th>
    <th scope="col">平均滞在時間</th>
    <th scope="col">コンバージョン数</th>
  </tr>
  <tr>
    <td>2007-01</td>
    <td>5624</td>
    <td>8021</td>
    <td>75.2%</td>
    <td>00:42</td>
    <td>4</td>
  </tr>
  <tr>
    <td>2007-02</td>
    <td>5021</td>
    <td>7533</td>
    <td>70.1%</td>
    <td>01:06</td>
    <td>5</td>
  </tr>
  <tr>
    <td>2007-03</td>
    <td>5847</td>
    <td>9105</td>
    <td>81.3%</td>
    <td>00:36</td>
    <td>2</td>
  </tr>
  <tr>
    <td>2007-04</td>
    <td>6185</td>
    <td>10230</td>
    <td>84.9%</td>
    <td>00:53</td>
    <td>6</td>
  </tr>
  <tr>
    <td>2007-05</td>
    <td>5133</td>
    <td>7688</td>
    <td>76.0%</td>
    <td>01:15</td>
    <td>5</td>
  </tr>
</table>

CSS
table.table001 {
  margin-top: 0.2em;
  margin-bottom: 1em;
  width: 570px;
  border-collapse: separate;
  border-spacing: 2px;
  font-size: 100%;
}

table.table001 caption {
  margin-top: 1em;
  text-align: right;
}

table.table001 th,
table.table001 td {
  padding: 4px 6px;
}

table.table001 th {
  background: #1B3AB4;
  text-align: center;
  white-space: nowrap;
  font-size: 92%;
  color: #FFF;
}

table.table001 td {
  background: #E5EAFF;
  text-align: right;
  white-space: nowrap;
}

解説

  • 表の題名をcaption要素で書き入れています。text-alignプロパティで表示位置を指定できます。
  • border-collapse: separate; border-spacing: 2px;でセル間に2pxの隙間を作っています。
  • th,tdbackgroundプロパティで背景色を入れています。