I am having troubles understanding CSS transition even when what I think is obvious should work but it doesn’t. What I am trying to accomplish is a little complex than this, but this simple example shows how I think I don’t understand how CSS transitions work yet.
I want the flex-direction
property of a container to change from column
to row
, but the change shouldn’t be instantaneous, but rather slowly so it can be seen. I’ve implemented the code below, but it still doesn’t work even when I think it should.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
</body>
</html>
.container {
margin: 0 auto;
width: 500px;
height: 500px;
display: flex;
flex-direction: column;
justify-content: space-evenly;
align-items: center;
background-color: teal;
transition: flex-direction 5s ease;
}
.box {
width: 100px;
height: 100px;
background-color: dodgerblue;
}
.container:hover {
flex-direction: row;
}
Thank you.
3
Answers
not everything in animatable. am sure you know that you can not animate display property, same goes for flex-direction, its not animatable.
Flex-direction is not an animatable property in CSS.
See here: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_animated_properties
This effect may be done through javascript however.
See: https://codepen.io/osublake/pen/eJGrPN
What you are trying is a nice idea, but transitions does not work with flex direction properties, only properties using quantified values of compatible units can transition between two of those values, like measurements and colors.
example