21days-responsive-css-challenge

Tutorial-2 | Flexbox items

Flex items are everything that’s inside a flexbox as soon as we take a container & we apply display:flex; everything becomes a flex item and we can do a whole bunch of cool flex stuff with it we’re controlling them individually so you can make everything shift from one side to the other but what if you want one thing to act differently that’s what you can do with a bunch of the flexbox properties that are dealing with the individual items and that’s what we’re gonna be exploring right now.

There’s five different properties we’ll be looking at and they are following :

  1. flex-grow
  2. flex-shrink
  3. flex-basis
  4. flex
  5. align-self
  6. order

1. flex-grow :

2. flex-shrink :

3. flex-basis :

A deep dive into flex-grow, flex-shrink & flex-basis

Let’s understand all the three concepts clearly with illustrations :

grow-shrink-basis

In the above image you can see there are 3 flex-items which have the following properties :

.one {
    /* prevents items from growing */
    flex-grow: 0;
    /* makes the items shrinkable */
    flex-shrink: 1;

    flex-basis: 250px;
}

.two {
    flex-grow: 0;
    flex-shrink: 1;

    flex-basis: 250px;
}

.three {
    flex-grow: 0;
    flex-shrink: 1;

    flex-basis: 250px;
}

flex-grow & flex-shrink have the default value whereas the flex-basis has a value of 250px, it means if the amount of space is available for the items to take up then they’ll take up the 250px space as specified in above code. And you can see all the three items are taking up 250px and there’s still some space left in the flexbox container that’s because flex-grow is set to 0 (default). and if we shrink the flexbox the flex-items will also shrink accordingly (see the below image) as they have flex-shrink set to 1 (default). Even if we don’t specify the flex-grow & shrink values the default values will apply anyway.

shrink illustration

Now, if we change the flex-grow to flex-grow:1; in the flex-item1 then what will happen is that the flex-item1 will grow and fill the empty space available in the flexbox like given in the image below.But the other two will remain the same.

flex-grow one item1 illustration

if we do the same to the flex-item2 then it will grow same as the flex-item1 and fill the empty space. Here both the item will grow equally and at same rate. But still the third will remain the same.

see the below image for clear understanding. item12 flex grow illustration

Same thing will happen if we apply the property to flex-item3.Here, all the items will grow equally.

all three grow illustration

Same thing will work with the shrink property too. They’ll shrink according to their shrink value.

Conclusion :

  1. flex-grow value = speed of growth of item

  2. flex-shrink value = speed of shrink of item.

4. flex :

Short hand for applying all these properties in a single line :

/* flex : flex-grow | flex-shrink | flex-basis ; */
flex : 0 1 250px;

5. align-self :

The last property remaining is order let’s see it & understand how it works.

6. order :

Let’s understand with example how the order will work.

.one {
    flex-grow: 1;
    flex-shrink: 1;
    flex-basis: 250px;
}

.two {
    flex-grow: 1;
    flex-shrink: 1;
    flex-basis: 250px;
    align-self: flex-end;
    order: 1;
}

.three {
    flex-grow: 1;
    flex-shrink: 1;
    flex-basis: 250px;
}

In the above code you can see the .two or flex-item2 has the order of 1. and other have no order value so it will take 0 as the default order.

The items with order:0; will remain same as earlier with no changes but the flex-item2 whose order we set to 1 will move to the last as it’s the highest order value among all items. See image below.

order illustration

Conclusion : The item with less order will appear before and those with the higher order value will appear after the items with less order.