Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS/Javascript fluid font size

My application has two views, a live mode and a preview mode. Both of these have windows which can be resized. They are the same html file. I am looking for a way to get the font-size to resize proportionally for both views when they are resized. I am using jQuery as my javascript framework. Is there a way of getting this to work in either CSS or jQuery?

like image 849
Franky Chanyau Avatar asked Aug 13 '26 22:08

Franky Chanyau


1 Answers

I know I am answering my own question, but the answer above was helpful but not enough to suffice as one. So this is how I ended up doing it.

First I set up this function which calculates a suitable font size based on a given width.

function getFontSize(width){
    sizew = width / 100;
    size = sizew * 1.5;
    return size;
};

Then I got the function to run on load and on re size of the window then modify the body font size accordingly.

$(window).resize(function(){
    size = getFontSize($(this).width());
    $("body").css("font-size", size + "px");
}).resize();

All you have to do from there is set up any element to have its font-size re-sizable by giving it an "em" or percentage font size.

like image 90
Franky Chanyau Avatar answered Aug 15 '26 12:08

Franky Chanyau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!