css, html - colspan not working the same on different tables [duplicate] - html

This question already has answers here:
How is table layout fixed rendered?
(1 answer)
CSS table-layout: fixed not working?
(2 answers)
Closed 4 years ago.
I have two html tables that I would like to align the same (the columns should be of equal width for the both tables). Both tables have a title that should span two columns at the start, and both should have four columns total.
What happens is even though the width of the columns is defined, the resize according to the column content.
td,
th {
padding: .5rem;
}
.red-header {
font-weight: 700;
background-color: rgb(184, 167, 177);
text-align: start;
}
<table>
<thead>
<tr>
<th colspan="2" width="35%" class="red-header">
THIS IS A LONG LONG HEADER STRING
</th>
<th width='35%' />
<th width="15%" />
<th width="15%" />
</tr>
</thead>
<tbody>
<tr>
<td width="35%">something</td>
<td width="35%">something else</td>
<td width="15%">something</td>
<td width="15%">something else</td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th colspan="2" width="35%" class="red-header">
THIS IS A LONG LONG HEADER STRING
</th>
<th width='35%' />
<th width="15%">something</th>
<th width="15%">something else</th>
</tr>
</thead>
<tbody />
</table>
How do I make the columns retain their size regardless of the content?

You set the colspan to 2 in the second table yet you have the second header still there but missing the closing tag. if you remove the second heading missing the tag it should fix your problem.
<table>
<thead>
<tr>
<th colspan="2" width="35%" class="red-header">
THIS IS A LONG LONG HEADER STRING
</th>
<th width='35%' />
<th width="15%" />
<th width="15%" />
</tr>
</thead>
<tbody>
<tr>
<td width="35%">something</td>
<td width="35%">something else</td>
<td width="15%">something</td>
<td width="15%">something else</td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th colspan="2" width="35%" class="red-header">
THIS IS A LONG LONG HEADER STRING
</th>
<th width="15%">something</th>
<th width="15%">something else</th>
</tr>
</thead>
<tbody />
</table>
or close the tag
<table>
<thead>
<tr>
<th colspan="2" width="35%" class="red-header">
THIS IS A LONG LONG HEADER STRING
</th>
<th width='35%' />
<th width="15%" />
<th width="15%" />
</tr>
</thead>
<tbody>
<tr>
<td width="35%">something</td>
<td width="35%">something else</td>
<td width="15%">something</td>
<td width="15%">something else</td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th colspan="2" width="35%" class="red-header">
THIS IS A LONG LONG HEADER STRING
</th>
<th width='35%' /></th>
<th width="15%">something</th>
<th width="15%">something else</th>
</tr>
</thead>
<tbody />
</table>

the one mistake i found your code is that you are missing closing for th tag in multiple places ..you can either use colspan or percentage of the width using both at same time dont make sense you can do this way using colspan
table{
width:100%
}
<table border='1'>
<thead>
<tr>
<th colspan="4" class="red-header">
THIS IS A LONG LONG HEADER STRING
</th>
</tr>
</thead>
<tbody>
<tr>
<td >something</td>
<td >something else</td>
<td >something</td>
<td >something else</td>
</tr>
</tbody>
</table>
<table border="1">
<thead>
<tr>
<th colspan="4" width="35%" class="red-header">
THIS IS A LONG LONG HEADER STRING
</th>
</tr>
</thead>
<tbody>
<tr>
<td >something</td>
<td >something else</td>
<td >something</td>
<td >something else</td>
</tr>
</tbody>
</table>

Related

Arrangements in Html Table

I stumbled around this table ⤵️
Problem Description
Can someone tell me how do I get
"/" And "Time" below the Days Row?
Here is code:
<table border="2">
<tr>
<th rowspan="3">Days
</th>
<th rowspan="3">Monday</th>
<th rowspan="3">Tuesday</th>
<th rowspan="3">Wednesday</th>
<th rowspan="3">Thursday</th>
<th rowspan="3">Friday</th>
<th rowspan="3">Saturday</th>
</tr>
<tr>
<th>/</th>
</tr>
<tr>
<th>Time</th>
</tr>
</table>
You need to remove the rowspan from the "Days" column (or set it to 1):
<table border="2">
<tr>
<th>Days</th>
<th rowspan="3">Monday</th>
<th rowspan="3">Tuesday</th>
<th rowspan="3">Wednesday</th>
<th rowspan="3">Thursday</th>
<th rowspan="3">Friday</th>
<th rowspan="3">Saturday</th>
</tr>
<tr>
<th>/</th>
</tr>
<tr>
<th>Time</th>
</tr>
</table>

How can I remove all left and right padding in an html table cell?

I am creating a timeline, with a column for each year represented in the timeline; I want these year columns to be as "skinny" (unwide) as possible. Right now they look like so:
How can I remove all the left and right padding within the cells between the years?
My current code is:
<table class="table table-sm table-bordered">
<thead class="thead-dark">
<tr>
<th scope="col">NAME</th>
<th scope="col">RELATIONSHIP</th>
<th scope="col">1794</th>
<th scope="col">1795</th>
<th scope="col">1796</th>
NOTE: I also want the rows to refrain from wrapping. The rows code is currently:
<tr>
<th scope="row">John Marshall Clemens</th>
<td>Father</td>
<td>~</td>
<td>~</td>
<td>~</td>
You can use it like this
<table cellpadding = "0">
<tr>
<th> Month </th>
<th> Savings </th>
</tr>
<tr>
<td> January </td>
<td> $100 </td>
</tr>
</table>
As you are using bootstrap you can remove any padding and margins with bootstrap spacing helper classes
It has a very flexible syntax to adding or removing paddings and margings. In your case you need to ad px-0 class to your corsponding th and td tags in head and body like bellow:
<table class="table table-bordered text-center">
<thead>
<tr>
<th>Name</th>
<th>Relation</th>
<th class="px-0">1794</th>
<th class="px-0">1795</th>
<th class="px-0">1796</th>
<th class="px-0">1797</th>
</tr>
</thead>
<tbody>
<tr>
<td>jhon</td>
<td>fater</td>
<td class="px-0">~</td>
<td class="px-0">~</td>
<td class="px-0">~</td>
<td class="px-0">~</td>
</tr>
<tr>
<td>mark</td>
<td>self</td>
<td class="px-0">~</td>
<td class="px-0">~</td>
<td class="px-0">~</td>
<td class="px-0">~</td>
</tr>
<tr>
<td>livy</td>
<td>wife</td>
<td class="px-0">~</td>
<td class="px-0">~</td>
<td class="px-0">~</td>
<td class="px-0">~</td>
</tr>
</tbody>
</table>
px-0 is equal to
.px-0 {
padding-right: 0 !important;
padding-left: 0 !important;
}
if you are using bootstrap then bootstrap itself provide padding of 12px with <td> and <th>. so for making table "skinny" you need to override css.
html
<table class="table table-bordered text-center">
<thead>
<tr>
<th>Name</th>
<th>Relation</th>
<th>1794</th>
<th>1795</th>
<th>1796</th>
<th>1797</th>
</tr>
</thead>
<tbody>
<tr>
<td>jhon</td>
<td>fater</td>
<td>~</td>
<td>~</td>
<td>~</td>
<td>~</td>
</tr>
<tr>
<td>mark</td>
<td>self</td>
<td>~</td>
<td>~</td>
<td>~</td>
<td>~</td>
</tr>
<tr>
<td>livy</td>
<td>wife</td>
<td>~</td>
<td>~</td>
<td>~</td>
<td>~</td>
</tr>
</tbody>
</table>
css
th, td {
padding: 0;
}

Wrong usage of th tag

We just wrote a basic html code but it gives one outputs when th is used and gives and another output when tr is used
This is the actual output (with tr)
<table width="100%">
<tr>
<td align="center" width="50%"><b>FIRST DEGREE PROGRAMMES (AIDED PROGRAMMES)</b></td>
<td align="center" width="50%"><b>FIRST DEGREE PROGRAMMES (UNAIDED PROGRAMMES)</b></td>
</tr>
<tr>
<td valign="top">
<table border="1" style=" border-collapse: collapse;">
<tr>
<th width="5%">No</th>
<th width="10%">DEGREE</th>
<th width="30%">PROGRAMME</th>
<th width="55%">COMPLEMENTARY COURSE</th>
</tr>
<tr>
<td>1</td>
<td>B.Sc</td>
<td>Botany & Biotechnology</td>
<td>1. Biochemistry</td>
</tr>
<tr>
<td>2</td>
<td>B.Com</td>
<td>Commerce</td>
<td>1. Finance/Computer Applications/Co-operation (Electives)</td>
</tr>
</table>
</td>
<td valign="top">
<table border="1" style=" border-collapse: collapse;">
<tr>
<th width="5%">No</th>
<th width="10%">DEGREE</th>
<th width="30%">PROGRAMME</th>
<th width="55%">COMPLEMENTARY COURSE</th>
</tr>
<tr>
<td>1</td>
<td>B.Com</td>
<td>Commerce</td>
<td>1. Finance (Electives)</td>
</tr>
<tr>
<td>2</td>
<td>B.Com</td>
<td>B.Com Accounts & Audit (Self Financing) </td>
<td>1. Accounts & Audit (Electives)</td>
</tr>
</table>
</td>
</tr>
</table>
This is the confusing output (with th)
<table width="100%">
<th>
<td align="center" width="50%"><b>FIRST DEGREE PROGRAMMES (AIDED PROGRAMMES)</b></td>
<td align="center" width="50%"><b>FIRST DEGREE PROGRAMMES (UNAIDED PROGRAMMES)</b></td>
</th>
<tr>
<td valign="top">
<table border="1" style=" border-collapse: collapse;">
<tr>
<th width="5%">No</th>
<th width="10%">DEGREE</th>
<th width="30%">PROGRAMME</th>
<th width="55%">COMPLEMENTARY COURSE</th>
</tr>
<tr>
<td>1</td>
<td>B.Sc</td>
<td>Botany & Biotechnology</td>
<td>1. Biochemistry</td>
</tr>
<tr>
<td>2</td>
<td>B.Com</td>
<td>Commerce</td>
<td>1. Finance/Computer Applications/Co-operation (Electives)</td>
</tr>
</table>
</td>
<td valign="top">
<table border="1" style=" border-collapse: collapse;">
<tr>
<th width="5%">No</th>
<th width="10%">DEGREE</th>
<th width="30%">PROGRAMME</th>
<th width="55%">COMPLEMENTARY COURSE</th>
</tr>
<tr>
<td>1</td>
<td>B.Com</td>
<td>Commerce</td>
<td>1. Finance (Electives)</td>
</tr>
<tr>
<td>2</td>
<td>B.Com</td>
<td>B.Com Accounts & Audit (Self Financing) </td>
<td>1. Accounts & Audit (Electives)</td>
</tr>
</table>
</td>
</tr>
</table>
We are confused why there is an extra td coming when we use th for main table. If you are viewing using a UC Browser, you can simply press Ctrl button and click on the browser output which will give you the actual table layout. So in first output there will only two tds and for second output there will be three td's.
This is because <th> is a table cell. Including table cells (<td>) inside table cells (<th>) will give you unexpected results. What you are looking for is <thead>: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/thead
As a boilerplate, you can define tables as below:
<table>
<!-- header -->
<thead>
<tr>
<th>Header content 1</th>
<th>Header content 2</th>
</tr>
</thead>
<!-- body -->
<tbody>
<tr>
<td>Body content 1</td>
<td>Body content 2</td>
</tr>
</tbody>
</table>

Header with a rowspan

Does anyone here knows how to make a table header like this (refer to the image below)?
I tried:
<thead>
<tr>
<th>No.</th>
<th>Creation Date</th>
<th>Week Day</th>
<th>Log Type</th>
<th colspan="5" rowspan="1" style="text-align: center">Time</th>
<th>action</th>
</tr>
<tr>
<th>IN/START</th>
<th>OUT/STOP</th>
</tr>
</thead>
but sadly, no luck, the table head is missed up, any help, suggestions, clues, recommendations, suggestions, ideas?
That's supposed to be colspan="2" on the one column and rowspan="2" on others. Use this:
table, th {border: 1px solid #ccc; border-collapse: collapse; font-weight: normal; font-family: 'Segoe UI'; margin: 0; padding: 0;}
table {width: 100%;}
th {padding: 5px;}
<table>
<thead>
<tr>
<th rowspan="2">No.</th>
<th rowspan="2">Creation Date</th>
<th rowspan="2">Week Day</th>
<th rowspan="2">Log Type</th>
<th colspan="2" style="text-align: center">Time</th>
<th rowspan="2">action</th>
</tr>
<tr>
<th>IN/START</th>
<th>OUT/STOP</th>
</tr>
</thead>
</table>
Used to colspan="" and rowspan="" carefully as like this
Try to this
th{
border:solid 1px red;
padding:5px;
}
<table border="0" cellpadding="0" cellspacing="0">
<thead>
<tr>
<th rowspan="2">No.</th>
<th rowspan="2">Creation Date</th>
<th rowspan="2">Week Day</th>
<th rowspan="2">Log Type</th>
<th colspan="2" style="text-align: center">Time</th>
<th rowspan="2">action</th>
</tr>
<tr>
<th>IN/START</th>
<th>OUT/STOP</th>
</tr>
</thead>
</table>
Try with following, It will be helpfull
<table>
<tr>
<th>No.</th>
<th>Creation Date</th>
<th>Week Day</th>
<th>Log Type</th>
<th>
<table>
<tr>Time</tr>
<tr>
<th>IN/START</th>
<th>OUT/STOP</th>
</tr>
</table>
</th>
<th>action</th>
</tr>
</table>
No need to use colspan and rowspan use this simple code and add your css according to your need.
<table border="1">
<tr>
<th>No.</th>
<th>Creation Date</th>
<th>Week Day</th>
<th>Log Type</th>
<th>
<table border="1">
<tr>
<p style="text-align: center !important;">Time</p>
</tr>
<tr>
<th>IN/START</th>
<th>OUT/STOP</th>
</tr>
</table>
</th>
<th>action</th>
</tr>
</table>
have a fun.
You Should use rowspan and colspan properly.
Tutorial
and here I wrote some HTML Code here, I hope it will be helpful.
<table cellpadding="0" cellspacing="0" >
<tr>
<th rowspan="2">No</th>
<th rowspan="2">CREATION DATE</th>
<th rowspan="2">WEEK DAY</th
<th rowspan="2">LOG TYPE</th>
<th colspan="2">TIME</th>
<th rowspan="2">ACTION</th>
</tr>
<tr>
<th >IN/START</th>
<th >OUT/STOP</th>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td>TBODY</td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>

Splitting a table cell into two columns in HTML

I have the following table:
<table border="1">
<tr>
<th scope="col">Header</th>
<th scope="col">Header</th>
<th scope="col">Header</th>
</tr>
<tr>
<th scope="row"> </th>
<td> </td>
<td>Split this one into two columns</td>
</tr>
</table>
And I wish to split the cell which contains "Split this one into two columns" into two cells/columns. How do I go about this?
Fiddle
Like this http://jsfiddle.net/8ha9e/1/
Add colspan="2" to the 3rd <th> and then have 4 <td>'s in your second row.
<table border="1">
<tr>
<th scope="col">Header</th>
<th scope="col">Header</th>
<th scope="col" colspan="2">Header</th>
</tr>
<tr>
<th scope="row"> </th>
<td> </td>
<!-- The following two cells will appear under the same header -->
<td>Col 1</td>
<td>Col 2</td>
</tr>
</table>
I came here for a similar problem I was facing with my table headers.
#MrMisterMan's answer, as well as others, were really helpful, but the borders were beating my game. So, I did some research to find the use rowspan.
Here's what I did and I guess it might help others facing something similar.
<table style="width: 100%; margin-top: 10px; font-size: 0.8em;" border="1px">
<tr align="center" >
<th style="padding:2.5px; width: 10%;" rowspan="2">Item No</th>
<th style="padding:2.5px; width: 55%;" rowspan="2">DESCRIPTION</th>
<th style="padding:2.5px;" rowspan="2">Quantity</th>
<th style="padding:2.5px;" colspan="2">Rate per Item</th>
<th style="padding:2.5px;" colspan="2">AMOUNT</th>
</tr>
<tr>
<th>Rs.</th>
<th>P.</th>
<th>Rs.</th>
<th>P.</th>
</tr>
</table>
You have two options.
Use an extra column in the header, and use <colspan> in your header to stretch a cell for two or more columns.
Insert a <table> with 2 columns inside the td you want extra columns in.
Change the <td> to be split to look like this:
<td><table><tr><td>split 1</td><td>split 2</td></tr></table></td>
is that what your looking for?
<table border="1">
<tr>
<th scope="col">Header</th>
<th scope="col">Header</th>
<th scope="col" colspan="2">Header</th>
</tr>
<tr>
<th scope="row"> </th>
<td> </td>
<td>Split this one</td>
<td>into two columns</td>
</tr>
</table>
Use this example, you can split with the colspan attribute
<TABLE BORDER>
<TR>
<TD>Item 1</TD>
<TD>Item 1</TD>
<TD COLSPAN=2>Item 2</TD>
</TR>
<TR>
<TD>Item 3</TD>
<TD>Item 3</TD>
<TD>Item 4</TD>
<TD>Item 5</TD>
</TR>
</TABLE>
More examples at http://hypermedia.univ-paris8.fr/jean/internet/ex_table.html.
Please try the following way.
<table>
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
<tr>
<td colspan="2">Sum: $180</td>
</tr>
</table>
Please try this way.
<table border="1">
<tr>
<th scope="col">Header</th>
<th scope="col">Header</th>
<th colspan="2">Header</th>
</tr>
<tr>
<td scope="row"> </td>
<td scope="row"> </td>
<td scope="col">Split this one</td>
<td scope="col">into two columns</td>
</tr>
</table>
https://jsfiddle.net/SyedFayaz/ud0mpgoh/7/
<table class="table-bordered">
<col />
<col />
<col />
<colgroup span="4"></colgroup>
<col />
<tr>
<th rowspan="2" style="vertical-align: middle; text-align: center">
S.No.
</th>
<th rowspan="2" style="vertical-align: middle; text-align: center">Item</th>
<th rowspan="2" style="vertical-align: middle; text-align: center">
Description
</th>
<th
colspan="3"
style="horizontal-align: middle; text-align: center; width: 50%"
>
Items
</th>
<th rowspan="2" style="vertical-align: middle; text-align: center">
Rejected Reason
</th>
</tr>
<tr>
<th scope="col">Order</th>
<th scope="col">Received</th>
<th scope="col">Accepted</th>
</tr>
<tr>
<th>1</th>
<td>Watch</td>
<td>Analog</td>
<td>100</td>
<td>75</td>
<td>25</td>
<td>Not Functioning</td>
</tr>
<tr>
<th>2</th>
<td>Pendrive</td>
<td>5GB</td>
<td>250</td>
<td>165</td>
<td>85</td>
<td>Not Working</td>
</tr>
</table>