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

233
Visualizações
How to get full width of an element inside an "overflow-x: auto;" area?

I coded this:

$(document).ready(function() {
  $("button").click(function() {
    alert("Width of #text: " + $("#text").width());
  });
});
* {
  margin: 0;
  padding: 0;
  font-family: sans-serif;
}

#container {
  width: 300px;
  height: 150px;
  background-color: grey;
  position: relative;
}

#scroll-area {
  overflow-x: auto;
  height: 100%;
}

#text {
  white-space: nowrap;
}
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

<div id="container">
  <div id="scroll-area">
    <div id="text">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</div>
  </div>
</div>

<button>Get width of #text</button>

I would like to calculate the width of the #text element that is inside the #container element. Currently, it shows me 300px as result. But that is the width of the container, and not of the text element.

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

0

Try with scrollWidth feature

$(document).ready(function() {
  $("button").click(function() {
    alert("Width of #text: " + $("#scroll-area")[0].scrollWidth);
  });
});
* {
  margin: 0;
  padding: 0;
  font-family: sans-serif;
}

#container {
  width: 300px;
  height: 150px;
  background-color: grey;
  position: relative;
}

#scroll-area {
  overflow-x: auto;
  height: 100%;
}

#text {
  white-space: nowrap;
}
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

<div id="container">
  <div id="scroll-area">
    <div id="text">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</div>
  </div>
</div>

<button>Get width of #text</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