Link to home
Start Free TrialLog in
Avatar of steva
steva

asked on

Setting html font-size in WordPress proportional to the viewport width

I've created a template in WordPress and I want the page it generates to scale as the viewport size changes. I've done this outside of WordPress by first expressing all my units in rems and then  setting the html font-size proportional to the viewport size by placing the code below in the DOM Ready section:

var viewport_width = $( window ).width();  // Returns width of the current browser viewport
		if (viewport_width < 1400) {	        
			font_pc = (viewport_width / 1400) * .625 * 100);   
			$(html).css("font-size", font_pc + '%');
		}

Open in new window


Actually this starts the scaling when the viewport is < 1400px and  In this example, if the viewport were 1200px font_pc would e 53.6% and that would become the html element font-size, reducing everything expressed in rems.

My question is, how can I pull this off for a WordPress template php file?

Thanks
Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

Are you familiar with the size units vw and vh? They should achieve exactly what you're wanting. These are defined as follows:


vw: 1/100th viewport width
vh: 1/100th viewport height
vmin: 1/100th of the smallest side
vmax: 1/100th of the largest side
(sourced from below link)

When using these units, there are some problems with browser scrollbars, but they can be overcome. Details in this article:
https://web-design-weekly.com/2014/11/18/viewport-units-vw-vh-vmin-vmax/
Avatar of steva
steva

ASKER

Thank you!
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.