Font-family is not inherited to the form input fields?
Solution 1
Yes, you need to place the font
in the input
tag.
input{
padding:5px;
font-size:16px;
font-family:'Lucida Casual', 'Comic Sans MS';
}
http://jsfiddle.net/jasongennaro/3fkNJ/1/
You can also use inherit
to set the font
on form
fields.
input, textarea, select { font-family:inherit; }
http://jsfiddle.net/jasongennaro/3fkNJ/7/
EDIT - explanation added
Most browsers render text inside form
elements as that of the user's operating system. This is to keep, as I understand it, a common look and feel for the user. However, it can all be overwritten by the code above.
Solution 2
there is nothing wrong with the code.. This is common as the input field takes the OS theme settings by default.. This has already been discussed in stackoverflow. Look into the below link for more details.
Why <textarea> and <textfield> not taking font-family and font-size from body?
Solution 3
Try changing your CSS body attribute to
body *{font-family:'Lucida Casual', 'Comic Sans MS';}
The * forces every child element to inherit the specified value within the CSS rule you have written because of the CSS rules over specification. See the fiddle here
This is handy if you want every element on your page to have the same font-family value, not so handy if you do want your forms to have different value.
Smashing Magazine has an article that may help you further.
I hope it helps.
Related videos on Youtube
sunjie
Updated on April 20, 2020Comments
-
sunjie about 4 years
Don't the html form input elements such as input text field or select box automatically inherit the font-family property from the body? For example:
body { font-family:'Lucida Casual', 'Comic Sans MS'; }
It will not use the above font in the below form input field:
<form> <div> <label for="name">Name</label> <input id="name" name="name" type="text" /> <div> </form>
Please have a look at http://jsfiddle.net/3fkNJ/
Is it common that we have to re-define the font family for the input fields, or am i doing something wrong?
-
J. M. Becker about 12 yearsThis will work, but it's sloppy practice. Performance takes a hit from the
*
selector, and the*
will take precedence in ways you might not implicitly expect. -
Ryan about 12 yearsIt doesn't make it "sloppy practice", it just means when being used the person needs to understand what it is they are doing hence the Smashing Magazine reference. As far as performance, yes it uses greater browser overhead but if it is appropriate depends on the objective, should every element have the same font regardless or jus a few elements? If it's just a few elements then specifying in the selector is better. To me sunjie is clear that he wants all elements to have the same font regardless so my solution would be appropriate to enforce this, but yes, it will have its tolls elsewhere.
-
J. M. Becker about 12 yearsI think you misunderstood, exactly what I claimed was "sloppy practice". While it would be preferable to avoid extraneous
*
selectors, just using one is not necessarily 'sloppy practice'. On the other hand, using your CSS to avoid inheritance idiosyncrasies IS "sloppy practice". Nowhere did the author state he wants universally precede selectors with a specified font-family. -
Ryan about 12 yearsWhere the author didn't specifically say he wants everything to be inherited it is implied in the first line of the question, as it is implied it is an appropriate solution. The reference has been supplied so depending on his needs he has the choice. I appreciate your comment
-
Ashraf Sabry over 9 yearsThanks for your answer. I think your solution won't hurt performance. A lot of articles recommend using
*, *:before, *:after { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; }
so, we have some universal selectors anyway. -
Manas Paldhe over 9 yearsThis does not work in case of jquery-autocomplete. Do you have a clue why?
-
Jason Gennaro over 9 years@ManasPaldhe it's probably because autocomplete has a more specific css selector. I checked jqueryui.com/autocomplete and it shows the
input
has anid
of "tags". Set the font in thatid
and you should be good.