I have a Microsoft Word document which uses a 10pt Arial font. I have re-created this document with HTML/CSS, specifying font: normal 10pt Arial;
in my stylesheet. When I print from the web page (from Chrome), the text appears significantly smaller.
I have my text wrapped in a 800px-wide container on my website. Might this be a cause? Besides this, to be honest I’m completely out of ideas & any help would be greatly appreciated.
phwd
20k5 gold badges49 silver badges78 bronze badges
asked Aug 31, 2011 at 0:04
0
It could be because you are specifying font sizes in pts
. pts
are pretty much inconsistent across platforms and do not scale. (px
don’t scale either) Try using em
.
See Also: http://www.w3.org/QA/Tips/font-size, CSS Font-Size em vs. px vs. pt vs. percent
(You will have to provide a sample of your HTML to get a more concrete answer)
answered Aug 31, 2011 at 4:27
NivasNivas
18.1k4 gold badges62 silver badges76 bronze badges
1
Have you tried converting your font-size to pixels or ems? I believe 10pt would roughly translate to 13px.
answered Aug 31, 2011 at 4:22
CharlesCharles
3232 silver badges11 bronze badges
The font-size
CSS property sets the size of the font. Changing the font size also updates the sizes of the font size-relative <length>
units, such as em
, ex
, and so forth.
Try it
Syntax
/* <absolute-size> values */
font-size: xx-small;
font-size: x-small;
font-size: small;
font-size: medium;
font-size: large;
font-size: x-large;
font-size: xx-large;
font-size: xxx-large;
/* <relative-size> values */
font-size: smaller;
font-size: larger;
/* <length> values */
font-size: 12px;
font-size: 0.8em;
/* <percentage> values */
font-size: 80%;
/* math value */
font-size: math;
/* Global values */
font-size: inherit;
font-size: initial;
font-size: revert;
font-size: revert-layer;
font-size: unset;
The font-size
property is specified in one of the following ways:
- As one of the absolute-size, relative-size or
math
keywords - As a
<length>
or a<percentage>
, relative to the element’s font size.
Values
xx-small
,x-small
,small
,medium
,large
,x-large
,xx-large
,xxx-large
-
Absolute-size keywords, based on the user’s default font size (which is
medium
). larger
,smaller
-
Relative-size keywords. The font will be larger or smaller relative to the parent element’s font size, roughly by the ratio used to separate the absolute-size keywords above.
<length>
-
A positive
<length>
value. For most font-relative units (such asem
andex
), the font size is relative to the parent element’s font size.For font-relative units that are root-based (such as
rem
), the font size is relative to the size of the font used by the<html>
(root) element. <percentage>
-
A positive
<percentage>
value, relative to the parent element’s font size.
Note: To maximize accessibility, it is generally best to use values that are relative to the user’s default font size.
-
math
Experimental
Special mathematical scaling rules must be applied when determining the computed value of thefont-size
property.
Description
There are several ways to specify the font size, including keywords or numerical values for pixels or ems. Choose the appropriate method based on the needs of the particular web page.
Keywords
Keywords are a good way to set the size of fonts on the web. By setting a keyword font size on the <body>
element, you can set relative font-sizing everywhere else on the page, giving you the ability to easily scale the font up or down on the entire page accordingly.
Pixels
Setting the font size in pixel values (px
) is a good choice when you need pixel accuracy. A px value is static. This is an OS-independent and cross-browser way of literally telling the browsers to render the letters at exactly the number of pixels in height that you specified. The results may vary slightly across browsers, as they may use different algorithms to achieve a similar effect.
Font sizing settings can also be used in combination. For example, if a parent element is set to 16px
and its child element is set to larger
, the child element displays larger than the parent element on the page.
Note: Defining font sizes in px
is not accessible, because the user cannot change the font size in some browsers. For example, users with limited vision may wish to set the font size much larger than the size chosen by a web designer. Avoid using them for font sizes if you wish to create an inclusive design.
Ems
Using an em
value creates a dynamic or computed font size (historically the em
unit was derived from the width of a capital «M» in a given typeface.). The numeric value acts as a multiplier of the font-size
property of the element on which it is used. Consider this example:
In this case, the font size of <p>
elements will be double the computed font-size
inherited by <p>
elements. By extension, a font-size
of 1em
equals the computed font-size
of the element on which it is used.
If a font-size
has not been set on any of the <p>
‘s ancestors, then 1em
will equal the default browser font-size
, which is usually 16px
. So, by default 1em
is equivalent to 16px
, and 2em
is equivalent to 32px
. If you were to set a font-size
of 20px on the <body>
element say, then 1em
on the <p>
elements would instead be equivalent to 20px
, and 2em
would be equivalent to 40px
.
In order to calculate the em
equivalent for any pixel value required, you can use this formula:
em = desired element pixel value / parent element font-size in pixels
For example, suppose the font-size
of the <body>
of the page is set to 16px
. If the font-size you want is 12px
, then you should specify 0.75em
(because 12/16 = 0.75). Similarly, if you want a font size of 10px
, then specify 0.625em
(10/16 = 0.625); for 22px
, specify 1.375em
(22/16).
The em
is a very useful unit in CSS since it automatically adapts its length relative to the font that the reader chooses to use.
One important fact to keep in mind: em values compound. Take the following HTML and CSS:
html {
font-size: 100%;
}
span {
font-size: 1.6em;
}
<div>
<span>Outer <span>inner</span> outer</span>
</div>
The result is:
Assuming that the browser’s default font-size
is 16px, the words «outer» would be rendered at 25.6px, but the word «inner» would be rendered at 40.96px. This is because the inner <span>
‘s font-size
is 1.6em which is relative to its parent’s font-size
, which is in turn relative to its parent’s font-size
. This is often called compounding.
Rems
rem
values were invented in order to sidestep the compounding problem. rem
values are relative to the root html
element, not the parent element. In other words, it lets you specify a font size in a relative fashion without being affected by the size of the parent, thereby eliminating compounding.
The CSS below is nearly identical to the previous example. The only exception is that the unit has been changed to rem
.
html {
font-size: 100%;
}
span {
font-size: 1.6rem;
}
Then we apply this CSS to the same HTML, which looks like this:
<span>Outer <span>inner</span> outer</span>
In this example, the words «outer inner outer» are all displayed at 25.6px (assuming that the browser’s font-size
has been left at the default value of 16px).
Ex
Like the em
unit, an element’s font-size
set using the ex
unit is computed or dynamic. It behaves in exactly the same way, except that when setting the font-size
property using ex
units, the font-size
equals the x-height of the first available font used on the page. The number value multiplies the element’s inherited font-size
and the font-size
compounds relatively.
See the W3C Editor’s Draft for a more detailed description of font-relative length units such as ex
.
Formal definition
Initial value | medium |
---|---|
Applies to | all elements. It also applies to ::first-letter and ::first-line . |
Inherited | yes |
Percentages | refer to the parent element’s font size |
Computed value | as specified, but with relative lengths converted into absolute lengths |
Animation type | a length |
Formal syntax
Examples
Setting font sizes
CSS
.small {
font-size: xx-small;
}
.larger {
font-size: larger;
}
.point {
font-size: 24pt;
}
.percent {
font-size: 200%;
}
HTML
<h1 class="small">Small H1</h1>
<h1 class="larger">Larger H1</h1>
<h1 class="point">24 point H1</h1>
<h1 class="percent">200% H1</h1>
Result
Specifications
Specification |
---|
CSS Fonts Module Level 4 # font-size-prop |
Browser compatibility
BCD tables only load in the browser
See also
When you want to draw attention to a piece of text on a page, few things are more effective than increasing the font size. And to do that, you’ll need to understand the CSS font-size property.
The size of your fonts draws the reader’s eye and establish a visual hierarchy. You’ll notice in this blog post that the title (<h1>) has the largest font size. Next, the heading elements (<h2>, <h3>, and <h4>) decrease in size until we reach the <p> elements beneath them.
Say you want to shrink or enlarge the default heading sizes, or you want to change the font size of other elements on the page. In that case, you can change the font size in CSS.
In this post, we’ll show you how to use the CSS font-size property, including:
- using an absolute-size unit
- using a relative-size unit
- using length value (like pixels, rems, or rems)
- using a percentage value
- using vw units to make font size responsive
font-size is the CSS property that controls the size of text on a webpage. There are several values you can use to define the font-size property. The example below includes different values and units you can use in CSS. The one you choose will depend on the needs and goals of your site.
See the Pen font-size: different values by HubSpot (@hubspot) on CodePen.
Let’s take a closer look at these values and discuss the pros and cons of each.
Absolute-size keyword
element { font-size: small; }
Absolute-size keywords are based on the default font size. Most commonly, the default font size is medium (which translates to 16 pixels or 1 em) though it can differ by browser and font family. Note that in HTML the default font size is 16 pixels.
The absolute-size keywords are:
- xx-small
- x-small
- small
- medium
- large
- x-large
- xx-large
- xxx-large
Here’s how each one looks in a browser:
See the Pen font-size: absolute size by HubSpot (@hubspot) on CodePen.
Absolute-size keywords make it easy to set text to a specified size and create a font hierarchy for your page.
However, they do not allow a user to change the text size in all browsers, which makes it a poor choice for accessibility. To be more inclusive of all users, try relative-size keywords instead.
Relative-size keyword
element { font-size: larger; }
Relative-size keywords set the font larger or smaller relative to the parent element’s font size. Relative sizes are roughly based on the ratio of the absolute-size keywords described above.
So, if a parent element has a font-size of large, a child element with a font-size of smaller will look as if it’s font size were medium. Let’s look at the code of this hypothetical.
See the Pen font-size: relative size by HubSpot (@hubspot) on CodePen.
Relative-size keywords make it easy to set text size relative to surrounding elements. Their advantage over absolute-size keywords is that they allow users to change the text size in all browsers, which makes it a good choice for accessibility.
Length
There are a few length values you can assign to the font-size property. Here, we’ll focus on the three most common: pixels, em units, and rem units.
Pixels
element { font-size: 32px; }
Using pixels (px) as your length lets you set fontsize with precision, regardless of the browser a visitor is using. It specifies exactly the number of pixels in height that you want a browser to render your text.
See the Pen font-size: px by HubSpot (@hubspot) on CodePen.
However, the fixed nature of pixels is also a drawback. They’re not optimized for all devices — CSS-Tricks found that websites on the iPad mini render the same as websites on the iPad, for example — and they’re not an accessible length value.
Because users cannot change the font size in some browsers, there are more inclusive and responsive options you can use, like em and rem. We’ll discuss those next.
Ems
element { font-size: 2em; }
The em unit sets the font size relative to the font size of the parent element. So, giving text a font-size of 2em will make this text twice the size of its surrounding text.
Setting font size in em units is ideal for an inclusive design. Since ems are a relative unit, users can adjust the text size in all browsers.
The only drawback is that ems compound. So, say a <span> element with a font size of 2em contains another <span> element. The nested <span> element would be twice the size, or 4em. See the code below.
See the Pen font-size: em by HubSpot (@hubspot) on CodePen.
Fortunately, the rem unit solves this compounding problem.
Rems
element { font-size: 2rem; }
Rems are a relative unit like ems, but they don’t compound. That’s because ems are font-relative units, meaning the font size is relative to the parent element’s font size, while rems are root-based. This means that font size is relative to the size of the font used by the <html> element, also called the root element.
Say I set the font size of the root element to 12px so that any text in the document that isn’t modified by CSS will be 12 pixels. But, I also want to change the font size of a <div> element that’s similar to the one mentioned in the example above. Let’s look at the code for that below.
See the Pen font-size: rem by HubSpot (@hubspot) on CodePen.
Notice how the nested <span> element is the same font size as the other <span> element.
For more guidance on when to use the px unit versus the rem unit, check out this helpful explainer video:
Percentage
element { font-size: 110%; }
A percentage value sets the font size of an element relative to the parent element’s font size.
Say a <div> element that’s set to 36px contains two <p> elements. The font-size of the first paragraph is set to 50% and the second paragraph is set to 200%. The first <p> element will be 18 pixels and the second will be 27 pixels. Here’s how that code looks in action:
See the Pen font-size: percentage by HubSpot (@hubspot) on CodePen.
Responsive Text
The property values described above all have one thing in common: They are not responsive. If you’d like to set your font size to be responsive for all devices and displays, you can use the viewport width unit, shortened to vw.
element { font-size: 10vw; }
The vw unit is another relative unit. However, it’s not relative to a parent element or root element, but to the width of the viewport. To be exact, 1 vw is equal to 1% of the viewport. Meaning, if the viewport is 100 pixels wide, 1vw equals 1 pixel.
Say I want the font size of my paragraph to be 10% of the width of the browser window. Here’s what the code looks like:
See the Pen font-size: vw by HubSpot (@hubspot) on CodePen.
And here’s what the text looks like when we resize the viewport:
Max Font Size in CSS
When setting your font size in vm units, be careful that your text doesn’t get too big on large screens. Unfortunately, CSS doesn’t have “max font size” property, but you can prevent your font from getting too big by using media queries.
You just have to use the media query at a certain screen size breakpoint and force the font size back into a set pixel value. Say I want to force my font size back to 30px when the viewport exceeds 1000px. Here’s what the code looks like:
See the Pen font-size: vw with media query by HubSpot (@hubspot) on CodePen.
And here’s what the text looks like when we resize the viewport:
Controlling Font Size
Changing font size in CSS is complex when compared to the ease of changing font size in Google Docs or Microsoft Word — but it can be mastered with some practice in HTML and CSS.
Editor’s note: This post was originally published in May 2020 and has been updated for comprehensiveness.
When you add text to your HTML file with an HTML tag, you won’t always want the text to remain the default size. You’ll want to be able to adjust how the text displays in the browser.
In this article, you will learn how to change the text size with an HTML tag.
Before you proceed, it is essential to know that there is only one way we can do this: through CSS’s font-size
property. We can use the font-size
property through inline, internal, or external styling.
In the past, we could adjust text size within our HTML tag without using CSS. But that was before HTML5. Then we added text using the <font>
tag, which can take in an attribute of size as seen below:
<font size="5">
Hello World
</font>
This size attribute can take in value from 1-7 in which the text size increases from 1 to 7. But like I said, this has long been depreciated, and most people don’t even know it existed.
In case you are in a rush to see how you can change the size of your text, then here it is:
// Using inline CSS
<h1 style="font-size: value;"> Hello World! </h1>
// Using internal/external CSS
selector {
font-size: value;
}
Suppose you are not in a rush. Let’s briefly dive right in.
How to Change Text Size With Inline CSS
Inline CSS allows you to apply styles to specific HTML elements. This means we are putting CSS into an HTML tag directly. We use the style attribute, which now holds all our styling.
<h1 style="...">Hello World!</h1>
We use the font-size
property alongside our value to change the text size using inline CSS. This value can use any of your preferred CSS units such as em, px, rem, and so on.
<h1 style="font-size:4em; "> Hello World! </h1>
<p style="font-size:14px; "> Any text whose font we want to change </p>
A perfect syntax would be:
<TEXT-TAG style="font-size:value;"> ... </TEXT-TAG>
How to Change Text Size With Internal or External CSS
The approach you use to change text size in internal and external CSS styling is similar, since you use a selector. The general syntax for this is:
selector {
font-size: value;
}
The selector can either be our HTML tag or maybe a class or an ID. For example:
// HTML
<p> Any text whose font we want to change </p>
// CSS
p {
font-size: 14px;
}
Or we could use a class:
// HTML
<p class="my-paragraph" > Any text whose font we want to change </p>
// CSS
.my-paragraph {
font-size: 14px;
}
Wrapping Up
In this article, you learned how to change the font/text size of an HTML element using CSS. You also saw how developers did it before the introduction of HTML5.
Also, keep in mind that it’s always better to style your HTML elements using internal or external styling, as it offers a lot of flexibility compared to inline styling.
For example, you can make use of one CSS class for all your p tags rather than having to add inline styles to all your p tag elements.
Using inline styles is not considered best practice because it results in a lot of repetition – you cannot reuse the styles elsewhere. To learn more, you can read my article on Inline Style in HTML.
I hope this tutorial gives you the knowledge to change the size of your HTML text so you can make it look better.
Have fun coding!
Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started
The font-size property defines the font size of the text.
Font size can be defined by the following ways:
- absolute-size
- relative-size
- length
- percentage
Absolute font size includes the following values:
- xx-small
- x-small
- small
- small
- x-large
- xx-large
Relative font size includes the following values:
- smaller
- larger
Lengths can be relative lengths (em, ex, px) and absolute lengths (in, cm, mm, pt, pc). Percentages specify an absolute font size relative to the font size of the parent element.
font-size: medium | xx-small | x-small | small | large | x-large | xx-large | smaller | larger | length | initial | inherit;
Example of the font-size property:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
h1 {
font-size: 24pt;
}
h3 {
font-size: 26px;
}
p {
font-size: 1em;
}
a {
font-size: 100%;
}
span {
font-size: x-small;
}
</style>
</head>
<body>
<span>This span is written with x-small value.</span>
<p>This paragraph is written with 1em font-size.</p>
<a href="https://www.w3docs.com/">This hyperlink is written with 100% font-size.</a>
<h3>We used x-small font size for this heading.</h3>
<h1>We set the font size 24pt for this heading.</h1>
</body>
</html>
Result
Percentage values are relative to the font-size of the parent element. The code below shows its usage:
Example of the font-size property specified in percentage:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
h3 {
font-size: 125%;
}
</style>
</head>
<body>
<h3>We used x-small font size for this heading.</h3>
<span>This span is written with x-small value.</span>
<p>This paragraph is written with 1em font-size.</p>
</body>
</html>
The em unit is considered to be a relative unit. It is based on the calculated value of its parent element’s font size. In the code below, the paragraph will be 32px, because 2×16=32, and the heading will have a font-size of 48px because 3×16=48px. This method is very useful because we can be sure that all the child elements will always be relative to each other.
Example of the font-size property with the «em» value:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
.container {
font-size: 16px;
}
p {
font-size: 2em;
}
h2 {
font-size: 3em;
}
</style>
</head>
<body>
<div class="container">
<h2>Here is the heading</h2>
<p>Here is the text.</p>
</div>
</body>
</html>
In the case of using the rem unit, the font-size depends on the value of the HTML element. In the example below, the rem unit is inherited from the HTML element, that’s why it is equal to 24px. So, the heading will have a font-size of 24px, because 1.5×16=24px.
Example of the font-size property with the «rem» value:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
html {
font-size: 16px;
}
h2 {
font-size: 1.5rem;
}
</style>
</head>
<body>
<div class="container">
<h2>Here is the heading</h2>
<p>Here is the text.</p>
</div>
</body>
</html>
In the case of the ex unit, 1ex is equal to the calculated height of the HTML element’s letter x. In the code example below, the HTML element is 15px. The x-height of that particular font will determine all the other font-sizes.
.exunit {
font-size: 15ex;
}
Viewport units (vw and vh) are used for setting the font-size of an element, which is relative to the size of the viewport.
- 1vw = 1% of viewport width
- 1vh = 1% of viewport height
.viewport {
font-size: 120vh;
}
Example of the font-size property with the «length» value:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
span {
color: green;
font-size: 2vh;
}
p {
color: red;
font-size: 1em;
}
.length {
color: orange;
font-size: 30px;
}
h3 {
color: lightblue;
font-size: 3ex;
}
h1 {
color: purple;
font-size: 24pt;
}
a {
color: blue;
font-size: 120%;
}
</style>
</head>
<body>
<h2>Font-size property</h2>
<span>This text is written with 2vh font-size.</span>
<p>This paragraph is written with 1em font-size.</p>
<div class="length">Example with 30px font-size length </div>
<h3>Example with 3ex font-size length.</h3>
<h1>We set the font size 24pt for this heading.</h1>
<a href="https://www.w3docs.com/">This hyperlink is written with 100% font-size.</a>
</body>
</html>
Example of the font-size property with the absolute-size values:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
.font-xxsmall {
color: grey;
font-size: xx-small;
}
.font-xsmall {
color: grey;
font-size: x-small;
}
.font-small {
color: grey;
font-size: small;
}
.fontSize-medium {
color: grey;
font-size: medium;
}
.font-large {
color: grey;
font-size: large;
}
.font-xlarge {
color: grey;
font-size: x-large;
}
.font-xxlarge {
color: grey;
font-size: xx-large;
}
</style>
</head>
<body>
<h1>Font-size property</h1>
<div class="font-xxsmall">Example with font-size xx-small property</div>
<div class="font-xsmall">Example with font-size x-small property</div>
<div class="font-small">Example with font-size small property</div>
<div class="font-medium">Example with font-size medium property</div>
<div class="font-large">Example with font-size large property</div>
<div class="font-xlarge">Example with font-size x-large property</div>
<div class="font-xxlarge">Example with font-size xx-large property</div>
</body>
</html>
Example of the font-size property with «smaller» and
«larger» values:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
.smaller {
color: red;
font-size: smaller;
}
.larger {
color: red;
font-size: larger;
}
</style>
</head>
<body>
<h1>font-size property</h1>
<div class="smaller">Example with font-size smaller property</div>
<div class="larger">Example with font-size larger property</div>
</body>
</html>