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

347
Visualizações
How to remove transition of a border without affecting the width

Whenever i toggle the class "dark_mode" the border color of the "width_animation" has a transition. Any ideas how could i remove the border transition without affecting the width transition? If i remove the transition of the class "width_animation" the width will go back to initial width after no hover and i don't want that, i want to be smooth.

function darkMode() {
    var element = document.body;
    element.classList.toggle("dark_mode");
}
.dark_mode {
  background-color: #333;
}

.dark_mode .width_animation {
  border-color: red;
}

.product_container {
  width: 300px;
  height: 400px;
  background-color: grey;
  position: relative;
}

.width_animation {
  width: 100px;
  height: auto;
  color: white;
  background-color: black;
  border: 5px solid white;
  font-size: 30px;
  padding-left: 10px;
  position: absolute;
  right: 0px;
  top: 150px;
  -webkit-transition: 1s ease-in-out;
  transition: 1s ease-in-out;
}

.width_animation:hover {
  width: 200px;
  -webkit-transition: 1s ease-in-out;
  transition: 1s ease-in-out;
  cursor: pointer;
}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
</head>

<body>
    <button onclick="darkMode()">DARK MODE</button>

    <div class="product_container">
        <div class="width_animation">
            hover
        </div>
    </div>

    <script src="script.js"></script>
</body>

</html>

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

0

Add width to the transition. This will apply transition to the width only. Make sure to add it to both sections. See below.

.width_animation {
  width: 100px;
  height: auto;
  color: white;
  background-color: black;
  border: 5px solid white;
  font-size: 30px;
  padding-left: 10px;
  position: absolute;
  right: 0px;
  top: 150px;
  -webkit-transition: width 1s ease-in-out;
  transition: width 1s ease-in-out;
}

.width_animation:hover {
  width: 200px;
  -webkit-transition: width 1s ease-in-out;
  transition: width 1s ease-in-out;
  cursor: pointer;
}
about 4 years ago · Juan Pablo Isaza Relatório

0

I might be reading the question wrong, but as I understand it you only want the border of the "hover" square to be red when Dark Mode is enabled, right? Further, you only want the width transition to occur on-hover, and the two should be mutually exclusive. Is that right?

.width_animation {
  width: 100px;
  height: auto;
  color: white;
  background-color: black;
  border: 5px solid white;
  font-size: 30px;
  padding-left: 10px;
  position: absolute;
  right: 0px;
  top: 150px;
  -webkit-transition: 1s ease-in-out;
  transition: 1s ease-in-out;
}

.dark_mode {
  background-color: #333;
}

/* The width_animation color transition will only apply when 
   the parent object has dark_mode enabled */
.dark_mode > .product_container > .width_animation {
  border-color: red;
}

.product_container {
  width: 300px;
  height: 400px;
  background-color: grey;
  position: relative;
}

.width_animation:hover {
  width: 200px;
  -webkit-transition: 1s ease-in-out;
  transition: 1s ease-in-out;
  cursor: pointer;
}

If you wanted to make it so that the "hover" square doesn't shrink after someone exits the hover animation (based on this part of your question "the width will go back to initial width after no hover and i don't want that"), you'll need to make .width_animation:hover a static style like below, and then use some javascript to add/remove the class on hover as desired.

.width_animation {
  width: 100px;
  height: auto;
  color: white;
  background-color: black;
  border: 5px solid white;
  font-size: 30px;
  padding-left: 10px;
  position: absolute;
  right: 0px;
  top: 150px;
  -webkit-transition: 1s ease-in-out;
  transition: 1s ease-in-out;
}

.dark_mode {
  background-color: #333;
}

/* The width_animation color transition will only apply when 
   the parent object has dark_mode enabled */
.dark_mode > .product_container > .width_animation {
  border-color: red;
}

.product_container {
  width: 300px;
  height: 400px;
  background-color: grey;
  position: relative;
}

.width_animation_hover {
  width: 200px;
  -webkit-transition: 1s ease-in-out;
  transition: 1s ease-in-out;
  cursor: pointer;
}
function darkMode() {
  var element = document.body;
  element.classList.toggle("dark_mode");
}

var hover = document.getElementsByClassName("width_animation")[0];
hover.addEventListener("mouseenter", function( event ) {
    if ( hover.classList.contains("width_animation_hover") ) {
    event.target.classList.remove("width_animation_hover");
  } else {
    event.target.classList.add("width_animation_hover");
  }
})
about 4 years ago · Juan Pablo Isaza Relatório

0

.width_animation {
   -webkit-transition: width 1s ease-in-out, border 0.1s linear;
   transition: width 1s ease-in-out, border 0.1s linear;
}

And You have not to write transition into scope of :hover{}. Because your going forward and backward transitions are the same.

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