Font-family is not inherited to the form input fields?

56,798

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.

Share:
56,798

Related videos on Youtube

sunjie
Author by

sunjie

Updated on April 20, 2020

Comments

  • sunjie
    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
    J. M. Becker about 12 years
    This 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
    Ryan about 12 years
    It 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
    J. M. Becker about 12 years
    I 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
    Ryan about 12 years
    Where 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
    Ashraf Sabry over 9 years
    Thanks 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
    Manas Paldhe over 9 years
    This does not work in case of jquery-autocomplete. Do you have a clue why?
  • Jason Gennaro
    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 an id of "tags". Set the font in that id and you should be good.