Fixed table row in table - html

I am trying to fix the position of first <tr> in <table> but it now looks like this:
but I would like to have it like this:
I tried:
<table id="myHeader" class="table table-striped table-hover table-bordered table-responsive"><tbody>
<tr style="position: fixed;">
<th>Sr.</th>
<th>Question</th>
<th class="mobiile">Your Answer</th>
<th>Right Answer</th>
<th>Marks</th>
</tr>

document.getElementById('theTable').addEventListener('scroll', scrollHeader);
function scrollHeader(e){
var table = e.currentTarget,
thead = table.getElementsByTagName('thead')[0];
thead.scrollLeft = table.scrollLeft;
}
table {
font-family:Arial, Helvetica, sans-serif;
color:#666;
font-size:15px;
background:#eaebec;
border:#ccc 1px solid;
border-radius:3px;
border-collapse:collapse; border-spacing: 0;
box-shadow: 0 1px 2px #d1d1d1;
display: block;
overflow: auto;
max-height: 200px;
width:425px;
}
table thead {
z-index: 1; /* put on top of other rows */
display: block;
position: fixed;
/* make sure this header width is same as table */
max-width: 500px;
overflow-y: auto; /* use auto so scrollbars only appear if they need to*/
overflow-x: hidden;
}
thead tr td {
background: #eaebec;
width: 65px;
min-width: 65px;
}
table tbody {
display: block;
padding-top: 40px;
}
table th {
padding:2px 2px 2px 2px;
border-top:0;
border-bottom:1px solid #e0e0e0;
border-left: 1px solid #e0e0e0;
background: #ededed;
}
table tr {
text-align: center;
}
/* only using !important to override your styles below */
table td {
margin-left: 0 !important;
width: 65px !important;
padding:2px 2px 2px 2px;
border-top:0;
border-bottom:1px solid #e0e0e0;
border-left: 1px solid #e0e0e0;
}
table tr:hover td {
background: #F7FE2E;
}
table tr, table td {
width: 65px;
min-width: 65px;
}
<div id='container'>
<div id='content'><h3><font color='blue'>Listcode - MP</font></h3>
<div id='wrapper'>
<table id="theTable"><thead><tr><td>Julian Date</td>
<td>Records</td>
<td>Sales</td>
<td>CPO</td>
<td>2006 Records</td>
<td>2006 Deals</td>
</tr></thead>
<tr><td>229</td><td>39784</td><td>0</td><td>10,542.76</td><td>0</td><td>39784</td></tr>
<tr><td>229</td><td>39784</td><td>0</td><td>10,542.76</td><td>0</td><td>39784</td></tr>
<tr><td>229</td><td>39784</td><td>0</td><td>10,542.76</td><td>0</td><td>39784</td></tr>
<tr><td>229</td><td>39784</td><td>0</td><td>10,542.76</td><td>0</td><td>39784</td></tr>
<tr><td>229</td><td>39784</td><td>0</td><td>10,542.76</td><td>0</td><td>39784</td></tr>
<tr><td>229</td><td>39784</td><td>0</td><td>10,542.76</td><td>0</td><td>39784</td></tr>
<tr><td>229</td><td>39784</td><td>0</td><td>10,542.76</td><td>0</td><td>39784</td></tr>
<tr><td>229</td><td>39784</td><td>0</td><td>10,542.76</td><td>0</td><td>39784</td></tr>
<tr><td>229</td><td>39784</td><td>0</td><td>10,542.76</td><td>0</td><td>39784</td></tr>
<tr><td>229</td><td>39784</td><td>0</td><td>10,542.76</td><td>0</td><td>39784</td></tr>
<tr><td>229</td><td>39784</td><td>0</td><td>10,542.76</td><td>0</td><td>39784</td></tr>
<tr><td>229</td><td>39784</td><td>0</td><td>10,542.76</td><td>0</td><td>39784</td></tr>
</table></div></div></div>

Related

table header colspan fix location scrollable

I'm having the issue where my colspan header is going behind my default header when I scroll down on my table. How do I fix this?
Also, it appears that on page load, the top of the scroll bar is higher than the top of the table, so when I scroll down the whole table moves up a bit. Not sure why that is.
I've attached some images down below. (P.S. This is for intro to web dev class, keep it simple if you can).
.opendata {
overflow: hidden;
overflow-y: scroll;
height: 500px;
font-size: 14px;
}
.sticky-header {
position: sticky;
top: 0;
border: 1px solid #000000;
}
table {
border-collapse: separate;
border-spacing: 0;
margin-left: auto;
margin-right: auto;
width: auto;
}
th,
td {
padding: 8px 15px;
border: 1px solid #000000;
}
th {
background: #a0a0a0;
}
td {
background: #e0e0e0;
}
<div class="opendata">
<table id='table'>
<thead>
<tr class="sticky-header">
<th colspan="4" style="font-size: x-large; color: white; text-align: center; background: rgb(6, 60, 122);">
E V I C T I O N S</th>
</tr>
<tr class="sticky-header">
<th>Date</th>
<th>Address</th>
<th>Borough</th>
<th>Zip Code</th>
</tr>
</thead>
<tbody>
</div>

How to hover row with nth-child

I trying to build this table like this
When I hover on any row, the first two cells can't hover even though I have written :hover for all tr tags in the body
Help me pls, tks all!
.wrap-table {
width: calc(100% - 40px);
height: calc(100vh - 235px);
overflow: auto;
background-color: #fff;
margin-left: 10px;
padding-right: 20px;
}
.m-table {
width: 100%;
height: fit-content;
background-color: #fff;
}
.m-table thead th{
position: sticky;
top: 0;
background-color: #ECEEF1;
padding-left: 10px;
padding-right: 8px;
text-align: left;
height: 48px;
border-bottom: 1px solid #c7c7c7;
border-right: 1px solid #c7c7c7;
text-transform: uppercase;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
td:first-child, th:first-child {
position:sticky;
left:0;
z-index:1;
background-color:white;
}
td:nth-child(2),th:nth-child(2) {
position:sticky;
left:37px;
z-index:1;
background-color:white;
}
td:nth-child(12),th:nth-child(12) {
position:sticky;
right: -20px;
z-index:1;
border-left: 1px solid #c7c7c7;
background-color:white;
}
th:first-child , th:nth-child(2) , th:nth-child(12) {
z-index:3
}
.m-table th {
position: sticky;
top: 0;
background: #eee;
}
.m-table tbody tr,td {
height: 48px;
border-bottom: 1px solid #e5e5e5;
cursor: pointer;
}
.m-table tbody td {
padding: 0;
padding-left: 10px;
padding-right: 8px;
max-width: 150px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
border-bottom: 1px solid #c7c7c7;
border-right: 1px dotted #c7c7c7;
}
.m-table tbody tr:hover {
background-color: #F3F8F8;
}
.m-table tbody tr:active {
background-color: #F4F5F6;
}
<table class="m-table" cellspacing="0">
<thead>
<tr>
<th class="sticky">
<input type="checkbox">
</th>
<th class="align-left sticky">MÃ NHÂN VIÊN</th>
<th class="align-left" style="width: 175px">tên nhân viên</th>
<th class="align-left">giới tính</th>
<th class="align-center">ngày sinh</th>
<th class="align-left" style="width: 150px">số cmnd</th>
<th class="align-left" style="width: 150px">chức danh</th>
<th class="align-left" style="width: 150px">tên đơn vị</th>
<th class="align-left" style="width: 120px">số tài khoản</th>
<th class="align-left">tên ngân hàng</th>
<th class="align-left">chi nhánh tk ngân hàng</th>
<th class="align-center sticky">chức năng</th>
</tr>
</thead>
<tbody>
<tr v-for="employee in employees" :key="employee.EmployeeId" :id="employee.EmployeeId">
<th>
<input type="checkbox">
</th>
<td class="sticky">NV0001</td>
<td>David Luiz</td>
<td>Male</td>
<td class="align-center">
01/01/1991
</td>
<td>376574567456</td>
<td>Teacher</td>
<td>School</td>
<td>465745747</td>
<td>Franch Bank</td>
<td>Paris</td>
<td class="sticky">
<MOption />
</td>
</tr>
</tbody>
</table>
you must remove "background-color: white" from this part of your code
td:nth-child(2),
th:nth-child(2) {
position: sticky;
left: 37px;
z-index: 1;
background-color: white;
}
or instead, you can change this part of your code to this
.m-table tbody tr:hover td {
background-color: #f3f8f8;
}
This rule has greater specificity, so it applies first and sets the background-color to white.
td:nth-child(2),th:nth-child(2) {
position:sticky;
left:37px;
z-index:1;
background-color:white;
}

HTML table text-alignment (tbody adapted to thead)

The Table with bad text-alignment
In the Picture u see the table with bad text-alignment. The elements of tbody don't fit to those of thead. It's important that the head of the table is fixed, so you can see it even when you scroll down.
The classes i am using for this table are table-hover and my own class "table_Bookingsystem" which propertys are listed below
.table_Bookingsystem{
table-layout: fixed;
border-collapse: collapse;
width: 100%;
margin-left: auto;
margin-right: auto;
text-align: left;
padding-top: 16px;
padding-bottom: 16px;
border: 1px solid #ccc !important;
}
.table_Bookingsystem tbody{
display:block;
width: 100%;
overflow: auto;
}
.table_Bookingsystem thead tr {
display: block;
}
.table_Bookingsystem tr{
border-bottom: 1px solid #ddd;
}
.table_Bookingsystem thead {
background: black;
color:#fff;
}
.table_Bookingsystem th, .table_Bookingsystem td {
padding: 5px;
text-align: center;
width: 5000px; //important line
}
.table_Bookingsystem tbody tr:nth-child(even) {
background-color: #e4ebf2;
color: #000;
}
<table class="table_Bookingsystem table-hover ">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Auto</th>
<th scope="col">IMEI</th>
<th scope="col">Heimatstadt</th>
<th scope="col">derzeitige Stadt</th>
</tr>
</thead>
<tbody #if(count($cars) > 9) style="height: 300px;" #endif>
#foreach($cars as $car)
<tr class="table-row clickable-row" data-href='/cars/{{$car->id}}'>
<th>{{$loop->iteration}}</th>
<td>{{$car->name}}</td>
<td>{{$car->IMEI}}</td>
<td>
<?php
$city = Illuminate\Support\Facades\DB::table('cities')->where('id','=', $car->id_homeCity)->get('name');
echo $city[0]->name; ?>
</td>
<td>
<?php
$city = Illuminate\Support\Facades\DB::table('cities')->where('id','=', $car->id_currentCity)->get();
echo $city[0]->name; ?>
</td>
</tr>
#endforeach
</tbody>
</table>
When i set the width to 250px it looks a little better but then there is a big edge when i have only 4 or less columns. Is there any way to keep the fixed header and put every -element of directly under the corresponding -element of ?
I write a basic table like you share in screenshot. You have added some extra CSS that's why your table ui disturb.
Step 1 - Remove below CSS, we don't need to block table row.
.table_Bookingsystem thead tr {
display: block;
}
Step 2 - Remove display: block; from table_Bookingsystem tbody
.table_Bookingsystem tbody{
width: 100%;
overflow: auto;
}
Step 3 - I just remove width: 5000px; from .table_Bookingsystem th, .table_Bookingsystem td, if you want apply width: 5000px; on each td/th, revert it.
All Mentioned are applied on below code snippet. Try it I hope it'll help you out. Thanks
.table_Bookingsystem th, .table_Bookingsystem td {
padding: 5px;
text-align: center;
}
.table_Bookingsystem{
table-layout: fixed;
border-collapse: collapse;
width: 100%;
margin-left: auto;
margin-right: auto;
text-align: left;
padding-top: 16px;
padding-bottom: 16px;
border: 1px solid #ccc !important;
}
.table_Bookingsystem tbody{
width: 100%;
overflow: auto;
}
.table_Bookingsystem tr{
border-bottom: 1px solid #ddd;
}
.table_Bookingsystem thead {
background: black;
color:#fff;
}
.table_Bookingsystem th, .table_Bookingsystem td {
padding: 5px;
text-align: center;
}
.table_Bookingsystem tbody tr:nth-child(even) {
background-color: #e4ebf2;
color: #000;
}
<table class="table_Bookingsystem">
<thead>
<tr>
<th>#</th>
<th>Auto</th>
<th>IMEI</th>
<th>Heimatstadt</th>
<th>derzeitige Stadt</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Auto</td>
<td>0001</td>
<td>Regensburg</td>
<td>Regensburg</td>
</tr>
<tr>
<td>1</td>
<td>Auto</td>
<td>0001</td>
<td>Regensburg</td>
<td>Regensburg</td>
</tr>
</tbody>
</table>
Try adding text-align: center; to the .table_Bookingsystem tbody rule as it's on the .table_Bookingsystem td rule.

Transparent table border with tr background

I use :nth-child(odd/even) selector to make different background for table rows for better readability. And now I have to set "border-spacing" or "border-color: transparent" for my table, to make background visible thru the space between cells. The problem is, that horizontal spacing should be different for different columns - the last column should have larger spacing than whole table (marked with red in the sample below).
How can I do that? Have no idea. Please help. Thank you!
body
{
background: #3a7;
}
table
{
border-collapse: separate;
border-spacing: 5px;
border: 0px solid #ffffff;
margin: 1em;
table-layout: fixed;
}
td
{
font-size: 1rem;
empty-cells: show;
/*background: white;*/
padding: 0;
}
td.last
{
font-size: 1rem;
empty-cells: show;
/*background: white;*/
/*border-left: 20px transparent;*/
border-left: 20px solid rgba(255,0,0,0.5);
}
tr:nth-child(odd)
{
padding: 0px;
margin: 0;
background: #fff;
color: #000;
border: 0px solid transparent;
overflow: visible;
}
tr:nth-child(even)
{
padding: 0px;
margin: 0;
background: #000;
color: #fff;
border: 0px solid transparent;
overflow: visible;
}
input
{
border: 0px;
outline: 0px transparent;
background-color: transparent;
color: inherit;
margin: 5px;
}
<table>
<tr>
<td><div><input></div></td>
<td><div><input></div></td>
<td class="last"><div><input></div></td>
</tr>
<tr>
<td><div><input></div></td>
<td><div><input></div></td>
<td class="last"><div><input></div></td>
</tr>
<tr>
<td><div><input></div></td>
<td><div><input></div></td>
<td class="last"><div><input></div></td>
</tr>
<tr>
<td><div><input></div></td>
<td><div><input></div></td>
<td class="last"><div><input></div></td>
</tr>
</table>
This should work
body {
background: #3a7;
}
td.last {
font-size: 1rem;
empty-cells: show;
}
tr:nth-child(odd) {
padding: 0px;
color: #000;
border: 0px solid transparent;
overflow: visible;
}
tr:nth-child(even) {
padding: 0px;
margin: 0;
color: #fff;
border: 0px solid transparent;
overflow: visible;
}
input {
border: 0px;
outline: 0px transparent;
background-color: transparent;
color: inherit;
margin: 5px;
}
tr:nth-child(odd) div {
background: white;
}
tr:nth-child(even) div {
background: black;
}
.last > div {
margin-left: 20px;
}
<table>
<tr>
<td><div><input></div></td>
<td><div><input></div></td>
<td class="last"><div><input></div></td>
</tr>
<tr>
<td><div><input></div></td>
<td><div><input></div></td>
<td class="last"><div><input></div></td>
</tr>
<tr>
<td><div><input></div></td>
<td><div><input></div></td>
<td class="last"><div><input></div></td>
</tr>
<tr>
<td><div><input></div></td>
<td><div><input></div></td>
<td class="last"><div><input></div></td>
</tr>
</table>
Another option is to set the last cell's background to none and instead color the background of the input field and its border:
https://jsfiddle.net/e1fnoa34/
You could make the last cell background: none, and give it padding-left: 20px. Then wrap the cell's content in a DIV or SPAN and apply the background styling to the inner wrapper instead.
Not ideal, but unfortunately you can't vary the with of the table cell spacing, and margin is ignored for table cells.

Top and Bottom Borders of the Table Header Disappear

I need your help.
Whenever the user scrolls down the table, both the top and bottom borders of the table header disappear. Once the user reaches the bottom of the table,
the top and bottom borders of the table header re-appear. How do you fix this? I am using IE7 and need the code to compliant to that particular browser.
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
/*------------------------------------------------------------------
Table Style
------------------------------------------------------------------ */
table a:link {
color: #666;
font-weight: bold;
text-decoration:none;
}
table a:visited {
color: #999999;
font-weight:bold;
text-decoration:none;
}
table a:active,
table a:hover {
color: #bd5a35;
text-decoration:underline;
}
table {
font-family:Arial, Helvetica, sans-serif;
color:#666;
font-size:12px;
background:#eaebec;
border-radius:3px;
border-collapse:collapse; border-spacing: 0;
box-shadow: 0 1px 2px #d1d1d1;
}
table th {
padding:10px 10px 10px 10px;
border-top:1px solid #ccc;
_border-bottom:1px solid #ccc;
border-right: 1px solid #ccc;
background: #ededed;
}
table tr {
text-align: center;
}
table td {
padding:10px;
border-bottom:1px solid #ccc;
border-left: 1px solid #ccc;
border-right: 1px solid #ccc;
background: #fafafa;
}
table tr:hover td {
background: #f2f2f2;
}
table th, table td {
width: 160px;
}
#container {
width: 740px;
height: 300px;
overflow-x: scroll;
overflow-y: scroll;
border-left: 1px solid #ccc;
}
table tr td:first-child, table tr th:first-child {
border-left: 0;
}
table thead {
_position:fixed;
position: relative;
}
table thead tr {
position: relative;
top: expression(this.offsetParent.scrollTop);
}
table tr:first-child td {
border-top: 1px solid #ccc;
}
</style>
</head>
<body>
<div id="container">
<table id="data">
<!-- Table Header -->
<thead>
<tr>
<th>Task Details</th>
<th>Firstname</th>
<th>Progress</th>
<th>Vital Task</th>
</tr>
</thead>
<!-- Table Header -->
<!-- Table Body -->
<tbody>
<tr>
<td>Create pretty table design</td>
<td> </td>
<td>100%</td>
<td>Yes</td>
</tr><!-- Table Row -->
<tr>
<td>Take the dog for a walk</td>
<td> </td>
<td>100%</td>
<td>Yes</td>
</tr><!-- Darker Table Row -->
<tr>
<td>Waste half the day on Twitter</td>
<td> </td>
<td>20%</td>
<td>No</td>
</tr>
<tr>
<td>Feel inferior after viewing Dribble</td>
<td> </td>
<td>80%</td>
<td>No</td>
</tr>
<tr>
<td>Wince at "to do" list</td>
<td> </td>
<td>100%</td>
<td>Yes</td>
</tr>
<tr>
<td>Vow to complete personal project</td>
<td> </td>
<td>23%</td>
<td>yes</td>
</tr>
<tr>
<td>Procrastinate</td>
<td> </td>
<td>80%</td>
<td>No</td>
</tr>
<tr>
<td>Hyperlink Example</td>
<td> </td>
<td>80%</td>
<td>Another</td>
</tr>
</tbody>
<!-- Table Body -->
</table>
</div>
</body>
</html>
After playing around with some of the css with a new set of eyes the next day, the following works for IE7:
<style type="text/css">
/*------------------------------------------------------------------
Table Style
------------------------------------------------------------------ */
table a:link {
color: #666;
font-weight: bold;
text-decoration:none;
}
table a:visited {
color: #999999;
font-weight:bold;
text-decoration:none;
}
table a:active,
table a:hover {
color: #bd5a35;
text-decoration:underline;
}
table {
font-family:Arial, Helvetica, sans-serif;
color:#666;
font-size:12px;
background:#eaebec;
border-radius:3px;
border-collapse:collapse; border-spacing: 0;
box-shadow: 0 1px 2px #d1d1d1;
border-left: 1px solid #ccc;
border-right: 1px solid #ccc;
}
table th {
padding:10px 10px 10px 10px;
border-bottom:1px solid #ccc;
border-left: 1px solid #ccc;
background: #ededed;
}
table tr {
text-align: center;
}
table td {
padding:10px;
border-bottom:1px solid #ccc;
border-left: 1px solid #ccc;
background: #fafafa;
}
table tr:hover td {
background: #f2f2f2;
}
table th, table td {
width: 160px;
}
#container {
width: 800px;
height: 300px;
overflow-x: scroll;
overflow-y: scroll;
}
table tr td:first-child, table tr th:first-child {
border-left: 0;
}
table thead {
position:fixed;
}
TABLE THEAD TR TH {
top:expression(this.offsetParent.scrollTop);
border-top:1px solid #ccc;
position:relative;
}
table tr:first-child td {
border-top: 0;
}
</style>
Hai I just modified your CSS code right below please try this.
table thead {
position:fixed;
}
table thead tr {
position: relative;
}