Skip to content Skip to sidebar Skip to footer

CSS Transition Width Of Line To Center

I am learning about CSS transitions and I'm trying to make 3 offset lines reduce width to their respective center points. Right now the width transition is occurring to the left an

Solution 1:

Use transform: scale() instead of width and the transform-origin will be the center by default.

const div = document.getElementById('lines')

div.addEventListener('click', function() {
	var className = div.getAttribute("class");

	if (className != 'open') {
		div.className = 'open';
	} else {
		div.className = '';
	}
})
#lines {
  width: 250px;
  height: 250px;
  position: absolute;
  cursor: pointer;
  transition: .25s ease-in-out;
}
#lines span {
  border: 1px solid black;
  display: block;
  position: absolute;
  height: 15px;
  width: 200px;
  background: #d3531a;
  border-radius: 9px;
  transition: transform .25s ease-in-out;
}
#lines span:nth-child(1) {
  top: 0px;
}
#lines span:nth-child(2) {
  top: 18px;
  left: 18px;
}
#lines span:nth-child(3) {
  top: 36px;
  left: 36px;
}

#lines.open span {
  transform: scaleX(0);
}
<div id="lines">
  <span></span>
  <span></span>
  <span></span>
</div>

Post a Comment for "CSS Transition Width Of Line To Center"