Docsity

CSS Flexbox Layout - Flex Container, Direction, Alignment & Responsiveness, Study notes of Technology

Toggle sidebar
bg1
CSS Flexbox Layout
Flex Container, Direction, Alignment & Responsiveness
A Complete Beginner-Friendly Guide to CSS Flexbox
Table of Contents
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
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21

Partial preview of the text

Download CSS Flexbox Layout - Flex Container, Direction, Alignment & Responsiveness and more Study notes Technology in PDF only on Docsity!

CSS Flexbox Layout

Flex Container, Direction, Alignment & Responsiveness

A Complete Beginner-Friendly Guide to CSS Flexbox

Table of Contents

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

Chapter 1: Introduction to CSS Flexbox

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.

Chapter 2: What is Flexbox?

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

Chapter 4: Flex Container

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.

Chapter 5: display: flex

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.

Chapter 6: Flex Items

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.

Chapter 8: flex-direction

The flex-direction property controls the direction of flex items. Syntax: flex-direction: value; Common values are: row row-reverse column column-reverse

Chapter 9: row Direction

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.

Chapter 10: row-reverse

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.

Chapter 11: column Direction

column arranges items vertically. .container { display: flex; flex-direction: column; } Result: 1 2 3 This is useful for: Mobile menus Vertical navigation

Chapter 14: flex-start

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.

Chapter 15: flex-end

Items are placed at the end of the main axis. justify-content: flex-end; Example: [1] [2] [3]

Chapter 16: center

The items are centered along the main axis. .container { display: flex; justify-content: center; } Result:

[1] [2] [3]

This is commonly used for navigation bars and button groups.

Chapter 17: space-between

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.

Chapter 18: space-around

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.

Chapter 19: space-evenly

space-evenly creates equal spacing between items and at the container's edges. justify-content: space-evenly; Example:

Chapter 22: center Alignment

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.

Chapter 23: flex-start and flex-end

Alignment

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.

Chapter 24: align-self

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.

Chapter 25: flex-wrap

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.

Chapter 26: flex-flow

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.

Chapter 29: Row and Column Gaps

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.

Chapter 30: order

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.

Chapter 31: flex-grow

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.

Chapter 34: The flex Shorthand

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

Chapter 35: Flexbox and Width

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.

Chapter 36: Flexbox and Height

Flexbox is also useful for vertical layouts. Example: .container { display: flex;