Is there a simple way to make html textarea and input type text equally wide?

喜欢而已 提交于 2020-01-01 07:33:09

问题


Is there a simple way of getting a HTML textarea and an input type="text" to render with (approximately) equal width (in pixels), that works in different browsers?

A CSS/HTML solution would be brilliant. I would prefer not to have to use Javascript.

Thanks /Erik


回答1:


You should be able to use

.mywidth {
  width: 100px;   
}
<input class="mywidth">
<textarea class="mywidth"></textarea>



回答2:


To answer the first question (although it's been answered to death): A CSS width is what you need.

But I wanted to answer Gaius's question in the answers. Gaius, you're problem is that you are setting the width's in em's. This is a good think to do but you need to remember that em's are based on font size. By default an input area and a textarea have different font faces & sizes. So when you are setting the width to 35em, the input area is using the width of it's font and the textarea is using the width of it's font. The text area default font is smaller, therefore the text box is smaller. Either set the width in pixels or points, or ensure that input boxes and textareas have the same font face & size:

.mywidth {
  width: 35em;
  font-family: Verdana;
  font-size: 1em;
}
<input type="text" class="mywidth"/><br/>
<textarea class="mywidth"></textarea>

Hope that helps.




回答3:


Someone else mentioned this, then deleted it. If you want to style all textareas and text inputs the same way without classes, use the following CSS (does not work in IE6):

input[type=text], textarea { width: 80%; }



回答4:


This is a CSS question: the width includes the border and padding widths, which have different defaults for INPUT and TEXTAREA in different browsers, so make those the same as well:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>width</title>
<style type="text/css">
textarea, input { padding:2px; border:2px inset #ccc; width:20em; }
</style>
</head>
<body>
<p><input/><br/><textarea></textarea></p>
</body>
</html>

This is described in the Box dimensions section of the CSS specification, which says:

The box width is given by the sum of the left and right margins, border, and padding, and the content width.




回答5:


Use CSS3 to make textbox and input work the same. See jsFiddle.

.textarea, .textbox {
    width: 200px;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box; 
}



回答6:


Yes, there is. Try doing something like this:

<textarea style="width:80%"> </textarea>
<input type="text" style="width:80%" />

Both should equate to the same size. You can do it with absolute sizes (px), relative sizes (em) or percentage sizes.




回答7:


Just a note - the width is always influenced by something happening on the client side - PHP can't effect those sorts of things.

Setting a common class on the elements and setting a width in CSS is your cleanest bet.




回答8:


Use a CSS class for width, then place your elements into HTML DIVs, DIVs having the mentioned class.

This way, the layout control overall should be better.




回答9:


As mentioned in Unequal Html textbox and dropdown width with XHTML 1.0 strict it also depends on your doctype. I have noticed that when using XHTML 1.0 strict, the difference in width does indeed appear.




回答10:


input[type="text"] { width: 60%; } 
input[type="email"] { width: 60%; }
textarea { width: 60%; }
textarea { height: 40%; }



回答11:


These days, if you use bootstrap, simply give your input fields the form-control class. Something like:

<label for="display-name">Display name</label>
<input type="text" class="form-control" name="displayname" placeholder="">

<label for="about-me">About me</label>
<textarea class="form-control" rows="5" id="about-me" name="aboutMe"></textarea>



回答12:


you can also use the following CSS:

.mywidth{
width:100px;
}
textarea{
width:100px;
}

HTML:

<input class="mywidth" >
<textarea></textarea>


来源:https://stackoverflow.com/questions/28713/is-there-a-simple-way-to-make-html-textarea-and-input-type-text-equally-wide

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!