Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

546
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda