Applying transition on flexbox justify-content property

You can't animate 'justify-content', but if you need to animate element to move from one side to other, you can do:

.parent {
   display: flex;
}

.menuItem:hover {
  flex: 1;
  animation: moveAnim .3s alternate 1;
}

@keyframes moveAnim {
  from { flex: none }
  to{ flex: 1 }
}

You can achieve your goal with two flex items, where the transition is on the "flex" property.

.container {
  display: flex;
  justify-content: center;
  border: 1px solid gray;
}

.content {
  flex: 1;
  max-width: fit-content;
  border: 1px solid orangered;
}

.dummy {
  flex: 0;
  transition: flex 1s;
  border: 1px solid limegreen;
}

.container:hover .hovered {
  flex: auto;
}
<div class="container">

  <div class="content"> hover over me </div>
  
  <div class="dummy hovered" />
  
</div>

Hey Fabricio, this section is for your question. Cheers

.container {
  display: flex;
  justify-content: center;
  border: 1px solid gray;
}

.content {
  width: fit-content;
  border: 1px solid orangered;
}

.dummy {
  transition: flex 1s;
  border: 1px solid limegreen;
}

.after {
  flex-grow: 1;
}

.before {
  flex-grow: 0;
}

.container:hover .before {
  flex-grow: 1;
}
<div class="container">

  <div class="dummy before" ></div>

  <div class="content"> hover over me </div>
  
  <div class="dummy after" ></div>
  
</div>

justify-content is not a transition-able property. You can find the full list of compatible properties here. All unlisted properties should simply snap to their new value.

If you happen to need to animate a single element only, you may try using absolute positioning.

#wrapper {
    position: relative;
}

#element {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    transition: all 300ms;
}

#element:hover {
    left: 0;
    transform: translateX(0);
}

Keep in mind that this will not work for multiple elements.

Tags:

Css

Flexbox