To make font that always fits nicely in its container, I always use Javascript to make div
s or a certain class
have their font-size
always equal to their width
and have their inner text elements defined in terms that font-size
.
Demo: http://jsfiddle.net/pjk8t5kb/
<div class="font-size-equals-width">
<h1>Here's a headline</h1>
<p>Here's some paragraph text</p>
</div>
.font-size-equals-width
{
padding: 5%;
border: 1px solid black;
}
.font-size-equals-width h1 { font-size: 0.2em; }
.font-size-equals-width p { font-size: 0.1em; }
function scaleFontSize ( )
{
$('.font-size-equals-width').each(function()
{
var thisContainer = $(this);
thisContainer.css('font-size', thisContainer.width().toString() + 'px');
});
}
$(window).resize(function ( )
{
scaleFontSize();
});
$(document).ready(function()
{
scaleFontSize();
});
My question is whether this is possible in CSS? From what I can tell, the only font-size
units are %
, em
, pt
and px
, none of which are connected to container width
or height
. Does Bootstrap have anything for this? (I always include Bootstrap in my project but I have a feeling that I under-utilize it.)