

























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


























Introduction to CSS Flexbox What is Flexbox? Why Do We Use Flexbox? Flex Container display: flex Flex Items Main Axis and Cross Axis flex-direction row Direction row-reverse Direction column Direction column-reverse Direction justify-content flex-start flex-end center space-between space-around space-evenly align-items stretch center Alignment flex-start and flex-end Alignment align-self flex-wrap flex-flow align-content gap Row and Column Gaps order flex-grow flex-shrink flex-basis The flex Shorthand
Flexbox and Width Flexbox and Height Creating Navigation Bars Creating Cards with Flexbox Centering Elements Flexbox Responsiveness Media Queries with Flexbox Complete Responsive Example Common Beginner Mistakes Practice Exercises Quick Revision Important Questions Final Summary
CSS Flexbox is one of the most useful layout systems in modern CSS. Before Flexbox, developers often used complicated techniques such as floats, positioning, and tables to create layouts. Flexbox makes it much easier to: Arrange elements horizontally Arrange elements vertically Center elements Create navigation bars Create cards Distribute available space Build responsive layouts For example: .container { display: flex; } This single line changes the layout behavior of the container and its children.
Flexbox stands for Flexible Box Layout.
.container { display: flex; } Flexbox can also easily center the boxes: .container { display: flex; justify-content: center; align-items: center; } Flexbox is especially useful for: Navigation menus Buttons Cards Headers Footers Login forms Sidebars Responsive layouts
The parent element becomes a Flex Container when we use: display: flex; Example:
One Two Three
.container { display: flex; } The three child elements become Flex Items. Remember: display: flex is applied to the parent, not directly to the children.
The most important Flexbox property is: display: flex; Example: .container { display: flex; } Default behavior is generally: Item 1 ā Item 2 ā Item 3 The items are placed along a horizontal main axis by default.
Flex items are the direct children of a flex container. Example:
A
Cross Axis ā Understanding these axes is extremely important because: justify-content works along the main axis. align-items works along the cross axis.
The flex-direction property controls the direction of flex items. Syntax: flex-direction: value; Common values are: row row-reverse column column-reverse
row is the default direction. .container { display: flex; flex-direction: row; } Items are arranged from left to right in a typical left-to-right document.
row-reverse reverses the row direction. .container { display: flex; flex-direction: row-reverse; } Example: 3 2 1 It is useful when you need to reverse the visual order of items.
column arranges items vertically. .container { display: flex; flex-direction: column; } Result: 1 2 3 This is useful for: Mobile menus Vertical navigation
Items are placed at the beginning of the main axis. justify-content: flex-start; Example: [1] [2] [3] This is the default behavior for many flex containers.
Items are placed at the end of the main axis. justify-content: flex-end; Example: [1] [2] [3]
The items are centered along the main axis. .container { display: flex; justify-content: center; } Result:
This is commonly used for navigation bars and button groups.
space-between places equal space between items. .container { display: flex; justify-content: space-between; } Example: [1] [2] [3] There is no extra space before the first item or after the last item from this distribution.
space-around creates space around each item. justify-content: space-around; The outer spaces are smaller than the space between adjacent items because each item receives space on both sides.
space-evenly creates equal spacing between items and at the container's edges. justify-content: space-evenly; Example:
To vertically center items in a row container: .container { display: flex; align-items: center; } For example: | | | [1] | | [2] | | [3] | | | This is very useful for headers and navigation bars.
You can align items toward the beginning: align-items: flex-start; Or toward the end: align-items: flex-end; The exact visual direction depends on the flex direction and writing mode.
align-self allows one flex item to override the container's align-items setting.
Example: .container { display: flex; align-items: center; } .box2 { align-self: flex-start; } Now .box2 can have a different cross-axis alignment from the other items.
By default, flex items try to remain on one line. Use: flex-wrap: wrap; to allow items to move onto additional lines. Example: .container { display: flex; flex-wrap: wrap; } This is very useful for responsive card layouts.
flex-flow is a shorthand for:
.container { display: flex; gap: 20 px; } Result: [Box 1] 20px [Box 2] 20px [Box 3] gap is usually cleaner than adding margins to every item.
You can specify separate row and column gaps. .container { display: flex; row-gap: 20 px; column-gap: 30 px; } Or use: gap: 20px 30px; The first value is the row gap and the second is the column gap.
The order property controls the visual order of flex items. Example: .box1 { order: 3 ; }
.box2 { order: 1 ; } .box3 { order: 2 ; } Items are displayed according to their order values. The default order is: order: 0 ; Important: Changing visual order with CSS does not necessarily change the logical or accessibility reading order. Use order carefully.
flex-grow controls how a flex item can grow when extra space is available. Example: .box1 { flex-grow: 1 ; } If several items have different grow values, available free space is distributed according to their ratios. Example: .box1 { flex-grow: 1 ; } .box2 {
For a row container, this commonly represents an initial width. For a column container, it commonly represents an initial height.
The flex property is a shorthand for: flex-grow flex-shrink flex-basis Example: .box { flex: 1 ; } A common layout pattern is: .container { display: flex; } .box { flex: 1 ; } This allows the boxes to share available space. Another example: .box { flex: 1 1 200 px; } This means:
grow = 1 shrink = 1 basis = 200px
Flexbox can help distribute width between items. Example: .container { display: flex; } .box { flex: 1 ; } The available horizontal space is shared between the flex items. You can also use: .box { width: 200 px; } But remember that flex sizing can interact with width, grow, shrink, basis, and the available container space.
Flexbox is also useful for vertical layouts. Example: .container { display: flex;