I am an HTML/CSS Noob and this is my first time making a website. The issue I can not seem to figure out is how I can get my table to "fit" in mobile view. As you can see the table does cuts off at the bottom and I want to have the entire table fit in the mobile view.
Normal View
Table Mobile view
table {
font-family: 'Pathway Gothic One', sans-serif;
width: 100%;
border-collapse: collapse;
margin-bottom: 10px;
font-size: 18px;
}
td, th {
border: 1px solid black;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: lightgray;
}
#media screen and (max-width: 767px) {
.left2, .main2 {
width: 100%;
}
thead {
display: none;
}
td {
display: block;
}
td:first-child {
background-color: black;
color: white;
}
td:nth-child(1)::before {
content: "Repair Type"
}
td {
text-align: center;
}
td::before {
float: left;
}
}
<table>
<thead>
<tr>
<th>Repair Type</th>
<th>Price</th>
<th>Turnaround Time</th>
</thead>
<tbody>
<tr>
<td>Battery Replacement</td>
<td>$50 - $100</td>
<td>Same Day</td>
</tr>
<tr>
<td>Camera Replacement</td>
<td>$30 - $105</td>
<td>Same Day</td>
</tr>
<tr>
<td>Charging Port Repair</td>
<td>$25 - $100</td>
<td>Same Day</td>
</tr>
<tr>
<td>Diagnostic</td>
<td>Free</td>
<td>Same Day</td>
</tr>
<tr>
<td>Ear Piece Replacement</td>
<td>$80</td>
<td>Same Day</td>
</tr>
<tr>
<td>Home Button Replacement - Select Models Only</td>
<td>$50 - $150</td>
<td>Same Day</td>
</tr>
<tr>
<td>Screen Repair</td>
<td>$30 - $150</td>
<td>Same Day</td>
</tr>
<tr>
<td>Water Damage Repair</td>
<td>$50 - $150</td>
<td>Same Day</td>
</tr>
<tr>
<td>Virus Removal</td>
<td>$80</td>
<td>Same Day</td>
</tr>
</tbody>
</table>
you can use the #media query for the table
.table-responsive {
min-height: .01%;
overflow-x: auto;
}
#media screen and (max-width: 767px) {
.table-responsive {
width: 100%;
margin-bottom: 15px;
overflow-y: hidden;
-ms-overflow-style: -ms-autohiding-scrollbar;
border: 1px solid #ddd;
}
.table-responsive > .table {
margin-bottom: 0;
}
.table-responsive > .table > thead > tr > th,
.table-responsive > .table > tbody > tr > th,
.table-responsive > .table > tfoot > tr > th,
.table-responsive > .table > thead > tr > td,
.table-responsive > .table > tbody > tr > td,
.table-responsive > .table > tfoot > tr > td {
white-space: nowrap;
}
}
what it does is that it looks for the display size and once a certain size is reached it triggers the CSS relative to that screen size, this is completely user-defined and you can modify it however you want
Related
How to we make the HTML table to responsive without using bootstrap or JQuery, it has more than eight-column.
I'm Using Percentage for each column even its not coming responsive when we increase the text
Scroll coming at the bottom. how to avoid scroll, I don't want to use the Unorder list or div elements
body{
font-family: arial, sans-serif;
font-size:12px
}
.text-to-right {
text-align: right;
}
.text-to-left {
text-align: left;
padding-left: 1rem;
}
table {
width: 100%;
border-left: 1px solid #f3f1f1;
border-bottom: 1px solid #f3f1f1fa;
border-spacing: 0;
}
.table-bordered > tbody > tr > td,
.table-bordered > tbody > tr > th,
.table-bordered > tfoot > tr > td,
.table-bordered > tfoot > tr > th,
.table-bordered > thead > tr > td,
.table-bordered > thead > tr > th {
border-left: 1px solid #f3f1f1;
border-bottom: 1px solid #d2cecefa;
/*box-shadow: 0 2px 4px 0 rgba(241, 230, 230, 0.16), 0 2px 10px 0 rgba(247, 245, 245, 0.12);*/
box-shadow: 0 1px 1px 0 #f3f1f1,0 1px 1px 0 #f3f1f1;
}
.table-bordered > tbody > tr > td:first-child,
.table-bordered > tbody > tr > th:first-child,
.table-bordered > tfoot > tr > td:first-child,
.table-bordered > tfoot > tr > th:first-child,
.table-bordered > thead > tr > td:first-child,
.table-bordered > thead > tr > th:first-child {
border-left-width: 0;
}
.table-bordered tbody tr td:last-child,.table-bordered thead tr th:last-child {
border-right: 1px solid #f3f1f1;;
}
.table-bordered > thead > th:last-child {
border-right: 1px solid #f3f1f1;
}
th {
padding: 1rem 0;
background: #192b4b;
color: #fff;
font-size: 1.3rem;
font-weight: normal;
border-left: 1px solid #f3f1f1;
}
td {
padding: 1rem;
text-transform: capitalize;
color: #6b5e5e;
}
.table-bright-data {
color: #007dc5;
font-size: 1.8rem;
font-family: 'Roboto Condensed', Arial, sans-serif;
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<table class="table-bordered">
<thead>
<tr>
<th width="05%">
S.No
</th>
<th class="text-to-left" width="23%">Name</th>
<th width="10%">Code</th>
<th width="12%">Type</th>
<th width="10%"> Profile</th>
<th width="13%">Time</th>
<th width="10%">Size</th>
<th width="11%">Date</th>
</tr>
</thead>
<tbody>
<tr>
<td class="align-center">
1
</td>
<td class="text-to-left table-bright-data">Responsive layout</td>
<td class="text-to-left">LDHSNSJ734674</td>
<td class="text-to-left">Fixed Interest </td>
<td class="text-to-left">high </td>
<td class="text-to-right">No minimum </td>
<td class="text-to-right">0 </td>
<td class="text-to-right">05-Dec-2001 </td>
</tr>
<tr>
<td class="align-center">
2
</td>
<td class="text-to-left table-bright-data">Responsive layout</td>
<td class="text-to-left">DHEYE933</td>
<td class="text-to-left">Responsive layout </td>
<td class="text-to-left"> </td>
<td class="text-to-right">No minimum </td>
<td class="text-to-right">0 </td>
<td class="text-to-right">05-Dec-2001 </td>
</tr>
</tbody>
</table>
</body>
</html>
You could collapse a table like this
CSS
#media (max-width: 1024px) {
.collapse {
display: block;
}}
HTML
<td class="collapse">
Content
</td>
<td class="collapse">
Content
</td>
I would add multiple classes with different #media (max-width: xxx) so they don't collapse all at once. How you want to collapse them, and in what order you should figur out yourself.
You can look over here https://css-tricks.com/responsive-data-tables/ if you got any more questions.
Personally I like using div over tables, e.g: http://www.responsivegridsystem.com/
I have a blog on Wordpress and I want to create a stylish colourful table on my blog please. Suggest me code for my blog
As #Alex pointed out, the material color palette is a modern and nice way to get colors. All you would need to do is apply it to the different segments of your table.
.myTable {
width: 500px;
border-collapse: collapse;
border-bottom: 1px solid #f44336;
font-family: sans-serif;
}
/* Header */
.myTable thead th {
background: #f44336;
color: #fff;
padding: 3em 1em 1em 1em;
font-weight: 400;
text-align: left;
}
.myTable thead th:nth-child(1) {
font-weight: 600;
}
/* Rows */
.myTable tbody tr td {
padding: 1em;
}
.myTable tbody tr:nth-child(even) td {
background: #ffcdd2;
}
.myTable tbody tr td:nth-child(1) {
font-weight: 600;
}
<table class="myTable">
<thead>
<tr>
<th>Row 1</th>
<th>Row 2</th>
<th colspan="2">Row 3 and 4</th>
</tr>
</thead>
<tbody>
<tr>
<td>1,1</td>
<td>1,2</td>
<td>1,3</td>
<td>1,4</td>
</tr>
<tr>
<td>2,1</td>
<td>2,2</td>
<td>2,3</td>
<td>2,4</td>
</tr>
<tr>
<td>3,1</td>
<td>3,2</td>
<td>3,3</td>
<td>3,4</td>
</tr>
<tr>
<td>4,1</td>
<td>4,2</td>
<td>4,3</td>
<td>4,4</td>
</tr>
</tbody>
</table>
I have a problem with my table cell width. On Chrome it works perfectly, but on Firefox it looks a lot different and I don't whats causing it.
This is the chrome one, looking perfect:
This is the Firefox one, being squeezed tight:
Any idea why this is? Heres my code.
/* Latest games */
.lp-latestgames {
height: 466px;
}
.lp-latestgames .title {
margin-left: 460px;
margin-top: 56px;
margin-bottom: 21px;
}
.lp-latestgames .table {
margin-bottom: 0;
}
.lp-latestgames .table thead {
background-color: rgba(0, 0, 0, 0.45);
}
.lp-latestgames .table thead th {
font-size: 16px;
font-weight: 500 !important;
color: white;
height: 49px;
vertical-align: middle;
}
.lp-latestgames .table thead > tr > th { border-bottom: none; }
.lp-latestgames .table tbody > tr > td {
height: 81px;
border-top: 2px solid #111316;
background-color: rgba(0, 0, 0, 0.19);
vertical-align: middle;
font-size: 14px;
font-weight: 500;
color: white;
}
.lp-latestgames .table tbody > tr:first-child > td { border-top: none; }
.lp-latestgames .table tbody > tr > td:first-child,
.lp-latestgames .table thead > tr > th:first-child{
/* ÜBERARBEITEN!!!!! */
padding-left: 460px;
max-width: 200px;
}
.lp-latestgames .table tbody > tr > td:last-child,
.lp-latestgames .table thead > tr > th:last-child{
/* ÜBERARBEITEN!!!!! */
padding-right: 460px;
max-width: 200px;
}
.lp-latestgames .table tbody > tr > td > .gd-c-versus {
display: block;
font-weight: normal;
font-family: Arial;
color: #494949;
}
.lp-latestgames .table thead > tr > th:nth-child(4),
.lp-latestgames .table thead > tr > th:nth-child(5),
.lp-latestgames .table tbody > tr > td:nth-child(4),
.lp-latestgames .table tbody > tr > td:nth-child(5) {
text-align: center;
}
<div class="lp-latestgames">
<!-- Games -->
<table class="table">
<thead>
<tr>
<th>Name <img src="img/gd_content_arrow_dn.png"></th>
<th>Price Pool <img src="img/gd_content_arrow_dn.png"></th>
<th>Entry <img src="img/gd_content_arrow_dn.png"></th>
<th>Avg Skill <img src="img/gd_content_arrow_dn.png"></th>
<th>Time Remaining <img src="img/gd_content_arrow_up.png"></th>
</tr>
</thead>
<tbody>
<tr>
<td>Im bored. Fite me<span class="gd-c-versus">1 vs 1</span></td>
<td><img src="img/gd_content_coin.png"> 20</td>
<td><img src="img/gd_content_coin.png"> 10</td>
<td><input type="text" value="730" class="circle"></td>
<td>00:00:32</td>
</tr>
<tr>
<td>EG vs LGD - Wild Cards Entry<span class="gd-c-versus">5 vs 5</span></td>
<td><img src="img/gd_content_coin.png"> 1.500.000</td>
<td><img src="img/gd_content_coin.png"> 20</td>
<td><input type="text" value="980" class="circle"></td>
<td>00:01:47</td>
</tr>
<tr>
<td>cyka blyat<span class="gd-c-versus">5 vs 5</span></td>
<td><img src="img/gd_content_coin.png"> 20</td>
<td><img src="img/gd_content_coin.png"> 10</td>
<td><input type="text" value="730" class="circle"></td>
<td>00:02:4</td>
</tr>
</tbody>
</table>
</div>
Have you declared <!DOCTYPE> in your html page, if not give it a try. This doctype is an instruction to the web browser about what version of HTML the page is written in.
Try adding this:
.table { table-layout: fixed }
See snippet
See POST
/* Latest games */
.lp-latestgames {
height: 466px;
}
.lp-latestgames .title {
margin-left: 460px;
margin-top: 56px;
margin-bottom: 21px;
}
.lp-latestgames .table {
margin-bottom: 0;
table-layout: fixed; /*<<<===== ADD THIS RULE RIGHT HERE */
}
.lp-latestgames .table thead {
background-color: rgba(0, 0, 0, 0.45);
}
.lp-latestgames .table thead th {
font-size: 16px;
font-weight: 500 !important;
color: white;
height: 49px;
vertical-align: middle;
}
.lp-latestgames .table thead > tr > th {
border-bottom: none;
}
.lp-latestgames .table tbody > tr > td {
height: 81px;
border-top: 2px solid #111316;
background-color: rgba(0, 0, 0, 0.19);
vertical-align: middle;
font-size: 14px;
font-weight: 500;
color: white;
}
.lp-latestgames .table tbody > tr:first-child > td {
border-top: none;
}
.lp-latestgames .table tbody > tr > td:first-child,
.lp-latestgames .table thead > tr > th:first-child {
/* ÜBERARBEITEN!!!!! */
padding-left: 460px;
max-width: 200px;
}
.lp-latestgames .table tbody > tr > td:last-child,
.lp-latestgames .table thead > tr > th:last-child {
/* ÜBERARBEITEN!!!!! */
padding-right: 460px;
max-width: 200px;
}
.lp-latestgames .table tbody > tr > td > .gd-c-versus {
display: block;
font-weight: normal;
font-family: Arial;
color: #494949;
}
.lp-latestgames .table thead > tr > th:nth-child(4),
.lp-latestgames .table thead > tr > th:nth-child(5),
.lp-latestgames .table tbody > tr > td:nth-child(4),
.lp-latestgames .table tbody > tr > td:nth-child(5) {
text-align: center;
}
<div class="lp-latestgames">
<!-- Games -->
<table class="table">
<thead>
<tr>
<th>Name
<img src="img/gd_content_arrow_dn.png">
</th>
<th>Price Pool
<img src="img/gd_content_arrow_dn.png">
</th>
<th>Entry
<img src="img/gd_content_arrow_dn.png">
</th>
<th>Avg Skill
<img src="img/gd_content_arrow_dn.png">
</th>
<th>Time Remaining
<img src="img/gd_content_arrow_up.png">
</th>
</tr>
</thead>
<tbody>
<tr>
<td>Im bored. Fite me<span class="gd-c-versus">1 vs 1</span>
</td>
<td>
<img src="img/gd_content_coin.png">20</td>
<td>
<img src="img/gd_content_coin.png">10</td>
<td>
<input type="text" value="730" class="circle">
</td>
<td>00:00:32</td>
</tr>
<tr>
<td>EG vs LGD - Wild Cards Entry<span class="gd-c-versus">5 vs 5</span>
</td>
<td>
<img src="img/gd_content_coin.png">1.500.000</td>
<td>
<img src="img/gd_content_coin.png">20</td>
<td>
<input type="text" value="980" class="circle">
</td>
<td>00:01:47</td>
</tr>
<tr>
<td>cyka blyat<span class="gd-c-versus">5 vs 5</span>
</td>
<td>
<img src="img/gd_content_coin.png">20</td>
<td>
<img src="img/gd_content_coin.png">10</td>
<td>
<input type="text" value="730" class="circle">
</td>
<td>00:02:4</td>
</tr>
</tbody>
</table>
</div>
Try to set th or td width using rem instead of px
Each rem equals 16px
I would like to know which would be the best method to create the table in its own div, just affecting itself, using the CSS.
I'm not sure which would be better to keep it in its own individual div.
<head>
<title>f.f. timetable</title>
<link rel="stylesheet" type="text/css" href="http://www.fightingfitlondon.co.uk/files/theme/timetable.css">
</head>
<body>
<div id="fftime" class="ff">
<table class="ff">
<thead>
<tr>
<th></th>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thursday</th>
<th>Friday</th>
</tr>
</thead>
<tbody>
<tr>
<td>07:50 - 08:35</td>
<td rowspan="5">Sprint Training</td>
<td>Sparring</td>
<td rowspan="5">Class 1</td>
<td rowspan="5">Class 2</td>
<td rowspan="5">Class 3</td>
</tr>
<tr>
<td rowspan="2">08:45 - 10:30</td>
<td rowspan="2">Skipping</td>
</tr>
<tr></tr>
<tr>
<td rowspan="2">10:40 - 12:20</td>
<td rowspan="2">Boxing</td>
</tr>
<tr></tr>
<tr>
<td colspan="6"></td>
</tr>
<tr>
<td rowspan="2">13:25 - 15:05</td>
<td rowspan="4">Private Lesson</td>
<td rowspan="2">Running Session</td>
<td rowspan="4">Class 4</td>
<td rowspan="4">Class 5</td>
<td rowspan="4">Class 6</td>
</tr>
<tr></tr>
<tr>
<td rowspan="2">15:20 - 17:00</td>
<td rowspan="2">Conditioning</td>
</tr>
<tr></tr>
</tbody>
</table>
</div>
</body>
</html>
Css
I can't seem to figure out which would be the best placements for the id selector.
#import url(http://fonts.googleapis.com/css?family=Open+Sans:300);
* {
box-sizing: border-box;
font-family: 'Open Sans', Helvetica, Arial, sans-serif;
}
html, body {
height: 100%;
margin: 0;
}
table {
width: 100%;
height: 100%;
border-collapse: collapse;
}
thead th,
tbody td:first-child:not([colspan="6"]) {
background: #456;
color: white;
}
tbody tr td:first-child {
text-align: left;
}
tbody td:not(:first-child) {
background: #EEF;
}
tbody td:not(:first-child):hover {
background: #E8E8FF;
}
td[colspan="6"] {
height: 5px;
padding: 0;
}
th, td {
width: 18.5%;
padding: 1%;
border: 1px solid #CCC;
text-align: center;
}
th:first-child,
td:first-child {
width: 7.5%;
}
Limtu
Reading your comments you're using global CSS rules which are the actual tag names such as table, th, thead so this will add styles to all your tables
Here is an example, hope this will help you out understand CSS rules
HTML:
<div class="tableWrapper">
<table id="tableID">
<thead>
</thead>
<tbody class="tableBody">
<tr>
<td>Hello</td>
</tr>
</tbody>
</table>
</div>
CSS:
.tableBody tr: nth-child(even)
{
border-color: green;
}
.tableBody tr:nth-child(odd)
{
border-color: blue;
}
This is how I would set out my table if it was going into it's own 'div' tag
<div id="AppendTableID/TableWrapperID">
<table id="tableID" class="className">
<thead>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
</tr>
</tbody>
</table>
</div>
Example 1:
#tableID
{
//CSS
}
Example 2
#tableID.className
{
//CSS
}
Below sort of works, but just shows structure
#ff.ffstyle {
box-sizing: border-box;
font-family: 'Open Sans', Helvetica, Arial, sans-serif;
}
#ff html, #ff body {
height: 100%;
margin: 0;
}
#ff table {
width: 100%;
height: 100%;
border-collapse: collapse;
}
#ff thead #ff th,
#ff tbody #ff td:first-child:not([colspan="6"]) {
background: #456;
color: white;
}
#ff tbody #ff tr #ff td:first-child {
text-align: left;
}
#ff tbody #ff td:not(:first-child) {
background: #EEF;
}
#ff tbody #ff td:not(:first-child):hover {
background: #E8E8FF;
}
#ff td[colspan="6"] {
height: 5px;
padding: 0;
}
#ff th, #ff td {
width: 18.5%;
padding: 1%;
border: 1px solid #CCC;
text-align: center;
}
#ff th:first-child,
#ff td:first-child {
width: 7.5%;
}
This seems to work, although the last tag is still reflecting in and effecting my nav menu which has the tag
#ff .ffstyle {
box-sizing: border-box;
font-family: 'Open Sans', Helvetica, Arial, sans-serif;
}
#ff html, body {
height: 100%;
margin: 0;
}
#ff table {
width: 100%;
height: 100%;
border-collapse: collapse;
}
#ff thead th,
#ff tbody td:first-child:not([colspan="6"]) {
background: #456;
color: white;
}
#ff tbody tr td:first-child {
text-align: left;
}
#ff tbody td:not(:first-child) {
background: #EEF;
}
#ff tbody td:not(:first-child):hover {
background: #E8E8FF;
}
#ff td[colspan="6"] {
height: 5px;
padding: 0;
}
<!--this tag below, still affecting my nav menu even though its id is
#ff-->
#ff th, td {
width: 18.5%;
padding: 1%;
border: 1px solid #CCC;
text-align: center;
}
#ff th:first-child,
td:first-child {
width: 7.5%;
}
I need your help.
How can the HTML/CSS coding be modified, so as to allow, when a user scrolls horizontally left and right, that the columns and data move in sync with it. Right now, when the user slides the horizontal bar back and fourth, my columns do not line up properly?
<!DOCTYPE html>
<html>
<head>
<!-- Upgrade MSIE5.5-8 to be compatible with modern browsers -->
<!--[if lt IE 9]>
<script src="http://ie7-js.googlecode.com/svn/version/2.1(beta4)/IE9.js"></script>
<![endif]-->
<style>
/* ----- Scrolling Table ----- */
.dataGridHeader {
position:relative;
padding-top:24px;
border-left: 1px solid #999;
border-bottom: 1px solid #999;
}
.dataGridContent {
overflow-x:scroll;
overflow-y:scroll;
height:144px;
}
.dataGridHeader thead tr {
position:absolute;
top:0;
left:0;
}
.dataGridHeader table thead tr th, .dataGridHeader table tbody tr td {
text-align:left;
height:0;
}
table.scrolltablestyle {
border-top: 1px solid #D9D9D9;
table-layout: fixed;
width: 1100px;
}
table.scrolltablestyle tbody tr td{
background: #fff;
text-align:left;
padding: 4px 9px;
border-right: 1px solid #999;
}
table.scrolltablestyle thead tr th{
background-color: #FFFFD9;
font-weight: normal;
text-align:left;
padding: 4px 9px 4px 9px;
border-bottom: 1px solid #999;
}
table.scrolltablestyle thead tr th {
border-right: 1px solid #999;
border-top: 1px solid #999;
}
table.scrolltablestyle tbody tr td{
border-right: 1px solid #999;
width: 200px;
}
table.scrolltablestyle tbody tr:last-child td{
border-bottom: 0;
}
</style>
</head>
<body>
<div class="dataGridHeader">
<div class="dataGridContent">
<table cellpadding="0" cellspacing="0" class="scrolltablestyle">
<thead>
<tr>
<th>Shopper Name</th>
<th> </th>
<th> </th>
<th>First Name</th>
<th>Last Name</th>
<th>User ID</th>
<th>Status</th>
<th>TestColumn</th>
</tr>
</thead>
<tbody>
<tr>
<td>C2C Fishing</td>
<td> </td>
<td> </td>
<td>John</td>
<td>Doe</td>
<td>C2C Fishing</td>
<td>Enabled</td>
<td>Enabled</td>
</tr>
<tr>
<td>C2C Fishing</td>
<td> </td>
<td> </td>
<td>John</td>
<td>Doe</td>
<td>C2C Fishing</td>
<td>Enabled</td>
<td>Enabled</td>
</tr>
<tr>
<td>C2C Fishing</td>
<td> </td>
<td> </td>
<td>John</td>
<td>Doe</td>
<td>C2C Fishing</td>
<td>Enabled</td>
<td>Enabled</td>
</tr>
<tr>
<td>C2C Fishing</td>
<td> </td>
<td> </td>
<td>John</td>
<td>Doe</td>
<td>C2C Fishing</td>
<td>Enabled</td>
<td>Enabled</td>
</tr>
<tr>
<td>C2C Fishing</td>
<td> </td>
<td> </td>
<td>John</td>
<td>Doe</td>
<td>C2C Fishing</td>
<td>Enabled</td>
<td>Enabled</td>
</tr>
<tr>
<td>C2C Fishing</td>
<td> </td>
<td> </td>
<td>John</td>
<td>Doe</td>
<td>C2C Fishing</td>
<td>Enabled</td>
<td>Enabled</td>
</tr>
<tr>
<td>C2C Fishing</td>
<td> </td>
<td> </td>
<td>John</td>
<td>Doe</td>
<td>C2C Fishing</td>
<td>Enabled</td>
<td>Enabled</td>
</tr>
<tr>
<td>C2C Fishing</td>
<td> </td>
<td> </td>
<td>John</td>
<td>Doe</td>
<td>C2C Fishing</td>
<td>Enabled</td>
<td>Enabled</td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>
Hi Jhon just remove the position:absolute from this class
.dataGridHeader thead tr
{
/*position:absolute;*/ //* this is the problem*/
}
Looking at the OP's code, it appears that the intention was to create a table with a vertically-fixed header row and scrolling body. Almost all of the pure CSS scrolling table designs I've seen that have a fluid width have the same problem: when scrolling sideways, the column headers don't stay with the columns. The accepted answer does solve this problem, but it also disables the vertically-fixed header, essentially turning this into a standard table.
For a solution that keeps the vertically-fixed header while also allowing proper horizontal scrolling, look here: jsfiddle
The code:
HTML
<div class="scrollingtable">
<div>
<div>
<table>
<caption>Top Caption</caption>
<thead>
<tr>
<th><div label="Column 1"/></th>
<th><div label="Column 2"/></th>
<th><div label="Column 3"/></th>
<th>
<!--more versatile way of doing column label; requires 2 identical copies of label-->
<div><div>Column 4</div><div>Column 4</div></div>
</th>
<th class="scrollbarhead"/> <!--ALWAYS ADD THIS EXTRA CELL AT END OF HEADER ROW-->
</tr>
</thead>
<tbody>
<tr><td>Lorem ipsum</td><td>Dolor</td><td>Sit</td><td>Amet consectetur</td></tr>
<tr><td>Lorem ipsum</td><td>Dolor</td><td>Sit</td><td>Amet consectetur</td></tr>
<tr><td>Lorem ipsum</td><td>Dolor</td><td>Sit</td><td>Amet consectetur</td></tr>
<tr><td>Lorem ipsum</td><td>Dolor</td><td>Sit</td><td>Amet consectetur</td></tr>
<tr><td>Lorem ipsum</td><td>Dolor</td><td>Sit</td><td>Amet consectetur</td></tr>
<tr><td>Lorem ipsum</td><td>Dolor</td><td>Sit</td><td>Amet consectetur</td></tr>
<tr><td>Lorem ipsum</td><td>Dolor</td><td>Sit</td><td>Amet consectetur</td></tr>
<tr><td>Lorem ipsum</td><td>Dolor</td><td>Sit</td><td>Amet consectetur</td></tr>
<tr><td>Lorem ipsum</td><td>Dolor</td><td>Sit</td><td>Amet consectetur</td></tr>
<tr><td>Lorem ipsum</td><td>Dolor</td><td>Sit</td><td>Amet consectetur</td></tr>
<tr><td>Lorem ipsum</td><td>Dolor</td><td>Sit</td><td>Amet consectetur</td></tr>
<tr><td>Lorem ipsum</td><td>Dolor</td><td>Sit</td><td>Amet consectetur</td></tr>
</tbody>
</table>
</div>
Faux bottom caption
</div>
</div>
CSS
<!--[if lte IE 9]><style>.scrollingtable > div > div > table {margin-right: 17px;}</style><![endif]-->
<style>
/*the following html and body rule sets are required only if using a % width or height*/
html {
width: 100%;
height: 100%;
}
body {
box-sizing: border-box;
width: 100%;
height: 100%;
margin: 0;
padding: 0 20px 0 20px;
text-align: center;
}
.scrollingtable {
box-sizing: border-box;
display: inline-block;
vertical-align: middle;
overflow: hidden;
width: auto; /*if you want a fixed width, set it here, else set to auto*/
min-width: /*0*/100%; /*if you want a % width, set it here, else set to 0*/
height: 188px/*100%*/; /*set table height here; can be fixed value or %*/
min-height: 0/*104px*/; /*if using % height, make this large enough to fit scrollbar arrows + caption + thead*/
font-family: Verdana, Tahoma, sans-serif;
font-size: 16px;
line-height: 20px;
padding: 20px 0 20px 0; /*need enough padding to make room for caption*/
text-align: left;
}
.scrollingtable * {box-sizing: border-box;}
.scrollingtable > div {
position: relative;
border-top: 1px solid black;
height: 100%;
padding-top: 20px; /*this determines column header height*/
}
.scrollingtable > div:before {
top: 0;
background: cornflowerblue; /*header row background color*/
}
.scrollingtable > div:before,
.scrollingtable > div > div:after {
content: "";
position: absolute;
z-index: -1;
width: 100%;
height: 100%;
left: 0;
}
.scrollingtable > div > div {
min-height: 0/*43px*/; /*if using % height, make this large enough to fit scrollbar arrows*/
max-height: 100%;
overflow: /*scroll*/auto; /*set to auto if using fixed or % width; else scroll*/
overflow-x: hidden;
border: 1px solid black; /*border around table body*/
}
.scrollingtable > div > div:after {background: white;} /*match page background color*/
.scrollingtable > div > div > table {
width: 100%;
border-spacing: 0;
margin-top: -20px; /*inverse of column header height*/
margin-right: 17px; /*uncomment if using % width*/
}
.scrollingtable > div > div > table > caption {
position: absolute;
top: -20px; /*inverse of caption height*/
margin-top: -1px; /*inverse of border-width*/
width: 100%;
font-weight: bold;
text-align: center;
}
.scrollingtable > div > div > table > * > tr > * {padding: 0;}
.scrollingtable > div > div > table > thead {
vertical-align: bottom;
white-space: nowrap;
text-align: center;
}
.scrollingtable > div > div > table > thead > tr > * > div {
display: inline-block;
padding: 0 6px 0 6px; /*header cell padding*/
}
.scrollingtable > div > div > table > thead > tr > :first-child:before {
content: "";
position: absolute;
top: 0;
left: 0;
height: 20px; /*match column header height*/
border-left: 1px solid black; /*leftmost header border*/
}
.scrollingtable > div > div > table > thead > tr > * > div[label]:before,
.scrollingtable > div > div > table > thead > tr > * > div > div:first-child,
.scrollingtable > div > div > table > thead > tr > * + :before {
position: absolute;
top: 0;
white-space: pre-wrap;
color: white; /*header row font color*/
}
.scrollingtable > div > div > table > thead > tr > * > div[label]:before,
.scrollingtable > div > div > table > thead > tr > * > div[label]:after {content: attr(label);}
.scrollingtable > div > div > table > thead > tr > * + :before {
content: "";
display: block;
min-height: 20px; /*match column header height*/
padding-top: 1px;
border-left: 1px solid black; /*borders between header cells*/
}
.scrollingtable .scrollbarhead {float: right;}
.scrollingtable .scrollbarhead:before {
position: absolute;
width: 100px;
top: -1px; /*inverse border-width*/
background: white; /*match page background color*/
}
.scrollingtable > div > div > table > tbody > tr:after {
content: "";
display: table-cell;
position: relative;
padding: 0;
border-top: 1px solid black;
top: -1px; /*inverse of border width*/
}
.scrollingtable > div > div > table > tbody {vertical-align: top;}
.scrollingtable > div > div > table > tbody > tr {background: white;}
.scrollingtable > div > div > table > tbody > tr > * {
border-bottom: 1px solid black;
padding: 0 6px 0 6px;
height: 20px; /*match column header height*/
}
.scrollingtable > div > div > table > tbody:last-of-type > tr:last-child > * {border-bottom: none;}
.scrollingtable > div > div > table > tbody > tr:nth-child(even) {background: gainsboro;} /*alternate row color*/
.scrollingtable > div > div > table > tbody > tr > * + * {border-left: 1px solid black;} /*borders between body cells*/
</style>