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

235
Visualizações
how to change fontsize of text in a div in javascript

I have a grid and in some of the grid items divs the text is too long. I've already added text-overflow:ellipsis, white-space: nowrap, and overflow: hidden. This prevents the text from overflowing as expected. but what I wanted to try, was to detect if there was overflow and if so, decrease the font size.

Below is the JavaScript I was attempting. basically it should loop through all data rows and if overflow is detected change the font size. It doesn't have to be Javascript but I thought this would be my best chance at accomplishing this. however the code is not changing the font size, can anyone please help me with changing the font size? in Javascript, jQuery, CSS, etc.

document.addEventListener('DOMContentLoaded', function() {
   //the div is called data_row adn tehy are dynamically generated
  var text_size = document.getElementsByClassName('data_row');

  for (var i = 0; i < text_size.length; i++) {
    if (text_size.scrollWidth > text_size.clientWidth) {
       //i also tried a specific font size ex. 12px
      text_size.style.fontSize = "small";
    }
  }
});

.data_row {
  text-align: center;
  border: 1 px solid;
  line-height: 25 px;
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Your variable text_size actually is some kind of an array. You must refer to text_size[i] if you want to access the style etc.

Cheers.

about 4 years ago · Juan Pablo Isaza Relatório

0

function change(){
  const text = document.getElementById('text');
  text.style.fontSize='36px';
}
<p id="text" style="font-size: 16px">lorem ipsum ...</p>
<button onclick="change()">change</button>

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