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

188
Visualizações
Z-index not working properly with overlapping divs

As you can see at this snippet, I'd like to create a container div with some text, the blueBox that goes up and another div that covers the container but not the blueBox. Anybody knows why the blueBox is not visible on top of red div even though it has z-index 4 and the red div has z-index 3?

#container{
  height:100px;
  width:100px;
  z-index:1;
  position:absolute;
  border:1px;
  background-color:yellow;
}
#moving1{
  height:30px;
  width:30px;
  background-color:blue;
  position:absolute; 
  left:20%;
  top:50%;
  z-index:4!important;
  transition: 1s ease;
}
#moving2{
  z-index: 3;
  height:100px;
  width:100px;
  position:absolute;
  top:0%;
  left:0%;
  background-color:red;
  transition: all 1s ease;
}
.moveLeft{
    transform: translateX(-110%);
}
.moveUp{
    transform: translateY(-200%);
}
<div id="container" onmouseleave="document.getElementById('moving2').classList.remove('moveLeft'); document.getElementById('moving1').classList.remove('moveUp');">
  container
  <div id="moving1"></div>
</div>
<div id="moving2" 
onmouseenter="this.classList.add('moveLeft');                   document.getElementById('moving1').classList.add('moveUp');"></div>

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

0

You are closing the container div before entering the blue square div. You need two children divs inside the container for the z-index to work.

In this case, you would need something like this:

#container{
  height:100px;
  width:100px;
  z-index:1;
  position:absolute;
  border:1px;
  background-color:yellow;
}
#moving1{
  height:30px;
  width:30px;
  background-color:blue;
  position:absolute; 
  left:20%;
  top:50%;
  z-index:4!important;
  transition: 1s ease;
}
#moving2{
  z-index: 3;
  height:100px;
  width:100px;
  position:absolute;
  top:0%;
  left:0%;
  background-color:red;
  transition: all 1s ease;
}
.moveLeft{
    transform: translateX(-110%);
}
.moveUp{
    transform: translateY(-200%);
}
<div id="container" onmouseleave="document.getElementById('moving2').classList.remove('moveLeft'); document.getElementById('moving1').classList.remove('moveUp');">
  container
  <div id="moving1"></div>
  <div id="moving2" onmouseenter="this.classList.add('moveLeft'); document.getElementById('moving1').classList.add('moveUp');"></div>
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

Additionally to the first answer, If I understand correctly you want an hover effect on those divs, if so you could remove "onmouseleave" and "onmouseenter" parameters and edit your file to look cleaner like so:

#container{
  height:100px;
  width:100px;
  z-index:1;
  position:absolute;
  border:1px;
  background-color:yellow;
}
#moving1{
  height:30px;
  width:30px;
  background-color:blue;
  position:absolute; 
  left:20%;
  top:50%;
  z-index:4!important;
  transition: 1s ease;
}
#moving2{
  z-index: 3;
  height:100px;
  width:100px;
  position:absolute;
  top:0%;
  left:0%;
  background-color:red;
  transition: 1s ease;
}
#container:hover #moving1{
    transform: translateY(-200%);
}
#container:hover #moving2{
    transform: translateX(-110%); 
}
<div id="container" >
  container
  <div id="moving1"></div>
  <div id="moving2"></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