Category : XHTML/CSS

previous entry label のススメ | table のアクセシビリティを向上させ next entry

table と caption の margin

Date2008-08-13 CategoryXHTML/CSS Tagscaption, CSS, table, XHTML

Firefox,Safari,Opera は IE に比べるとだいぶWeb標準に準拠してレンダリングされます。しかし,仕様に詳しく定められていないところは(追記参照)ブラウザごとのクセがあり,表示結果が微妙に違ったりします。以前,table に caption を入れたとき,margin を合わせるのにちょっと苦労した経験があったので,改めて調べてみました。

caption は表題を示す要素です。table の子要素になり,table の開始タグの直後に記述します。

table の margin-top が入る位置

<p>テキストテキスト</p>
<table>
  <caption>サンプル(ここがcaption)</caption>
  <tr>
    ......
  </tr>
</table>
table {
  margin-top: 50px;
}

Firefox3,Opera9.5,Safari3 のそれぞれの表示結果をみてみましょう。

Firefoxのレンダリング

Operaのレンダリング

Safariのレンダリング

Firefoxの場合,table の margin-top が caption との間に入っています。

caption にだけ上下の margin を入れてみる

table {
  margin-top: 0;
}

table caption {
  margin-top: 30px;
  margin-bottom: 30px;
}

Firefoxのレンダリング

Operaのレンダリング

Safariのレンダリング

これは顕著な違いが出てきています。

Firefox は予想通り。

Opera は上の margin が若干狭くなっています。caption のテキストのベースラインから上が,ちょうど30pxになっていました。

Safari は上の margin が下にまわっています。

table の上と caption の上下の margin を入れてみる

table {
  margin-top: 50px;
}

table caption {
  margin-top: 30px;
  margin-bottom: 30px;
}

Firefoxのレンダリング

Operaのレンダリング

Safariのレンダリング

ブラウザによってかなりの違いがあることがお分かりいただけたかと思います。

クロスブラウザのCSS

IE6,7 では caption に margin が効かないことをあわせると,いまのところ table 上部と caption 上下の margin は指定しないほうが無難でしょう。

  • table 上部の余白は,前の要素の margin-bottom で調整
  • caption と table 本体の間隔は caption の padding-bottom で調整
p {
  margin-bottom: 50px;
}

table {
  margin-top: 0;
}

table caption {
  padding-bottom: 10px;
}

InternetExplorerのレンダリング

Firefoxのレンダリング

Operaのレンダリング

Safariのレンダリング

参考

複数並んだ table間のマージンについては to-R の西畑さんの記事を参考にしてください。

サンプルページを用意しましたので,あわせてご覧ください。

追記

vantguarde さんのはてなブックマークでご指摘をいただきました。caption の margin については仕様に定められていました。以下を参考にしてください。

こうしてみると,Firefox,Opera,Safari とも仕様のとおりにレンダリングされてはいませんね。

Search

RSS feed

あわせて読みたい

あわせて読みたいブログパーツ

Contact me

  • My status Skype : webmugi

Validation

  • Valid XHTML 1.0 Strict
  • Valid CSS!

Top