CSS テーブル
CSSマニュアルCSSを使って、HTMLテーブルのデザインをカスタマイズすることができます。
テーブルのボーダーの設定
テーブルのボーダーを設定するには、borderプロパティを使用します。ボーダーの太さ、スタイル、および色を設定できます。
table {
border-collapse: collapse;
}
th, td {
border: 1px solid black;
}テーブルセルのパディングの設定
テーブルセルのパディングを設定するには、paddingプロパティを使用します。
th, td {
padding: 8px;
}テーブルヘッダーのスタイル
テーブルヘッダー(th)にスタイルを適用することができます。たとえば、背景色やフォントの太さを設定できます。
th {
background-color: #f2f2f2;
font-weight: bold;
}奇数/偶数行の背景色を交互に設定
奇数行と偶数行の背景色を交互に設定するには、:nth-child擬似クラスを使用します。
tr:nth-child(odd) {
background-color: #f2f2f2;
}
tr:nth-child(even) {
background-color: #ffffff;
}| 名前 | 年齢 | メールアドレス |
|---|---|---|
| 田中 太郎 | 30 | taro.tanaka@example.com |
| 鈴木 花子 | 25 | hanako.suzuki@example.com |
| 佐藤 一郎 | 40 | ichiro.sato@example.com |
| 山田 次郎 | 35 | jiro.yamada@example.com |
| 木村 三郎 | 28 | saburo.kimura@example.com |
| 高橋 四郎 | 32 | shiro.takahashi@example.com |
テーブルのホバーエフェクト
テーブルの行にホバーエフェクトを追加するには、:hover擬似クラスを使用します。
つまり、テーブルの行やセルにマウスカーソルが乗っている時にスタイルを変えることができます。
tr:hover {
background-color: #ddd;
}| 名前 | 年齢 | メールアドレス |
|---|---|---|
| 田中 太郎 | 30 | taro.tanaka@example.com |
| 鈴木 花子 | 25 | hanako.suzuki@example.com |
| 佐藤 一郎 | 40 | ichiro.sato@example.com |
| 山田 次郎 | 35 | jiro.yamada@example.com |
| 木村 三郎 | 28 | saburo.kimura@example.com |
| 高橋 四郎 | 32 | shiro.takahashi@example.com |

コメント