- HOME
- > BLOG CATEGORY
- 【CSS】tableデザインとはCSSでレイアウトを整えよう!
- お知らせ
- NEW 2025.01.18 【jQuery】セレクトボックスにアンカーを設定する方法
- お知らせ
- 2025.01.13 【WordPress】投稿画面のタグをチェックボックスにする方法を解説
- お知らせ
- 2025.01.11 【CSS】文字の縁取りについて解説!
こんな方に読んでほしい
table
関連のデザインについて学びたい方へ前回は、table-layoutについての記事になりました。
table-layout
プロパティは、テーブルのレイアウトアルゴリズムを指定します。
table-layout: auto
とは、セルの内容に応じて、列幅のサイズが自動調整されます。
table-layout: fixed
とは、widthで指定されたセル以外の列幅は同じサイズになります。
auto、fixedの基本的な使い方についての記事になります。
[記事の内容]
tableボーダーとは、表示形式を指定する際に使います。
書き方は以下のようになります。
/*collapseの指定*/
table { border-collapse: 表示形式; }
separate
とは、セル間のボーダーを空けて表示します。
初期値になるため、指定しても変化はありません。
全体に背景色を指定する
見出し1 | 見出し2 | 見出し3 |
---|---|---|
クマくん | リンゴ | 1個120円(税込) |
サルくん | バナナ | 1本140円(税込) |
<table class="table01">
<tbody>
<tr>
<th class="top">見出し1</th>
<th>見出し2</th>
<th>見出し3</th>
</tr>
<tr>
<td>クマくん</td>
<td>リンゴ</td>
<td>1個120円(税込)</td>
</tr>
<tr>
<td>サルくん</td>
<td>バナナ</td>
<td>1本140円(税込)</td>
</tr>
</tbody>
</table>
/*--table01--*/
table.table01 {
table-layout: fixed;
width: 100%;
background-color: #f1f1f1; /*--全体の背景色を指定--*/
color: #333; /*--全体の文字色を指定--*/
}
/*--全体の共通指定--*/
table.table01,
table.table01 td,
table.table01 th {
border: solid 1px #333;
border-collapse: separate; /*--separateを指定--*/
}
th.top {
width: 100px;
}
collapse
とは、セル間のボーダーを空けずに表示します。
collapseを指定する際には、table
、th
、td
に指定するようにしましょう。
全体に背景色を指定する
見出し1 | 見出し2 | 見出し3 |
---|---|---|
クマくん | リンゴ | 1個120円(税込) |
サルくん | バナナ | 1本140円(税込) |
<table class="table01">
<tbody>
<!--以下省略-->
</tbody>
</table>
/*--table01--*/
table.table01 {
table-layout: fixed;
width: 100%;
background-color: #f1f1f1; /*--全体の背景色を指定--*/
color: #333; /*--全体の文字色を指定--*/
}
/*--全体の共通指定--*/
table.table01,
table.table01 td,
table.table01 th {
border: solid 1px #333;
border-collapse: collapse; /*--collapseを指定--*/
}
th.top {
width: 100px;
}
背景色を指定する際には、background-color
プロパティを指定します。
全体に背景色、見出しのみに背景色、偶数、奇数ごとに背景色を変えるなどの指定が可能です。
全体の背景色を指定する際には、table
タグに「background-color」を指定しましょう。
今回は全体の背景をグレー色 (#f1f1f1)、文字色を黒色(#333333)の指定にしています。
全体に背景色を指定する
見出し1 | 見出し2 | 見出し3 |
---|---|---|
クマくん | リンゴ | 1個120円(税込) |
サルくん | バナナ | 1本140円(税込) |
<table class="table01">
<tbody>
<!--以下省略-->
</tbody>
</table>
/*--table01--*/
table.table01 {
table-layout: fixed;
width: 100%;
background-color: #f1f1f1; /*--全体の背景色を指定--*/
color: #333; /*--全体の文字色を指定--*/
}
/*--全体の共通指定--*/
table.table01,
table.table01 td,
table.table01 th {
border: solid 1px #333;
border-collapse: collapse;
}
th.top {
width: 100px;
}
見出しの背景色を指定する際には、th
タグに「background-color」を指定しましょう。
thとは、「Table Header(見出し)の略。表の見出しとして使用します。
今回はthの背景をピンク色 (#fd6158)、文字色を白色(#ffffff)の指定にしています。
見出しに背景色を指定する
見出し1 | 見出し2 | 見出し3 |
---|---|---|
クマくん | リンゴ | 1個120円(税込) |
サルくん | バナナ | 1本140円(税込) |
<table class="table01">
<tbody>
<!--以下省略-->
</tbody>
</table>
/*--table01--*/
table.table01 {
table-layout: fixed;
width: 100%;
background-color: #f1f1f1; /*--全体の背景色を指定--*/
color: #333; /*--全体の文字色を指定--*/
}
table.table01 th {
background-color: #fd6158; /*--thの背景色を指定--*/
color: #fff; /*--thの文字色を指定--*/
}
/*--上記と同じ装飾になります 以下省略--*/
偶数、奇数の背景色を指定する際には、tr
タグに「background-color」を指定しましょう。
「even」は偶数になります。背景色をグレー色(#f9f9f9)
「odd」は奇数になります。背景色を緑色(#9fe2b0)
見出しに背景色を指定する
見出し1 | 見出し2 | 見出し3 |
---|---|---|
クマくん | リンゴ | 1個120円(税込) |
サルくん | バナナ | 1本140円(税込) |
クマくん | リンゴ | 1個120円(税込) |
サルくん | バナナ | 1本140円(税込) |
クマくん | リンゴ | 1個120円(税込) |
サルくん | バナナ | 1本140円(税込) |
<table class="table01">
<tbody>
<!--以下省略-->
</tbody>
</table>
/*--table01--*/
table.table01 {
table-layout: fixed;
width: 100%;
background-color: #f1f1f1; /*--全体の背景色を指定--*/
color: #333; /*--全体の文字色を指定--*/
}
table.table01 th {
background-color: #fd6158; /*--thの背景色を指定--*/
color: #fff; /*--thの文字色を指定--*/
}
/*--偶数の背景色を指定--*/
table.table01 tr:nth-child(even) {
background-color: #f9f9f9;
}
/*--奇数の背景色を指定--*/
table.table01 tr:nth-child(odd) {
background-color: #9fe2b0;
}
/*--上記と同じ装飾になります 以下省略--*/
マウスが重なった行の背景色を変更する事で、どの行を見ているかが分かりやすくなります。
背景色を指定する際には、tr
タグにhoverを指定し「background-color」を指定します。
今回はtrの背景を緑色 (#9fe2b0)の指定にしています。
マウスが重なった行の背景色
見出し1 | 見出し2 | 見出し3 |
---|---|---|
クマくん | リンゴ | 1個120円(税込) |
サルくん | バナナ | 1本140円(税込) |
クマくん | リンゴ | 1個120円(税込) |
サルくん | バナナ | 1本140円(税込) |
クマくん | リンゴ | 1個120円(税込) |
サルくん | バナナ | 1本140円(税込) |
<table class="table01">
<tbody>
<!--以下省略-->
</tbody>
</table>
/*--table01--*/
table.table01 {
table-layout: fixed;
width: 100%;
background-color: #f1f1f1; /*--全体の背景色を指定--*/
color: #333; /*--全体の文字色を指定--*/
}
table.table01 th {
background-color: #fd6158; /*--thの背景色を指定--*/
color: #fff; /*--thの文字色を指定--*/
}
/*--マウスが重なった際の指定--*/
table.table01 tr:hover {
cursor: default;
background-color: #9fe2b0;
}
/*--上記と同じ装飾になります 以下省略--*/
HTMLで記述した際には、左寄せになりますが、センター寄せをする際には、text-align: center
を指定します。
表全体をセンター寄せにするには、table
タグに、「text-align: center」に指定します。
td
タグに対してクラス名を付け、「text-align: right」を指定し、右寄せにすることも可能です。
文字のセンター寄せについて
見出し1 | 見出し2 | 見出し3 |
---|---|---|
クマくん | リンゴ | 1個120円(税込) |
サルくん | バナナ | 1本140円(税込) |
<table class="table01">
<tbody>
<tr>
<td>クマくん</td>
<td>リンゴ</td>
<td class="price">1個120円(税込)</td>
</tr>
</tbody>
</table>
/*--table01--*/
table.table01 {
table-layout: fixed;
width: 100%;
text-align: center; /*--全体をセンター寄せに指定--*/
background-color: #f1f1f1; /*--全体の背景色を指定--*/
color: #333; /*--全体の文字色を指定--*/
}
table.table01 th {
background-color: #fd6158; /*--thの背景色を指定--*/
color: #fff; /*--thの文字色を指定--*/
}
/*--右寄せに指定--*/
table.table01 td.price {
text-align: right;
}
/*--上記と同じ装飾になります 以下省略--*/
セルの幅を広げるのは、th
、td
タグに「padding」を指定しましょう。
「padding: 8px(上下) 16px(左右)」の指定をしてします。
セルの幅について
見出し1 | 見出し2 | 見出し3 |
---|---|---|
クマくん | リンゴ | 1個120円(税込) |
サルくん | バナナ | 1本140円(税込) |
<table class="table01">
<tbody>
<tr>
<td>クマくん</td>
<td>リンゴ</td>
<td class="price">1個120円(税込)</td>
</tr>
</tbody>
</table>
/*--table01--*/
table.table01 {
table-layout: fixed;
width: 100%;
text-align: center; /*--全体をセンター寄せに指定--*/
background-color: #f1f1f1; /*--全体の背景色を指定--*/
color: #333; /*--全体の文字色を指定--*/
}
table.table01 th {
background-color: #fd6158; /*--thの背景色を指定--*/
color: #fff; /*--thの文字色を指定--*/
}
/*--右寄せに指定--*/
table.table01 td,
table.table01 th {
border: solid 1px #333;
border-collapse: collapse;
padding: 8px 16px; /*--paddingを指定--*/
}
/*--上記と同じ装飾になります 以下省略--*/
今回はこれで以上です。
tableに関してはセルの結合などがあります。下記の参考記事から確認してみてください。
tableの基本的な作成についての記事になります。
2025.01.11
2024.10.05
2024.05.15
2024.05.09
2025.01.18
2025.01.13
2025.01.11
2025.01.11
2024.12.22
2024.12.21
2023.06.15
2022.06.30
2020.03.22
2020.03.06
© 2024 shu-naka-blog