I have a div of width 60% and overflow-x set to scroll.
<div style="width: 60%; overflow-x: scroll">
</div>
Inside that, I have a table with 1 row and dynamic number of cells (th's) within that row.
<div style="width: 60%; overflow-x: scroll">
<table>
<thead>
<tr>
<th style="width: 20px;">
<input type="checkbox" />
</th>
<th style="width: 300px">Name</th>
<th style="width: 300px">Email</th>
#foreach (Group group in groups)
{
<th style="width: 150px">#group.Name</th>
}
</tr>
</thead>
</table>
</div>
When this is rendered, I'm having two problems
1) The table adjusts its width to the 100% of the div. What I want is the table to be much more wider than the div. That is why the div has "overflow-x: scroll", so that the table is scrollable horizontally.
2) The cells (th's) are not rendered with the widths I gave them
Note: The "#group.Name" inside the dynamically created th's should generally have less than the 150px that I gave to the th's.
How can I solve these two problems?
Try <th nowrap style="..."> to force cells to widen instead of wrapping.
As I can see You know widths of each column, so You know total table width. If total table width is 1000px, adding :
style="width:1000px"
to table tag should solve the problem. I've tested this approach in Chrome, and it works well.
The cause is that you are setting width to all TDs.
When table is rendered by browser, the engine compute the widths of each TDs to match Table width.
It will distribute the overflow/missing pixels to the Table columns, squeezing or stretching them. The distribution is based by columns width (by percentage), larger columns get more larger in absolute number of pixels.
If the table has columns that don't have width specified, this distribution falls only on them, ignoring columns with explicit width.
Surely, if table is CSS computed with width: auto or no width's set, the columns stay with their size and table width will be the sum of columns size (plus table borders, etc..)
You have 2 ways to fix your table:
set table CSS width to auto.
At least 1 TD must have no width - a pivot column.
Use one of them, or both..
Related
I have the following example: https://stackblitz.com/edit/js-ivvupc?file=index.html
My table should be responsive, which means, if the window gets to small, the table should be scrollable. The basics for that are already implemented in the outer div:
<div style="width: 80%; height: 50%; overflow: auto">
The main problem is the following:
Every column has a different width in percentage.
Furthermore the second column 'State' should have a min-width, if the window gets to small.
Which means: If the window is big enough, use width: 25%, otherwise use min-width of 75px.
As mentioned above, the table should be horizontal scrollable when the table (espacially with the 75px wide 'State' column) gets to big.
Summarized:
When the window is big enough use the width of the columns in percentage.
When the window isn't big enough, show a scrollable table with column 'State' in fixed width of 75px
I've tried it with this code, but it doesn't work:
<th style="width: 25%; min-width: 75px">
<div>State</div>
</th>
I looked carefully at your code.
I have found a solution.
Please delete "table-layout: fixed" in the second line of your code.
It will work.
Best regards.
Good morning everybody!
I'm trying to make a table with size based on %. The width works fine, but i'm having some problems with height. When te user resizes the screen to a certain size the table just stop decreasing it's height, growing outside the div. Below some prints:
Normal size
Resized screen
I've already tried to change the display, the overflow, the position, all without success. When it comes to a certain size the table just stop decreasing it's height.
Below the css to the table and the parenting div:
.tblMotivos {
table-layout:fixed;
border: 0 solid white;
-moz-box-sizing: border-box;
width: 100%!important;
min-height: 100%!important;
}
.divFundoMotivos{
padding: 0 !important;
background-color: white;
height:88%!important;
}
And the HTML:
<div class="col-sm-12 divFundoMotivos">
<table class="tblMotivos" border="1" id="tblMotivos" style="table-layout:fixed;">
<thead style="background-color:darkgray;">
<tr style="border-color:white;">
<td class="tdHeaderMotivos" style="width:44%;padding-left:1%;">Motivo</td>
<td class="tdHeaderMotivos" style="width:16%;">#</td>
<td class="tdHeaderMotivos" style="width:20%;">Meta</td>
<td class="tdHeaderMotivos" style="width:20%;">Perf.</td>
</tr>
</thead>
<tbody>
#if motivos.Count > 0 Then
#for each motivo As motivoRetencao In motivos
#<tr>
<td class="tdBodyMotivos" style="padding-left:2%;">#motivo.motivo</td>
<td class="tdBodyMotivos tdBodyMotivosValor">#motivo.qtde</td>
<td class="tdBodyMotivos tdBodyMotivosValor">#motivo.meta %</td>
<td class="tdBodyMotivos tdBodyMotivosValor fontWhite" style="#(If(motivo.performance >= motivo.meta, "background-color:green", If(motivo.performance >= ((motivo.meta * 85) / 100), "background-color:yellow;color:black!important", "background-color:red")))">#motivo.performance %</td>
</tr>
Next
End If
</tbody>
</table>
</div>
Thanks in advance. Best regards.
i agree with using media query
here is the default media query used by twitter bootstrap
https://scotch.io/tutorials/default-sizes-for-twitter-bootstraps-media-queries
implementing that media, you will need to adjust some properties such as font size, etc based on screen size to fit your need
I've had similar issues with css display: table; mixed with the height property also in the past. Most browser consider the height on browser property to be actually min-height. If the table require more height, it will simply take it... And min + max-height are not considered by Firefox (but they are by Chrome).
Your best bet would be either doing responsive content INSIDE the table, using inline-block or flexbox instead of table or try to use some javascript for responsiveness...
Hope it help.
Guides that might help you:
Guide for flexbox: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
Guide for centering in css: https://css-tricks.com/centering-css-complete-guide/
References:
min-height and table cells
from: http://www.w3.org/TR/CSS21/visudet.html#propdef-max-height
In CSS 2.1, the effect of 'min-height' and 'max-height' on tables, inline tables, table cells, table rows, and row groups is undefined.
from: http://www.w3.org/TR/CSS21/tables.html#height-layout
The height of a 'table-row' element's box is calculated once the user agent has all the cells in the row available: it is the maximum of the row's computed 'height', the computed 'height' of each cell in the row, and the minimum height (MIN) required by the cells. A 'height' value of 'auto' for a 'table-row' means the row height used for layout is MIN. MIN depends on cell box heights and cell box alignment (much like the calculation of a line box height). CSS 2.1 does not define how the height of table cells and table rows is calculated when their height is specified using percentage values. CSS 2.1 does not define the meaning of 'height' on row groups.
In CSS 2.1, the height of a cell box is the minimum height required by the content. The table cell's 'height' property can influence the height of the row (see above), but it does not increase the height of the cell box.
You could try making the text responsive, this would give you some more space.
or you could use a media query to remove the margins between the cells at certain heights.
How do I force table to take some space for headers (vertical and horizontal) and make all other cells (<td>) equal size no matter what?
<table>
<tr>
<th></th><th...
</tr>
<tr>
<th></th><td...
</tr>
<tr...
</table>
JSFiddle here
Would like all grey squares to have same size... any way to do it via CSS?
For cell of equal size add this rule
table {
table-layout: fixed;
}
Further information on MDN
If you also need to reduce the height of your cells just remove height=100% from the table and set an height to the <th> elements
I need a table with fixed column widths in px on my Bootstrap 3 project. Every <th> in my table <table id="items" class="table table-striped table-condensed"> has style=##px but its not assigning it. I've tried adding style="width:auto; overflow: scroll;" to the table but it does't work. Also tried removig every class on my table but colums wont grow.
Please help!
You could specify a <colgroup> section:
<table>
<colgroup>
<col span="1">
<col span="1">
</colgroup>
...
</table>
... and then set the width of your columns in CSS:
col {
width:200px;
}
If you are using the bootstrap .table class on the table element, you will also need to set the width of your table to auto.
See this jsfiddle.
Bootstrap 3 applies a width: 100% to the <table class="table">. In order to honor this, the browser will stretch cells to fill the remaining space. In Chrome 35 (I haven't tested in anything else), rules seem to be:
If some columns are fixed, but not all, then the fixed column widths are honored, and the remaining width of the table is split between the remaining columns that have no specified width.
If all <col> or <colgroups> have a specified (fixed) width, the browser will attempt to treat the widths like a percentage proportional to the width of the table. So if a two col's in a table have 100px, and 200px widths respectively, they may get 33% and 66% of the width respectively. This is not always the case the case though. The rules for this behavior seem to be quite complex and are probably browser-specific.
See this fiddle for a few examples and some experimentation.
http://jsfiddle.net/bzuillsmith/Nuhxj/129/
The solution finally was to remove 'table' class from the <table>. That way you can specify de width on <th> by css or directly on its width attribute. With the table class the width of the column can't be set even specifying a <colgroup> like a suggested answer.
Example:
<table width="500">
<tr>
<td width="50">1</td>
<td width="100%">2</td>
<td width="100">3</td>
</tr>
</table>
If I'm not mistaken, if a width is given as a percentage, it's a percent of the total width of the parent element, not a percent of the remaining width, correct?
Okay, fine, so what happens when we mix percentages and pixel widths? In the example above, column 2 takes up as much space as it can, and squishes 1 and 2 against the sides but it does not hide them; it allows them enough room to display their contents, even if overflow:hidden is set.
If instead we change column 2 to 10%, again the percentage seems to take priority, however it still seems to respect the proportionality of 1 and 3 -- 3 is still twice as wide as 1, even if the exact widths aren't as specified.
So what if we want to set 2 of the columns, and let the 3rd column fill the remaining space? Easy! Just completely omit the value.
Fine, but what if we want to set one column to an explicit pixel width, and have the 2nd be twice the width of the 3rd to fill the remaining space? Is that even possible in HTML/CSS?
sample fiddle
This seems to work in Chrome, but not IE or Edge:
<table style="width: 500px;">
<tr style="display: flex;">
<td style="width: 50px;">1</td>
<td style="flex-grow: 2;">2</td>
<td style="flex-grow: 1;">3</td>
</tr>
</table>
However, with <div> it appears to work in Chrome and Edge:
<div style="width: 500px; display: flex;">
<div style="width: 50px;">1</div>
<div style="flex-grow: 2;">2</div>
<div style="flex-grow: 1;">3</div>
</div>
I haven't used flex much, but I believe the number after flex-grow is the amount of remaining space to be taken up by each element in a flex element.
Edit: Flexbox layout is currently in W3C Candidate Recommendation. You can read more about it here.