Skip to main content

How to Make Buttons and Text Boxes Use the Page's Font with font: inherit in CSS

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

Text boxes and buttons don't use the page's font: Chromium draws them in its own 13.33-pixel text, whatever you set on body. One rule, font: inherit, makes them take the font of the element they sit in.

Syntax​

Syntax
<form controls> {
font: inherit;
}
PartWhat it does
<form controls>The elements that should use the page's font, as a selector list: button, input, select and textarea.
fontThe shorthand that sets every font property at once: the family, the size, the weight and the rest.
inheritTakes each value from the parent element.

MDN's font page explains the shorthand, and its guide to styling web forms notes that some form controls don't inherit font-family and font-size from their parents by default.

The Example​

The example is a search box and a button under a line of text, on a page set to 18-pixel Arial. First you see the box and the button keep their own small text, then one rule makes them match.

Step 1: Write the Code​

Create a file named index.html in a text editor, such as Visual Studio Code:

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>font: inherit</title>
<style>
body {
font-family: Arial, Helvetica, sans-serif;
font-size: 18px;
}
</style>
</head>
<body>
<p>Search the menu:</p>
<p><input value="Coffee"> <button type="button">Search</button></p>
</body>
</html>

The body rule sets the font for the page: Arial, at 18 pixels.

Step 2: Open It in a Browser​

Double-click index.html. It opens in your browser:

The page in Chromium: the line Search the menu: in 18-pixel Arial, and below it a text box holding Coffee and a Search button, both in visibly smaller text.

The text in the box and on the button is smaller than the line above it: Chromium gave them 13.33 pixels, while the paragraph has 18. The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.

Step 3: Add font: inherit​

Add a rule for button and input to the style element, so the file looks like this:

index.html, the same page with a second rule in its style element, for button and input, that sets font to inherit.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>font: inherit</title>
<style>
body {
font-family: Arial, Helvetica, sans-serif;
font-size: 18px;
}

button,
input {
font: inherit;
}
</style>
</head>
<body>
<p>Search the menu:</p>
<p><input value="Coffee"> <button type="button">Search</button></p>
</body>
</html>

Step 4: Reload the Page​

Save the file and reload the page in your browser:

The page in Chromium: the same page with the text box and the Search button now in the same 18-pixel text as the line above.

What Happened​

  • Before the rule, the box and the button were 13.33 pixels, Chromium's own size for form controls, although the page was set to 18.
  • After it, both are 18 pixels in the page's Arial stack. font: inherit took every font value from the paragraph they sit in, which got its font from body.
  • The box and the button grew to fit the bigger text.

Common Mistakes​

MistakeWhat happensDo this instead
Using only font-family: inheritOnly the family follows the page: the text box stayed 13.33 pixelsUse font: inherit, which covers the size too
Leaving out select and textareaThey keep their own fonts too: Chromium gave a textarea 13.33-pixel monospace text, and a select 13.33 pixelsList all four: button, input, select, textarea

Troubleshooting​

SymptomLikely causeFix
Form controls have smaller text than the pageThey don't inherit the font by defaultAdd font: inherit to them
A text area is in a typewriter-like fontChromium draws text areas in monospaceInclude textarea in the rule

FAQ​

Does it work for text areas and drop-down lists too?​

Yes. With button, input, select, textarea { font: inherit; }, a textarea and a select both came out in the page's 18-pixel Arial.

Why font and not font-family?​

font: inherit brings the family, the size and the weight in one line. font-family: inherit on its own changed only the family, and left the text box at 13.33 pixels.

Conclusion​

Form controls keep their own font unless you tell them otherwise. One rule, button, input, select, textarea { font: inherit; }, makes them follow the page.

Additional Resources​