HTML table wrap td - html

I've two tables like below:
<table width="100px">
<tr>
<td>
<table width="100%">
<tr>
<td>One</td>
<td>Two</td>
<td>Three</td>
<td>Four</td>
<td>Five</td>
<td>Six</td>
<td>Seven</td>
<td>Eight</td>
<td>Nine</td>
<td>Ten</td>
</tr>
</table>
</td>
</tr>
</table>
When I run this my internal table is exceeding parent table width (100px) because it is having more TDs. How can restrict this?
All values are coming in the same row and it is going out of reserved area (100px). Is there any way to display values in multiple rows with the above code?

You cannot do this with <table> layout. Even adding the following CSS will not fix it, instead the cells are just rendered over each other.
table {
table-layout:fixed;
overflow:hidden;
}
The HTML table column element <col> element will also not really help since it will only apply to the first <td>.
One approach would be to use a non-<table> layout instead, for example:
HTML
<table>
<tr>
<td>
<div id="container">
<div>One</div><div>Two</div><div>Three</div><div>Four</div><div>Five</div>
<div>Six</div><div>Seven</div><div>Eight</div><div>Nine</div><div>Ten</div>
</div>
</td>
</tr>
</table>
CSS
table {
width:100px;
}
#container div {
display:inline-block;
}
See demo of how <col> does not work and how the <div> layout wraps at 100px.

U can't fit 10 words like this in 100px with normal size font.
I think you might want to use <tr></tr>(row) tags for each word instead of td(cell)
Or you really want the 100px row to be split in 10 cells and to contain those words? (I think if might be somehow possible but u won't see the words :D or maybe if you used little font and somehow made the words to be rotated about 90 degrees...)
This Q/A Word-wrap in an HTML table might help u in that case.

add a class to table and set this css code
table.restrict { table-layout:fixed; }
table.restrict td { overflow: hidden; }

It will help you:
Fixed Table Cell Width
Use style attr 'table-layout:fixed' with table
or try this:
// div css
<style>
.frame {
overflow-x:scroll;
width:100px;
}
</style>
<div class='frame'>
<table width='100px'>
<tr>
<td>
<table width='100%'>
<tr>
<td>One</td>
<td>Two</td>
<td>Three</td>
<td>Four</td>
<td>Five</td>
<td>Six</td>
<td>Seven</td>
<td>Eight</td>
<td>Nine</td>
<td>Ten</td>
</tr>
</table>
</td>
</tr>
</table>
</div>

Related

Getting HTML span element to use the rest of td's width

I would like the span element width to be the rest of td's width.
Here is a very simple example. In other words, now I have some code, but the span element will be too wide. It will go over the latest td element.
Here is the HTML code which has some basic CSS code, too. I'm not allowed to modify the basic strucuture of this table. All I want to do is modifying my span element.
<table style="width:100%;">
<tr>
<td style="width:30px;">1</td>
<td>Go <span style="display:inline-block;width:100%;background-color:black;">to...</span></td>
<td>#</td>
</tr>
</table>
How should I modify the span element to get it use the rest of the td but no more?
this could be achieved using flex box
td div {
display: flex;
}
td span {
flex-grow: 1;
}
<table style="width:100%;" border=1>
<tr>
<td style="width:30px;">1</td>
<td>
<div>
Go <span style="background-color:black;">to...</span>
</div>
</td>
<td>#</td>
</tr>
</table>
Here is my own solution to my problem. This is the best way which I was able to find out.
<table style="width:100%;">
<tr>
<td style="width:30px;">1</td>
<td style="padding-left:65px;">Go to...</td>
<td>#</td>
</tr>
</table>

How to make <table> column right-aligned

<table>
<colgroup>
<col>
<col style='text-align:right'>
</colgroup>
<tbody>
<tr>
<td>Sample text</td>
<td>This text should be right-aligned</td>
</tr>
<tr>
<td></td>
<td>
<div id='spacer' style='width:100px'>
I'm a spacer
</div>
</td>
</tr>
</tbody>
</table>
Results in text that isn't right aligned. If the same style is applied to the td element it works fine. How do I right-align text in a column without having to apply a style to every td element?
Add the following CSS:
table.tableClass tr td:nth-child(2) {
text-align: right;
}
the number after nth-child( is the column, so for this case it would be 2 because it's the second column. Fiddle: http://jsfiddle.net/p97vdo2p/1/
HTML isn't rendered by column, it's rendered by rows. That's why there <tr></tr> (table rows) exist, but <tc></tc> (table columns) do not. That being said, the easiest way to style a column in HTML, is by applying the same CSS class to every <td></td> in the given column. You can type in the same class to every one manually, or you could write it programmatically, via javascript or a server-side script.

DIV in <td> float right

I got a table with a couple <td>:
<table>
<tr>
<td>First</td>
<td>Second</td>
<td style="padding:20px;">
<div>
Third
</div>
</td>
</tr>
</table>
What I want to do is to place the "Third" <td> (with the div) to the right side of the table and the "First" and "Second" <td> should stay left.
Style with float:right; didn't work for me...
You need to make your table's width 100%, then control the widths of your first 2 columns, and finally right-align your third column.
http://jsfiddle.net/25Mqa/1/
<table>
<tr>
<td class="first">First</td>
<td class="second">Second</td>
<td class="third">Third</td>
</tr>
</table>
CSS:
table { width:100%; }
.first, .second { width:50px; }
.third { text-align:right; }
The problem is that the width of a <table> is determined by its content, by default. If you want the <table> to span 100% width (of its containing block) like block-level elements do, you can either add table-layout: fixed; and then specify your width - or just give it a width, depending on what you're after, e.g.
table {
width: 100%;
}
http://jsfiddle.net/QEaAd/2/
try add style="text-align:right;"
<table>
<tr>
<td>First</td>
<td>Second</td>
<td style="padding:20px; text-align:right;">
<div>
Third
</div>
</td>
</tr>
</table>
Only if you have 2 divs one near other:
<div id="fr">div1</div>
<div id="fr">div2</div>
you can float them right:
<style>
#fr{float:right;}
</style>
<table style="width: 100%;">
<tr>
<td>First</td>
<td>Second</td>
<td style="padding:20px; display: block; float: right;">
<div>
Third
</div>
</td>
</tr>
</table>
http://jsfiddle.net/pbesD/
I believe this does what you want, however from what I understand, floating table elements will cause problems in versions of Internet Explorer <8
I dont know what you are trying to do with tables and divs? But I normally use this for emailers.
I use the align attribute for td's. This helps a lot in making sure your layout looks the way you want. And it works in all browsers :)
FIDDLE
HTML:
<table width="100%">
<tr>
<td>First</td>
<td>Second</td>
<td style="padding:20px;" align="right">
<div>
Third
</div>
</td>
</tr>
</table>
In Bootstrap 5.2 you can add .text-start and .text-end to your text class to align elements inside a table.

How to make table row fixed at the top

I have a table without th elements, Only td elements are there. Is there any way to make My first row fixed(label). The table is like this
<table>
<tr>
<td>Name:</td>
<td>Age:</td>
</tr>
<tr>
<td>John</td>
<td>20</td>
</tr>
<tr>
......
</tr>
</table>
I want to make first row with the fields Name and Age as fixed. So that during the scrolling the labels will not disappear.
I've been searching for an answer for this problem for a while and finally I've found something that works very nice.
The secret is in this piece of code that makes the scrolling possible in the table
thead, tbody, tr, td, th { display: block; }
But you can find a full example here http://jsfiddle.net/T9Bhm/7/.
Hope it helps! :)
All of the solutions have a drawback: header row is not properly aligned with the content. One of the workarounds used was setting the width to some value (either absolute or percent).
Basing on pedromendessk answer I was able to find a neat solution to this question using answer from:
how-do-i-make-a-table-scrollable
So for the table presented in question solution would be:
tr:first-child {
position: sticky;
top: 0;
background: white;
}
Personally I'd use th for table headers, because using tr:first-child won't work correctly after adding thead and tbody.
i wrote plugin which can freeze any row (not only th) at the top of page or container. feel free to use it.
http://maslianok.github.io/stickyRows/
Setting position:fixed should do that for you:
<tr style="position:fixed">
<td>Name:</td>
<td>Age:</td>
</tr>
Edit:
<tr style="position:fixed;top:0;background:#FFF;">
<td>Name:</td>
<td>Age:</td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
Example: http://jsfiddle.net/aVQjN/
On the row that you want to stay fixed, set the style position: fixed and set a background color to that row, or you will end up having two layers of text when you scroll the list.
Another issue to pay attention to is the fact that the first row will be hidden under the fixed row, due to how the fixed position style works. to fix this put in a blank row.
<table width="400" border="1">
<tr style="position: fixed; background-color: grey;">
<td width="200">
Name
</td>
<td width="200">
Age
</td>
</tr>
<tr>
<td width="200">
</td>
<td width="200">
</td>
</tr>
<tr>
<td>
John
</td>
<td>
28
</td>
</tr>
<tr>
<td>
Jacob
</td>
<td>
22
</td>
</tr>
<tr>
<td>
Nicole
</td>
<td>
12
</td>
</tr>
</table>
See link for my full code.
http://jsfiddle.net/brettadamsga/yeAhU/
Use of data table we can fixed the header and footer also.
Find the below Code......
tbl_Purcordr=$("#tbl_Purcordr").DataTable({
'paging': true,
'pageLength': 25,
'bLengthChange': false,
'sorting': false,
'filter': true,
'info': false,
'scrollY': 360,
'background': 'none',
'fixedHeader': {
'header': true,
'footer': true
}
});

How to make two horizontally aligned tables?

Every time I create a HTML table, it starts on a new paragraph. Is it possible to override this behaviour, so that I have two tables on the same row?
I'm using also CSS.
You'll want to style them, display: inline; or float both the tables.
I've always found it easier to just create a table that contains the 2 tables if i want them to always be on same row.
<table>
<tr>
<td>
<table id="table1">
<tr>
<td></td>
</tr>
</table>
</td>
<td>
<table id="table2">
<tr>
<td></td>
</tr>
</table>
</td>
</tr>
</table>
styling works too though.
set display: inline-block; for each table.
online demo: http://jsfiddle.net/bitsmix/s7Bec/
jsfiddle
If you need the horizontally aligned tables to fill 100% width, you need:
display:inline-table; and float:left;