Reputation: 1667
I have a container whose width is fluid (depending on the size of the browser window), though it has a max-width of 950px. The container has a nested header h1 element which has some text as a strapline.
I want to somehow make this header text get smaller as the container gets narrower (using jQuery).
I think it'll work something like this:
I'd like it to work dynamically during the browser resize if possible.
Any ideas?
Cheers, James
Upvotes: 0
Views: 275
Reputation: 47968
$(window).resize(function(){
var widthRatio = $('#container').width()/950;
$('#header1').css('font-size', 24 * widthRatio);
});
Upvotes: 1
Reputation: 110429
The method you've outlined should work just fine. The conversion from string to number should be handled using parseFloat
, since the font size is returned in (IIRC) em, e.g.:
var hText = $("#hText").css("font-size");
var textSize = parseFloat(hText);
Yes, it is possible to do this during the browser resize, using
(window).bind('resize', function () {
// Your implementation
});
Upvotes: 1