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

159
Visualizações
Display data from an array depending on box width

I have an array of items that is to be displayed inside a box of width 500px. Suppose the array is [john, johnbbab, johnabraham] and I need to display either the complete string or give ellipses accordingly if the text overflow. eg: If the box cannot hold the second item i.e johnbbab, the contents of the box will look like [john, ...].

The issue I'm facing is to determine how to render this depending on the width of the text assuming the fontsize is 10px.

PS: Need this to be done is Javascript

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Hum, can't you just put thos name in with a class ? With that, you can loop over all and calc the sum of width of them. You can check if the sum if highter or lower than the parent and make your change if needed.

No need to know the font-size or box's width with that.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use flexbox and set the div content based on the width of the div. You can try tailoring this to suite your needs

// short width 190px
// const list = ["john", "johnbbab", "johnabraham"];

// long width 491
const list = ["john", "johnbbab", "johnabraham", "peterparker","johnsnow","josemourinho","olegunnar"]; 


// if width is short
document.getElementById("names").innerHTML = list;

// you can set maybe 190px as width to shrink the names
if (document.getElementById("names").offsetWidth > 190) {

document.getElementById("names").innerHTML = `${list[0]}...`;

}
#names {
width: fit-content;
background-color: green;
padding: 5px;
color: white;
}
<div id="names" ></div>

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