Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

349
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!