2011-05-17 229 views
8

如果寬度設置爲100%,我遇到了顯示太寬的textarea(可能是任何輸入元素)的問題。這是我的CSS。Textarea在100%寬度溢出父容器

首先,我重置所有樣式。

html, body, div, span, applet, object, iframe, 
h1, h2, h3, h4, h5, h6, p, blockquote, pre, 
a, abbr, acronym, address, big, cite, code, 
del, dfn, em, img, ins, kbd, q, s, samp, 
small, strike, strong, sub, sup, tt, var, 
b, u, i, center, 
dl, dt, dd, ol, ul, li, 
fieldset, form, label, legend, 
table, caption, tbody, tfoot, thead, tr, th, td, 
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary, 
time, mark, audio, video { 
    margin: 0; 
    padding: 0; 
    border: 0; 
    font-size: 100%; 
    font: inherit; 
    vertical-align: baseline; 
} 
/* HTML5 display-role reset for older browsers */ 
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section { 
    display: block; 
} 
body { 
    line-height: 1; 
} 
ol, ul { 
    list-style: none; 
} 
blockquote, q { 
    quotes: none; 
} 
blockquote:before, blockquote:after, 
q:before, q:after { 
    content: ''; 
    content: none; 
} 
table { 
    border-collapse: collapse; 
    border-spacing: 0; 
} 

然後將樣式應用到我的表單。

form fieldset { 
    border: none; 
    border: 1px solid #ff0000; 
    padding: 5px; 
} 
form fieldset legend { 
    font-size: 20px; 
    font-weight: bold; 
} 
form textarea { 
    width: 100%; 
    height: 500px; 
} 

最後我的HTML。

<form method="post"> 
    <fieldset> 
     <label for="area">Content</label> 
     <textarea id="area" name="area"></textarea> 
    </fieldset> 
</form> 

在Chrome和Firefox(沒有測試過別人還),textarea的是正確的左側填充爲5px,但右邊的文本區域或者是與字段集,或者溢出只是有點齊平。

感興趣的一件事是,如果我從頁面中刪除文檔類型,所有東西都能正確顯示。

回答

2

重置也textarea,我沒有看到它在您的重置CSS。

可能是繼承了一些利潤。

+0

是的,就是這個問題! – mellowsoon 2011-05-17 19:15:39

0

的主要原因是:http://en.wikipedia.org/wiki/Internet_Explorer_box_model_bug

當IE是不能在標準模式的投訴(即,當大多DOCTYPE沒有提及),一個元件的寬度包括它的空白和邊距。這導致textarea溢出。

嘗試刪除由您或瀏覽器提供的textarea的任何邊距或填充。

7

雖然兩個答案(目前)提供有用的相關信息,既沒有實際提供的解決方案,這是簡單地添加box-sizing: border-box;textarea,即

form textarea { 
    width: 100%; 
    box-sizing: border-box; 
    height: 500px; 
} 

box-sizing: border-box;在所有現代瀏覽器包括IE8支持(但不是IE7),並且意味着在計算佈局時使用元素的寬度(包括邊框和填充)。

缺省值通常爲content-box,僅使用元素的內部寬度。大多數瀏覽器爲textarea提供它們自己的默認borderpadding樣式,但並不總是box-sizing,所以結果可能是width: 100%;意味着父寬度加上瀏覽器的默認邊框和填充,如果它們不爲零,則顯然大於父容器的寬度。