<input name="txtId" type="text" size="20" />
or
<input name="txtId" type="text" style="width: 150px;" />
Which one is optimal cross-browser code?
Of course it depends on requirements, but I'm curious to know how people decide and on what basis.
Definition and UsageThe width attribute specifies the width of the <input> element. Note: The width attribute is used only with <input type="image"> . Tip: Always specify both the height and width attributes for images. If height and width are set, the space required for the image is reserved when the page is loaded.
This can be done by using the size attribute or by width attribute to set the width of an element. The size attribute works with the following input types: text, search, tel, url, email, and password and not on type date, image, time for these we can use width attribute. Create a div in which we are putting our input.
If you simply want to specify the height and font size, use CSS or style attributes, e.g. //in your CSS file or <style> tag #textboxid { height:200px; font-size:14pt; } <!
You can use both. The css style will override the size
attribute in browsers that support CSS and make the field the correct width, and for those that don't, it will fall back to the specified number of characters.
Edit: I should have mentioned that the size
attribute isn't a precise method of sizing: according to the HTML specification, it should refer to the number of characters of the current font the input will be able to display at once.
However, unless the font specified is a fixed-width/monospace font, this is not a guarantee that the specified number of characters will actually be visible; in most fonts, different characters will be different widths. This question has some good answers relating to this issue.
The snippet below demonstrates both approaches.
@font-face { font-family: 'Diplomata'; font-style: normal; font-weight: 400; src: local('Diplomata'), local('Diplomata-Regular'), url(https://fonts.gstatic.com/s/diplomata/v8/8UgOK_RUxkBbV-q561I6kFtXRa8TVwTICgirnJhmVJw.woff2) format('woff2'); unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2212, U+2215; } @font-face { font-family: 'Open Sans Condensed'; font-style: normal; font-weight: 300; src: local('Open Sans Condensed Light'), local('OpenSansCondensed-Light'), url(https://fonts.gstatic.com/s/opensanscondensed/v11/gk5FxslNkTTHtojXrkp-xBEur64QvLD-0IbiAdTUNXE.woff2) format('woff2'); unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2212, U+2215; } p { margin: 0 0 10px 0; } input { font-size: 20px; } .narrow-font { font-family: 'Open Sans Condensed', sans-serif; } .wide-font { font-family: 'Diplomata', cursive; } .set-width { width: 220px; }
<p> <input type="text" size="10" class="narrow-font" value="0123456789" /> </p> <p> <input type="text" size="10" class="wide-font" value="0123456789" /> </p> <p> <input type="text" size="10" class="narrow-font set-width" value="0123456789" /> </p> <p> <input type="text" size="10" class="wide-font set-width" value="0123456789" /> </p>
I suggest, probably best way is to set style's width in em unit :) So for input size of 20 characters just set style='width:20em'
:)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With