I have an input field on a page that looks something like this:
<input type="text" size="20" name="whatever" />
Imagine that I wanted to keep the size attribute and not use CSS instead, and that I wanted to now add a select box to the same form of the same width:
<select name="whatever2">
<option>an option</option>
</select>
What attribute should I add to get the select box to match the width of the input field?
I initially thought ‘size’ but no – that’s the number of options to display, second thought was ‘style=”width:20em”‘ but it’s bigger…
From the spec:
So
sizefor text and password fields represents the number of characters – it’s not used to define the pixel width. Depending on the font used to style the input field, the pixel width of the field may differ.For this field:
The browser will by default render it wide enough to fit 25 characters.
Now, you could theoretically use the above information to determine what
widthto give it in CSS, but I haven’t bothered with that myself yet. Besides, as oft-noted, how form elements are rendered on pages varies widely from browser to browser, so it may not be a reliable way to size your fields.Remember that setting a
widthusing CSS always overrides thesizeattribute.