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

152
Visualizações
Center of image always in the center of div

I was trying to create a carousel replicating the one as in https://www.fcbarcelona.com/en/ Barca's website. It was pretty easy, though I can't wrap my head around, on how to make the background-image be centered inside my div, despite my divs size, which fluctuates.

Here is what I got so far.

const one = document.getElementById('one')
const two = document.getElementById('two')
const three = document.getElementById('three')

one.addEventListener("mouseover", ()=>{
  one.style.flex= " 1 0 50%";
  two.style.flex= " 1 0 25%";
  three.style.flex= " 1 0 25%";
  one.style.transition = '0.4s ease';
});

two.addEventListener("mouseover", ()=>{
  one.style.flex= " 1 0 25%";
  two.style.flex= " 1 0 50%";
  three.style.flex= " 1 0 25%";
  two.style.transition = '0.4s ease';
});

three.addEventListener("mouseover", ()=>{
  one.style.flex= " 1 0 25%";
  two.style.flex= " 1 0 25%";
  three.style.flex= " 1 0 50%";
  three.style.transition = '0.4s ease';
});
.main-body{
  display: flex;
}

.column{
  cursor: pointer;
  background-image:url("https://www.basketblog.gr/articleimages/LebronJames_April20212112121212.jpg");
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
  height:500px;
}

#one{
  flex: 1 0 25%;
}

#two{
  flex: 1 0 50%;
}

#three{
  flex: 1 0 25%;
}
<body>
  <div class="main-body">
      <div class="column" id="one">
      </div>
      <div class="column" id="two">
      </div>
      <div class="column" id="three">
      </div>
  </div>  
</body>

I know my code seems a little lazy, I just wanted it to work

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

0

You can use background-position property to do so.

By giving center value for the same would fix the problem I guess.

Try background-position: center

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