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;
}
#linesspan {
  border: 1px solid black;
  display: block;
  position: absolute;
  height: 15px;
  width: 200px;
  background: #d3531a;
  border-radius: 9px;
  transition: transform .25s ease-in-out;
}
#linesspan:nth-child(1) {
  top: 0px;
}
#linesspan:nth-child(2) {
  top: 18px;
  left: 18px;
}
#linesspan:nth-child(3) {
  top: 36px;
  left: 36px;
}

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

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