ホームページやブログにデータをきれいに並べたいとき、HTML(エイチティーエムエル)の表(<table>
)はとても便利です。でも初めて作ると「表を作ったのに線(枠線)が出ない!」と困った経験はありませんか?
実は、HTMLだけでは枠線が表示されません。
よくある初心者の疑問:
- 「なぜ枠線が表示されないの?」
- 「どうやって線をつければいいの?」
- 「線が二重になってしまう…」
- 「色や太さを変えたい」
この記事でわかること:
- HTMLでの表の基本構造
- 枠線をつける複数の方法
- CSSを使った美しいデザイン
- 実際のプロジェクトで使える実用例
- よくあるトラブルと解決法
この記事を読めば、美しく見やすい表を作れるようになります!
HTMLの表(table)の基本構造

基本的なHTMLテーブルの作り方
まず、枠線のない基本的な表の構造を理解しましょう。
基本構造の説明
<table>
<tr>
<th>見出し1</th>
<th>見出し2</th>
<th>見出し3</th>
</tr>
<tr>
<td>データ1</td>
<td>データ2</td>
<td>データ3</td>
</tr>
</table>
各タグの役割:
<table>
: 表全体を定義<tr>
: 表の行(Table Row)<th>
: 表の見出しセル(Table Header)<td>
: 表のデータセル(Table Data)
より詳細な構造
<table>
<thead>
<tr>
<th>商品名</th>
<th>価格</th>
<th>在庫</th>
</tr>
</thead>
<tbody>
<tr>
<td>りんご</td>
<td>100円</td>
<td>50個</td>
</tr>
<tr>
<td>みかん</td>
<td>80円</td>
<td>30個</td>
</tr>
</tbody>
</table>
構造化タグの意味:
<thead>
: 表のヘッダー部分<tbody>
: 表のデータ部分<tfoot>
: 表のフッター部分(合計などに使用)
なぜ枠線が表示されないのか?
HTMLの基本原理:
- HTML: 構造と内容を定義
- CSS: 見た目とスタイルを制御
つまり: HTMLで表の構造を作っても、見た目の装飾(枠線など)は別途指定する必要があります。
tableに枠線をつける方法【基本編】
1. border属性を使う方法【最も簡単】
基本的な使い方
<table border="1">
<tr>
<th>商品</th>
<th>価格</th>
</tr>
<tr>
<td>りんご</td>
<td>100円</td>
</tr>
<tr>
<td>みかん</td>
<td>80円</td>
</tr>
</table>
結果:
┌────────┬────────┐
│ 商品 │ 価格 │
├────────┼────────┤
│ りんご │ 100円 │
├────────┼────────┤
│ みかん │ 80円 │
└────────┴────────┘
border属性の値による違い
<!-- 細い線 -->
<table border="1">...</table>
<!-- 太い線 -->
<table border="3">...</table>
<!-- 線なし -->
<table border="0">...</table>
重要な注意点
❌ 現在は非推奨:
- HTML5では
border
属性は非推奨 - CSSでの指定が標準的な方法
- 柔軟性に欠ける
✅ 緊急時のみ使用:
- 簡単なテストやプロトタイプ
- HTMLメールでの使用
- 古いブラウザ対応
2. CSSを使った現代的な方法【推奨】
最も基本的なCSS枠線
<style>
table {
border-collapse: collapse;
width: 100%;
}
table, th, td {
border: 1px solid #000000;
}
th, td {
padding: 8px;
text-align: left;
}
</style>
<table>
<tr>
<th>商品名</th>
<th>価格</th>
<th>在庫数</th>
</tr>
<tr>
<td>りんご</td>
<td>100円</td>
<td>50個</td>
</tr>
<tr>
<td>みかん</td>
<td>80円</td>
<td>30個</td>
</tr>
</table>
各プロパティの詳細説明
border-collapse の役割:
/* 隣接する枠線を結合 */
border-collapse: collapse;
/* 枠線を分離(デフォルト) */
border-collapse: separate;
比較例:
<!-- collapse: 線が重ならない -->
<table style="border-collapse: collapse; border: 1px solid black;">
<tr><td style="border: 1px solid black;">セル1</td></tr>
</table>
<!-- separate: 線が二重になる -->
<table style="border-collapse: separate; border: 1px solid black;">
<tr><td style="border: 1px solid black;">セル1</td></tr>
</table>
枠線のカスタマイズ方法【応用編】

1. 枠線の色を変える
基本的な色指定
<style>
table, th, td {
border: 1px solid #3366cc; /* 青い枠線 */
border-collapse: collapse;
}
</style>
部分的な色指定
<style>
table {
border-collapse: collapse;
}
th {
border: 2px solid #2c3e50; /* 見出しは濃い色 */
background-color: #ecf0f1;
}
td {
border: 1px solid #bdc3c7; /* データは薄い色 */
}
</style>
2. 枠線の太さを変える
太さの指定方法
<style>
table {
border-collapse: collapse;
}
/* 外枠を太く */
table {
border: 3px solid #2c3e50;
}
/* 内側の線を細く */
th, td {
border: 1px solid #bdc3c7;
padding: 12px;
}
</style>
段階的な太さ設定
<style>
table {
border-collapse: collapse;
}
/* 最外枠: 太い */
table {
border: 3px solid #2c3e50;
}
/* 見出し行: 中程度 */
th {
border: 2px solid #34495e;
background-color: #ecf0f1;
}
/* データセル: 細い */
td {
border: 1px solid #bdc3c7;
}
</style>
3. 枠線のスタイルを変える
様々な線のスタイル
<style>
.solid-border { border: 2px solid #000; } /* 実線 */
.dashed-border { border: 2px dashed #000; } /* 破線 */
.dotted-border { border: 2px dotted #000; } /* 点線 */
.double-border { border: 3px double #000; } /* 二重線 */
.ridge-border { border: 3px ridge #000; } /* 立体的(凸) */
.groove-border { border: 3px groove #000; } /* 立体的(凹) */
</style>
<table class="solid-border" style="border-collapse: collapse;">
<tr>
<th class="dashed-border">見出し1</th>
<th class="dotted-border">見出し2</th>
</tr>
<tr>
<td class="solid-border">データ1</td>
<td class="double-border">データ2</td>
</tr>
</table>
4. 特定の辺だけに枠線を付ける
個別の辺指定
<style>
table {
border-collapse: collapse;
}
/* 上下のみ枠線 */
.horizontal-only {
border-top: 1px solid #000;
border-bottom: 1px solid #000;
}
/* 左右のみ枠線 */
.vertical-only {
border-left: 1px solid #000;
border-right: 1px solid #000;
}
/* 下のみ枠線 */
.bottom-only {
border-bottom: 2px solid #3498db;
}
</style>
<table>
<tr>
<th class="bottom-only">見出し1</th>
<th class="bottom-only">見出し2</th>
</tr>
<tr>
<td class="horizontal-only">データ1</td>
<td class="horizontal-only">データ2</td>
</tr>
</table>
実用的な表のデザインパターン
1. シンプルなビジネステーブル
<style>
.business-table {
width: 100%;
border-collapse: collapse;
font-family: Arial, sans-serif;
margin: 20px 0;
}
.business-table th {
background-color: #f8f9fa;
border: 1px solid #dee2e6;
padding: 12px;
text-align: left;
font-weight: bold;
color: #495057;
}
.business-table td {
border: 1px solid #dee2e6;
padding: 12px;
color: #212529;
}
.business-table tr:nth-child(even) {
background-color: #f8f9fa;
}
.business-table tr:hover {
background-color: #e9ecef;
}
</style>
<table class="business-table">
<thead>
<tr>
<th>商品名</th>
<th>価格</th>
<th>在庫</th>
<th>カテゴリ</th>
</tr>
</thead>
<tbody>
<tr>
<td>りんご</td>
<td>¥100</td>
<td>50個</td>
<td>果物</td>
</tr>
<tr>
<td>みかん</td>
<td>¥80</td>
<td>30個</td>
<td>果物</td>
</tr>
<tr>
<td>レタス</td>
<td>¥150</td>
<td>25個</td>
<td>野菜</td>
</tr>
</tbody>
</table>
2. モダンなデザインテーブル
<style>
.modern-table {
width: 100%;
border-collapse: collapse;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
border-radius: 8px;
overflow: hidden;
}
.modern-table th {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 15px;
text-align: left;
font-weight: 600;
border: none;
}
.modern-table td {
padding: 15px;
border-bottom: 1px solid #e0e0e0;
color: #333;
}
.modern-table tr:last-child td {
border-bottom: none;
}
.modern-table tr:hover {
background-color: #f5f5f5;
}
</style>
<table class="modern-table">
<thead>
<tr>
<th>プロジェクト名</th>
<th>担当者</th>
<th>進捗</th>
<th>締切</th>
</tr>
</thead>
<tbody>
<tr>
<td>Webサイト制作</td>
<td>田中太郎</td>
<td>75%</td>
<td>2024-02-15</td>
</tr>
<tr>
<td>モバイルアプリ開発</td>
<td>佐藤花子</td>
<td>60%</td>
<td>2024-03-01</td>
</tr>
<tr>
<td>データ分析</td>
<td>山田次郎</td>
<td>90%</td>
<td>2024-01-30</td>
</tr>
</tbody>
</table>
3. 最小限のシンプルテーブル
<style>
.minimal-table {
width: 100%;
border-collapse: collapse;
font-family: system-ui, -apple-system, sans-serif;
}
.minimal-table th {
border-bottom: 2px solid #000;
padding: 10px;
text-align: left;
font-weight: 600;
}
.minimal-table td {
border-bottom: 1px solid #ddd;
padding: 10px;
}
.minimal-table tr:last-child td {
border-bottom: none;
}
</style>
<table class="minimal-table">
<tr>
<th>項目</th>
<th>値</th>
</tr>
<tr>
<td>名前</td>
<td>山田太郎</td>
</tr>
<tr>
<td>年齢</td>
<td>30歳</td>
</tr>
<tr>
<td>職業</td>
<td>エンジニア</td>
</tr>
</table>
4. カラフルなデータテーブル
<style>
.colorful-table {
width: 100%;
border-collapse: collapse;
font-family: Arial, sans-serif;
}
.colorful-table th {
background-color: #3498db;
color: white;
padding: 12px;
text-align: center;
border: 1px solid #2980b9;
}
.colorful-table td {
padding: 12px;
text-align: center;
border: 1px solid #bdc3c7;
}
.colorful-table .high {
background-color: #e74c3c;
color: white;
}
.colorful-table .medium {
background-color: #f39c12;
color: white;
}
.colorful-table .low {
background-color: #2ecc71;
color: white;
}
</style>
<table class="colorful-table">
<thead>
<tr>
<th>タスク</th>
<th>優先度</th>
<th>期限</th>
</tr>
</thead>
<tbody>
<tr>
<td>システム修正</td>
<td class="high">高</td>
<td>今日</td>
</tr>
<tr>
<td>資料作成</td>
<td class="medium">中</td>
<td>明日</td>
</tr>
<tr>
<td>会議準備</td>
<td class="low">低</td>
<td>来週</td>
</tr>
</tbody>
</table>
レスポンシブテーブルの作成

1. 基本的なレスポンシブ対応
<style>
.responsive-table {
width: 100%;
border-collapse: collapse;
margin: 20px 0;
}
.responsive-table th,
.responsive-table td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
.responsive-table th {
background-color: #f2f2f2;
}
/* モバイル対応 */
@media screen and (max-width: 600px) {
.responsive-table {
font-size: 12px;
}
.responsive-table th,
.responsive-table td {
padding: 6px;
}
}
</style>
2. 横スクロール対応
<style>
.table-container {
overflow-x: auto;
margin: 20px 0;
}
.scrollable-table {
width: 100%;
border-collapse: collapse;
min-width: 600px; /* 最小幅を指定 */
}
.scrollable-table th,
.scrollable-table td {
border: 1px solid #ddd;
padding: 12px;
white-space: nowrap;
}
.scrollable-table th {
background-color: #f8f9fa;
position: sticky;
top: 0;
}
</style>
<div class="table-container">
<table class="scrollable-table">
<thead>
<tr>
<th>商品名</th>
<th>価格</th>
<th>在庫数</th>
<th>カテゴリ</th>
<th>供給元</th>
<th>最終更新</th>
</tr>
</thead>
<tbody>
<tr>
<td>りんご</td>
<td>¥100</td>
<td>50個</td>
<td>果物</td>
<td>農園A</td>
<td>2024-01-15</td>
</tr>
<!-- 他の行... -->
</tbody>
</table>
</div>
よくある疑問とトラブルシューティング
Q1: なぜtableだけにborderを指定してもうまくいかないの?
問題の原因
<!-- これだけでは不十分 -->
<table style="border: 1px solid black;">
<tr>
<th>見出し</th>
<td>データ</td>
</tr>
</table>
解決方法
<!-- 正しい方法 -->
<table style="border-collapse: collapse;">
<tr>
<th style="border: 1px solid black;">見出し</th>
<td style="border: 1px solid black;">データ</td>
</tr>
</table>
理由:
table
要素は外枠のみ- 各セル(
th
、td
)には個別に指定が必要 border-collapse: collapse
で境界線を統合
Q2: border-collapseは必要?
collapse なしの場合
<style>
.without-collapse {
border: 1px solid black;
}
.without-collapse th,
.without-collapse td {
border: 1px solid black;
}
</style>
<table class="without-collapse">
<tr>
<th>見出し</th>
<td>データ</td>
</tr>
</table>
結果: 二重の境界線が表示される
collapse ありの場合
<style>
.with-collapse {
border: 1px solid black;
border-collapse: collapse;
}
.with-collapse th,
.with-collapse td {
border: 1px solid black;
}
</style>
<table class="with-collapse">
<tr>
<th>見出し</th>
<td>データ</td>
</tr>
</table>
結果: 境界線が統合され、すっきりとした表示
Q3: 枠線が表示されない場合
考えられる原因と解決方法
原因1: CSSの書き間違い
/* 間違い */
table, th, td {
boarder: 1px solid black; /* typo: boarder */
}
/* 正しい */
table, th, td {
border: 1px solid black;
}
原因2: セレクタの優先度
/* 優先度が低い */
table { border: 1px solid black; }
/* 優先度を上げる */
table { border: 1px solid black !important; }
原因3: 継承の問題
/* 明示的にすべての要素に適用 */
table, table th, table td {
border: 1px solid black;
}
Q4: 印刷時に枠線が表示されない
解決方法
@media print {
table, th, td {
border: 1px solid black !important;
-webkit-print-color-adjust: exact;
color-adjust: exact;
}
}
高度なテーブル装飾テクニック

1. 交互行の背景色(ゼブラストライプ)
<style>
.zebra-table {
width: 100%;
border-collapse: collapse;
}
.zebra-table th,
.zebra-table td {
border: 1px solid #ddd;
padding: 10px;
text-align: left;
}
.zebra-table th {
background-color: #f2f2f2;
}
.zebra-table tr:nth-child(even) {
background-color: #f9f9f9;
}
.zebra-table tr:hover {
background-color: #f5f5f5;
}
</style>
2. 固定ヘッダー
<style>
.fixed-header-table {
width: 100%;
border-collapse: collapse;
max-height: 300px;
overflow-y: auto;
display: block;
}
.fixed-header-table thead,
.fixed-header-table tbody {
display: block;
}
.fixed-header-table thead {
position: sticky;
top: 0;
background-color: #f2f2f2;
}
.fixed-header-table tr {
display: table;
width: 100%;
table-layout: fixed;
}
.fixed-header-table th,
.fixed-header-table td {
border: 1px solid #ddd;
padding: 10px;
text-align: left;
}
</style>
3. ソート可能なテーブル
<style>
.sortable-table {
width: 100%;
border-collapse: collapse;
}
.sortable-table th {
background-color: #f2f2f2;
border: 1px solid #ddd;
padding: 10px;
cursor: pointer;
user-select: none;
}
.sortable-table th:hover {
background-color: #e2e2e2;
}
.sortable-table th.asc::after {
content: ' ▲';
}
.sortable-table th.desc::after {
content: ' ▼';
}
.sortable-table td {
border: 1px solid #ddd;
padding: 10px;
}
</style>
<script>
function sortTable(column) {
// ソート機能の実装
// 実際の使用では、JavaScriptライブラリを使用することを推奨
}
</script>
パフォーマンスとアクセシビリティ
1. アクセシビリティの向上
<table role="table" aria-label="商品一覧">
<caption>2024年1月の商品在庫状況</caption>
<thead>
<tr>
<th scope="col">商品名</th>
<th scope="col">価格</th>
<th scope="col">在庫数</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">りんご</th>
<td>¥100</td>
<td>50個</td>
</tr>
<tr>
<th scope="row">みかん</th>
<td>¥80</td>
<td>30個</td>
</tr>
</tbody>
</table>
2. パフォーマンス最適化
/* 大きなテーブルの場合 */
.large-table {
table-layout: fixed; /* 固定レイアウトで高速化 */
width: 100%;
}
.large-table th,
.large-table td {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
まとめ:美しく機能的なテーブルを作ろう
HTMLで表に枠線をつける方法をマスターすることで、データを見やすく、美しく表示できるようになります。
重要なポイントのおさらい
基本的な枠線の設定:
- CSS推奨:
border
プロパティを使用 - 必須設定:
border-collapse: collapse
- セル指定:
table, th, td
すべてに適用
現代的なアプローチ:
- レスポンシブデザインへの対応
- アクセシビリティの考慮
- パフォーマンスの最適化
避けるべき方法:
- HTML属性
border
の使用(非推奨) - インラインスタイルの乱用
- アクセシビリティへの配慮不足
コメント