- CSS font-size-adjust Property
- Browser Support
- Syntax
- Property Values
- CSS font-size-adjust : How to auto-adjust your font size
- How CSS font-size-adjust works
- Browser compatibility for font-size-adjust
- The CSS font-size property
- The CSS font-size-adjust property
- Over 200k developers use LogRocket to create better digital experiences
- Conclusion
- Is your frontend hogging your users’ CPU?
- How To Auto Adjust Your Font Size With CSS font-size-adjust?
- CSS font-size Property
- CSS font-size-adjust Property
- How CSS font-size-adjust Works?
CSS font-size-adjust Property
The font-size-adjust property gives you better control of the font size when the first selected font is not available.
When a font is not available, the browser uses the second specified font. This could result in a big change for the font size. To prevent this, use the font-size-adjust property.
All fonts have an «aspect value» which is the size-difference between the lowercase letter «x» and the uppercase letter «X».
When the browser knows the «aspect value» for the first selected font, the browser can figure out what font-size to use when displaying text with the second choice font.
Default value: | none |
---|---|
Inherited: | yes |
Animatable: | yes. Read about animatable |
Version: | CSS3 |
JavaScript syntax: | object.style.fontSizeAdjust=»0.58″ Try it |
Browser Support
The numbers in the table specify the first browser version that fully supports the property.
Syntax
Property Values
Value | Description |
---|---|
number | Defines the aspect value to use |
none | Default value. No font size adjustment |
initial | Sets this property to its default value. Read about initial |
inherit | Inherits this property from its parent element. Read about inherit |
CSS font-size-adjust : How to auto-adjust your font size
In this article, we’ll take a look at how the font-size-adjust attribute helps you auto-adjust your font size. We’ll cover the following topics:
How CSS font-size-adjust works
Developers often employ numerous font families and font sizes on a single webpage for style and readability purposes. A developer might, for example, use the Open Sans typeface for headlines and the Roboto Mono font for the body.
When a font cannot be accessed, the browser falls back on the second font provided, which may cause a significant shift in font size, and, in turn, shift around other parts of our UI. To give a better depiction of this as it would appear when implemented, take a look at the below illustration.
This example compares two typefaces with differing x-heights and demonstrates how the x-height of one may be adjusted to match the x-height of the other using font-size-adjust . Though both examples use the same two fonts, in the first line without font-size-adjust applied, all of the lowercase letters in the first font are much taller than those in the second font.
This is where font-size-adjust comes in handy — in the second line, the attribute adjusts the size of the letters in the second font to match the x-height of the first font’s letters.
font-size-adjust is useful because it determines font readability based on the size of lowercase letters, rather than the size of capital letters, and adjusts the size of lowercase letters to the size of those in the font currently in use.
Using the font-size-adjust attribute can help prevent this from happening by giving you more control over the font size. However, not all typefaces are supported by all browsers, and using unsupported typefaces may make your website look weird, as previously discussed. Let’s take a look at browser compatibility in the next section.
Browser compatibility for font-size-adjust
Before we move into the details, let’s review browser support for the font-size-adjust property. As of writing of this article, the CSS font-size-adjust property is currently only supported by Firefox by default.
Chrome and Opera support this property behind the “Experimental Web Platform Features” flag, which may be activated in chrome:/flags , and ranges from version 43 to 30. The CSS font-size-adjust attribute isn’t currently supported by Edge or Safari.
Now, let’s take a brief look at the CSS font-size property more broadly before diving into the CSS font-size-adjust property.
The CSS font-size property
The font-size CSS property sets the size of the font overall. There are some key things to note about the font-size property:
- When the font-size property is set to a fixed value in em , the size is calculated based on the font size of the parent element
- If other elements are stated in em , changing the font size of one element may impact the font size of others
- When font size is provided as a percentage, it is calculated relative to the font size of the parent element
Here is a sample of the font-size syntax:
/ absolute-size / font-size: medium | xx-small | x-small | small | large | x-large | xx-large; / relative-size / font-size: smaller | larger; / percentage / font-size: 10%; / length / font-size: 5px;
The CSS font-size-adjust property
As we mentioned before, the CSS attribute docs specify that the element’s font size should be adjusted based on the height of lowercase letters, rather than the height of uppercase letters.
When the primary font type cannot be accessed, the CSS font-size-adjust property allows developers to control font size at the component level. In such cases, a font backup is referenced, and the browser switches to display the secondary font.
But if there is a variation in the aspect ratio of the desired original and current fonts, this may cause issues with legibility and accessibility. The CSS font-size-adjust property may be used in situations where we need to retain the text’s legibility while maintaining its aesthetic.
The font-size-adjust syntax is as follows:
number | none | initial | inherit;
- Number : The font-size-adjust property is set to a number
- None : This is the default value
- Initial : Sets the value of this property to its default
- Inherit : The font-size-adjust property is passed down from parent to child
Let’s look at an example. Say that the font-size-adjust property receives a value of 0.5 , which will return a font size that is half the given font size for lowercase letters.
Over 200k developers use LogRocket to create better digital experiences
Learn more →
We can also set this property as a number and multiply it by the font-size property to make it compatible with browsers that do not support font-size-adjust . Here’s another example:
font-size: 20px; font-size-adjust: 0.5;
This sets the x-height of the font’s lowercase letters to 20 * 0.5 , which is equal to 10px .
In browsers that do not support font-size-adjust , a 20px font will still be executed when using this method.
Based on the image above, the difference between the two fonts isn’t very obvious. Whereas, if you use none , the predefined font, and its given size:
You can more clearly see that there is a clear difference between these two fonts. The size of both sentences is based on the font-size property, using the predefined font-size.
Conclusion
You should now have a better understanding of what the CSS font-size-adjust property does, why it’s important, and how to include it in your CSS styles.
You can start using this property in your web applications in order to improve legibility and aesthetics. Given that it is only supported by one browser, to avoid size difference due to browser incompatibility, it’s probably still a best practice to pick typefaces with closely comparable ratios.
Thank you, and happy styling.
Is your frontend hogging your users’ CPU?
As web frontends get increasingly complex, resource-greedy features demand more and more from the browser. If you’re interested in monitoring and tracking client-side CPU usage, memory usage, and more for all of your users in production, try LogRocket.https://logrocket.com/signup/
LogRocket is like a DVR for web and mobile apps, recording everything that happens in your web app, mobile app, or website. Instead of guessing why problems happen, you can aggregate and report on key frontend performance metrics, replay user sessions along with application state, log network requests, and automatically surface all errors.
Modernize how you debug web and mobile apps — Start monitoring for free.
How To Auto Adjust Your Font Size With CSS font-size-adjust?
Many developers use multiple font families and font size to have a better representation of a web page. For example, a developer might use the Bold Comic Sans font for titles and Roboto font for the body. However, this might make your website look cluttered, as many browsers do not support all the fonts. CSS font-size-adjust property can avoid such a situation by making the font size auto-adjust.
CSS font-size-adjust accepts several units of measurement within which font sizes are displayed, including pixels, em, rem, keywords, and viewport units. It may be applied to CSS Classes and IDs, still on elements themselves. Therefore, it may be used as a great hack to stop a significant decrease within the font size just if your first font choice does not load.
However, before understanding the CSS font-size-adjust property, it is important to consider that all your CSS elements should look perfect on all screen sizes. You can use LT Browser free dev-friendly browser for mobile view debugging on which you can see the mobile view of your website on Android and iOS resolutions.
Now let us quickly have a look at the CSS font-size property.
TABLE OF CONTENT
CSS font-size Property
CSS font-size property indicates a glyphs’ desired height based on the font. For the scalable font, the scalable factor is applied to calculate the font-size. However, for a non-scalable font, the absolute unit of the font-size is matched with the set size of the font.
- absolute-number: This value refers to the computed font sizes.
- relative-size: It has the value relative to the font-size computed and font-size table.
- length-percentage: Specifies that the font size is absolute.
CSS font-size-adjust Property
CSS font-size-adjust property provides the developers with honest management of the font size by allowing them to modify the font size of a part when the primarily selected font-type is not available.
In such situations, a font fallback takes place, and the browser uses the second specified font. This might result in a big issue if there is a difference between the aspect ratio of the initial and present font used. In such circumstances, where we need to take care of the look and maintain the text’s readability, the CSS font-size-adjust property can be used.
The font-size-adjust CSS property states that the font size of the element needs to be modified on the basis of the height of lowercase letters instead of the peak of uppercase letters.
- none: Specifies that the font’s x-height is not preserved
- number: This value refers to the aspect number of the first used font, and then the rest available fonts will be scaled according to the following formula: c = (a/a’) s
c | adjusted font size to use |
a | aspect value as specified by the font-size-adjust property |
a’ | aspect value of the actual font |
s | font size value |
If 15px Roboto (with an aspect value of 0.50) was unavailable and the next given font had an aspect value of 0.40, the font size of the substitute font used would be, 15*(0.50/0.40) = 18.75px
How CSS font-size-adjust Works?
Here is a demonstration of how the font-size-adjust works, making font size auto-adjust. Below is an example that compares two different font types having different aspect ratios. The x-heights of the lower case letters of the two fonts can be modified to match the x-height of the other font.
In the above image, the text on the left-hand side has been applied with a font named Comic Sans, which has an aspect value of 0.53. In contrast, the text on the right-hand side has been used with the Calibri font, which has an aspect value of 0.47. This results in an exceedingly small-looking text. On the underside line, a font-size-adjust of 0.53 has been applied because of which the font-size on the underside hand now has its font-size modified according to the aspect ratio specified.
The value of the CSS font-size-adjust property influences the used value of the ‘font-size’ but does not affect it’s enumerated value. It has its effects on the dimensions of relative units supported by font metrics like ex and ch of the fonts available primarily but does not damage the scale of em units.
As the numeric values of line-height check with the calculated size of font-size, the CSS font-size-adjust property does not modify the used value of line-height; rather, it auto-adjusts the font size.
In CSS, authors often specify the ‘line-height’ as a multiple of the font-size. Since the CSS font-size-adjust property affects the used value of font-size, authors should make sure to set the line height when CSS font-size-adjust is used.
Note: Setting the line height too tightly may end up in overlapping lines of text during this situation.