Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize text to totally fill container

I have a div with a fixed height and a fluid width (15% of body width). I want the paragraph text inside to totally fill the div; not overflow and not underfill.

I've tried with jQuery to increment the text size until the height of the paragraph is equal to the height of the container div. At that point, the text should be totally covering the div. The only problem is that font-size increments in 0.5px values. You can't do 33.3px; it has to be either 33.0px or 33.5px. So at 33px, my text is far too short to cover the div, and at 33.5px it overflows.

Does anyone have any ideas on how to remedy this? There are lots of plugins for making text fill the whole width of a container, but not for text that has to fill both the width and height. At least, none that I've seen.

<head>
<style type="text/css">
.box {
    display:block;
    position:relative;
    width:15%;
    height:500px;
    background:orange;
}
    .box p {
        background:rgba(0,0,0,.1); /* For clarity */
    }
</style>
</head>

<body>

<div class="box">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. <br><br>
    Suspendisse varius nibh quis urna porttitor, pharetra elementum nisl egestas. Suspendisse libero lacus, faucibus id convallis sit amet, consequat vitae odio.</p>
</div>

<script type="text/javascript" src="js/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
function responsiveText(){
    var i = 0.5;

    do {
        $(".box p").css("font-size",(i) + "px");

        var textHeight          =  $(".box p").height(),
            containerHeight     =  $(".box").height();
        i += 0.5;   

    } while ( textHeight < containerHeight );           // While the height of the paragraph block is less than the height of the container, run the do...while loop.
} responsiveText();


$(window).resize(function(e) {
    responsiveText();
});
</script>
</body>

Text set at 33px. It is too short. Text set at 33px. It is too short.

Text set at 33.5px. It overflows. Text set at 33.5px. It overflows.

like image 848
Jesse Macleod Avatar asked Aug 27 '14 13:08

Jesse Macleod


People also ask

How can I resize text so it doesn’t break on resize?

Let us count the ways. If you set type with vw (viewport width) units, you can find an exact number where the text pretty closely fits the container and doesn’t break as you resize. I’d call this a magic number. In this case, font-size: 25.5vw; works down to a 320px viewport, but still will break much lower than that.

What to do with data larger than the container?

This is a simple problem and a simple solution. Often in the world of dynamic data we find ourselves with data that is larger than the container. There are ways of handling this like a scrolling div or adding ellipses to chopped of text, but what if you absolutely must see all of the text and are willing to change to font size to accomplish this?

What is the best font size for text in a container?

If you set type with vw (viewport width) units, you can find an exact number where the text pretty closely fits the container and doesn’t break as you resize. I’d call this a magic number. In this case, font-size: 25.5vw; works down to a 320px viewport, but still will break much lower than that.

Is there a way to speed up font size?

You can modify the fontstep variable to resize at a faster rate if you want. I find that resizing in increments of two makes the most sense. That's all there is to it. Enjoy. EDIT: I added a bit of a timeout to give the webfont time to load first...


1 Answers

I adjusted your function to use em units instead of px, and adjusted the increment from "0.5" to "0.003".

The preview pane is a little unreliable, so check it out in its own window.

Keep in mind:

  • The larger the increment, the fewer loop iterations (and the less processing).
  • The smaller the increment, the "finer" the adjustment and the result.
like image 186
Jeromy French Avatar answered Oct 18 '22 05:10

Jeromy French