My original concern was: table are flowing out of the div container. I've searched on how to solve it and ended up using table-layout fixed (How to prevent HTML tables from becoming too wide) and with this concern.
This question maybe tagged duplicate but these questions are different from mine: Fixed table layout not filling specified table width,
table-layout fixed not working
class of the containing div
.cardio-form-container table {
border-collapse: collapse;
border: solid 1px #000;
table-layout: fixed;
word-wrap:break-word;
width: 100%;
}
.cardio-form-container table td {
border: solid 1px #000;
}
<div class="cardio-form-container"><table>.....
<tr>
<td>Contact #</td>
<td>TextBoxTextBoxTextBoxTextBoxTextBoxTextBoxTextBox</td>
<td>Locality</td>
<td>TextBox</td>
<td>FILE #</td>
<td>TextBox</td>
<td colspan="2">TextBox</td>
<td>TAPE #</td>
<td>TextBox</td>
<td colspan="2">TextBox</td>
</tr>
<tr>
<td>Referring MD</td>
<td>TB</td>
<td>Weight</td>
<td>TB</td>
<td>Sonographer</td>
<td colspan="6"></td>
I also tried max-width: 100% with and without important!, set width="100%" in the html table.
Table rows doesn't fit the table and some rows are longer than the other. The image should be able to clearly describe my question.
The issue is likely caused by the mismatched number of <td>s on each row, you could try changing the last bit <td colspan="6"></td> to 7.
.cardio-form-container table {
border-collapse: collapse;
border: solid 1px #000;
table-layout: fixed;
word-wrap:break-word;
width: 100%;
}
.cardio-form-container table td {
border: solid 1px #000;
}
<div class="cardio-form-container">
<table>
<tr>
<td>Contact #</td>
<td>TextBoxTextBoxTextBoxTextBoxTextBoxTextBoxTextBox</td>
<td>Locality</td>
<td>TextBox</td>
<td>FILE #</td>
<td>TextBox</td>
<td colspan="2">TextBox</td>
<td>TAPE #</td>
<td>TextBox</td>
<td colspan="2">TextBox</td>
</tr>
<tr>
<td>Referring MD</td>
<td>TB</td>
<td>Weight</td>
<td>TB</td>
<td>Sonographer</td>
<td colspan="7"></td>
</tr>
</table>
</div>
You need to have your column counts match:
http://jsfiddle.net/5bcL5csu/
Increase your colspan to 7:
<td colspan="7">asdf</td>
Related
I am trying to design the following table using html and css how do I proceed with it. Thank you in advance.
This solution will save you from having to use nested tables. The trick is that you really have four rows, not three, and make use of colspan and rowspan.
Note that you need to set a height for the td in order to ensure they are even.
table {
width: 100%;
border-collapse: collapse;
}
td {
border: 1px solid black;
height: 30px;
}
<table>
<tr>
<td colspan="2"></td>
<td rowspan="2"></td>
</tr>
<tr>
<td rowspan="2"></td>
<td rowspan="2"></td>
</tr>
<tr>
<td rowspan="2"></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</table>
you can try this one:
HTML
<table>
<tr>
<td>
<table>
<tr>
<td colspan="2">Th</td>
</tr>
<tr>
<td>Th</td>
<td>Th</td>
</tr>
<tr>
<td>Th</td>
<td>Th</td>
</tr>
</table>
</td>
<td>Th</td>
</tr>
</table>
CSS
table
{
width:100%;
height:100px;
text-align:center;
border:2px solid gray;
border-collapse: collapse;
}
td
{
border:2px solid gray;
}
.container
{
width:100%;
}
.container .header
{
width:100%;
height:200px;
background:#5076BB;
}
.container .slider
{
width:100%;
height:500px;
background:#5076BB;
}
DEMO HERE
UPDATED DEMO HERE
Read the tutorial: http://www.w3schools.com/html/html_tables.asp
In particular read the part about the attributes rowspan="" and colspan=""
Example:
<td colspan="2">This table data will span two columns</td>
<td rowspan="2">This table data will span two rows</td>
I have one table.
I would like to set different cell width for different rows.
For example, at the third row, I would like to set the EQUAL column width of 3rd and 4th column. No matter the 3rd column has text overflow.
original output:
expected output:
Is it possible to do so? Thanks you very much!
<style type="text/css"> .tg {border-collapse:collapse;border-spacing:0;border-color:#aabcfe;margin:0px auto;} .tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#aabcfe;color:#669;background-color:#e8edff;} .tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#aabcfe;color:#039;background-color:#b9c9fe;} .tg .tg-s6z2{text-align:center} .tg .tg-vn4c{background-color:#D2E4FC} .tg .tg-0ord{text-align:right} .table {table-layout: fixed;} </style>
<table class="tg" table width="500"> <tr>
<th class="tg-s6z2" colspan="6">Results</th> </tr> <tr>
<td class="tg-vn4c" width="5%">No</td>
<td class="tg-vn4c" width="15%">Competition</td>
<td class="tg-vn4c" width="20%">John</td>
<td class="tg-vn4c" width="20%">Adam</td>
<td class="tg-vn4c" width="20%">Robert</td>
<td class="tg-vn4c" width="20%">Paul</td> </tr> <tr>
<td class="tg-031e">1</td>
<td class="tg-031e">Swimming</td>
<td class="tg-0ord" colspan="2">line1<br>line 2<br>line 3<br>12345678901234567890123451234567890<br><br></td>
<td class="tg-0ord" colspan="2">ABCD<br><br></td> </tr> </table>
To answer the question:
Technically, yes it is possible with tables using empty cells, colspans and manipulating borders. In fact, I made it.
Don't do this
table {
width: 100%;
border-collapse: collapse;
}
th,
td {
border: solid 1px #000;
padding: 10px;
}
.noborder {
border: none;
border-top: solid 1px #000;
border-bottom: solid 1px #000;
}
.leftborder {
border-left: solid 1px #000
}
.rightborder {
border-right: solid 1px #000
}
<h1>Don't do this!</h1>
<table>
<tr>
<th>No</th>
<th>Comp</th>
<td>John</td>
<td class="noborder">Matthew</td>
<td class="noborder"></td>
<td>Mark</td>
<td>Luke</td>
</tr>
<tr>
<td>1</td>
<td>Swimming</td>
<td colspan="2" class="noborder">Details</td>
<td colspan="3" class="noborder leftborder rightborder">Details</td>
</tr>
</table>
Practically though, this is overly complex and impossible to maintain; it also defeats the purpose of a table.
Possible alternative
This is a possible idea, though it could still be difficult to maintain and I don't know exactly what you are trying to display.
Sticking with a table
Depending on what information you are trying to display, you could use a table for this with a different layout. The rowspans join the information relevant to each name.
table {
width: 100%;
border-collapse: collapse;
}
th,
td {
border: solid 1px #000;
padding: 10px;
}
<table>
<thead>
<tr>
<th scope="col">No</th>
<th scope="col">Competition</th>
<th scope="col">Name</th>
<th scope="col">Details 1</th>
<th scope="col">Details 2</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row" rowspan="5">1</th>
<th scope="row" rowspan="5">Swimming</th>
<td>John</td>
<td rowspan="3">This info is relevant to John, Steven and Mark</td>
<td rowspan="2">This info is relevant to John and Steven</td>
</tr>
<tr>
<td>Steven</td>
</tr>
<tr>
<td>Mark</td>
<td rowspan="2">This info is relevant to Mark and Peter</td>
</tr>
<tr>
<td>Peter</td>
<td>The info is only relevant to Peter</td>
</tr>
</tbody>
</table>
you can use style sheet that sounds like hacks to this.. because doing this in table property isn't possible
see this jsfiddle
I'm encountering a problem when styling an dynamic generated table. The user can choose how many columns there have to be, some of them have got a fixed length. How can I let the other give a percentage of the space left, without having to specify the exact width of the columns every time AND without ending up with different column widths with different data/different browsers?
Example:
<style type="text/css">
table{
width:800px;
border:1px solid #CCCCCC;
/* table-layout: fixed; */
}
table td {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
border:1px solid #EEEEEE;
}
table tbody td.active{
text-align:center;
width:100px; /* fixed */
}
table tbody td.option{
width:100px; /* fixed */
}
table tbody td.nonfixed{
width:auto;
}
</style>
<table>
<thead>
<tr>
<td>Name</td>
<td>Description</td>
<td>Active</td>
<td colspan="2">Options</td>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="5">+ Add new row<td>
</tr>
</tfoot>
<tbody>
<tr>
<td class="nonfixed">[Name 1]</td>
<td class="nonfixed">[Description 1]</td>
<td class="active">[X]</td>
<td class="option">Edit</td>
<td class="option">Delete</td>
</tr>
<tr>
<td class="nonfixed">[Name 2]</td>
<td class="nonfixed">[Description 2]</td>
<td class="active">[0]</td>
<td class="option">Edit</td>
<td class="option">Delete</td>
</tr>
</tbody>
</table>
In the example both "nonfixed" columns should have the exact same width. This should also work when the user adds a nonfixed column or switches the first column with the last etc.
Who's able to help me out?
I see two possible approaches... either use a script to calculate the flexible-width columns' widths and average them, or use nested tables to split the two flex cols at 50%:
<table>
<tr>
<td class="fixed"></td>
<td class="fixed"></td>
<td class="fixed"></td>
<td class="flex-wrapper">
<table width="100%">
<tr>
<td width="50%"></td>
<td width="50%"></td>
</tr>
</table>
</td>
</tr>
</table>
Let's take 4 table columns - ID, Text, Date, Action. In my case table have always constant width - in example 960px.
How can I create such table as :
*-*------------------------------------*----------*----*
|1| Some text... |May 2011 |Edit|
*-*------------------------------------*----------*----*
|2| Another text... |April 2011|Edit|
*-*------------------------------------*----------*----*
As we can see, ID, Date and Action adjust their width to content, Text is as long as possible....
Is that possible to do without setting specific width of columns ? When ID = 123 or Date = November 2011, columns should automatically be wider...
Using a 100% width on the wide td and a fixed width for the table along with white-space:nowrap, this can be done:
Demo
HTML
<table>
<tr>
<td>1</td>
<td width="100%">Some text... </td>
<td>May 2011</td>
<td>Edit</td>
</tr>
<tr>
<td>2</td>
<td width="100%">Another text... </td>
<td>April 2011</td>
<td>Edit</td>
</tr>
</table>
CSS
table
{
...
width:960px;
}
td
{
...
white-space:nowrap;
}
basically, it's just like this: http://jsfiddle.net/49W5A/ - you have to set the cell-width to something small (like 1px) to make them stay as small as possible.
but as you'll see, theres one problem with the date-fields doing a line-wrap. to prevent this, just add white-space: nowrap; for your text-field: http://jsfiddle.net/ZXu7U/
working example:
<style type="text/css">
.table{
width:500px;
border: 1px solid #ccc;
}
.table td{
border: 1px solid #ccc;
}
.id, .date, .action{
width:1px;
}
.date{
white-space: nowrap;
}
</style>
<table class="table">
<tr>
<td class="id">1</td>
<td class="text">Some Text...</td>
<td class="date">May 2011</td>
<td class="action">Edit</td>
</tr>
<tr>
<td class="id">2</td>
<td class="text">Another Text...</td>
<td class="date">April 2011</td>
<td class="action">Edit</td>
</tr>
</table>
My best advice to you is to not touch the widths of the table, the table automatically layouts in a way that does all cells best.
However, if you'd like to push through, I'd use width: 1px; on the cells that needs adjusting (one of each column is enough). Also use white-space: nowrap on all cells. that will make sure the lines don't break.
Try this:
.id, .date, .action is the table cells (td).
CSS:
.id, .date, .action {
width: 1em;
}
It worked for me.
The width:1em will not cut the text but force the width size to the minimum.
The best way that I've found for setting table column widths is to use a table head (which can be empty) and apply relative widths for each table head cell. The widths of all cells in the table body will conform to the width of their column head. Example:
HTML
<table>
<thead>
<tr>
<th width="5%"></th>
<th width="70%"></th>
<th width="15%"></th>
<th width="10%"></th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Some text...</td>
<td>May 2018</td>
<td>Edit</td>
</tr>
<tr>
<td>2</td>
<td>Another text...</td>
<td>April 2018</td>
<td>Edit</td>
</tr>
</tbody>
</table>
CSS
table {
width: 600px;
border-collapse: collapse;
}
td {
border: 1px solid #999999;
}
View Result
Alternatively, you can use colgroup as suggested here.
I have a table with 2 rows and variable columns. I tried width = 100% for the column. So the first content in the view will fit. But suppose if i am changing the contents dynamically then it is not dynamically increase/decrease the HTML table column size.
If you want the cells to resize depending on the content, then you must not specify a width to the table, the rows, or the cells.
If you don't want word wrap, assign the CSS style white-space: nowrap to the cells.
You can try this:
HTML
<table>
<tr>
<td class="shrink">element1</td>
<td class="shrink">data</td>
<td class="shrink">junk here</td>
<td class="expand">last column</td>
</tr>
<tr>
<td class="shrink">elem</td>
<td class="shrink">more data</td>
<td class="shrink">other stuff</td>
<td class="expand">again, last column</td>
</tr>
<tr>
<td class="shrink">more</td>
<td class="shrink">of </td>
<td class="shrink">these</td>
<td class="expand">rows</td>
</tr>
</table>
CSS
table {
border: 1px solid green;
border-collapse: collapse;
width:100%;
}
table td {
border: 1px solid green;
}
table td.shrink {
white-space:nowrap
}
table td.expand {
width: 99%
}
Well, me also I was struggling with this issue: this is how I solved it: apply table-layout: auto; to the <table> element.