Html table width - html

I have code like this:
<table width="625px">
<tr style="background-color:#CCC; font-size:14px;">
<td style="padding:8px; color:#333"><center><?=stripslashes($arr['signature']);?> </center></td>
</tr>
If $arr['signature'] is a long string, like this gjuytfdcrtrfdscvythgfvtfdgtrfdvbgfvcfgbdc the width doesn't help and it goes very wide. How can I prevent that?

there are many ways, depends on what you wish to do.
If you apply overflow:hidden then the overflown text would be hidden. You can use overflow:scroll; inside a div like <td style="padding:8px; color:#333"><center><div style="overflow:scroll; width:625px;"><?=stripslashes($arr['signature']);?></div> </center></td>
This will create a scroll for the overflow text, only for this cell (there is need to give width for this method).
Here an example for the overflow:scroll;

Try:
<td style="width: 160px; max-width: 160px;">
or
<td style="width: 160px; overflow-x: hidden;">
or both together.
See
http://www.highdots.com/forums/html/limit-width-table-cell-271764.html

Use the overflow property:
<table width="625px" style="overfow: hidden;">

use the overflow style property on the "td" tag
<td style="overflow-x: hidden;">your text here</td>

See the section on table widths in the CSS2 spec: http://www.w3.org/TR/CSS2/tables.html#width-layout
Roughly, tables will choose the maximum of the specified width and the sum of the widths of their columns. You have to constrain the width of the column, perhaps by setting a width on the element (you'll want to choose your overflow style here as well).
Example:
<table width="625px">
<tr style="background-color:#CCC; font-size:14px;">
<td style="padding:8px; color:#333; width: 625px; overflow: hidden; text-align:center"><?=stripslashes($arr['signature']);?></td>
</tr>
</table>
(Also note that <center> has been long deprecated; please use the text-align CSS property.)

I remember spending hours on that. Use the table-layout:fixed style.
<table style="width:625px;table-layout:fixed;overflow:hidden">
Each column will take the size of its first cell.

Related

Fixed width of columns in html table

I am trying to use a table where I will define fixed width columns. Each column width is pre-defined and applied to a 'th' element in the table header via:
ng-style='{"width":get_width[i]+"px"}', where the get_width function returns a number.
This works just fine as long as the text does not exceed the column width specified. If it does, the width changes to accommodate the entire text.
Question, how can I use CSS to force the column width to my specification? The text may be truncated if longer.
Thx in advance.
You can truncate text using css: FIDDLE
CSS
.div1 {
white-space:nowrap;
width:12em;
overflow:hidden;
text-overflow:ellipsis;
border:1px solid #000000;
}
HTML
<p>This div uses "text-overflow:ellipsis":</p>
<table>
<tr>
<td><div class="div1">This is some long text that will not fit in the box.</div></td>
</tr>
</table>
If you can, set the width of the table as a whole by styling the <table> element. Evidently a lot of implementations of CSS expand the columns to fit text until they bump up against the maximum width of the table.
Source
you need to set table layout fixed and set overflow hidden for cells and you have to use
<table class="fixed-table">
<col width="20px" />
<col width="30px" />
<col width="40px" />
<tr>
<td>text</td>
<td>text</td>
<td>text</td>
</tr>
</table>
.fixed-table{ table-layout:fixed; word-break: break-all;}
.fixed-tabletd { overflow: hidden; }

3 column table, widths: auto, 50%, 50%, overflow hidden

I'm trying to recreate a daily schedule view in a mobile website design. The PC version looks like this:
It will have several rows, and up to 5 or 6 columns. I think a table will be best, but can't find the right CSS/HTML to get this to work how I want.
I want the first column to have an auto width, to fit the content, and the rest to be equal (evenly distributed). The entire table will be 100% width.
I can get this by setting the column widths as follows: 0; 50%; 50%; -- and not using table-layout: fixed; The problem is, I can't have the width of any cells getting wider just because the content is too large. If I use table-layout: fixed, it keeps the cells the correct size, but the first column is 0 width, instead of auto/fit. I tried placing the content inside each cell in a span or div and setting those to: width: 100%; overflow: hidden;, but I don't think the width: 100% really works inside a table that isn't fixed.
If I really have to, I'll set a fixed width for the first column, but I'd like to avoid this because I don't want to use fixed font sizes -- especially because this will be a mobile website, for smart-phones and tablets.
I might be able to do something by using nested tables or floats... the first column not being part of the same table, but I'm hoping there is a super clean solution I'm missing, and I can keep all of this in a single table.
EDIT: As requested, here is one version of my code that I have tried. The styles with x in front of the names are just different things I have tried (I add the x to quickly remove, and easily put back):
<table cellpadding="0" cellspacing="0" style="width: 100%; xwhite-space: nowrap; xtable-layout: fixed;">
<tr>
<td style="width: 0; background-color: Lime;">
Time
</td>
<td style="width: 50%; background-color: Silver;">
ERIC
</td>
<td style="width: 50%; background-color: Gray;">
DONNA
</td>
</tr>
<tr>
<td>8:00am</td><td> </td><td>Do Something</td>
</tr>
<tr>
<td>9:00am</td><td style="width: 50%; overflow: hidden;"><div style="width: 100%; height: 100%; overflow: hidden;">Do Something else with more text so we can see how this works when too long and really longer than it ever should be</div></td><td style="width: 50%;"> </td>
</tr>
</table>
The above version is as close as I've got, but the long text for "ERIC" at 9am wraps to multiple lines. If I change it to not wrap, then the cell gets too wide (even with overflow: hidden).
Your table width is 100% and the second and third columns are width 50% each and your first column is 0%. Definitely, it doesn't work because it has already used up the 100% width for the second and third columns.
In case it isn't possible to do what I want with one table, here is a nested table solution that doesn't seem as bad as I thought:
<table cellpadding="0" cellspacing="0" style="width: 100%;">
<tr>
<td style="width: 0;">
<table cellpadding="0" cellspacing="0">
<tr><td>Time</td></tr>
<tr><td>8:00am</td></tr>
<tr><td>9:00am</td></tr>
</table>
</td>
<td>
<table cellpadding="0" cellspacing="0" style="width: 100%; table-layout: fixed; white-space: nowrap;">
<tr><td>ERIC</td><td>DONNA</td></tr>
<tr><td style="overflow: hidden;">This cell has a lot of text so that I can test for overflow issues even if I make my browser window very wide</td><td> </td></tr>
<tr><td> </td><td>Whatever</td></tr>
</table>
</td>
</tr>
</table>
Fiddle: http://jsfiddle.net/fWFPm/4/

How to force min-height on table

I have this code :
<table cellspacing="0" cellpadding="0" border="0" style="width:415px">
<tbody>
<tr valign="top">
<td style="font-family:Arial;min-height:60px;font-size:12px;line-height:14px;">
This is my text that I need in 2 lines
</td>
</tr>
<tr valign="top">
<td style="font-size:12px;line-height:14px">
Second Line
</td>
</tr>
</tbody>
</table>
As you can see, the first tr/td should be height 60px (min-height:60px) but in fact it isn't.
For many reasons, I can't use height directly (this code is formatted trought back office system, in a newsletter).
So, how can I take the whole height on the td trought min-height?
Also, tried putting min-height:60px; on tr, but nothing change...
min-height doesn't work for table elements:
In CSS 2.1, the effect of 'min-width' and 'max-width' on tables, inline tables, table cells, table columns, and column groups is undefined.
I can only assume this applies to td and tr as well.
What should always work is wrapping the content in a div, and applying min-height to that, as shown in this JSFiddle:
<td style="font-family:Arial;min-height:60px;font-size:12px;line-height:14px;">
<div style="min-height: 60px; background-color: green">
This is my text that I need in 2 lines
</div>
</td>
Edit: You say this doesn't work with Outlook.
Alternative idea: Place a 60 px tall image in the td, and make it float: left:
<td>
<img src="..." style="float: left">
</td>
Use <td height="60"> not CSS height or min-height
For HTML email set your table cell as <td height="60"> and it will treat that as the min-height. If your content is more than 60px, it will expand accordingly.
Put a DIV in the cell, style the DIV instead.
Min-height doesn't works on tables.
It is sometimes useful to constrain the height of elements to a certain range. Two properties offer this functionality: min-height & max-height
But these can't be used on non-replaced inline elements, table columns, and column groups.
You can't set min-height and min-width, but you can use some CSS3 for achievements this same effect.
.default-table table {
border-collapse: collapse;
}
.default-table table td {
padding: 0;
}
.default-table tr:before {
width: 0px;
content: '';
float: left;
overflow: hidden;
height: 28px;
font-size: 0;
}
.default-table {
overflow: hidden;
}
<div class="default-table">
<table>
<tbody>
<tr>
<td>Steve</td>
<td>Smith</td>
<td>stevesmith#gmail.com</td>
</tr>
<tr>
<td>Jone</td>
<td>Polanski</td>
<td>jonep#gmail.com</td>
</tr>
</tbody>
</table>
</div>
but if u having collapse or padding in td. You must give for .default-table table minus margin-left.
HTML :
<table></table>
CSS :
table{
height:0px; /*Set any facultative length value to Height (percentage value doesn't work)*/
min-height:100vh;
}
That's how I always resolve this problem ...
Add display block
<td style="font-family:Arial;min-height:60px;font-size:12px;line-height:14px;display:block;">
Here's a solution that works in Outlook (tested) and other e-mail clients:
<td style="mso-line-height-rule:exactly;line-height:300px;"> </td>
This is cleaner than using an image, which could negatively affect your spam score, and does the exact same thing.
If you have other content in the <td> that you don't want to have that line height, you can just wrap the non-breaking space in a <span> and set the line-height on that tag:
<td><span style="mso-line-height-rule:exactly;line-height:300px"> </span>**Other content without 300px line-height here**</td>
The reason height or min-height works on <div> tags and not <td> is because <td> are set to display:table-cell and do not respect height the same way that display:block (<div>) elements do.
I have resolved this issue by adding display:block; to its style as
<td style="display:block; min-height:200px;">
min-height does not work in td, Set height that will work like min-height and automatic increase height if needed. That is worked for me
Here is a solution that does not depend on the height in pixels. It works in all email clients:
<table cellspacing="0" cellpadding="0" border="0" style="width:415px">
<tbody>
<tr valign="top">
<td style="font-family:Arial;font-size:12px;line-height:14px;">
This is my text that I need in 2 lines
</td>
<td style="font-family:Arial;font-size:12px;line-height:14px;">
<br/><br/>
</td>
</tr>
<tr valign="top">
<td style="font-size:12px;line-height:14px">
Second Line
</td>
</tr>
</tbody>
The solution works by adding a zero-width column with two lines to the right of the first one. It uses the  character, which is a non-breaking zero-width space.
It may be reviving a 2012 post, for those who searched and found this post like me:
Note: Check these addresses for the email client support before using this method, at the time of writing this answer, the support was around 50% -ish.
E-mail client support range of :first-child
E-mail client support range of ::before
table tr:first-child td:before {
min-height: 100px;
display: block;
content: ""
}
<table border="1">
<tr>
<td></td>
</tr>
<tr>
<td></td>
</tr>
</table>
What I found !!!, In tables CSS td{height:60px;} works same as CSS td{height:60px;}

Prevent a table in overflow:auto div from shrinking

I'm having a bit of an issue getting some stylesheet behavior that I want. I'm not even sure if it's possible. Basically I'm attempting to place a table with a variable number of cells with static cell width in a DIV with overflow: auto, and my goal is that when the tables width extends past the width of the container DIV that it becomes scrollable.
This isn't the case. The cells get shrunk together. A very basic representation (with inline styles for ease on this; not actually in the application haha) of the code:
<div style="width: 1000px; overflow-x: auto;">
<table>
<tr>
<td style="width:400px;">
This
</td>
<td style="width:400px;">
Should
</td>
<td style="width:400px;">
Scroll!
</td>
</tr>
</table>
</div>
Is there anyway I can do this with CSS, or am I going to have to go back to setting the width inline on a second div containing the table through calculations?
Works if you set the width on the table itself.
<table style="width:1200px;">
The td will always shrink to the necessary size, they won't push the table wider in that situation.
using CSS can done like below but make sure you use id or class for applying css if you have more then one table or div.
<style>
div { width: 400px; overflow-x: auto; }
table { width:1200px; }
table td { width:400px; }
</style>
<div>
<table>
<tr>
<td>
This
</td>
<td>
Should
</td>
<td>
Scroll!
</td>
</tr>
</table>
</div>
This should help
<table style="width: max-content;">

IE doesn't recognize TD width?

I wonder why IE doesn't seem to recognize the width I specify?
Basically I have this code:
<table>
<tr>
<td valign="top" align="right" class="left_frame"></td>
</tr>
</table>
CSS:
.left_frame {
background: url(images/side.gif) repeat-y;
width: 17px;
}
Even if I add width="17" inside the <td></td> tags, the width still doesn't change. This is quite frustrating because the problem seems to be very simple.
I'd say it's because there's no content in your <td>
Try adding a in there so the cell has some content, and see how that goes.
Alternatively, placing a height on the cell may work as well, depending on your requirements.
Basically the cell is a flat line at the moment, and needs something to tell it how tall it is, in order to draw the background in.
Example: http://jsfiddle.net/MvBf5/