Search code examples
cssfontsscale

Have text scale up in size to fit the container


Using CSS, and specifically no onload javascript, would it be possible to do this:

You have a cell of 150px wide and 100px in height. It contains a text, e.g.: $20,00 or any variant of that.

You want it to perfectly fit the size of the container.

I'd be able to do it with javascript, size up the text until a container without padding/margin reaches the cell's width and/or height. Or contain it in something that has an overflow set to auto and see when it overflows or something.

Can it be done with pure CSS?

Take into account that the font isn't a fixed size font, either. Use Arial if you like.


Solution

  • One thing that I have done that isnt perfect but can get the job done in some cases is to use a @media rule for screen sizes smaller then X pixels, set the font size smaller or larger.

    h2{
      font-size:25px
    }
    
    @media screen and (max-width: 850px){/* for screens smaller then 850px, set the font size smaller */
       h2{
         font-size:19px;
       }
    }
    

    Of course this would only work assuming the texts container size is based on the window size in some way.