How can I automatically re-size an html tag so that the text fits on a single line and does not need to wrap?

resize text to fit div css
css shrink text to fit
css scale font-size to fit div
css font-size based on text length
css wrap text to next line
react fit text to container
css fit text to container
javascript resize text to fit div

I have a typical web store and each product has an tag with the product title. Some of the product names are very long and cause the tag to word wrap up to three lines.

Is there a way, either in pure CSS or javascript or jQuery to dynamically change the font size of the tag so that it is the maximum size necessary to fill the parent div but -without- requiring a word wrap?

So far I've tried using jQuery to add a series of classes based on the number of chars in the text, but that has been far from precise. For example:

jQuery:

$('h1').each( function() { 
   if( $(this).html().length > 15 )
       $(this).addClass('long');
 }
 etc...

CSS:

       h1.entry-title.long 
       { font-size: 2.2em; letter-spacing: normal; }

Can anyone suggest a more elegant/accurate solution?

You could perhaps consider to use a responsive unit of measure for the font, to adapt it to the container, also on resize; if the container is the body, what you are looking for is Viewport-percentage lengths.

Cheers

How can I wrap or break long text/word in a fixed width span , behave that way by adding display: block; to your CSS. Text in Textarea not Wrapping . my text does not automatically go on to the next line; it just runs in one long line with no line breaks. I need to confirm so

Considering that you say that some of the product names are so long that they span 3 lines it could get pretty ugly I guess, therefore I've added the option on turning on text-overflow: ellipsis and adding the text to the title (so that the full text will appear as a tooltip on mouseover) if the font-size drops below 16px.

So... basically, what happens is that the functions turn on white-space: nowrap with a scrollbar on each of the H1's and then run a loop where font-size is reduced for every iteration until the need for a scrollbar disappear or the size meets the limit. In the end, the scrollbar is hidden again.

You just need to set the lowest acceptable size (min_font_size) and possible additional filters on H1.

On the count of elegance, I guess... Well... perhaps a shrink animation could be added?

jQuery.fn.hasHScrollBar = function () {
   return this.get(0).scrollWidth > this.innerWidth();
}

$(document).ready(function () {

    // Set minimum size:
    var min_font_size = 16;

    $('h1').each(function () {
        var font_size = parseInt($(this).css("font-size"));
        $(this).css("white-space", "nowrap");
        $(this).css("overflow", "scroll");
        while ($(this).hasHScrollBar()) {
            if (font_size < min_font_size) {
                $(this).css("text-overflow", "ellipsis");
                break;
            }
            $(this).css("font-size", font_size + "px");
            font_size--;
        }
        $(this).css("overflow", "hidden");
    });
});

Possible distraction:

Add opacity 0 to body or container:

body {
    opacity: 0;
    transition: opacity 0.4s ease-in;
}

And a restore class:

.fade-in {
   opacity: 1;
}

Normalize the page on ready (before or after the font-adjustment):

$(document).ready(function () {
   $('body').addClass("fade-in");

“word-wrap: break-word” in CSS, How do I wrap text in next line in CSS? It would seem that word-wrap should work. MSDN site states that the word-wrap style can be applied to either the <select> or <option> element, but I've yet to see it work.

FWIW: When I just opened this page to see the new answers I was greeted with a new StackOverflow feature: A suggested answer. And that page has some interesting options including FitText and a solution in straight CSS using SVG.

Font scaling based on width of container

In HTML, how can I change the size of text on my web page?, . word-break: break-all; It is used to break the words at any character to prevent overflow. Start studying HTML Tags Quiz. Learn vocabulary, terms, and more with flashcards, games, and other study tools.

resize font to fit in a div (on one line), To use this code, simply add the "font_fix" class to any divs containing text needing to be fit to it in one line. For a header, this may require an extra div around the header. Then, either call this function once for a fixed size div, or set it to a resize and/or orientation listener for varying sizes. Marion has been hired by a small business to determine why its web site does not attract users. As Marion interviews different members of the company, she learns that when the site was first created, the external web developer did not produce any documents specifying the basic requirements for this site.

Fitting Text to a Container, There are a number of ways to go about putting some text in a where the text pretty closely fits the container and doesn't break as you resize. @import url('​https://fonts.googleapis.com/css?family=Bowlby+One+SC'); So you need a width and height on the element for it to do it's thing. margin: 0 auto; Select the HTML tag below that configures a button that, when clicked, will automatically reset form fields to their default values. both <input type="reset"> and <button type="reset">Reset</button> Select the form control below that does not use the <input> tag.

The Flash MX Project, The following list describes the option settings of the Input Text Type: Line option, you can set the input text field to Single Line, Multiline, Multiline No Wrap, or Password. Select Multiline No Wrap if you do not want the word wrap feature​. by automatically applying the corresponding HTML tags to the editable text fields. The above line is so long that it ought to wrap. But because it has no spaces, it does not wrap. Instead the entire text is really on one line. In Dreamweaver they show you the text going on and on to the right, about 3 screen lengths. But in Firefox, the text past the div boundary (ie. to the right of the boundry) does not show up.

Comments
  • do you need to show the complete text? if not, I would use text-overflow:ellipsis and add the full text as tooltip on the element
  • I definitely need to show the complete text.
  • Possible duplicate of Font scaling based on width of container
  • This actually works. The only downside is that, on all the devices I've tested so far, it creates a noticeable visual glitch--the text displays as huge for an instant and then snaps to the proper size. Is there a way to get rid of that? (ie. to have the resizing invisible?)
  • That's the downside of fixing displays after everything have clicked into place and found the space to cramped. I was partially joking on the subject of elegance, but I guess a distraction is needed. I haven't actually done animations on shrinking text before, so I can't say whether it might be possible to turn it into an "Oomph" effect that might actually be an attraction.
  • Other than setting the default size considerably smaller and closer to how the majority appears would lessen the impact.. I guess Another way might be to create classes from the rendered font-sizes, save them to a css file and add them to each h1 somehow perhaps linked with a record id...Like a dictionary of sorts. How is the data generated?
  • It's a typical WordPress site. LAMP
  • And BTW--I did get the joke. There are apparently other solutions out there, but the client isn't paying to do more research.