{"id":21545,"date":"2013-05-06T08:21:57","date_gmt":"2013-05-06T15:21:57","guid":{"rendered":"http:\/\/css-tricks.com\/?page_id=21545"},"modified":"2015-05-20T12:16:23","modified_gmt":"2015-05-20T19:16:23","slug":"font-size","status":"publish","type":"page","link":"https:\/\/css-tricks.com\/almanac\/properties\/f\/font-size\/","title":{"rendered":"font-size"},"content":{"rendered":"
The Length values<\/a> (e.g. px, em, rem, ex, etc) which are applied to It accepts the following absolute keyword values:<\/p>\n These absolute values are mapped to specific font sizes as computed by the browser. But you may also use two keyword values which are relative to the font size of the parent element.<\/p>\n Other absolute values include For example, if the parent element has a font size of Percentage values, such as setting a font-size of 110%, are also relative to the parent element\u2019s font size as shown in the demo below:<\/p>\n See the Pen qdbELL<\/a> by CSS-Tricks (@css-tricks<\/a>) on CodePen<\/a>.<\/p>\n The em unit is a relative unit based on the computed value of the font size of the parent element. This means that child elements are always dependent on their parent to set their font-size. For example:<\/p>\n In the example above, the paragraph will have a font-size of 16px because 1 x 16 = 16px whereas the heading will be 32px because 2 x 16 = 32px. There are many advantages to scaling type up depending on the font-size of the parent element, namely we can wrap elements within a container and know that all of the children will always be relative to one another:<\/p>\n See the Pen Figuring out how the em unit works<\/a> by CSS-Tricks (@css-tricks<\/a>) on CodePen<\/a>.<\/p>\n In the case of rem units, however, the font-size is dependent on the value of the root element (or the In the above example, the rem unit is equal to 16px (because it is inherited from the This unit is supported by the following browsers:<\/p>\nfont-size<\/code> property specifies the size, or height, of the font.
font-size<\/code> affects not only the font to which it is applied, but is also used to compute the value of em, rem, and ex length units.<\/p>\n
p {\r\n font-size: 20px;\r\n}<\/code><\/pre>\n
font-size<\/code> can accept keywords, length units, or percentages as values. It\u2019s important to note however that when it\u2019s declared as part of the
font<\/a><\/code> shorthand property,
font-size<\/code> is a mandatory value. If it is not included in the shorthand, the entire line is ignored.<\/p>\n
font-size<\/code> cannot be negative.<\/p>\n
Absolute keywords and values<\/h3>\n
.element {\r\n font-size: small;\r\n}<\/code><\/pre>\n
\n
xx-small<\/code><\/li>\n
x-small<\/code><\/li>\n
small<\/code><\/li>\n
medium<\/code><\/li>\n
large<\/code><\/li>\n
x-large<\/code><\/li>\n
xx-large<\/code><\/li>\n<\/ul>\n
mm<\/code> (millimeters),
cm<\/code> (centimeters),
in<\/code> (inches),
pt<\/code> (points) and
pc<\/code> (picas). One point is equal to 1\/72 of an inch whilst one pica is equal to 12 points \u2014 these values are typically used for printed documents.<\/p>\n
Relative keywords<\/h3>\n
.element {\r\n font-size: larger;\r\n}<\/code><\/pre>\n
\n
larger<\/code><\/li>\n
smaller<\/code><\/li>\n<\/ul>\n
small<\/code>, a child element with a defined relative size of
larger<\/code> will make the font size equal to
medium<\/code> for the child element. <\/p>\n
Percentage values<\/h3>\n
.element {\r\n font-size: 110%;\r\n}<\/code><\/pre>\n
The em unit<\/h3>\n
.element {\r\n font-size: 2em;\r\n}<\/code><\/pre>\n
<div class=\"container\">\r\n <h2>This is a heading<\/h2>\r\n <p>This is some text.<\/p>\r\n<\/div><\/code><\/pre>\n
.container {\r\n font-size: 16px;\r\n}\r\n\r\np {\r\n font-size: 1em;\r\n}\r\n\r\nh2 {\r\n font-size: 2em;\r\n}<\/code><\/pre>\n
The rem unit<\/h3>\n
html<\/code> element).<\/p>\n
html {\r\n font-size: 16px;\r\n}\r\n\r\np {\r\n font-size: 1.5rem;\r\n}<\/code><\/pre>\n
html<\/code>\/root element) and thus the font size for all paragraph elements will compute to 24px (1.5 x 16 = 24). Unlike em units, the paragraph will ignore the styling of all its parents besides the root.<\/p>\n