Loading...

Go Back

Next page
Go Back Course Outline

CSS Full Course


Flexbox Layout in CSS

Flexbox Layout Guide

1. Flexbox Fundamentals:

Flexbox is a CSS layout model that allows items inside a container to be arranged in flexible, responsive ways.

2. Flex Container Properties:

a. display: flex

This property turns an element into a flex container, making its children flex items.

                        .container {
                          display: flex;
                        }
                            
Flex Item 1
Flex Item 2

b. flex-direction

This property defines the direction of the flex items inside the container. It can be row (default), row-reverse, column, or column-reverse.

                        .container {
                          display: flex;
                          flex-direction: row; /* Items placed in a row */
                        }
                            
Item 1
Item 2


c. justify-content

This property aligns flex items along the main axis (horizontal by default). Values include: flex-start, flex-end, center, space-between, space-around, space-evenly.

                        .container {
                          display: flex;
                          justify-content: center; /* Centers items along the horizontal axis */
                        }
                            
Item 1
Item 2

d. align-items

This property aligns flex items along the cross axis (vertical by default). It can take values like flex-start, flex-end, center, stretch, and baseline.

                        .container {
                          display: flex;
                          align-items: center; /* Centers items vertically */
                        }
                            
Item 1
Item 2

e. flex-wrap

This property controls whether flex items should wrap onto multiple lines if needed. Values: nowrap, wrap, wrap-reverse.

                        .container {
                          display: flex;
                          flex-wrap: wrap; /* Items will wrap when necessary */
                        }
                            
Item 1
Item 2
Item 3
Item 4


3. Flex Item Properties:

a. flex-grow

This property defines how much a flex item should grow relative to the other items. A value of 0 means it won’t grow.

                        .item {
                          flex-grow: 1; /* Item will grow to fill available space */
                        }
                            
Item 1
Item 2
Item 3

b. flex-shrink

This property defines how much a flex item should shrink if the container becomes smaller. A value of 0 means it won’t shrink.

                        .item {
                          flex-shrink: 0; /* Item will not shrink */
                        }
                            
Item 1
Item 2
Item 3

Flexbox is a powerful layout system in CSS that allows you to align and distribute space between items in a container with ease. By using various flex properties, you can create responsive layouts with minimal effort.



Go Back

Next page