I have a <div>
element that resizes as the browser window resizes.
Inside the <div>
I have a paragraph of text:
<div style="width:80%; height:80%; margin:10%;">
<p>Paragraph of text. Paragraph of text. Paragraph of text.</p>
</div>
I want the text to change font-size
as I resize the <div>
, so that the text will occuypy 100% of the available space.
How can I achieve this effect?
Would it be with a percentage font-size?
Would I have to use Javascript?
Thanks in advance!
The way I solved this problem was to use javascript to measure the container and then set the font size in px on that container. Once that baseline is set for the container then the relative font sizing of all the content will scale correctly using em or %.
Syntax: font-size-adjust: number|none|initial|inherit; Below are the examples that illustrates the use of font-size-adjust property.
If the font-size you want is 12px , then you should specify 0.75em (because 12/16 = 0.75). Similarly, if you want a font size of 10px , then specify 0.625em (10/16 = 0.625); for 22px , specify 1.375em (22/16).
To change font size in HTML, use the CSS font-size property. Set it to the value you want and place it inside a style attribute. Then add this style attribute to an HTML element, like a paragraph, heading, button, or span tag.
There's a jquery plugin for this: http://fittextjs.com/
HTML
<div id="change" style="margin:10%;">
<p>Paragraph of text. Paragraph of text. Paragraph of text.</p>
</div>
CSS
#change {
width: 80%;
height: 80%;
border: 1px solid black;
overflow: hidden;
font-size: 1em;
}
JAVASCRIPT
$(function() {
while( $('#change div').height() > $('#change').height() ) {
$('#change div').css('font-size', (parseInt($('#change div').css('font-size')) - 1) + "px" );
}
});
What you need is called vw
. Example CSS: font-size: 80vw;
. vw
means viewerport width, and 80vw
= 80%
of the device screen width
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With