テーブルでヘッダーを固定して、ボディのみスクロールさせたいです。
その際にヘッダーとボディ部に input 要素があると、ヘッダーとボディがborderのpx分ずれてしまいます。
どのようにすれば、ずれなくなりますでしょうか?
ご存知の方、ご教示頂ければ幸いです。
よろしくお願い致します。
html
1<div style="margin:20px;"> 2<table class="table"> 3 <thead> 4 <tr> 5 <th class="record_0">ずれない</th> 6 <th class="record_0">border分ずれる</th> 7 <th class="record_1">th:120px</th> 8 <th class="record_2">いいい</th> 9 <th class="record_3">ううう</th> 10 <th class="record_4">えええ</th> 11 <th class="record_5">おおお</th> 12 </tr> 13 </thead> 14 <tbody> 15 <?php for($i=0; $i<20; $i++){?> 16 <tr> 17 <td>td:120px</td> 18 <td><input type="text" class="record_0" value="td:121px"></td> 19 <td><input type="text" class="record_1" value="input:120px"></td> 20 <td><input type="text" class="record_2"></td> 21 <td><input type="text" class="record_3"></td> 22 <td><input type="text" class="record_4"></td> 23 <td><input type="text" class="record_5"></td> 24 </tr> 25 <?php }?> 26 </tbody> 27</table> 28</div>
thead,tbody { display: block; /* スクロールさせるために変更 */ } table{ width: auto !important; } tbody, thead, td, th{ border: solid 3px; /* ずれるのがわかりやすいように線を太く */ border-collapse: collapse; /* 動いてない? */ } tbody{ overflow-y: scroll; height: 200px; } td,th { table-layout: fixed; } th{ background: #eef85766; } td{ padding: 0px !important; } .record_0, .record_1{ width:120px; height:40px; } .record_2{ width:200px; height:40px; } .record_3, .record_4, .record_5 { width:70px; height:40px; } /* スマートじゃなさそう、もっといい方法はないのか?*/ th.record_xxx{ width: 99px; } td.record_xxx{ width: 100px; }
【追記】
今回作成したい画面として、行数が最大100行近くの画面となります。
そのため、可能な限りjavascriptなどでのDOM操作は控えたいと考えています。
HTML, CSSで設定できる方法はないでしょうか?
※最初から1pxを考慮して、widthを指定するのはスマートな感じがしないため、よりよい方法があればご教授いただければ幸いです。

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/11/10 08:05 編集
2017/11/10 08:13