Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

544
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda