Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applying transition on flexbox justify-content property

Tags:

css

flexbox

I have a CSS class that on hover should change the justify-content property from center to flex-start with a delay of 1s.

So everything works fine and is being delayed except from the justify-content.

.menuItem:hover {
    transition-property: all;
    transition-delay: 1s;
    justify-content: flex-start;
}

Am I doing something wrong? And If so, how can I achieve that behavior?

like image 434
Golan Kiviti Avatar asked Aug 22 '16 13:08

Golan Kiviti


People also ask

Can you transition justify content?

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.

When using flexbox the justify content property can be used to distribute the space between the Flex items along the main axis?

The justify-content property aligns the flexible container's items when the items do not use all available space on the main-axis (horizontally). Tip: Use the align-items property to align the items vertically.


3 Answers

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.

like image 71
skreborn Avatar answered Oct 04 '22 12:10

skreborn


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>
like image 45
Rich Avatar answered Oct 03 '22 12:10

Rich


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 }
}
like image 20
GEkk Avatar answered Oct 05 '22 12:10

GEkk