Table Header with fixed header without using div - html

I want table header with fixed width without using div tag outside table. I need some solution in which i just need to change css file only. i have tried by putting table in side div with style:
width:100%;overflow-x:scroll
and on minimizing the browser's height and width, it is working fine.
But this is not what i want. I want to change in css style only, to avoid change in all the jsp files in my project
PSB my code:
CSS
h3 {
font: bold 1.2em Verdana, Helvetica, Arial, sans-serif;
background: #B7BBD6;
padding: 5px;
margin: 10px 0 0 0;
clear: both;
}
.list {
float: left;
width: 100%;
color: #000099;
border: 1px solid #DEDFDE;
margin: 0 0 15px 0;
overflow-x: scroll
}
.list th, .list td {
vertical-align: top;
border: 1px solid #B7BBD6
}
.even {
background: #F1F1F1
}
/*.odd {background: #F1F1F1}*/
.odd {
background: #FFF
}
HTML
<h3>Test Table</h3>
<table class="list">
<tr>
<th>a</th>
<th>b</th>
<th>c</th>
<th>d</th>
<th>e</th>
<th>f</th>
<th>g</th>
<th>h</th>
<th>i</th>
</tr>
<tr class="odd">
<td>98052826</a></td>
<td>Company</td>
<td>asdfsafdsa</td>
<td>asdfsadf</td>
<td>asdfsafs</td>
<td>sadfsaf</td>
<td>1234</td>
<td>asfsdf</td>
<td>asfsafdf</td>
</tr>
<tr class="even">
<td>234568992</a></td>
<td>Private</td>
<td>asfsdfaf</td>
<td>asfsaf</td>
<td>safsdafsdf</td>
<td>Some address</td>
<td>3344</td>
<td>&safsdafsda</td>
<td>sadfsaddfdaf</td>
</tr>
<table>
Please suggest??

Set the following to your css stylesheet:
table{width: 100%;table-layout: fixed;}
Edit
If you could use jQuery, its easy:
$(document).ready(function(){
/*define table class then place your class here instead of 'table'*/
var tablewidth = $('table').width();
/*define h3 class then place your class here instead of 'h3'*/
$('h3').css('width',tablewidth - 10);
});
demo

h3{
position:fixed;
width:100%
}
table{
top:60px;
position:relative;
}
demo

Related

center table in page HTML

I'm trying to center this header in the middle of the page. Not sure why margin-left:auto and margin-right:auto isn't working. It won't register align:center either for the table. The table is only a header. Here's the code I have for the css and table.
<style>
table {
margin-top: 2em;
margin-left: auto;
margin-right: auto;
}
table th {
background: linear-gradient(to bottom right, #6688FF, #AACCFF);
height: 4em;
text-align: center;
text-transform: capitalize;
color:white;
border:#ccc 1px solid;
}
table tr {
text-align: center;
}
table thead {
position:fixed;
}
</style>
<table class="head">
<%--header --%>
<thead>
<tr>
<th>one</th>
<th>two</th>
<th>three</th>
</tr>
</thead>
<%--body --%>
<tbody>
</tbody>
</table>
Since you're making the thead fixed, it's pulled out of the normal flow. Below, I've demonstrated one way to center it horizontally independent of its surroundings. Otherwise, you're better off making the table fixed, since semantically they should continue their parent / child relationship.
Is there a reason that you're a table for this?
table {
margin-top: 2em;
}
table th {
background: linear-gradient(to bottom right, #6688FF, #AACCFF);
height: 4em;
text-align: center;
text-transform: capitalize;
color:white;
border:#ccc 1px solid;
}
table tr {
text-align: center;
}
table thead {
position:fixed;
left: 50%;
transform: translateX(-50%);
}
<table class="head">
<thead>
<tr>
<th>one</th>
<th>two</th>
<th>three</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
remove position fixed from table thead selector and it should work

Confused about an example of build a table with fixed header

Could anyone help me to point out which styles is the key to build this fixed header table? This is the only example I find out useful, but I can not figure out which are the most important styles make this happen.
Also I am curious why there needs to be a DIV and padding for th td to make this happen?
<section class="">
<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>
<tr>
<td>cellpadding</td>
<td>pixels</td>
<td>Not supported in HTML5. Specifies the space between the cell wall and the cell content</td>
</tr>
<tr>
<td>cellspacing</td>
<td>pixels</td>
<td>Not supported in HTML5. Specifies the space between cells</td>
</tr>
<tr>
<td>frame</td>
<td>void, above, below, hsides, lhs, rhs, vsides, box, border</td>
<td>Not supported in HTML5. Specifies which parts of the outside borders that should be visible</td>
</tr>
<tr>
<td>rules</td>
<td>none, groups, rows, cols, all</td>
<td>Not supported in HTML5. Specifies which parts of the inside borders that should be visible</td>
</tr>
<tr>
<td>summary</td>
<td>text</td>
<td>Not supported in HTML5. Specifies a summary of the content of a table</td>
</tr>
<tr>
<td>width</td>
<td>pixels, %</td>
<td>Not supported in HTML5. Specifies the width of a table</td>
</tr>
</tbody>
</table>
</div>
</section>
<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;
height: 200px;
}
table {
border-spacing: 0;
width:100%;
}
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>
Your question is a little vague. The code you posted doesn't have a fixed header in the since that they are using the position: fixed on an element. That code is basically a regular old table with data in it. The "tricky" part is the user creates a new header section that sits over the existing thead which is why you see the code like this
<th>
Table attribute name
<div>Table attribute name</div>
</th>
The "Table attibute name" text is being hidden with the th {color: transparent;} and the text in the div is showing in its place.
As for the div class="container"thats creating the scrollable container using:
.container {
overflow-y: auto;
height: 200px;
}
I believe the user is using the table and div combo just so they could have a header with scrollable data underneath and it would all be aligned properly.
If you want a real simplified version of this code without using tables you could use this:
HTML
<div class="wrap">
<div class="top">Title here</div>
<div class="scroll">
<div class="text">All your base are belong to us</div>
</div>
</div>
CSS
html, body{
margin:0;
padding:0;
height:100%;
}
.wrap {
margin: 0 auto;
width: 600px;
}
.top {
border: 1px solid red;
background: lightgray;
line-height: 50px;
text-align: center;
}
.scroll {
background: pink;
height: 200px;
overflow-y: auto;
}
.text {
height: 1000px;
}
Here is the JS.Fiddle if you want to play around with it. Hope that helps.

How to get rid of extra height from td?

My html markup is:
<table>
<tbody>
<tr>
<td><span class="test">This is span.</span></td>
</tr>
</tbody>
css is:
.test{
font-size : 12px;
}
My span element take a height of 12px. So td should also have a height of 12px. But if I inspect in chrome developers tool, td has a height of 21px. Where is 9px coming from? How to get rid of it? Here is my code pen
set span to display:block
.test{
font-size : 12px;
display:block;
background : rgba(255,0,0,.5);
}
for removing default margin and padding of all elements use this
*{
margin:0;
padding:0;
}
* {
margin: 0;
padding: 0;
}
td {
background: black;
padding: 0;
margin: 0;
}
.test {
font-size: 12px;
display: block;
background: rgba(255, 0, 0, .5);
}
<table>
<tbody>
<tr>
<td><span class="test">This is span.</span>
</td>
</tr>
</tbody>
</table>
Edit
the issue is because of inline-block one fix is adding font-size:0; to parent removes the white-space
demo - http://jsfiddle.net/s4ufsbd3/
td {
background: black;
padding: 0;
margin: 0;
font-size:0;
}
* {
margin: 0;
padding: 0;
}
td {
background: black;
padding: 0;
margin: 0;
font-size: 0;
}
.test {
font-size: 12px;
display: inline-block;
background: rgba(255, 0, 0, .5);
}
<table>
<tbody>
<tr>
<td><span class="test">This is span.</span><span class="test">This is span.</span>
<span class="test">This is span.</span>
<span class="test">This is span.</span>
</td>
</tr>
</tbody>
</table>
Vitorino Fernandes solution does work, but you will have issues when you have text inside td since font size : 0px;.
<table>
<tbody>
<tr>
<td>SOME TEXT HERE<span class="test">This is span.</span></td>
</tr>
</tbody>
</table>
Here's my css to fix that issue.
td{
line-height : 0;
}
.test{
font-size : 12px;
display:inline-block;
background : rgba(255,0,0,.5);
line-height : 1;
}
Just add this style to td and check style="line-height : 0;"
As suggested, the line-height is the cause of this space. Also, others suggestions may works as well, but with this you can also adjust the height over and/or bellow the text to your taste using "margin".
HTML
<table>
<tbody>
<tr>
<td><p>This is a paragraph. This paragraph is looooooooooooooooong and wants overlap.</p></td>
</tr>
</tbody>
</table>
CSS
table {
border-collapse: collapse; /*only if you don't want border space between the cells*/
}
td {
font-size:12px;
padding: 0;
margin: 0;
line-height:0;
}
p {
display:block;
line-height: 1em;
margin-top: 0px; /*adjust to your necessities*/
margin-bottom: 0px; /*adjust to your necessities*/
}

Why this CSS settings is not applied at my tables?

I have the following CSS problem.
I have these 2 tables (the first one represent the header and the second one contains the content):
<div>
<table border="1" class="standard-table-cls innerTable">
<thead>
<tr>
<th width="14.2%">Codice RM</th>
<th width="14.2%">Autore Firma</th>
<th width="14.2%">Data Firma</th>
<th width="14.2%">Acq Riserva</th>
<th width="14.2%">Consegna Finale</th>
<th width="14.2%">Descrizione RM</th>
<th width="14.2%">Imponibile</th>
</tr>
</thead>
</table>
</div>
<div class="overflowContainer">
<table border="1" class="standard-table-cls innerTable scrollableTable">
<tbody>
<%
int count = 0;
for (RM currentRM : salDettaglio.getRM()) {
String test = currentRM.getAcqRiserva();
String evenOrOdd;
if((count & 1) == 0) {
evenOrOdd = "even";
}
else {
evenOrOdd = "odd";
}
count++;
%>
<tr id="rmRow" class=<%=evenOrOdd %> >
<td width="14.2%"><%=currentRM.getCodiceRm()%></td>
<td width="14.2%"><%=currentRM.getAutoreFirma()%></td>
<td width="14.2%"><%=currentRM.getDataFirma()%></td>
<td width="14.2%"><%=currentRM.getAcqRiserva()%></td>
<td width="14.2%"><%=currentRM.getConsegnaFinale()%></td>
<td width="14.2%"><%=currentRM.getDescrizioneRM()%></td>
<td width="14.2%"><%=currentRM.getImponibile().toString()%></td>
</tr>
<%}%>
</tbody>
</table>
</div>
Both the tables have setted the standard-table-cls and the innerTable classes.
This is the code of the standard-table-cls class:
table.standard-table-cls {
margin: 0px 0px 0px 0px !important;
width: 100%;
border : #76818a 1px solid;
border-collapse: collapse;
text-align: center;
font: 11px Verdana, Geneva, Arial, Helvetica, sans-serif;
text-decoration:none;
color:#76818a;
table-layout: fixed;
}
And this is the code of the innerTable class (I want that a table that have the innerTable class setted have a width=70% of the total space and that it is floated on the right):
.innerTable{
width: 70%;
float: right;
}
The problem is that using the previous settings seems that don't see the CSS settings related to the .innerTable class.
The strange thing is that if I set the style inline, in this way:
<table border="1" class="standard-table-cls innerTable" style="width: 70%; float: right;">
it works.
Why? What am I missing? How can I solve this issue?
Tnx
Try this instead:
table.innerTable { }
table.standard-table-cls is more specific than .innerTable, so it overrides your style in .innerTable.
table.standard-table-cls styles are overriding you .innerTable.
For this, you should specify your styles like giving an id or using !important.
1. Using ID:
<table border="1" class="standard-table-cls" id="innerTable">
And CSS:
#innerTable{
width: 70%;
float: right;
}
2. Using !important (not the best way to do it though):
.innerTable{
width: 70% !important;
float: right !important;
}
Note: You can use other ways of specifying yours elements.
You can see how to specify your elements through this chart:
probably is a problem of specificity: "Specifics on CSS Specificity"
try:
table.standard-table-cls {
margin: 0px 0px 0px 0px;
width: 100%;
border : #76818a 1px solid;
border-collapse: collapse;
text-align: center;
font: 11px Verdana, Geneva, Arial, Helvetica, sans-serif;
text-decoration:none;
color:#76818a;
table-layout: fixed;
}
.innerTable{
width: 70% !important;
float: right !important;
}

Responsive table

I need help with a responsive table. What's needed is to basically have it change to a 'mobile version' upon resizing, however the mobile version is a little different to the main style of it, as the image shows.
I've currently got this: http://jsfiddle.net/MLsZ8/
HTML:
<table class="crafting">
<thead>
<tr>
<th style="width:15%">Name</th>
<th style="width:20%">Ingredients</th>
<th style="width:205px;">Input > Output</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>Ore Blocks</td>
<td>Gold Ingots, Iron Ingots, Diamonds and Lapis Lazuli Dye</td>
<td><img width="204" height="112" title="Crafting Ore Blocks" src="http://www.minecraftxl.com/images/crafting/Crafting-Ore-Blocks1.gif" alt="Crafting Ore Blocs from Ingots" /></td>
<td>Turn ingots or diamonds into a placeable block. Can be used for storage or to show off.</td>
</tr>
</tbody>
</table>
CSS:
td {
border:0;
}
table.crafting {
border-spacing:0;
border-collapse:collapse;
}
.crafting th {
border:2px solid #f3f3f3;
padding:5px;
}
.crafting td {
border:2px solid #f3f3f3;
padding:5px;
vertical-align:top;
}
.crafting tr {
background:#c6c6c6;
}
.crafting-name {
font-weight:bold;
border-bottom:0 !important;
background:#c6c6c6;
}
.crafting-ingredients {
border-top:0 !important;
border-bottom:0 !important;
background:#bcbcbc;
}
.crafting-img {
width:205px;
border-bottom:0 !important;
border-top:0 !important;
background:#c6c6c6;
}
.crafting-desc {
border-top:0 !important;
background:#bcbcbc;
}
If you are not opposed to changing the overall format of the HTML, I have a solution that might be a bit easier to handle...
If you change the current table structure to a series of div elements, you can nest each table row into a container div.
I'll give you an example for one "row":
<div class="tableRow">
<div class="columnOne"> content </div>
<div class="columnTwo"> content </div>
<div class="columnThree"> content </div>
<div class="columnFour"> content </div>
</div>
Then, using CSS, you could set .tableRow {width: 100%}. From here, you could set the column widths based on your needs. From your example, it looks like you could do:
.columnOne {width: 10%; float: left;}
.columnTwo {width: 15%; float: left;}
.columnThree {width: 30%; float: left;}
.columnFour {width: 45%; float: left;}
Then, when you reach your mobile view breakpoint, using a #media query, you can do the following:
.columnOne, .columnTwo, .columnThree, .columnFour {width: 100%}
This will cause the columns to effectively become rows of width: 100%.
Option 1:
Full tables
http://jsfiddle.net/2655u/
Option 2
Convert tables to div in used mediaqueries
HTML
<div class="title">
<div class="name">Name</div>
<div class="ingredients">Ingredients</div>
<div class="field">Input > Output</div>
<div class="description">Description</div>
</div>
<div class="responsive">
<div class="name">Ore Blocks</div>
<div class="ingredients">Gold Ingots, Iron Ingots, Diamonds and Lapis Lazuli Dye</div>
<div class="field">
<img width="204" height="112" title="Crafting Ore Blocks" src="http://www.minecraftxl.com/images/crafting/Crafting-Ore-Blocks1.gif" alt="Crafting Ore Blocs from Ingots" />
</div>
<div class="description">Turn ingots or diamonds into a placeable block. Can be used for storage or to show off.</div>
</div>
CSS
div {
display: table;
width: 100%;
table-layout: fixed;
}
div > div {
display: table-cell;
background : #C6C6C6;
border:2px solid #f3f3f3;
padding:5px;
vertical-align : top;
}
div.title {
text-align : center;
font-weight:bold;
}
div.name {
width : 90px;
}
div.ingredients {
width : 150px;
}
div.field {
width : 205px;
}
#media only screen and (max-width: 767px) {
.title {display:none;}
.responsive div {
display : block;
width : auto;
text-align : center;
background : white;
}
.responsive div.ingredients {background : #C6C6C6;}
.responsive div.description {background : #C6C6C6;}
}
Example: http://jsfiddle.net/2DTSG/
Well, I was also searching for the same one day. Got the following. It follows the same approach, converting columns to rows when getting viewed in smaller device.
http://css-tricks.com/responsive-data-tables/
Before moving ahead see the Live Demo
One simple solution is to have two tables: a regular table (with class full) and a mobile one (with class mobile). Then you can use a media query to switch between them at a particular screen size.
If your website isn't particularly heavy, this is an approach that will save a lot of headache.
Example fiddle: http://jsfiddle.net/QDrPb/
.mobile {
display:none;
}
#media (max-width:767px) {
.full {
display:none;
}
.mobile {
display:block;
}
}
Twitter Bootstrap is a nice thing to achieve table-responsiveness.
http://getbootstrap.com/
You have to download it from the above link and add the css file.
After that, apply like this: http://getbootstrap.com/css/#tables-responsive
I hope this may help your need.
Thanks
here simple demo please reffer this link for pure css demo fiddle
/*by Ñ££¿ Upadhyay*/
body {
font-family: "Open Sans", sans-serif;
line-height: 1.25;
}
table {
border: 1px solid #ccc;
border-collapse: collapse;
margin: 0;
padding: 0;a
width: 100%;
table-layout: fixed;
}
table caption {
font-size: 18px;
margin: 10px;
}
table tr {
background: #f8f8f8;
border: 1px solid #ddd;
padding: 10px;
}
table th,
table td {
padding: 10px;
text-align: center;
}
table th {
font-size: 14px;
letter-spacing: 0;
text-transform: uppercase;
}
#media screen and (max-width: 600px) {
table {
border: 0;
}
table caption {
font-size: 14px;
}
table thead {
border: none;
clip: rect(0 0 0 0);
height: 1px;
margin: -1px;
overflow: hidden;
padding: 0;
position: absolute;
width: 1px;
}
table tr {
border-bottom: 3px solid #ddd;
display: block;
margin-bottom: 5px;
}
table td {
border-bottom: 1px solid #ddd;
display: block;
font-size: 14px;
text-align: right;
}
table td:before {
/*
* aria-label has no advantage, it won't be read inside a table
content: attr(aria-label);
*/
content: attr(data-label);
float: left;
font-weight: bold;
text-transform: uppercase;
padding-right: 70px;
}
table td:last-child {
border-bottom: 0;
}
}
<table>
<caption>Statement Summary</caption>
<thead>
<tr>
<th scope="col">Account</th>
<th scope="col">Due Date</th>
<th scope="col">Amount</th>
<th scope="col">Period</th>
<th scope="col">Period</th>
</tr>
</thead>
<tbody>
<tr>
<td data-label="Account">Visa - 3412</td>
<td data-label="Due Date">04/01/2016</td>
<td data-label="Amount">$1,190</td>
<td data-label="Period">03/01/2016 - 03/31/2016</td>
<td data-label="Period">03/01/2016 - 03/31/2016</td>
</tr>
<tr>
<td scope="row" data-label="Account">Visa - 6076</td>
<td data-label="Due Date">03/01/2016</td>
<td data-label="Amount">$2,443</td>
<td data-label="Period">02/01/2016 - 02/29/2016</td>
<td data-label="Period">03/01/2016 - 03/31/2016</td>
</tr>
<tr>
<td scope="row" data-label="Account">Corporate AMEX</td>
<td data-label="Due Date">03/01/2016</td>
<td data-label="Amount">$1,181</td>
<td data-label="Period">02/01/2016 - 02/29/2016</td>
<td data-label="Period">03/01/2016 - 03/31/2016</td>
</tr>
<tr>
<td scope="row" data-label="Acount">Visa - 3412</td>
<td data-label="Due Date">02/01/2016</td>
<td data-label="Amount">$842</td>
<td data-label="Period">01/01/2016 - 01/31/2016</td>
<td data-label="Period">03/01/2016 - 03/31/2016</td>
</tr>
</tbody>
</table>
Responsive Tables JavaScript Plugin
<link rel="stylesheet" href=".../dist/css/table-fluid.css"/>
<script src=".../dist/js/table-fluid.js"></script>
<table class="table-fluid">
<thead>
...
</thead>
<tbody>
...
</tbody>
</table>
Use JavaScript function
window.tableFluid('.table-fluid');
https://www.npmjs.com/package/table-fluid
https://github.com/maestro888/table-fluid
Table cells cannot rearrange they way you want - rows and columns are locked and cannot float. All you can do is change the layout WITHIN each cell. You will need to change your mark-up completely to make that happen.