简体   繁体   English

CSS Textarea宽度错误也左右边距

[英]CSS Textarea width is wrong also margin left and right

I created a fiddle to show something strange. 我创造了一个小提琴来展示一些奇怪的东西。 I created a DIV 200px width and inside this DIV i inserted a textarea using CSS. 我创建了一个DIV 200px宽度,在这个DIV中,我使用CSS插入了一个textarea。 Because it is a box div like the rest of the design I need the texarea to have 10px margin left and 10px margin right, so I edited the margins and set the width of textarea to 180 because if outer div is 200px and 10px padding left - 10px padding right I have 180px left to be used in textarea width 因为它是一个盒子div,就像设计的其余部分一样,我需要texarea剩下10px的边距和10px的边距,所以我编辑了边距并将textarea的宽度设置为180,因为如果外部div为200px且剩余10px填充 - 10px填充权我还剩180px用于textarea宽度

Here is the code I used: 这是我使用的代码:

CSS CSS

#messenger {
    margin-left:10px;   margin-right:10px;    margin-top:10px;    margin-bottom:10px;
    width:180px;      height:64px;  overflow:auto;
    background:#ffffff;    font-size:11px;   font-weight:normal;   color:#333333;
    border:1px solid #666666; 
    padding-left:5px;padding-right:5px;padding-top:2px;
}

#outer {width:200px;background:#cccccc;}

HTML HTML

<div id="outer">
<textarea name="message" id="messenger" form="chat" maxlength="100" onFocus="if(this.value==this.defaultValue)this.value='';">
Your message here
</textarea>
</div>

well it is looking pretty bad here is the demo: 好吧,看起来很糟糕这里是演示:

https://jsfiddle.net/f3d1t223/ https://jsfiddle.net/f3d1t223/

还有截图

I really need it to have borders 1px too. 我真的需要它也有1px的边框。 Any ideas why this is happening? 任何想法为什么会这样?

Check out this one. 看看这一个。

 #messenger { margin:10px; width:168px; height:64px; overflow:auto; background:#ffffff; font-size:11px; font-weight:normal; color:#333333; border:1px solid #666666; padding-left:5px; padding-right:5px; padding-top:2px; } #outer {width:200px;background:#cccccc;} 
 <div id="outer"> <textarea name="message" id="messenger" form="chat" maxlength="100" onFocus="if(this.value==this.defaultValue)this.value='';"> Your message here </textarea> </div> 

acording to your code you have padding and border size also so need to reduce those values from the width of the textarea. 根据你的代码,你有填充和边框大小也需要从textarea的宽度减少这些值。

/*Add*/
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;

https://jsfiddle.net/svansoeren/yaj10txk/13/ https://jsfiddle.net/svansoeren/yaj10txk/13/

Add on your textarea this code: 在您的textarea上添加此代码:

/*Add*/
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
resize: vertical;

Link 链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM