Table with flexible fixed header row and scrollable body [duplicate] - html

This question already has answers here:
HTML table with 100% width, with vertical scroll inside tbody
(13 answers)
Closed 7 years ago.
I want to fix the first row and the scrollable body. When I like to fix with the css like this
thead
{
display: block;
width: 500px;
}
tbody
{
display: block;
width: 500px;
height: 200px;
overflow: auto !important;
}
But the headers are disaligned. the column and the rows are disaligned.
th,td
{
width:50px;
}
I really don't like to fix the <th> element with the 'width' property. I want that as flexible. Any idea for that flexible fixed header?

try to use in your style may help you
<style>
html, body{
margin:0;
padding:0;
height:100%;
}
section {
position: relative;
border: 1px solid #000;
padding-top: 37px;
background: #500;
}
section.positioned {
position: absolute;
top:100px;
left:100px;
width:800px;
box-shadow: 0 0 15px #333;
}
.container {
overflow-y: auto;
}
table {
border-spacing: 0;
width:100%;s
}
td + td {
border-left:1px solid #eee;
}
td, th {
border-bottom:1px solid #eee;
background: #ddd;
color: #000;
padding: 10px 25px;
}
th {
height: 0;
line-height: 0;
padding-top: 0;
padding-bottom: 0;
color: transparent;
border: none;
white-space: nowrap;
}
th div{
position: absolute;
background: transparent;
color: #fff;
padding: 9px 25px;
top: 0;
margin-left: -25px;
line-height: normal;
border-left: 1px solid #800;
}
th:first-child div{
border: none;
}
</style>
in html
<section>
<div class="container">
<table>
<thead>
<tr class="header">
<th>
Table attribute name
<div>Table attribute name</div>
</th>
<th>
Value
<div>Value</div>
</th>
<th>
Description
<div>Description</div>
</th>
</tr>
</thead>
<tbody>
<tr>
<td>align</td>
<td>left, center, right</td>
<td>Not supported in HTML5. Deprecated in HTML 4.01. Specifies the alignment of a table according to surrounding text</td>
</tr>
<tr>
<td>bgcolor</td>
<td>rgb(x,x,x), #xxxxxx, colorname</td>
<td>Not supported in HTML5. Deprecated in HTML 4.01. Specifies the background color for a table</td>
</tr>
<tr>
<td>border</td>
<td>1,""</td>
<td>Specifies whether the table cells should have borders or not</td>
</tr>
</tbody>
</table>
</div>
</section>

Related

Weird space between td and its border element : CSS tables

I am trying to create a CSS table with border all over using the standard method of applying top and left borders to all td elements (apart from those in first column and first row). And then there is a border on the <table>. This method suits my use-case better than one in which we use border-collapse.
But then there is a bug which resembles inner margin on td elements. This margin like space isn't always there but only occurs on some viewports for some reason. Here's my code:
*{
padding: 0;
margin: 0;
}
html, body {
height: 100%;
width: 100%;
}
.table-container {
display: flex;
flex-direction: column;
align-items: center;
padding-bottom: 50px;
font-size: 20px;
}
table {
font-size: 15px;
border-spacing: 0;
border: 4px solid black;
border-radius: 35px 35px 0 0;
border-left: none;
border-top: none;
width: 90vw;
background-color: white;
margin-top: 50px;
text-align: center;
}
td {
box-sizing: content-box;
border-left: 4px solid black;
border-top: 4px solid black;
height: 60px;
}
thead td:first-child {
border-top-left-radius: 35px;
}
thead td:last-child {
border-top-right-radius: 33px;
}
thead td {
font-size: 20px;
font-weight: 500;
}
table button {
cursor: pointer;
height: 100%;
border: 0;
background-color: white;
color: #d90429;
}
.progress-bar{
background-color: orange;
}
.progress-bar > div{
background-color: green;
height: 100%;
width: 50%
}
<div class="table-container">
<table>
<thead>
<tr>
<td>Task Type</td>
<td>Target Time (in Minutes)</td>
<td>Percentage Achieved</td>
<td>Add/Delete Tasks</td>
</tr>
</thead>
<tbody>
<tr>
<td>Maths</td>
<td>120</td>
<td class="progress-bar">
<div>50</div>
</td>
<td>
<button type="button">
Delete
</button>
</td>
</tr>
</tbody>
</table>
</div>
Here's one viewport in which this bug can be seen in action:
I guess running above code snippet and looking at the output in full screen will reproduce this bug
EDIT
As pointed out by KIKOSoftware in comments, the problem seems to be specific to chrome.
My Version of Chrome happens to use 3.991px for border instead of 4px.
on a personal level, having had a thousand problems with borders in HTML tables,
I ended up opting for a radical solution: no border at all!
I use border-collapse: separate; and I play on border-spacing
in addition it lightens the css to write
I recommend it
in your case:
* {
padding : 0;
margin : 0;
}
html, body {
height : 100%;
width : 100%;
background-color: #658d8d; /* this one is mine... */
}
.table-container {
display : flex;
flex-direction : column;
align-items : center;
padding-bottom : 50px;
font-size : 20px;
margin: 1em;
}
table {
font-size : 15px;
border-collapse : separate;
border-spacing : 4px;
background-color : black;
border-radius : 33px 33px 0 0;
width : 90vw;
}
thead td {
font-size : 20px;
font-weight : 500;
}
td {
background-color : white;
height : 60px;
text-align : center;
}
thead td:first-child { border-top-left-radius: 29px; }
thead td:last-child { border-top-right-radius: 29px; }
table button {
cursor : pointer;
height : 100%;
width : 100%;
border : 0;
background-color : white;
color : #d90429;
}
.progress-bar {
background-color: orange;
}
.progress-bar > div {
background-color : green;
height : 100%;
width : 50%
}
<div class="table-container">
<table>
<thead>
<tr>
<td>Task Type</td>
<td>Target Time (in Minutes)</td>
<td>Percentage Achieved</td>
<td>Add/Delete Tasks</td>
</tr>
</thead>
<tbody>
<tr>
<td>Maths</td>
<td>120</td>
<td class="progress-bar"><div>50</div></td>
<td><button type="button">Delete</button>
</td>
</tr>
</tbody>
</table>
</div>

How to expand an image in a table cell on hover (through CSS)

I'm trying to make it so that images in cells in an html table expand when you hover over them. I want this to be universal to all tables on my website. Here's my code so far:
td img {
height: 150px;
}
td img:hover{
height: 175px;
}
This code makes the images appear at their correct height, but nothing hapens when I hover over them. Obviously I'm doing something wrong, but what is it?
Consider defining the width and height of the cells and adjust the image within those parameters. This way the rows don't shift around when you hover -- change table image size on hover
html {
padding: 50px;
}
table {
width: 600px;
padding: 10px;
background: #FFF;
border: 1px solid #BBB;
}
th {
padding: 10px;
}
td {
vertical-align: middle;
text-align: center;
padding: 5px;
border: 1px solid #DDD;
}
td:first-child {
width: 50px;
}
td:last-child {
width: 200px;
height: 200px;
}
td img {
width: 75%;
height: auto;
}
td img:hover{
width: 90%;
height: auto;
}
<table>
<thead>
<th>Image Title</th>
<th>Image</th>
</thead>
<tbody>
<tr>
<td>Image 1</td>
<td><img src="http://www.logoeps.net/wp-content/uploads/2013/06/stackoverflow_logo.jpg" /></td>
</tr>
</tbody>
</table>
you can do it with CSS
td:hover img{
height:175px;
}
you can use jQuery for this also :
$('td img').on('mouseenter',function(){
$(this).css({
'height':'175px'
});
});
* {
margin: 0;
padding: 0;
}
img {
height: 150px;
width: auto
}
td:hover img {
background: red;
height: 200px;
}
<table>
<tr>
<th>Title</th>
<th>Image</th>
</tr>
<tr>
<td>Image Title</td>
<td><img src="https://torange.biz/photofx/5/8/image-profile-picture-beautiful-exotic-flower-5532.jpg" alt=""></td>
</tr>
</table>

CSS for table scroll not working properly

I have an HTML page in which there is a table which populates data from a database table and I am trying to restrict the size of the table by placing it in a div like in the following
<div id="scrollablebody">
<table class="clientTable">
<thead>
<tr>
<th>Grade</th>
<th>Term</th>
<th colspan="3">Actions</th>
</tr>
</thead>
<!--ko foreach: products-->
<tr>
<td class="clientproductHeader" data-bind="text: $data">
</td>
<td class="clientproductHeader" colspan="13"></td>
</tr>
<tbody data-bind="foreach: ko.observableArray($root.datainput()).extendsdistinct('Product').index.Product()[$data]">
<tr data-bind="template: { name: $root.displayMode, data: $data }"></tr>
</tbody>
<!--/ko-->
</table>
</div>
CSS for div
#scrollablebody{height:500px;overflow-y:auto;width:100%;}
But for some reasons the text in tbody is occupying all the space like in the following image
As you can see in the above picture the row with c5+ is unusually occupying lot of space
CSS for the Table
.clientTable {
max-width: 100%;
background-color: grey;
height:75%;
border-collapse: collapse;
border-spacing: 0;
margin-bottom: 20px;
width: 98%;
margin-left:0;
margin-right:100px;
float: left;
overflow:scroll;
}
table.clientTable thead tr .header {
background-image: url(images/bg.png);
background-repeat: no-repeat;
background-position: center right;
cursor: pointer;
}
table.clientTable td {
padding: 1px;
line-height: 10px;
text-align: center;
/*background-color:#3C78B5;*/
vertical-align: auto;
border: 1px solid #0088cc;
width: 120px;
}
.clientTable th {
padding: initial;
line-height: normal;
text-align: center;
width: initial;
height: 20px;
border: 1px outset gray;
background-color: black;
color: white;
cursor: pointer;
}
Change height to max-height. It's going grow to size if you don't specify and have the overflow as auto.

Removing spacing from between table cells [duplicate]

This question already has answers here:
How do I remove the double border on this table?
(4 answers)
Closed 8 years ago.
(EDIT: Solved. I was on the right track with the border-collapse, but I had to use ctrl+f5 to see it)
Tried using border-collapse and border-spacing to remove them, but it didn't work.
Code:
<main>
<div class="adminr1">
<section class="adminc1">
<table class="adminResults">
<thead>
<td>cell</td>
<td>cell</td>
</thead>
<tr>
<td>cell</td>
<td>cell</td>
</tr>
</table>
</section>
</div>
</main>
CSS:
*
{
margin: 0;
padding: 0;
font-size: small;
font-family: Roboto;
vertical-align: middle;
text-decoration: none;
}
main
{
font-size: 0;
line-height: 1.5;
text-align: center;
margin: 0 auto;
width: 86%;
min-width: 1000px;
}
section
{
border: 1px solid #BBB;
background: #FFF;
border-radius: 7px;
display: inline-block;
overflow: hidden;
}
.adminr1
{
display: inline-block;
width: 66%;
height: 700px;
margin-right: 5px;
font-size: 0;
margin-bottom: 10px;
}
.adminc1
{
width: 100%;
height: 100%;
font-size: 0;
}
/*Table Styles:*/
.adminResults
{
width: 100%;
border: 1px solid #000;
}
.adminResults thead
{
border: 1px solid #000;
}
.adminResults tr td
{
border-left: 1px solid #000;
border-right: 1px solid #000;
}
So far, this is the only page I have which uses a table, so I have no table-related styles anywhere else that could be blocking or overwriting the properties I'm trying to add, nor do i have any border-related files on other elements applied generally enough to do the same thing.
I'm obviously missing something, because this seems like it should be a very easy thing to do.
Use border-collapse property to remove spacing between cells
table.adminResults{
border-collapse:collapse;
}
Fiddle Demo
add the border-collapse:collapse; to table.
.adminResults{width:100%;border:1px solid #000;border-collapse: collapse;}
Here is an example:
http://jsfiddle.net/kheema/n4rsy/1/
Did your try to add border="0" cellpadding="0" cellspacing="0" inside table? Like this:
<table class="adminResults" border="0" cellpadding="0" cellspacing="0">
Set border-spacing: 0px; and border-collapsing: seperate; on the Table.
.adminResults {
width: 100%;
border: 1px solid #000;
border-spacing: 0px;;
border-collapse: seperate;
}
Check out this updated: Fiddle Demo

Table row not expanding to full width

I have a table and when I set the table to a width of 100% and the table rows to a width pf 100% nothing happens or changes to the width.
.Table-Normal {
position: relative;
display: block;
margin: 10px auto;
padding: 0;
width: 100%;
height: auto;
border-collapse: collapse;
text-align: center;
}
.Table-Normal thead tr {
background-color: #E74C3C;
font-weight: bold;
}
.Table-Normal tr {
margin: 0;
padding: 0;
border: 0;
border: 1px solid #999;
width: 100%;
}
.Table-Normal tr td {
margin: 0;
padding: 4px 8px;
border: 0;
border: 1px solid #999;
}
.Table-Normal tbody tr:nth-child(2) {
background-color: #EEE;
}
<table id="top-leader" class="Table-Normal">
<thead>
<tr>
<td>Position</td>
<td>Name</td>
<td>Kills</td>
<td>Deaths</td>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>John Doe</td>
<td>16 Kills</td>
<td>0 Deaths</td>
</tr>
<tr>
<td>2</td>
<td>John Smith</td>
<td>13 Kils</td>
<td>1 Death</td>
</tr>
<tr>
<td>3</td>
<td>Bob Smith</td>
<td>11 Kills</td>
<td>0 Deaths</td>
</tr>
</tbody>
</table>
Remove display: block in .Table-Normal
Fiddle
.Table-Normal {
position: relative;
//display: block;
margin: 10px auto;
padding: 0;
width: 100%;
height: auto;
border-collapse: collapse;
text-align: center;
}
By specifying display: block you've overriding the default value from the browser stylesheet for the table element: display: table. Removing display: block or replacing it with display: table fixes this.
As people have mentioned you have to remove display:block; for this to work. If you need to keep scrolling functionality wrap the table inside a div and set overflow rules on that
<div class = "table-container">
<table>...</table>
</div>
.table-container{
height:100px
width:100px
overflow-x:scroll;
table{
display: table;
width:100%;
}
}