CSS flexbox - difference between display: flex and display: inline-flex

Published: 12 January 2022
on channel: Sergiy Prygara
5,634
195

https://www.smashingmagazine.com/2018...

The key to understanding the difference between display: flex and display: inline-flex is that each display property actually consists of two components: outer display model and inner display model. Let's take a look at 3 scenarios to help us understand what happens when we add display: flex; and display: inline-flex; to our container divs.

Scenario#1. Without adding any display: flex; or display: inline-flex; our two containers default to display: block (see Chrome dev tools) and our containers stack vertically.

Scenario#2. When we add display: flex to our containers, 2 things happen:
1. The outer display model of our flex container becomes "block" and acts like a block level element in the normal flow. In other words, the container becomes display: "block" flex.

2.The inner display model of our flex container becomes flex, so items inside flex container (direct children) become flex items and participate in the flex layout. If we don't declare any flex-direction property they default to flex-direction: row and are placed along the main axis (horizontally in our case).

Scenario#3. When we add display: inline-flex to our containers, again 2 things happen:

1.The outer display model of our flex container becomes "inline" and the container acts as inline level element. In other words, the container becomes display: "inline" flex.

2. However, same as in Scenario#2 , the inner display model of our flex container remains flex so its direct children participate in a flex layout.

The main difference between the above two display properties is in their outer display models: block or inline which dictates how they behave within the layout.


On this page of the site you can watch the video online CSS flexbox - difference between display: flex and display: inline-flex with a duration of hours minute second in good quality, which was uploaded by the user Sergiy Prygara 12 January 2022, share the link with friends and acquaintances, this video has already been watched 5,634 times on youtube and it was liked by 195 viewers. Enjoy your viewing!