CSS HTML Forcing table column to specific size - html

I have the following within a JSFiddle: http://jsfiddle.net/X37V7/
HTML
<caption>Table 1 Example</caption>
<table id="fx">
<tr>
<th class="a75">Values</th>
<td class="a25 rotate overflow he rr">1</td>
<td class="a25 rotate overflow he rr">1</td>
<td class="a25 rotate overflow he rr">1</td>
<td class="a25 rotate overflow he rr">1</td>
</tr>
<tr>
<th class="a75">Initial value</th>
<td class="a25">One</td>
<td class="a25">Two</td>
<td class="a25">Three</td>
<td class="a25">Four</td>
</tr>
</table>
<caption>Table 2 Example</caption>
<table id="fx2">
<tr>
<th class="a75">Values</th>
<td class="a25 rotate overflow he rr">Long Text Example</td>
<td class="a25 rotate overflow he rr">1</td>
<td class="a25 rotate overflow he rr">1</td>
<td class="a25 rotate overflow he rr">1</td>
</tr>
<tr>
<th class="a75">Initial value</th>
<td class="a25">One</td>
<td class="a25">Two</td>
<td class="a25">Three</td>
<td class="a25">Four</td>
</tr>
CSS
body {
width:750px;
}
table {
display:block;
border-collapse:collapse;
width:100%;
overflow:hidden;
border-style:solid;
border-width:1px;
}
tr,th,td {
overflow:hidden;
text-overflow:ellipsis;
white-space:nowrap;
word-wrap:break-word;
border-style:solid;
border-width:1px;
}
th {
width:5%;
}
.over {
overflow:hidden;
}
.a24 {
width:10%;
}
.a75 {
width:60%;
}
.he {
height:130px;
}
.rotate {
-webkit-transform:rotate(270deg);
}
.rr {
height:-1px;
width:130px;
margin-left:-30px;
}
#fx,#fx2 {
table-layout:fixed;
overflow:hidden;
}
There are two table examples. Both are set to fixed width.
The first table shows correctly as it has text within the cells that are smaller than its dimensions.
The second table shows what happens when longer text is inserted - the column grows wider.
Is there a way of forcing the column/cell to stay the size it is set, regardless of its content?

You will be able to achieve this if you set a max-width value to the desired cells you want fixed.
Please note the fixed width must be in pixels so the cell knows when to cause overflow.
(However if your table has a fixed width this is not a problem).
.a25{max-width:75px;}
Either overflow / text-overflow or text-wrap must also be applied to designate what the text should do. This is already present in your example. (As below)
(Note .a25 is incorrectly .a24 in your demo)
CLICK FOR WORKING DEMO
FULL CSS (As per your demo)
body{width:750px}
table{
display:block;
border-width: 1px;
border-style: solid;
border-collapse: collapse;
width: 100%;
overflow: hidden;
}
#fx{
table-layout: fixed;
overflow: hidden;
}
#fx2{
table-layout: fixed;
}
tr, th, td{
border-width:1px;
border-style: solid;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
th{
width: 5%;
}
.over{overflow: hidden;}
.a25{width:10%;max-width:75px;}
.a75{width:60%}
.he{height:130px;}
.rotate{-webkit-transform:rotate(270deg);}
.rr{height:-1px;width:130px;margin-left:-30px;}
CLICK FOR CLEAN DEMO
CSS (CLEAN VERSION)
table{
table-layout: fixed;
border-collapse: collapse;
width: 100%;
}
tr, th, td{
border:1px solid #888;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.w25{width:10%;max-width:75px;}
.w75{width:60%}
.h130{height:130px;}
This example is currently a fluid table with overflow.
To fixed cell size either replace max-width with width or replace table width with fixed value.

The problem is that you have set the white-space: nowrap; your stopping it from wrapping around the lines, change this to white-space: wrap;. Hope that helps.
Please view fiddle http://jsfiddle.net/X37V7/

Use Below CSS Code:
CSS:-
tr, th, td{
border-width:1px;
border-style: solid;
overflow: hidden;
text-overflow: ellipsis;
word-wrap: break-word;
white-space:wrap;
}
table td.a25{
max-height:70px;
}

You can do this with CSS alone:
Only assign box class [as defined below css class] to the column you want to have fixed width.
.box {
-o-text-overflow: ellipsis; /* Opera */
text-overflow: ellipsis; /* IE, Safari (WebKit) */
overflow:hidden; /* don't show excess chars */
white-space:nowrap; /* force single line */
width: 300px; /* fixed width */
}
Hope this helped!

Related

Display table with 'display: block;' at the center of a page

I have a very large table that doesn't fit a page and I added horizontal scrollbar like this:
.table-x-scroll {
display: block;
overflow-x: auto;
white-space: nowrap;
}
And <table class="table-x-scroll">...</table>
But I also want my table to be in the center of a page. I used to use align="center" but now, when I added display: block it doesn't work. Table is always at the left. How can I do these two things at the same time?
Would be better to use a wrapper for the table and add overflow to that wrapper not the table it self. This way you can control the behavior better.
And also use margin:0 auto on table to center it horizontally. No need for display:block
See below or better in jsFIddle
table {
border: 2px solid red;
width: 300px;
margin: 0 auto;
}
td {
border: 1px solid red;
}
.wrapper {
width: 100%;
overflow-x: auto;
}
<div class="wrapper">
<table>
<tr>
<td>a</td>
<td>a</td>
</tr>
<tr>
<td>a</td>
<td>a</td>
</tr>
<tr>
<td>a</td>
<td>a</td>
</tr>
</table>
</div>
Try to add margin-left:auto; margin-right:auto; in your style and specify the width
.table-x-scroll {
display: block;
overflow-x: auto;
white-space: nowrap;
margin-left:auto;
margin-right:auto;
width:50%;
}
<table class="table-x-scroll" border="1">
<tbody>
<row>
<td>AA</td>
<td>BB</td>
</row>
</tbody>
</table>
Try adding Margin : 0 auto
.table-x-scroll {
display: block;
overflow-x: auto;
white-space: nowrap;
margin : 0 auto
}
I can see that others have answered the aligning problem. But i couldn't see the "Doesn't fit to page" error so here is a solution for that:
In the css you specified overflow-x: auto;
If you change that to overflow-x scroll; it will make it scale to whatever size you choose to specify. and make it scrollable. The scrollbar within is possible to edit/remove, but it will still scroll ^^
I personally recommend the vw,vh values
example:
.bunnydiv {font-size: 1vw, 1vh;} This will make it scale to 1% of the width and of the height of the monitor. This can be specified with either vh, vw, vh and vw. Not both paramaters are needed, and it will scale accordingly :-)
I found some code I made myself some time ago, use this as a reference :-)
.div_scroll
{overflow-y: scroll;
display: block;
top:0px;
right: 0px;
position: absolute;
transform: translate(-23.6vw, 15vh);
border: 3px solid #dddddd;
max-height: 75vh;
width: 76vw;
margin: 0px;
background-color: white;}
It's copy-pasted, and setup this way for readability. Don't format you code like this unless it works better for you :-)

Responsive table column with CSS

There's a table with three columns, the first and the last are fixed and the middle one should be fluid.
The problem - inside of the middle column there's text with nowrap and it prevents it from being fluid.
How that could be done?
How it looks on wide page (correct behaviour):
How it should look on narrow page:
How it actually looks on narrow page (incorrect behaviour, see scrollbar):
The code https://jsfiddle.net/8c9msa71/
td {
border: 1px solid black;
}
.fluid {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
<table>
<tr>
<td>first</td>
<td class="fluid">very-very-very-very-very-very-long-second-text</td>
<td>third</td>
</tr>
</table>
you can do it by adding a div
td { border: 1px solid black; }
.fluid { position: relative; width: 70%;}
.fluid div {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
position: absolute;
left: 5px;
right: 5px;
top: 0;
}
<table width="100%">
<tr>
<td>first</td>
<td class="fluid">
<div>very-very-very-very-very-very-long-second-text</div>
</td>
<td>third</td>
</tr>
</table>
I noticed the solution to add a div.
If you don't want to or need to add a div, you could use max-width.
Explanation:
Your first problem is that none of your elements has a width attribute set. To start with I would set a max-width of your "very-very-long-second-text". For example you can add max-width: 60vw; to your .fluid. If you're not familiar with the vw syntax, read more here: https://www.w3.org/TR/css3-values/#vw
By only adding this, you'll be almost there. You'll still have one problem left: On very small devices / resolutions, you notice that your third table-data, <td></td> will disappear out of visible area.
Instead of collapsing ALL the content, I recommend using display: inline-block; on your table data <td></td>. What this does is that it will display your table data inline as long as they have enough space to be inline. In addition a small part of the information will be visible, instead of the result of NO information visible at all. When the available area becomes smaller (i.e. resizing the window), they will start jumping down one by one.
Full CSS:
td {
border: 1px solid black;
display: inline-block;
}
.fluid {
max-width: 60vw;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}

HTML table border and text overflow

I'm displaying an HTML table using some CSS as follows.
<style type="text/css">
.style-class {
table-layout: fixed;
width: 20%;
white-space: nowrap;
overflow: hidden;
text-overflow:ellipsis;
border: 1px;
}
</style>
<table rules="all" class="style-class">
<tr>
<td>a</td>
<td>Text overflow ellipsis demo.</td>
</tr>
<tr>
<td>b</td>
<td>Text overflow ellipsis demo.</td>
</tr>
</table>
Mozilla FireFox (29.0.1) displays it correctly as follows.
Google Chrome (35.0.1916.153 m) displays it with missing right and bottom borders as can be seen in the following snap shot.
Internet Explorer (8) displays this table with no borders at all as follows.
Can this table be displayed with all borders along with text-overflow:ellipsis which is important as displayed by FireFox as can be seen in the respective snap shot?
By moving the overflow and border rules to cells rather than the whole table, you can get the effect you need. Try this CSS:
.style-class {
table-layout: fixed;
width: 20%;
border-collapse: collapse;
border: 1px solid #000;
}
.style-class td {
border: 1px solid #000;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}

Overflow: auto does not work in Firefox

I have a table. Its <td> have overflow: auto.
The width is set to be 100px. In Firefox only, text that exceeds 100px is not hidden and replaced with a scrollbar.
How can I hide content and have a scrollbar when it exceeds the width of its container?
http://jsfiddle.net/be6tM/10/
this question from here maybe solve your problem
nickb answer: "Try wrapping it in a <div>. I'm pretty sure the overflow attribute is not defined for a <td> element, at least in HTML4 it's not."
try to put your overflow:auto to the wrapper hope this can help you
pre, div {
width:100%;
overflow: auto !important;
}
working demo
The easier way to do this would be to add this to the Html
<td class="first">
<div>Don ovonMrLongNameIsMe!!!</div>
</td>
and this to the CSS
div {
overflow:auto;
}
td {
border: 1px solid rgb(0,0,0);
min-width: 100px;
max-width: 100px;
}
Working Example:
div {
overflow:auto;
}
td {
border: 1px solid rgb(0,0,0);
min-width: 100px;
max-width: 100px;
}
<table>
<tr>
<td class="first">
<div>Don ovonMrLongNameIsMe!!!</div>
</td>
</tr>
</table>

CSS table td width - fixed, not flexible

I have a table and I want to set a fixed width of 30px on the td's. the problem is that when the text in the td is too long, the td is stretched out wider than 30px. Overflow:hidden doesn't work either on the td's, I need some way of hiding the overflowing text and keeping the td width 30px.
<table cellpadding="0" cellspacing="0">
<tr>
<td>first</td><td>second</td><td>third</td><td>forth</td>
</tr>
<tr>
<td>first</td><td>this is really long</td><td>third</td><td>forth</td>
</tr>
</table>
It's not the prettiest CSS, but I got this to work:
table td {
width: 30px;
overflow: hidden;
display: inline-block;
white-space: nowrap;
}
Examples, with and without ellipses:
body {
font-size: 12px;
font-family: Tahoma, Helvetica, sans-serif;
}
table {
border: 1px solid #555;
border-width: 0 0 1px 1px;
}
table td {
border: 1px solid #555;
border-width: 1px 1px 0 0;
}
/* What you need: */
table td {
width: 30px;
overflow: hidden;
display: inline-block;
white-space: nowrap;
}
table.with-ellipsis td {
text-overflow: ellipsis;
}
<table cellpadding="2" cellspacing="0">
<tr>
<td>first</td><td>second</td><td>third</td><td>forth</td>
</tr>
<tr>
<td>first</td><td>this is really long</td><td>third</td><td>forth</td>
</tr>
</table>
<br />
<table class="with-ellipsis" cellpadding="2" cellspacing="0">
<tr>
<td>first</td><td>second</td><td>third</td><td>forth</td>
</tr>
<tr>
<td>first</td><td>this is really long</td><td>third</td><td>forth</td>
</tr>
</table>
you also can try to use that:
table {
table-layout:fixed;
}
table td {
width: 30px;
overflow: hidden;
text-overflow: ellipsis;
}
http://www.w3schools.com/cssref/pr_tab_table-layout.asp
It is not only the table cell which is growing, the table itself can grow, too.
To avoid this you can assign a fixed width to the table which in return forces the cell width to be respected:
table {
table-layout: fixed;
width: 120px; /* Important */
}
td {
width: 30px;
}
(Using overflow: hidden and/or text-overflow: ellipsis is optional but highly recommended for a better visual experience)
So if your situation allows you to assign a fixed width to your table, this solution might be a better alternative to the other given answers (which do work with or without a fixed width)
The above suggestions trashed the layout of my table so I ended up using:
td {
min-width: 30px;
max-width: 30px;
overflow: hidden;
}
This is horrible to maintain but was easier than re-doing all the existing css for the site. Hope it helps someone else.
This workaround worked for me...
<td style="white-space: normal; width:300px;">
Put a div inside td and give following style width:50px;overflow: hidden; to the div
Jsfiddle link
<td>
<div style="width:50px;overflow: hidden;">
<span>A long string more than 50px wide</span>
</div>
</td>
Chrome 37.
for non fixed table:
td {
width: 30px;
max-width: 30px;
overflow: hidden;
}
first two important! else - its flow away!
Just divide the number of td to 100%. Example, you have 4 td's:
<html>
<table>
<tr>
<td style="width:25%">This is a text</td>
<td style="width:25%">This is some text, this is some text</td>
<td style="width:25%">This is another text, this is another text</td>
<td style="width:25%">This is the last text, this is the last text</td>
</tr>
</table>
</html>
We use 25% in each td to maximize the 100% space of the entire table