FlexBox in CSS

Lord Analytics
7 min readAug 1, 2023

--

Hey everyone!!!

I hope you are quite familiar with basic css. So,

Let us embark on a captivating journey into the world of Flexbox with me. Discover the magic of CSS layout made easy, and master responsive designs effortlessly. Let’s unlock the power of Flexbox together!

flexbox meme displayed here

The Flexbox layout(Flexible Box) module aims at providing a more efficient way to lay out, align and distribute space among items in a container, even when their size is unknown and/or dynamic (thus the word “flex”).

Flexbox Properties and it’ s use in CSS:

display

This defines a flex container; inline or block depending on the given value. It enables a flex context for all its direct children.

.container {
display: flex; /* or inline-flex */
}

Note: that CSS columns have no effect on a flex container.

flex-direction

This establishes the main-axis, thus defining the direction flex items are placed in the flex container. Flexbox is (aside from optional wrapping) a single-direction layout concept. Think of flex items as primarily laying out either in horizontal rows or vertical columns.

/* 1st fig */
.container {
display: flex;
flex-direction: row;
}
/* 2nd fig */
.container {
display: flex;
flex-direction: row-reverse;
}
/* 3rd fig */
.container {
display: flex;
flex-direction: column;
}
/* 4th fig */
.container {
display: flex;
flex-direction: column-revrse;
}
  • row (default): left to right in ltr; right to left in rtl
  • row-reverse: right to left in ltr; left to right in rtl
  • column: same as row but top to bottom
  • column-reverse: same as row-reverse but bottom to top

justify-content

This defines the alignment along the main axis. 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 | flex-end | center | space-between | space-around | space-evenly | start | end | left | right ... + safe | unsafe;
}
  • flex-start (default): items are packed toward the start of the flex-direction.
  • flex-end: items are packed toward the end of the flex-direction.
  • center: items are centered along the line
  • space-between: items are evenly distributed in the line; first item is on the start line, last item on the end line
  • space-around: items are evenly distributed in the line with equal space around them. Note that visually the spaces aren’t equal, since all the items have equal space on both sides. The first item will have one unit of space against the container edge, but two units of space between the next item because that next item has its own spacing that applies.

Note: There are other properties like left, right, etc. You can learn more about them here.

align-items

This defines the default behavior for how flex items are laid out along the cross axis on the current line. Think of it as the justify-content version for the cross-axis (perpendicular to the main-axis).

.container {
align-items: stretch | flex-start | flex-end | center | baseline | first baseline | last baseline | start | end | self-start | self-end + ... safe | unsafe;
}
  • stretch (default): stretch to fill the container (still respect min-width/max-width)
  • flex-start / start / self-start: items are placed at the start of the cross axis. The difference between these is subtle, and is about respecting the flex-direction rules or the writing-mode rules.
  • flex-end / end / self-end: items are placed at the end of the cross axis. The difference again is subtle and is about respecting flex-direction rules vs. writing-mode rules.
  • center: items are centered in the cross-axis
  • baseline: items are aligned such as their baselines align.

order

By default, flex items are laid out in the source order. However, the order property controls the order in which they appear in the flex container.

.item {
order: 5; /* default is 0 */
}

flex-wrap

By default, flex items will all try to fit onto one line. You can change that and allow the items to wrap as needed with this property.

.container {
flex-wrap: nowrap | wrap | wrap-reverse;
}
  • nowrap (default): all flex items will be on one line
  • wrap: flex items will wrap onto multiple lines, from top to bottom.
  • wrap-reverse: flex items will wrap onto multiple lines from bottom to top.

flex-flow

This is a shorthand for the flex-direction and flex-wrap properties, which together define the flex container’s main and cross axes. The default value is row nowrap.

.container {
flex-flow: column wrap;
}

align-self

This allows the default alignment (or the one specified by align-items) to be overridden for individual flex items.

Please see the align-items explanation to understand the available values.

.item {
align-self: auto | flex-start | flex-end | center | baseline | stretch;
}

Note:float, clear and vertical-align have no effect on a flex item.

align-content

This aligns a flex container’s lines within when there is extra space in the cross-axis, similar to how justify-content aligns individual items within the main-axis.

.container {
align-content: flex-start | flex-end | center | space-between | space-around | space-evenly | stretch | start | end | baseline | first baseline | last baseline + ... safe | unsafe;
}
  • normal (default): items are packed in their default position as if no value was set.
  • flex-start / start: items packed to the start of the container. The (more supported) flex-start honors the flex-direction while start honors the writing-mode direction.
  • flex-end / end: items packed to the end of the container. The (more support) flex-end honors the flex-direction while end honors the writing-mode direction.
  • center: items centered in the container
  • space-between: items evenly distributed; the first line is at the start of the container while the last one is at the end
  • space-around: items evenly distributed with equal space around each line
  • space-evenly: items are evenly distributed with equal space around them
  • stretch: lines stretch to take up the remaining space

Where to use:

In the last, always keeps these do’s and don’ts in mind:

Do’s:

  1. Use Flexbox for one-dimensional layouts: Flexbox is designed to handle one-dimensional layouts, either in a row (main axis) or column (cross axis). It works best when you need to arrange elements in a single row or column.

2. Use flex container and flex items appropriately: Apply display: flex; to the parent container and display: flex-item; to its children to create a flexbox layout.

3. Use flex-direction to control the layout flow: Set flex-direction to row for a horizontal layout or column for a vertical layout. You can also reverse the order using row-reverse or column-reverse.

4. Use flex-wrap for responsive behavior: When the container size is insufficient to accommodate all items, use flex-wrap: wrap; to allow items to wrap onto the next line.

5. Utilize justify-content and align-items for alignment: Use justify-content to align items along the main axis and align-items to align items along the cross axis.

6. Use flex-grow and flex-shrink to manage item size: Use flex-grow to make items grow proportionally and flex-shrink to make items shrink when the container is too small.

7. Utilize flex-basis to control initial item size: Set flex-basis to set the initial size of the flex item before distribution.

8. Combine flex shorthand for cleaner code: Instead of setting flex-grow, flex-shrink, and flex-basis separately, use the flex shorthand property to set all three at once.

9. Use align-self for individual item alignment: Override the container's align-items for individual items using align-self.

Don’ts:

  1. Avoid using Flexbox for two-dimensional layouts: If you need a grid-based layout with both row and column alignment, consider using CSS Grid Layout instead.

2. Don’t mix Flexbox and floats: Using Flexbox and floats together can lead to unexpected behavior and layout issues. Stick to one layout model.

3. Don’t use unnecessary nested flex containers: Nesting flex containers should be done judiciously, as excessive nesting can lead to complex and hard-to-maintain layouts.

4. Avoid excessive use of flex: 1;: While flex: 1; can be helpful to distribute remaining space, using it excessively may cause items to grow disproportionately.

5. Don’t forget vendor prefixes: Depending on browser support, you may need to use vendor prefixes (-webkit-, -moz-, -ms-) for some Flexbox properties. Consider using a CSS preprocessor like Autoprefixer to handle this automatically.

6. Avoid using fixed heights or widths: Let Flexbox handle the sizing based on content and available space instead of using fixed dimensions. This ensures better responsiveness.

7. Don’t rely solely on Flexbox for complex layouts: For more complex layouts, consider combining Flexbox with other layout techniques like CSS Grid or positioning.

Remember that Flexbox is well-suited for certain scenarios and can greatly simplify many layout tasks. However, it’s essential to understand its limitations and when to use alternative layout methods for more complex requirements. Always test your layouts across different browsers and devices to ensure proper cross-browser compatibility and responsiveness.

Support:

If you have any queries, you can comment or send a mail at vikashanand04@gmail.com.

--

--

Lord Analytics
Lord Analytics

Written by Lord Analytics

0 Followers

Adventures, Movies, Technology and Analytics