100% width Textarea ignores parent element width in IE7
I have the following text box in a table:
<table width="300"><tr><td> <textarea style="width:100%"> longstringlongstringlongstringlongstringlongstringlongstringlongstringlongstringlongstringlongstringlongstringlongstringlongstring </textarea> </td></tr></table> With a long line in the text field, textarea is stretched to fit on one line in IE7, but retains a width of 300 pixels in other browsers.
Any ideas on how to fix this in IE?
Apply width to td , not table .
EDIT: @Emmett - Width can also be easily applied via CSS.
td { width: 300px; } gives the desired result. Or, if you are using jQuery, you can add width with a script:
$('textarea[width=100%]').parent('td').css('width', '300px'); A point is more than one way to apply width to a table cell if development restrictions hinder direct application.
@ Peter Meyer , Jim Robert
I tried different overflow values, but to no avail.
The experiment with different values for the wrap attribute and word-wrap style was also not fruitful.
EDIT:
@ dansays , seanb
Due to some uncomfortable application-specific restrictions, the width can only be applied to the table.
@ travis
Setting style = "word-break: break-all;" kind of work! In IE7 and FF, it still wraps differently. I will accept this answer if nothing comes better.
Another hacker option, but the only option that works for me - none of the other suggestions on this page is to wrap the text field in one cell table with a fixed table location.
<table style="width:100%;table-layout:fixed"><tr><td> <textarea style="width:100%">longstringlongstringlongstringlongstringlongstringlongstringlongstringlongstringlongstringlongstringlongstringlongstringlongstring</textarea> </td></tr></table> Another very hacky option, if you are stuck with a lot of restrictions, but know what the surrounding dom looks like:
style="width:100%;width:expression(this.parentNode.parentNode.parentNode.parentNode.width +'px')" not really, but it works in IE7.
Using jquery or the like would be a much more neat solution, but it depends on other limitations that you have.
You tried...
overflow: hidden;
??
I'm not sure what this should be in the text box table ... experiment a bit
or how about:
overflow: scroll; Edit:
I really tested this. I think the behavior is like that because the width is on the table, which I think (I have nothing to support it). I read for a long time that the width of the table is the recommended width, but it can be expanded to accommodate its contents. Not sure. I know if you use a <DIV> and not a table, this works. Also, if you apply a width of 300 pixels to the containing <TD> element, unlike the <TABLE> element, it also works. Also, overflow: scroll does nothing !: P
Nice, funny IE behavior, of course!
IE also supports the CSS 3 word-break property.
The best I could find to get it to work was a bit of a hack:
wrap textarea with <div style="width:300px; overflow:auto;"> might want to play with the overflow value
Overflow property is the way to go. In particular, if you want additional text to be ignored, you can use "overflow: hidden" as the css property in the text.
In the general case, when the browser has an indestructible object, such as a long line without spaces, it may have a conflict between various size restrictions - the values of the line (long) and its container (short). If you see different behavior in different browsers, they just solve this conflict in different ways.
By the way, there is a good trick available for long lines - the <wbr> tag. If your browser sees a longstringlongstring, then it will try to put it in the container as a single solid line, but if it does not fit, it will break this line in half on wbr. This is basically a break point with an implicit request not to break there if possible (sort of like a hyphen in printed texts). By the way, this is a bit incorrect in some versions of Safari and Opera - check out this general page for more details.
I have encountered this problem before. This is due to how HTML analyzes the width of the table and cell.
You set 300 exactly as width until the content of the element can never exceed this (setting a div with a specific width inside and an overflow rule is my favorite way).
But in the absence of such a solution, as indicated above, the moment when ANY element pushes you beyond this width, all bets are disabled. The element is becoming as wide as it is for placing content.
Extra tip is to specify the width values in any set of quotes to correctly determine the value ( <table width='300' ). If someone comes and changes it to%, he will ignore%, otherwise.
Unfortunately, you will always have problems with line breaks that do not have “natural” breaks in IE, unless you can do something to break them by code.
To solve this problem, you use a space in your text, and you use this code too
overflow:hidden Give the width in pixels. This should work correctly.