VOOZH about

URL: https://blog.logrocket.com/flexing-with-css-flexbox-b7940b329a8a/

โ‡ฑ Flexing with CSS Flexbox - LogRocket Blog


2018-06-18
1070
#css
Nosa Obaseki
273
๐Ÿ‘ Image

See how LogRocket's Galileo AI surfaces the most severe issues for you

No signup required

Check it out
๐Ÿ‘ Image

Flexbox also known as flexible box is a type of layout model in CSS that makes it super easy to design responsive layouts.

The whole idea behind the Flexbox layout model is to allow elements to be laid out in any direction, flex their size to either fill up unused space or shrink to avoid overflowing their parent element, either horizontally or vertically.

To truly be able to flex with Flexbox, we have to understand how it works.

Letโ€™s break it down into its properties into two namely;

  1. Flex Container
  2. Flex Items

Flex Container

This is the parent html element that houses the items you want to lay out.

To use any of the flex properties, this container has to be created.

Itโ€™s what creates the context that allows every other flex properties to work.

See the Pen
Flex 1
by Obaseki Nosa (@c0depanda)
on CodePen.

Creating a Flex container

align-items

This allows you to align the items in the flex container vertically, regardless of the height of the item with respect to their flex container or each other.

The values it accepts are: flex-start | flex-end | center | baseline | stretch

See the Pen
Flex โ€“ align-items
by Obaseki Nosa (@c0depanda)
on CodePen.

Example showing all the different value the align-items property takes

justify-content

This is the opposite of align-items, it aligns its item horizontally regardless of the width of the item with respect to their container or each other.

Values: flex-start | flex-end | center | space-between | space-around | space-evenly

See the Pen
justify content
by Obaseki Nosa (@c0depanda)
on CodePen.

Showcasing Justify-content values

flex-wrap

The flex-wrap property specifies whether the flex items should break to the next line or not.


๐Ÿš€ Sign up for The Replay newsletter

The Replay is a weekly newsletter for dev and engineering leaders.

Delivered once a week, it's your curated guide to the most important conversations around frontend dev, emerging AI tools, and the state of modern software.

Over 200k developers use LogRocket to create better digital experiences

๐Ÿ‘ Image
Learn more โ†’

By default all flex items will try to fit in a single line, but this property tells the browser to break them into another line when they become way too many to fit in a single line.

This line we speak of is also known as a Flex line.

Values: nowrap | wrap | wrap-reverse

See the Pen
Flex Wrap
by Obaseki Nosa (@c0depanda)
on CodePen.

Flex-wrap with itโ€™s values

align-content

This property modifies the behavior of the flex-wrap property.

It essentially behaves like the align-items property, only that it aligns the flex lines instead of the flex items.

To make this property work, flex-wrap: wrap has to be set on the flex container and the flex lines have to be more than one.

Values: flex-start | flex-end | center | space-between | space-around | stretch

See the Pen
align-content
by Obaseki Nosa (@c0depanda)
on CodePen.

Align content with all itโ€™s accepted values

flex-direction

This defines which direction the browser should stack the Flex items i.e vertically or horizontally.

Values: row | row-reverse | column | column-reverse

See the Pen
flex-direction
by Obaseki Nosa (@c0depanda)
on CodePen.

Flex-direction example showcasing the various values

flex-flow

This is a shorthand for flex-direction and flex-wrap so instead of writing the below;

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

you can just do this;


More great articles from LogRocket:


.container {
 display: flex;
 flex-flow: column wrap;
}

Flex Items

This are the direct children of a Flex container and just like the Flex container, they also have properties that can be applied to them.

orders

This property allows you to change the order in which the Flex items are arranged. i.e according to the HTML structure source code.

Flex items that are within the same container are laid out in ascending order based on the order value, while flex items that have the same order value are laid out in the order they appear in the source code.

Values: <integers> (positive or Negative)

See the Pen
Order
by Obaseki Nosa (@c0depanda)
on CodePen.

Example showing how order for flex items work

align-self

As the name implies, the property allows you to align the selected flex item vertically inside its flex container.

It basically works the same way align-items works, only that itโ€™s meant for an individual Flex item.

This is useful for when you only want to align a single Flex item instead of all the Flex items in a Flex container.

Note: This property overrides the flex containerโ€™s align-items property.

Values: auto | flex-start | flex-end | center | baseline | stretch

See the Pen
align-self
by Obaseki Nosa (@c0depanda)
on CodePen.

Align-self Flex Property

flex-basis

This property sets the initial size of the selected flex item. Itโ€™s very similar to the way width property works.

The flex-basis property accepts values in length unit i.e px, em, rem, percentage etc. It also accepts auto which basically decides how the flex items would be distributed based on if a flex-grow is set or based off the content size of the flex item.

See the Pen
flex-basis
by Obaseki Nosa (@c0depanda)
on CodePen.

Flex basis showing auto, 200px & 40%

flex-grow

This property gives flex items magical powers such that they can grow to fill up a flex container or take more space than other elements in the container.

It basically specifies what amount of space inside the flex container the item should take up.

Values: <integers> (positive)

See the Pen
flex-grow
by Obaseki Nosa (@c0depanda)
on CodePen.

Flex-grow with its values

flex-shrink

This is the opposite of flex-grow which basically lets the flex item shrink when necessary. I.e Other items in the flex-container are trying to take more space.

Values: <integers> (positive)

See the Pen
flex-shrink
by Obaseki Nosa (@c0depanda)
on CodePen.

Example showing Flex-shrink

Based on the example above, there are 5 Flex items with flex-basis: 200px set on them which makes is 1000px in total.

The Flex container on the other hand has been set to 400px which is smaller than the flex items but because flex-wrap defaults to nowrap we would have all the items trying to fit the Flex container thus distributing theirselves evenly.

Once the flex-shrink values are set on this items they would start to adjust based on the values.

The higher the flex-shrink value is the more it would shrink compared to other Flex items.

flex

This is a shorthand property for flex-grow , flex-shrink and flex-basis but the flex-shrink and flex-basis are optional, i.e you can just set only the flex-grow and the rest will be set to their defaults.

By default the values are set to flex: 0 1 auto i.e flex-grow: 0 , flex-shrink: 1 and flex-basis: auto.

Found this article helpful? Donโ€™t forget to clap and share ๐Ÿ™‚

Is your frontend hogging your users' CPU?

As web frontends get increasingly complex, resource-greedy features demand more and more from the browser. If youโ€™re interested in monitoring and tracking client-side CPU usage, memory usage, and more for all of your users in production, try LogRocket.

๐Ÿ‘ LogRocket Dashboard Free Trial Banner

LogRocket lets you replay user sessions, eliminating guesswork around why bugs happen by showing exactly what users experienced. It captures console logs, errors, network requests, and pixel-perfect DOM recordings โ€” compatible with all frameworks.

LogRocket's Galileo AI watches sessions for you, instantly identifying and explaining user struggles with automated monitoring of your entire product experience.

Modernize how you debug web and mobile apps โ€” start monitoring for free.


๐Ÿ‘ Image
๐Ÿ‘ Image
๐Ÿ‘ Image

Stop guessing about your digital experience with LogRocket

Get started for free

Recent posts:

Debug Next.js apps with AI agents and next-browser

Learn how next-browser gives AI agents runtime context for debugging Next.js apps, including React props, hydration, PPR, forms, and performance.

๐Ÿ‘ Image
Emmanuel John
Jun 17, 2026 โ‹… 9 min read

Stop hardcoding LLM SDKs: Dynamic LLM routing with OpenRouter and Next.js

Build dynamic LLM routing in Next.js with OpenRouter, TanStack AI, task classification, model fallbacks, and cost-aware routing.

๐Ÿ‘ Image
Chizaram Ken
Jun 16, 2026 โ‹… 13 min read

What is TSRX?: What JSX would look like if it were designed today

TSRX adds first-class control flow, conditional hooks, and scoped styles to React via a TypeScript compiler extension โ€” no new framework required.

๐Ÿ‘ Image
Ikeh Akinyemi
Jun 12, 2026 โ‹… 6 min read

How to add authentication to a React Native app with Better Auth

Learn how to build a full React Native auth system using Better Auth and Expo โ€” with email/password login, Google OAuth, session persistence, and protected routes.

๐Ÿ‘ Image
Chinwike Maduabuchi
Jun 9, 2026 โ‹… 13 min read
View all posts

Hey there, want to help make our blog better?

Join LogRocketโ€™s Content Advisory Board. Youโ€™ll help inform the type of content we create and get access to exclusive meetups, social accreditation, and swag.

Sign up now