"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Input Size vs Width: Which is Best for Cross-Browser Compatibility?

Input Size vs Width: Which is Best for Cross-Browser Compatibility?

Published on 2024-11-12
Browse:135

Input Size vs Width: Which is Best for Cross-Browser Compatibility?

Input size vs width

When specifying the size of an input field, you have two options: use the size attribute or the width CSS property. But which one is the best cross-browser code?

The answer is: 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.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3