Forcing the table and its cells to have fix width - html

I am working with a web template , which have define the following inside a CSS file:-
div.dataTables_length select {
width: 50px;
}
.dataTables_filter input, .dataTables_length select {
display: inline-block;
margin-bottom: 0;
}
And the table is defined as follow:-
<div class="box-content">
<table class="table table-striped table-bordered bootstrap-datatable datatable">
<thead>
<tr>
<th></th> <th></th>
</tr></thead>
<tbody>
<tr>
Btu currently I am facing a problem is that the template will automatically truncate the value and display only part of it. So for example if there is a long description value the cell might display part of it. So is there a way to modify the table so that it will have the following:-
The table cell will always have the same width, but dynamic height.
The table should always have the same width, so it will not go out of layout in case there is a long text.
To display the full text of each field, and the cell should atomically move to a new line.
Regards
:::EDIT:::
This is how the table will be displayed incase the table columns contain long test:-

The question of truncation has already been addressed in the comments. I will address the width of the table.
If you want to set the width of a table column you need to either set the same width for all columns or give specific widths to certain columns.
.table th, .table td { width: 50px; } /* sets the default value of all columns */
.table .name { width: 100px; word-wrap: break-word; } /* overrides the default with specific value for certain column*/
<table class="table">
<tr>
<td class="name">James T. Kirk</td>
<td class="name">Spock</td>
</tr>
</table>
I would shy away from using percentage (%) on column widths, especially if the text will fluctuate in length.
EDIT -
I see what you mean. You need to put a css style of word-wrap: break-word; I updated the code above and made a jsfiddle demo.

Related

Displaying empty table cells without

I'm trying to create a table of empty cells on which I'm attaching Javascript to toggle a class, so I just need to give them a height and width. The problem is when I create the table, while it has padding, I can't get it to maintain it's height. I'm trying to avoid using as when I do, it creates the mouseover of highlighting text, and multiple clicks on the box can select the text.
Looking online, empty-cells: show; comes up as the constant answer, but it doesn't seem to keep the height. I've considered doing it as a series of inline-block divs, but then borders become messy, as the borders don't collapse.
I literally just have an empty table
<table style="border-collapse: collapse; empty-cells: show;">
<tr>
<td style="height: 1.3em; padding: 4px 6px;"></td>
<td style="height: 1.3em; padding: 4px 6px;"></td>
</tr>
</table>
I feel like this is an old HTML problem, and I'm missing some simple answer.
I had seen the question that was suggested as the duplicate, but as the answer there is 5 years old, I thought there must be something more modern for addressing this problem. If there isn't, I guess the visibility trick is the way to go.
Is there a specific minimum height that you want? If so, you can do something like this:
table tr td:first-child::after {
content: "";
display: inline-block;
vertical-align: top;
min-height: 60px;
}
Source code from omelniz originally posted here: Can I use a min-height for table, tr or td?
Try this
For <th> and <td> :
th:empty::before,td:empty::before{content:'\00a0';visibility:hidden}
For <td> only :
td:empty::before{content:'\00a0';visibility:hidden}
Description:
'\00a0' is code for single space
visibility:hidden to hide that single space

html number of columns in table

I am trying to create the following html table:
I can't work out why my current implementation is not yielding my desired result. As you can see in the snippet, the last <td> in the second <tr> spans 2 cols, and not the middle <td>.
table{
width: 100%;
}
table tr td {
background-color: #ddd;
}
table tr td[colspan="2"]{
background-color: #0a0;
}
table tr td[colspan="4"]{
font-size: 16px;
text-align:center;
background-color: #8C0000;
color: #fff;
}
<table>
<tbody>
<tr>
<td colspan="4"><b>Full width column</b></td>
</tr>
<tr>
<td>Column 1</td>
<td colspan="2">Column 2</td>
<td>Column 3</td>
</tr>
</tbody>
</table>
In researching I found the following from: https://www.w3.org/TR/WD-html40-970917/struct/tables.html
There are several ways to determine the number of columns:
Count the number of columns as specified by COL and COLGROUP elements which can only occur at the start of the table (after the
optional CAPTION).
Scan each row in turn to compute the number of columns needed for each row, taking into account cells that span multiple rows and/or
columns. Set the number of columns for the table to be the maximum
number of columns from each row. For any row that has less than this
number of columns, the end of that row should be padded with empty
cells. The "end" of a row depends on the directionality of the table.
[deprecated] Use the cols attribute on the TABLE element. This is the weakest method since it doesn't provide any additional information about
column widths. This may not matter, however, if the author uses style
sheets to specify widths.
So what have I misunderstood about this functionality?
Please also note, that I am aware that I can use css to specifically set the widths of the <td>'s I want to know why my current implementation is not working.
In fact the second td does span two columns, but the width of columns in a simple HTML table depends on the contents of the table cells. If you add the following style attributes containing widths to the tds, their widths are distributed as desired (i.e. 25/50/25%):
(Note: You could/should also apply CSS classes to those tds and create external CSS rules for those classes)
table{
width: 100%;
}
table tr td[colspan="4"]{
font-size: 16px;
text-align:center;
background-color: #008CD1;
color: #fff;
}
<table>
<tbody>
<tr>
<td colspan="4"><b>Nonverbal skills</b></td>
</tr>
<tr>
<td style="width: 25%; background-color: #888;">energised</td>
<td colspan="2" style="width: 50%; background-color: green;">Gestures</td>
<td style="width: 25%; background-color: #888;">Under energised</td>
</tr>
</tbody>
</table>
table.table td:nth-child(1) {text-align: center; font-weight: bold; width: 70px;}

HTML table ignoring element-style width

HTML table ignoring element-style width
I have an HTML table where certain cells have very long text contents.
I want to specify a width (in pixels) for these cells using jQuery, but the rendered table just ignores the given width.
Is there any way to force the table to respect this width?
Thanks!
JSFiddle: http://jsfiddle.net/sangil/6hejy/35/
(If you inspect the cell you can see the the computed width is different than the element-style width)
HTML:
<div id="tblcont" class="tblcont">
<table id="pivot_table">
<tbody>
<tr>
<th id="h0" >product</th>
<th id="h1" >price</th>
<th id="h2" >change</th>
</tr>
<tr>
<!-- this is the cell causing trouble -->
<td id="c00" >Acer 2400 aaaaaaaaaaaaaaaaaaaaaaaaaa</td>
<td id="c01" >3212</td>
<td id="c02" >219</td>
</tr>
<tr>
<td id="c10" >Acer</td>
<td id="c11" >3821</td>
<td id="c12" >206</td>
</tr>
</tbody>
</table>
</div>
CSS:
.tblcont {
overflow: hidden;
width: 500px;
}
table {
table-layout: fixed;
border-collapse: collapse;
overflow-x: scroll;
border-spacing:0;
width: 100%;
}
th, td {
overflow: hidden;
text-overflow: ellipsis;
word-wrap: break-word;
}
th {
height: 50px;
}
​Javascript:
$(document).ready(function() {
// THIS LINE HAS NO EFFECT!
$('#c00').width(30);
});​
I can see from your fiddle that you already have a good grasp on how to get the word truncation and such in-place. I think you may find it useful to do something like the following:
<table>
<colgroup>
<col style="width: 30px;" /> <!-- this style affects the whole 1st column -->
<col />
<col />
</colgroup>
<!-- the rest of your table here -->
</table>
This method works with the HTML specification in a way that is compliant - and will resize the whole column. If you instead change the display of the cell to inline-block, as mentioned above, what will happen is that you take the cell out of the table's flow - and other stylistic changes may cease working.
By styling the entire col using the code above, you use the table element's table-layout: fixed styling to your advantage instead.
Additionally - I noticed that you have the cells set up to use text-overflow: ellipsis; Check out this article on quirksmode to understand why it's not working. The fix you need is to make the following edit:
th, td {
border: solid #4682B4 1px;
overflow: hidden;
text-overflow: ellipsis;
word-wrap: break-word;
text-align: center;
white-space: nowrap; /* Add this line */
}
Table cells by default fit to their content and ignore your width.
Other possibility to the already provided answers:
Surround the text with some other container:
<td id="c00" ><div>Acer 2400 aaaaaaaaaaaaaaaaaaaaaaaaaa</div></td>
And change its width:
$('#c00 div').width(30);
You have a few issues:
table-layout: fixed tells the columns to be equal.
Then, even if you take that out, your text is wider than 30 pixels, with no spaces, so it's not going to go narrower than that "aaaaaaaaaa" etc. You'll need to make the text smaller, or add spaces.
Finally, width should be "30px" (in quotes).
Hope that helps.
Try this:
$('#c00').css("width","30px");
or this:
<td id="c00" style='width:30px'>
If you are using IE, you may need to have Compatability Mode on. Also, make sure you are importing the proper jQuery plugin.

How to align TH Header with TD in TBody

I'm having problems trying to embed a table in an existing HTML page with some CSS.
This CSS is hiding the header of the table by default with a style definition like:
.tablestuff thead {
display: none;
}
But I want the table to show, so I tried setting the style on the thead element with "display:block" (with javascript). That makes the header display, but the columns of the header don't line up with the td columns.
I have reduced my HTML to the following (hopefully with minimal typos) and showing the style on the thead element as set by javascript.
<div class="tablestuff">
<table border="1">
<thead style="display:block">
<tr>
<th id="th1" style="width: 20px"></th>
<th id="th2" style="width: 20px"></th>
</tr>
</thead>
<tbody>
<tr>
<td headers="th1" style="width: 20px"></td>
<td headers="th2" style="width: 20px"></td>
</tr>
</tbody>
</table>
</div>
How can I make both the header show and also align correctly with the td columns?
CSS includes more display modes than the commonly used none, inline, inline-block, and block. There are quite a few, in fact.
In this case, it appears what you want to use instead of display:block; is display:table-header-group;.
Here are some examples of the different styles, as applied to your table:
http://jsfiddle.net/CrYdz/1
The problem is caused by the display:block in the style attribute for the thead.
Change it to display:table-header-group
When you want to show the thead element use this value: display: table-header-group;
To set same width for table header and table body in table:
<table style="table-layout:fixed">
In case nothing fixes it. move your <tr> inside thead to tbody.
this was the only solution in my case since i had so many complications already.
Maybe the content of the THs is wider than the content of the TDs and in reality the width is not 20px as set.
So, because you first load the page without the thead, the table gets the width of the TDs. Then, when you display the THEAD by js, the table width continues being the same but probably the THs have different width.
By default, th and td should be aligned. If you want to leave it as default, just put display: unset:
.tablestuff thead {
display: unset;
}
Plain JavaScript:
document.querySelector("thead").style.display = "unset";
jQuery:
To make the jQuery's $(".tablestuff thead").show() method works, your css needs to be defined like this:
.tablestuff thead[style*='display: block'] {
display: unset !important;
}
This is because .show() will set the display to block by default. The above css will set it back to unset whenever it's set to block.
show and hide th instead of thead with the css
/* to hide */
.tablestuff thead th{
display: none;
}
/* to show */
.tablestuff thead th{
display: table-cell;
}

HTML/CSS - Extend element width to visible area (beyond width of containing element)

I have a web page that I'm generating, where there is a h2 with a green background labeling a table. The table can have any number of columns and I would like the h2 element to extend horizontally as far as the user can scroll, so that there is always a green bar above the table. The effect I'm trying to achieve is a green bar that spans at least the width of the table, so that it is always directly above the table, no matter how far the user scrolls.
Here is what it currently looks like (the red outline shows approximately the edges of the containing html, body, and div elements):
And here are the relevant pieces of code:
css:
h2 {
font-family:Arial;
font-size:20pt;
color:#FFFFFF;
text-align:left;
font-weight:normal;
background-color:#00693C;
clear: both;
padding:2px;
margin: 0px;
}
table.response {
border:1px outset;
border-collapse: separate;
}
table.response td {
font-size: 14px;
padding: 3px;
border:1px inset;
}
html:
<h2>[snip]</h2>
<table class="response">
<tbody>
<tr>
<td>[snip]</td>
[...]
</tr>
[...]
</tbody>
</table>
Things I have tried already that haven't worked:
A <thead> element containing a single <tr> and a single <th> where the colspan on the <th> is set to the number of columns in the table. This works for smaller numbers of columns, but for larger tables (for example, with colspan="6000"), some browsers (specifically, Firefox 11) render the cell (and its background) as only taking up one column.
A <thead> element containing a single <tr> and one <th> with colspan="2" and the background-color CSS property set for the <tr> element. Using Firefox's Inspect Element feature, it showed that the <tr> spanned the entire width of the table, but the background was only applied to the one cell.
A <thead> element containing a single <tr> and one <th> with colspan="2" and another <th> for every remaining column of the table. I tried to remove the separation between cells on the <thead>, but I was unable to.
My question is this: Is there a way to achieve the effect of having the green bar extend at least the entire width of the table, and if so, what is it? I would prefer not to use JavaScript or have to generate style code when I generate the HTML for the table.
Add float: left or display: inline-block to the element that contains the table and h2.
Add
div#container { display: inline-block; }
To your CSS, and embed the <h2> and <table> inside a DIV with that name:
<div id='container'>
<h2>...</h2>
<table>
...
</table>
</div>
That should work in any modern browser.