I have a container:
<div id="container"><h1>LONG TITLE LINE</h1></div>
and css:
#container {
width: 50%;
height: 50%;
}
#container h1 {
font-size: XXXXXX;
}
"XXXXXX" <-- where i want the font size to be based on the width of the page/container.
QUESTION: is it possible to have the font-size of the h1 based on the width of the page? in css3? i'm sure it can be done using JS, but like to avoid that if it is possible.
The font size can be set with vw (viewport) unit, which means the viewport width. The viewport is the browser window size. 1vw = 1% of viewport width. If the viewport is 50cm wide, 1vw is 0.5cm.
Using width, max-width and margin: auto; Then, you can set the margins to auto, to horizontally center the element within its container. The element will take up the specified width, and the remaining space will be split equally between the two margins: This <div> element has a width of 500px, and margin set to auto.
First and foremost, the current font-size is equal to 100% (i.e. 12pt = 100%).
For example, suppose the font-size of the <body> of the page is set to 16px . 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).
I don't think it's possible in CSS, but this might be interesting to you:
http://fittextjs.com/
An other option dependng on your layout is to use vw
units :
vw : 1/100th of the width of the viewport.(source MDN)
If your #container
width is set as a percentage of the viewport, font-size will adapt to it's width :
DEMO
CSS :
#container h1 {
font-size: 5vw;
}
Browser support for vw
units is IE9+, see canIuse for more info.
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