benhowdle89
benhowdle89

Reputation: 37454

Implement a max-font-size style rule with CSS/javascript/jquery

I am using this code to dynamically resize text:

function fontResize(){
   $('.features').css('font-size', ($(window).width()/100) + 'px');
}

fontResize();
$(window).resize( function () {fontResize()});

This works great for pulling the browser inwards but when i'm stretching the screen out on my 21" iMac, the text is too big for a set size DIV i have. how can i implement a solution whereby i can cap the maximum font size on the text. Will i be able to override this function above and put a max-font-size of 13px?

Thanks

Upvotes: 2

Views: 6071

Answers (2)

Nono
Nono

Reputation: 188

You could implement a sizeRange to define maximum and minimum font sizes.

function fontResize(){
  var sizeRange = [6, 13];  
  var size = $(window).width()/100;

  if ( size <= sizeRange[1] && size >= sizeRange[0] ) {
    $('.features').css('font-size', size + 'px');
  } else {
    // font-size out of range
  }
}

Upvotes: 2

vinceh
vinceh

Reputation: 3510

function fontResize(){
   var size = $(window).width()/100;
   if ( size < 13 )
       $('.features').css('font-size', size + 'px');
}

Upvotes: 4

Related Questions