Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

543
Views
Adjusting font size automatically to a fixed table column size

So I have this dynamic table with names that I would like to adjust so that text does not wrap to the next line and still is visible. The easiest way would be to have the text size auto-adjust to the container width so that the height whould be the same for all cells: How it looks nowHow it looks now

How I would like it to lookHow I would prefer it to look...

Anyone has some good hints?

Of course I could always have some rigid function that changes the font-size of a specific name based on the length of the text. But I was hoping that there was some cool CSS code that could do the trick!

I know that I can automatically adjust with the canvas size with (vw) but that does not help when the width of my columns are fixed.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I suggest storing the preferred height of the element.

For example, say it needs to be 64px high.

then just use some javascript to lower the font size till it is equal to or less than the preferred height.

you just need to set the preferredHeight to the height you want and then just add the fixHeightFontSize class to each item you want to resize the font of

Normal Javascript

let preferredHeight = 64; // in pixels
document.querySelector('.fixHeightFontSize').each(function(index){
    document.querySelector(this).css({ 'fontSize' : 120}); // just give the font an inital large value, this is only needed for bugs.
    while(size = document.querySelector(this).height() > preferredHeight){
         let newFontSize = parseInt(document.querySelector(this).css("fontSize")) - 1;
         document.querySelector(this).css({ 'font-size' : newFontSize});
    }
});

Jquery Version

let preferredHeight = 64; // in pixels
$('.fixHeightFontSize').each(function(index){
    $(this).css({ 'fontSize' : 120}); // just give the font an inital large value, this is only needed for bugs.
    while(size = $(this).height() > preferredHeight){
         let newFontSize = parseInt($(this).css("fontSize")) - 1;
         $(this).css({ 'font-size' : newFontSize});
    }
});

I'm using jquery but you would be able to impletment this in normal javascript

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!