I want a table that is 100% if the page that can contain any number of columns, dynamically created. Each column can contain very long words, so long that they all might not fit on one page. To fix this i used table-layout: fixed
which made all columns of the table visible on the page. The problem is that I still want the width of each column to be dynamic so that if a column have short words it should be shorter than the one with the long word.
Example: jsfiddle.
Table 1 always shows all columns but when the page is wide enough it breaks the word even though there are free space in other columns.
Table 2 works fine when the page is wider than the columns but the first column pushes the other columns out of the screen/onto other objects when the window is smaller.
Is there a way to get it all? A table that always contains all columns and columns that are not wider than they have to be to fit? I want it to break the words if it has to rather than overflowing the table.
I could accept a js/jquery solution but if it's possible with css that is preferable.
Edit:
Small table: Note: asasdasdasdasdasdasdasdasdasdasdasd is one word that is shortened because the table can't be larger than this.
+--------------------+----------+---------+
|asasdasdasdasdasdasd|qweqweqweq|zxczxczxc|
|asdasdasdasdasd | | |
+--------------------+----------+---------+
Large table: Note: all columns are not of equal size, preferably they increase with empty spaces equally distributed.
+--------------------------------------+-------------+------------+
|asasdasdasdasdasdasdasdasdasdasdasd |qweqweqweq |zxczxczxc |
+--------------------------------------+-------------+------------+
When table-layout: fixed is applied, the content no longer dictates the layout, but instead, the browser uses any defined widths from the table's first row to define column widths. If no widths are present on the first row, the column widths are divided equally across the table, regardless of content inside the cells.
By using CSS, the styling of HTML elements is easy to modify. To fix the width of td tag the nth-child CSS is used to set the property of specific columns(determined by the value of n) in each row of the table.
Dynamic columns is a feature that allows one to store different sets of columns for each row in a table. It works by storing a set of columns in a blob and having a small set of functions to manipulate it.
To set the table width in HTML, use the style attribute. The style attribute specifies an inline style for an element. The attribute is used with the HTML <table> tag, with the CSS property width.
If I understand this could be a starting point:
var tds;
$("table").each(function() {
tds = $("td");
for(var x = 0; x < tds.length; x++){
tds.eq(x).css('max-width', '100px');
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With