I need flexible lenght of first two columns when i'm entering text, but fixed lenght of third column and that table doesn't leave div element (but doesn't move the div). Third column is on right side of the div element. In the first column to fit all text entered, second how many can fit. Second column field can ends with "...". Sorry for my bad english. code - jsfiddle.net/17s8gz3L
My code:
<div>
<table border="1">
<tr>
<td>textbssanssadsadasdacccccccccccc</td>
<td>textsasas</td>
<td>text</td>
</tr>
<tr >
<td>textsadasdadadsads</td>
<td>text</td>
<td>textsasasd</td>
</tr>
</table>
<div>
table{
width:100%;
overflow:hidden;
}
td:last-of-type {
float:right;
}
td{
overflow:hidden;
white-space:nowrap;
}
div{
border:1px solid red;
width:60%;
}
Adding classes and styling those should do the trick.
Can't remember the exact CSS attributes but you should do something like:
In html:
<tr>
<td class='col1'></td>
<td class='col2'></td>
<td class='col3'></td>
<tr>
<tr>
<td class='col1'></td>
<td class='col2'></td>
<td class='col3'></td>
<tr>
In css:
div{
border:1px solid red;
width:60%;
}
div.col1{
width:auto;
}
div.col2{
width:auto;
}
div.col3{
width:20%;
//change the 20% to whatever static width you need use % or px, i recommend using %.
}
Related
I want that a table column uses the minimum of place but after a certain width, it should wrap.
Here is what I have:
<table id="#table">
<tr>
<td>Content with a fix with</td>
<td class="min">This content should only use the necessary place but wrap after 200px</td>
</tr>
</table>
<style>
#table{
width: 100%;
}
.min {
width: 1%;
white-space: nowrap;
}
</style>
This makes the column use only the place which it needs but it it ets too long, it will make the table endless longer and I want to set the maximum of the allowed space.
Edit:
I think that with js it would be possible to calculate the width and change the css but I would prefer a solution without javascript or jquery.
Edit 2: I forgot to mention that the table has a width of 100%. So if I follow the given answer, the table cell has its autowidth (which is too wide) and a max-width so if the text is short, the td has a white space which I do not want.
Edit 3: Here is an image which explains better than me.
#billTable{
width:100%;
}
#numberTd{
text-align: center;
width:18%;
}
#weightTd{
text-align: center;
width:20%;
}
#nameTd{
text-align: left;
width: 1%;
white-space: nowrap;
}
#kgPriceTd{
text-align: right;
width:20%;
}
#priceTd {
text-align: right;
}
<div style="width:550px;">
<table>
<tr>
<th id='numberTd'>Packetzahl</th>
<th id='weightTd'>Kg</th>
<th id='nameTd'>Artikel</th>
<th id='kgPriceTd'>CHF / Kg</th>
<th id='priceTd' colspan="2">Total</th>
</tr>
<tr>
<td id='numberTd'>1</td>
<td id='weightTd'>0.688</td>
<td id='nameTd' class='min'>Siedfleisch</td>
<td id='kgPriceTd'>44.00</td>
<td id='priceTd'>8.2</td>
</tr>
</table>
</div>
You can control max width of an element by using max-width
https://developer.mozilla.org/en-US/docs/Web/CSS/max-width
The max-width CSS property sets the maximum width of an element. It prevents the used value of the width property from becoming larger than the value specified by max-width.
.min {
max-width: 200px;
}
<table>
<tr>
<td>Content with a fix with</td>
<td class="min">This content should only use the necessary place but wrap after 200px</td>
</tr>
</table>
You can apply width in percentages for the flexible columns, space it out so it looks good or make your table not 100%
Use the CSS property "max-width". I've attached an example with a border that shows this in use.
table, td {
border:1px solid #555;
border-collapse:collapse;
}
.min {
max-width:200px;
}
<table>
<tr>
<td>Content with a fix with</td>
<td class="min">This content should only use the necessary place but wrap after 200px</td>
</tr>
</table>
I'm going to try to explain myself.
I would like to achieve this:
I need to create a table with 4 columns and many td (lines).
But, I need the single lines to have automatic column width based on their content.
Normally, this is what happens with columns, the largest td (line) dictate the size of the column in which is contained:
Instead. I am trying to achieve this:
I want to still organise elements in a Table (columns) so that I can organise the content for ex. by alphabetic properties.
BUT I need the columns to look like this:
Any Ideas?
You can set display:inline for the columns (<td>). A better alternative would be to use <div> and flexbox.
td {
border:2px solid red;
display: inline;
}
td:nth-child(1) {
background:yellow;
}
td:nth-child(2) {
background:blue;
}
td:nth-child(3) {
background:grey;
}
td:nth-child(4) {
background:orange;
}
table {
border:1px dashed black;
}
<table>
<tr>
<td>A</td>
<td>B</td>
<td>C</td>
<td>D</td>
</tr>
<tr>
<td>Ant</td>
<td>Bear</td>
<td>Cat</td>
<td>Dog</td>
</tr>
</table>
thead{
width: calc(100% - 17px);
display:block;
}
tbody{
display:block;
overflow-y: auto;
height:100px;
}
http://jsfiddle.net/mkgBx/
I can adjust width of thead to match that of tbody + scrollbar.
However, in cases when scrollbar is not displayed, tbody stretches further to the right.
Is there any way to fix tbody width?
I don't think you can set the tbodys width to ignore the presence of the scrollbar. Would a change to the structure of the mark-up be acceptable?
CSS
table{
font: 11px arial;
width: 245px;
}
th, td{
border:1px solid #000;
}
div {
height:90px;
width: 262px; /*table width + 17px */
overflow-y: auto;
}
HTML
<table>
<thead>
<tr>
<th style="width:165px">Name</th>
<th>Country</th>
</tr>
</thead>
</table>
<div>
<table>
<tbody>
<tr>
<td style="width:165px">Mart Poom</td>
<td>Estonia</td>
</tr>
<tr>
<td>David Loria</td>
<td>Kazakhstan</td>
</tr>
<tr>
<td>Wojciech Szczęsny</td>
<td>Poland</td>
</tr>
<tr>
<td>Gianluigi Buffon</td>
<td>Italy</td>
</tr>
<tr>
<td>Igor Akinfeev</td>
<td>Russia</td>
</tr>
</tbody>
</table>
</div>
http://jsfiddle.net/nF2NL/1/
This allows the div to provide the scrolling and the table to remain a set width.
You need to use JavaScript to handle this. Check if the scrollbar appears and adjust the width of the tbody accordingly.
Here's the JS code relevant to this where t is the tbody element:
if (t.clientHeight == t.scrollHeight)
{
t.style.width = "224px";
}
I've modified your JSFiddle to show you how it can work in your case.
I would not explicitly declare the <tbody> and <thead> tags for your table, as this is unnecessary. One solution to your problem would be to make the row (<tr>) of <th>s position: fixed. Someone else had a similar problem here: How to make a tables header static when scrolling
with width 100% it adapts to the table with, doesn't matter if it has a scroll or not.
thead{
width: 100%;
display:block;
}
I managed to obtain this kind of table layout:
fixed - dynamic(50%) - dynamic(50%) - fixed
http://jsfiddle.net/ihtus/ksucU/
But how do I get this kind? fixed - dynamic(30%) - dynamic(70%) - fixed
Here's my CSS:
table {
width:100%;
border-collapse:collapse;
table-layout:fixed;
}
td {
border: 1px solid #333;
}
Like this:
<table>
<tr>
<td style="width:200px;">
200px width - content
</td>
<td width="30%">
dynamic width - content
</td>
<td width="70%">
dynamic width - content
</td>
<td style="width:100px;">
100px width - content
</td>
</tr>
</table>
table {
width:100%;
border-collapse:collapse;
table-layout:fixed;
}
td {
border: 1px solid #333;
}
http://jsfiddle.net/7dSpr/
The general approach is the same as the one Monkieboy used, but you should avoid inline styles. ( by that I mean writing style="someting" ) in your html file. You should use classes and CSS instead.
First give the td a class like this <td class="thin-column">text here</td>,
then in your CSS use that to apply styles: .thin-column:{ width: 30% }
Essentially what I want is a table, with one row, 5 cells... The first / left cell should be left justfied, the last / right cell should be right justified, and the middle 3 cells need to be centered with equal amounts of spacing between each cell. The table itself is "100% width, so that is where the spacing between cells would come from.
How would I write this (using html / css)? "table" tags or "divs" etc are both valid, I don't really mind which approach is taken as long as the end result looks correct.
Edit:
The problem is the spacing; the table itself isn't an issue, but simply setting the alignment on the cells will not work correctly; the free space between the cells is not 100% divided equally between the cells.
I also don't want to specify cell width as the content is dynamic and there is no way to know before hand how much width is needed.
HTML only version
<table>
<tr>
<td width="20%"></td>
<td width="20%" align="center"></td>
<td width="20%" align="center"></td>
<td width="20%" align="center"></td>
<td width="20%" align="right"></td>
</tr>
</table>
CSS Version
<table>
<tr>
<td style="width:20%;"></td>
<td style="width:20%; text-align:center"></td>
<td style="width:20%; text-align:center"></td>
<td style="width:20%; text-align:center"></td>
<td style="width:20%; text-align:right"></td>
</tr>
</table>
If you are using a table, assign unique ids to each cell and then use css to justify as required, e.g.
HTML:
<td id="firstcell">...</td>
<td id="secondcell">...</td>
<td id="thirdcell">...</td>
<td id="fourthcell">...</td>
<td id="fifthcell">...</td>
CSS:
table {table-layout:fixed;} /* ensure the widths are absolutely fixed at the specified width*/
td{ width: 20%;} /* allocate space evenly between all 5 cells */
td#firstcell {text-align:left;}
td#secondcell, td#thirdcell, td#fourthcell {text-align:center;}
td#fifthcell {text-align:right;}
td
{
text-align:center;
}
td:first-child
{
text-align:left;
}
td:last-child
{
text-align:right;
}
<style type="text/css">
.five_columns {
width: 100%;
}
.five_columns > div {
width: 20%;
float: left;
text-align: center;
overflow: auto;
}
.five_columns > div:first-child {
text-align: left;
}
.five_columns > div:last-child {
text-align: right;
}
</style>
<div class="five_columns">
<div>...</div>
<div>...</div>
<div>...</div>
<div>...</div>
<div>...</div>
</div>
overflow: auto is set because if you strictly want the width to be the same there really isn't much you can do (as far as I know) other than force scrollbars on anything that's too long.