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

144
Visualizações
Move relative object position when scrollbar appears using CSS

On my page, I'm displaying a log file in a div element with overflow-y:auto. In the top right corner of the div, I'm overlaying a close button div with position:relative.

When the scrollbar appears, the button is overlaying the scrollbar which is hard to see and looks ugly. You can see an example here: https://jsfiddle.net/4azw0rLf/

Moving it with javascript when scrollHeight exceeds clientHeight feels like a hack. Is there an option in CSS to move the close button to the left for the width of the scrollbar as soon as it appears?

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

0

You can wrap your terminal and move your close button inside. I created a minimal example starting from your code.

EDIT

With the first answer the close button scrolled along with the text, I corrected using the position: sticky; and top:0px;

It is not compatible with all browsers, but with most, you can check compatibility here.

const terminal = document.getElementById("terminal");

addText = () => {
  terminal.innerHTML += "overflowing line<br>";
}
#container-terminal {
  position: relative;
  overflow-y: auto;
  border: 2px solid;
  height: 100px;
  width: 200px;
}

#terminal {
  position: absolute;
  height: 100%;
  width: 100%;
}

#closeBtn {
  background-color: red;
  position: -webkit-sticky;
  position: sticky;
  top:0px;
  width: 20px;
  display: inline-block;
  float: right;
}
<div onclick="addText()" style="cursor:pointer;">Click to add text</div><br>

<div id="container-terminal">
  <div id="terminal">CSS only<br>CSS only<br>CSS only<br>CSS only<br>CSS only<br></div>
  <div id="closeBtn">X</div>
</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