投稿日: | 最終更新日:
角丸とストライプのテーブル
表を表現したいときにtableタグを利用します。また、css3の登場によりtableの行を偶数と奇数に分類できるようになりました。css3未対応のブラウザでもJQueryを使ってストライプ(しましま)デザインにすることができます。
テーブルの装飾
1.テーブル全体
角丸デザインにするには、css3の「border-radius」を使います。
ただし、「border-radius」の指定だけでは足りないため注意してください。
以下のようにセルにborder(線)を指定している場合、「border-collapse」の値を変更する必要があります。
border-collapseをsepareteに設定する
「border-collapse」の値が「collapse」のままだと、角丸になりません。
「border-collapse」の値を「separete」にすれば角丸になります。
値 | 効果 |
---|---|
collapse | 隣接するセルのborderを重ねる |
separate | 隣接するセルのborderの間に余白を作る |
border-spacingを0に設定する
border-collapseをsepareteに設定すると、セルとセルの間に余白ができてしまいます。
border-spacingを0に設定して余白を除去します。
ただし、IE7ではborder-spacingが使えません。
cssハックを利用し、IE7のみに「border-collapse: collapse」を指定します。
<table> <thead> <tr> <th>A</th><th>B</th> </tr> </thead> <tbody> <tr> <td>A1</td> <td>B1</td> </tr> <tr> <td>A2</td> <td>B2</td> </tr> <tr> <td>A3</td> <td>B3</td> </tr> </tbody> </table>
table { margin-bottom: 1em; width: 100%; border: 1px solid #ccc; border-spacing: 0; border-collapse: separate; border-radius: 5px; background: #fff; color: #666; *border-collapse: collapse; }
2.th、td要素のborder指定
テーブルのセル(th、td)にborderを指定します。
四辺にborderを引くのではなく、左辺と上辺にborderを指定します。
td, th{ border-top: 1px solid #ccc; border-left: 1px solid #ccc; vertical-align: top; padding: .5em 1em; }
3.不必要なborderを消す
2.の方法ですと、tableの上辺と左辺のborderが2重になります。
不要なborderを消します。
1行目のth要素はborder-topをnoneにします。
一番左の列のth、td要素はborder-leftをnoneにします。
一番上、一番左を指定する場合は、「:first-child」擬似クラスを利用します。
一番上を指定するときは、「tr:first-child th」を指定します。
一番左を指定するときは、「td:first-child, th:first-child」を指定します。
tr:first-child th{ border-top: none; } td:first-child, th:first-child { border-left: none; }
thead、tbodyを記述した場合の注意点
table要素の中に、theadやtbodyを記述することがよくあります。
そのとき、first-childの場所が変わりますので注意して下さい。
・table要素(theadやtbodyを記述しない場合)の「first-child」のtr
・thead要素の「first-child」のtr
・tbody要素の「first-child」のtr
theadやtbodyがある場合、「tr:first-child」と記述するとそれぞれの要素の一番最初の行が指定されます。
thead、tbodyを付けた場合、セレクタを以下のように書き換えましょう。
thead tr:first-child th{ border-top: none; } tbody tr:first-child th{ border-top: none; }
4.th要素に背景指定
th要素に背景を指定します。
CSSグラデーションと、box-shadowで白のハイライトを入れます。
すると、th要素に背景を指定すると、tableの角丸の上に四角形th要素がのります。
th { background:#f7e1e5; background-image: -webkit-gradient(linear, left top, left bottom, from(#f7e1e5), to(#eed5db)); background-image: -moz-linear-gradient(top center, #f7e1e5, #eed5db); background-image: -o-linear-gradient(top, #f7e1e5, #eed5db); box-shadow: 0 1px rgba(255, 255, 255, 1) inset; color: #80555c; text-shadow: 0 1px 1px #fff; font-weight: bold; }
5.四隅を全て角丸にする
背景を指定しますと、角丸のborderが欠けてるように見えます。
th要素自体もborder-radiusで角丸にします。
・左上の角丸
thead tr:first-child th:first-child { border-radius: 5px 0 0 0; }
・右上の角丸
thead tr:first-child th:last-child{ border-radius: 0 5px 0 0; }
・左下の角丸
tbody tr:last-child td:first-child { border-radius: 0 0 0 5px; }
・右下の角丸
tbody tr:last-child td:last-child { border-radius: 0 0 5px 0; }
6.行をストライプにする
行数が多い場合、行がストライプをいれます。
すると、視覚上ユーザーに優しいページとなります。
CSS3では、「:nth-child()」擬似クラスを使って偶数行・奇数行を指定します。
値 | 対象 |
---|---|
tr:nth-child(even) | 偶数行 |
tr:nth-child(odd) | 奇数行 |
tr:nth-child(even){ background: #f8f8f8; }
IE7、8でストライプにするには?
jQueryを利用し、奇数行・偶数行を指定します。
jQueryで偶数行にeven、奇数行にoddというクラスをつけます。
$(function(){ $("table tr:even").addClass("even"); $("table tr:odd").addClass("odd"); });
tr.even{ background: #f8f8f8; }
列をストライプにするには?
「colgroup」要素を使えば、cssで列を装飾することができます。
<table id="sample"> <colgroup class="first"></colgroup> <colgroup class="c_a"></colgroup> <colgroup class="c_b"></colgroup> <colgroup class="c_c"></colgroup> <colgroup class="c_d"></colgroup> <colgroup class="c_e"></colgroup> <thead> <tr> <th> </th> <th>A</th> <th>B</th> <th>C</th> <th>D</th> <th>E</th> </tr> </thead> <tbody> <tr> <th>border-radius</th> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <th>:first-child</th> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <th>:last-child</th> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <th>:nth-child</th> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> </tbody> <tfoot> <tr> <td colspan="6">メッセージ</td> </tr> </tfoot> </table>
table { margin-bottom: 1em; width: 100%; border: 1px solid #ccc; border-spacing: 0; border-collapse: separate; border-radius: 5px; background: #fff; color: #666; *border-collapse: collapse; } td, th{ border-top: 1px solid #ccc; border-left: 1px solid #ccc; vertical-align: top; padding: .5em 1em; } td:first-child, th:first-child { border-left: none; } td:first-child{ white-space: nowrap; } thead tr:first-child th{ border-top: none; } th { background:#f7e1e5; background-image: -webkit-gradient(linear, left top, left bottom, from(#f7e1e5), to(#eed5db)); background-image: -moz-linear-gradient(top center, #f7e1e5, #eed5db); background-image: -o-linear-gradient(top, #f7e1e5, #eed5db); box-shadow: 0 1px rgba(255, 255, 255, 1) inset; color: #80555c; text-shadow: 0 1px 1px #fff; font-weight: bold; } #sample tr:nth-child(even){ background: #none; } tbody th{ background: none; } #sample tr:hover { background: #fbf7e3; } colgroup.c_b, colgroup.c_d{ background: #e7e7e7; }
7.マウスを乗せると背景が変わる
マウスのカーソルを乗せると、行の背景色が変わるようにします。
tr:hover { background: #fbf7e3; }
- Python 114
- 制作 54
- RaspberryPi 41
- Django 40
- WordPress 40
- Linux 27
- VPS 22
- JavaScript 21
- PHP 20
- HTML・CSS 19
- AWS 16
- 仮想環境 15
- レスポンシブデザイン 13
- マイコン 11
- WEB全般 11
- 動画製作 9
- Webサービス 8
- 統合開発環境 8
- 機械学習 8
- PyCharm 7
- jQuery 7
- AfterEffects 7
- 起業・設立 7
- Django REST framework 6
- C# 6
- デザイン 6
- SEO 6
- pydata 6
- Visual Studio 5
- 数学 5
- 携帯サイト 5
- heroku 5
- Mac 5
- illustrator 5
- node.js 5
- Anaconda 5
- Nginx 4
- Jupyter Notebook 4
- インフラ 4
- Google Colaboratory 4
- symfony 4
- Webスクレイピング 3
- photoshop 3
- Go言語 3
- PC 3
- ツール 3
- Docker 3
- facebook 3
- 作業効率化 3
- データベース 3
- Cloud9 3
- コマンド 2
- micro:bit 2
- Kali Linux 2
- Webサーバー 2
- MariaDB 2
- ドローン 2
- コンテナ 2
- DaVinci Resolve 2
- ネットワーク 2
- Java 2
- movie 2
- PCDJ 2
- 音楽 2
- XSERVER 2
- Ansible 1
- Vue.js 1
- JSON 1
- Bootstrap 1
- バージョン管理システム 1
- SSL 1
- S3 1
- ムームードメイン 1
- ネットワーク 1
- アニメーション 1
- D3.js 1
- Rhino 1
- アニメ 1
- git 1
- windows 1
- アクセス解析 1
- スマートフォン 1
- アフィリエイトノウハウ 1
- 知識 1
- TypeScript 1
- 役立つ本・書籍 1
- データサイエンス 1
- ESP32 1
- AI 1
- ownCloud 1
- API 1