Do you use text-size-adjust in your global styles?

Recently, I stumbled upon an enigmatic CSS issue that manifested only on real mobile devices, particularly iOS Safari. Certain fonts appeared disproportionately large, disrupting the layout integrity. Trying to fix this by applying different font sizes…


This content originally appeared on DEV Community and was authored by Dijana Pavlovic

Recently, I stumbled upon an enigmatic CSS issue that manifested only on real mobile devices, particularly iOS Safari. Certain fonts appeared disproportionately large, disrupting the layout integrity. Trying to fix this by applying different font sizes in dev tools, and even using !important had no effect. You can see below the desired screen display and the screen I ended up seeing.

Image description

Understanding what’s going on

Upon investigating, I found out that sometimes mobile browsers dynamically adjust font sizes to enhance readability for users. The rendering behavior of mobile browsers differs from that of desktop browsers. Unlike desktop browsers, which typically render pages at the width of the device screen, mobile browsers often utilize a wider viewport, commonly set at 980px as a default, to accommodate for websites that are not optimized for smaller screens. This will inevitably lead to horizontal scroll. To counteract this, the rule below is commonly employed in HTML to ensure that the viewport scales to fit the device size accurately.

<meta name="viewport" content="width=device-width, initial-scale=1" />

However, while scaling down to fit a mobile screen, some browsers may render text too small, prompting the application of a text inflation algorithm to enlarge it for improved legibility. This algorithm sometimes leads to unexpected font enlargement, particularly noticeable when an element spanning the full width of the screen experiences text size augmentation without altering the layout. To address this behavior and maintain consistent text sizing across devices, we can utilize the CSS text-size-adjust property.

The text-size-adjust empowers web authors to disable or adjust this automatic text inflation behavior, particularly crucial for websites designed with small screens in mind (which I believe majority of new websites are). This property controls the text inflation algorithm used on some smartphones and tablets. Other browsers will ignore it. The default size adjustment affects text and form controls, whether those form controls contain text (e.g., text inputs, selects) or do not (e.g., radio buttons, checkboxes).

It has 3 possible values:

  • auto: This value instructs renderers to utilize the default size adjustment when rendering text on small devices.
  • none: Renderers are directed not to perform any size adjustment for text displayed on small devices.
  • percentage[0,∞]: Renderers must not do size adjustment but instead the computed value of font-size must be multiplied by this percentage. Using 100% is equivalent to using none.

My approach to fixing this

I fixed the text inflation problem by adding this property to the body as part of our global css styles (can be added to html too). I recommend adding this rule to your global/reset/normalize css file:

body {
    // prevent font-size inflation on small devices
    text-size-adjust: 100%;
    -ms-text-size-adjust: 100%;
    -moz-text-size-adjust: 100%;
    -webkit-text-size-adjust: 100%;
}

For iOS safari only -webkit-text-size-adjust works but I included other vendors and a non-vendor property to add support for other browsers. It's very important to add the non-vendor property at the beginning. If added at the end, in Safari iOS it will override the -webkit one and since it's not supported it will not be applied, so things will work as default.

Few other considerations to keep in mind:

  • Experimental nature: It’s crucial to exercise caution when implementing the text-size-adjust property in production environments, as it remains an experimental feature. Browser support and implementation may undergo changes over time.
  • Definition of “small device”: There’s still ambiguity surrounding the definition of what constitutes a “small device”.
  • Some people were complaining that using none will prevent zooming on mobiles, but that wasn’t the case when I tested, none and 100% worked the same as per mdn explanation.

How others use it

Several popular CSS reset/normalization libraries incorporate some variation of text-size-adjust.

Thanks for reading!😊 Here are additional resources if you ever feel inclined to read more about this.


This content originally appeared on DEV Community and was authored by Dijana Pavlovic


Print Share Comment Cite Upload Translate Updates
APA

Dijana Pavlovic | Sciencx (2024-09-10T20:18:46+00:00) Do you use text-size-adjust in your global styles?. Retrieved from https://www.scien.cx/2024/09/10/do-you-use-text-size-adjust-in-your-global-styles/

MLA
" » Do you use text-size-adjust in your global styles?." Dijana Pavlovic | Sciencx - Tuesday September 10, 2024, https://www.scien.cx/2024/09/10/do-you-use-text-size-adjust-in-your-global-styles/
HARVARD
Dijana Pavlovic | Sciencx Tuesday September 10, 2024 » Do you use text-size-adjust in your global styles?., viewed ,<https://www.scien.cx/2024/09/10/do-you-use-text-size-adjust-in-your-global-styles/>
VANCOUVER
Dijana Pavlovic | Sciencx - » Do you use text-size-adjust in your global styles?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/10/do-you-use-text-size-adjust-in-your-global-styles/
CHICAGO
" » Do you use text-size-adjust in your global styles?." Dijana Pavlovic | Sciencx - Accessed . https://www.scien.cx/2024/09/10/do-you-use-text-size-adjust-in-your-global-styles/
IEEE
" » Do you use text-size-adjust in your global styles?." Dijana Pavlovic | Sciencx [Online]. Available: https://www.scien.cx/2024/09/10/do-you-use-text-size-adjust-in-your-global-styles/. [Accessed: ]
rf:citation
» Do you use text-size-adjust in your global styles? | Dijana Pavlovic | Sciencx | https://www.scien.cx/2024/09/10/do-you-use-text-size-adjust-in-your-global-styles/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.