English 中文(简体)
HTML textarea not correctly sized in table in ie8. Doctype problem?
原标题:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Untitled Page</title>
    <style type="text/css">       
        #textarea1 {
            width: 100%;
            overflow:hidden;
        }
        #table1 {
    background-color:#0000ff;
    width: 100px;
        }
    </style>
</head>
<body>
    <form id="form1">
    <table id="table1" >
        <tr><td><textarea  cols="0" rows="2" id="textarea1"></textarea></td></tr>
    </table>
    </form>
</body>
</html>

The textarea is not rendered correctly by ie8 in the table (it is wider than its cell). If I remove the doctype tag it renders correctly. Am I doing something stupid

最佳回答

IE, for some reason, interprets 100% width of a table cell as 100px-(padding/2), inherited from your #table1 width property. Now here s where it really screws up: the table has a default cellspacing of 3 pixels, so technically, your cell should be 94px wide, and 100% of that width should be... 94px. Instead, it s applying 100px.

If you explicitly set the width property of the table cell, it can correct it, but only after you set the cellspacing and cellpadding to 0, and the border-width of the textarea to 0.

e.g.:

#textarea1 {
width: 100%;
overflow:hidden;
margin:0;
padding:0;
border:0;
}
#table1 {
background-color:#0000ff;
width: 100px;
padding:0;
margin:0;
border-spacing:0;
border-collapse:collapse;
}
#table1 td {
width:100px;
padding:0;
margin:0;
}

The alternative, if you want normal borders around your textarea, is to define a width for the td as 100px - border-wdith*2.

Hope that helps, nd

问题回答

cols="0" seems a bit daft. Set that to a sensible value for users without CSS.

You might want to try removing borders and padding from the textarea:

#textarea1 {
    width: 100%;
    overflow:hidden;
    margin: 0;
    border-style: none;
    padding: 0;
}

width refers to the width of the element’s content area, i.e. the area inside the margin, borders and padding

I found out that if you re using <asp:textbox> control, with attributes runat="server" and TextMode="MultiLine", it works fine. And if you view the source - strange enough - what you get is a html textarea just as you would have defined it (I could not find any differences between the asp rendering and a straight-forward html definition of a textbox within a table).

If you have asp.net anyway, you can use it easily that way, it renders nicely. All you have to do is to put the prefix "ctl00_MainContent_" before the ID (or whatever ASP.NET renders in your case depending on the surroundings) - just look it up once via "view source" in IE or with the developer toolbar.

So in jQuery, a

$( #ctl00_MainContent_myTextbox ).val()

would retrieve the value, if

<asp:TextBox ID="myTextbox" runat="server" TextMode="MultiLine" style="height:50px; width:90%"></asp:TextBox>

is defined in your code. You can put the prefix #ctl00_MainContent_ in a variable for your convenience, e.g.:

var aspPrefix= #ctl00_MainContent_ ; // global variable
var textValue = $(aspPrefix+ myTextbox ).val(); // example to get value

Everything else works as you would expect it.





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签