A Guide to Flexbox

A Guide to Flexbox

·

3 min read

What is Flexbox?

It is a way of aligning your elements in a container in a more systematic way, it helps to fit your divisions in a container with proper space, and alignment.

It provides you with the same functionality provided by positioning property but in more simplified and straightforward way. Remember flexbox properties are mostly first applied to outer div

So let's start the journey of learning CSS.

Normal layout of div

Screenshot_1.jpg

now just assigning display = flex will align the items side by side change the alignmemt of items on main axis on the main container div

  .container {
    display: flex;
  }

Screenshot_4.jpg

flex-direction

The flex-direction property specifies how flex items are placed in the flex container, by setting the direction of the flex container’s main axis. This determines the direction in which flex items are laid out.

flex-direction: row; (default value) - lays out item from left to right

flex-direction: row-reverse -lays out item from right to left side in main axis

flex-direction: column -lays out flex item in column wise thus changing the main axis and its properties

flex-direction: column-reverse;

Example

      .container {
        display: flex;
        flex-direction: column;
      }

Screenshot_5.jpg

justify-content

The justify-content property aligns flex items along the main axis of the current line of the flex container. This is done after any flexible lengths and any auto margins have been resolved. Typically it helps distribute extra free space leftover when either all the flex items on a line are inflexible, or are flexible but have reached their maximum size. It also exerts some control over the alignment of items when they overflow the line.

        .container{
        justify-content:flex-start;
        justify-content:center;
        justify-content:flex-end;
        }

justify-content:flex-start(default)

justify-content:flex-end

Screenshot_7.jpg

justify-content:space-between

Screenshot_8.jpg

align-items

This applies to individual items div's inside the main flex container.Flex items can be aligned in the cross axis of the current line of the flex container, similar to justify-content but in the perpendicular direction. align-items sets the default alignment for all of the flex container’s items, including anonymous flex items. align-self allows this default alignment to be overridden for individual flex items. (For anonymous flex items, align-self always matches the value of align-items on their associated flex container.)

align-items:flex-end

image.png

Code Snippet

image.png

      align-items: stretch;
      align-items: flex-start;
      align-items: flex-end;
      align-items: center;
      align-items: baseline;
      align-items: auto;

order

order property is used to arrange items in main axis but changes the order in which they are displayed according to priority of items. The items are then placed in the visual order according to that integer, lowest values first. If more than one item has the same integer value, then within that group the items are laid out as per source order

image.png

image.png

Code snippet

image.png

align-content

The CSS align-content property sets the distribution of space between and around content items along a flexbox's cross-axis

flex-wrap

The flex-wrap property controls whether the flex container is single-line or multi-line, and the direction of the cross-axis, which determines the direction new lines are stacked in.

      .container {
        display: flex;
        flex-wrap: wrap;

      }

image.png

Hope this Helps