Learn CSS Flexbox in 20 Minutes (Course)

Coding2GO
6 Aug 202420:37

Summary

TLDRThis script offers an in-depth exploration of CSS Flexbox, a powerful tool for creating responsive layouts. It explains the core concepts of flexbox, including the main and cross axes, alignment properties like 'justify-content' and 'align-items', and the use of 'flex-wrap' for responsive design. The tutorial also covers advanced features such as 'flex-grow', 'flex-shrink', and the application of minimum and maximum sizes for elements. It highlights the ease of centering elements and managing overflow with flexbox, providing practical examples and tips for web developers looking to enhance their layouts.

Takeaways

  • 😎 Understanding Flexbox can significantly improve one's ability to create responsive layouts in CSS.
  • πŸ“ To align elements within a flex container, it's essential to consider both the main and cross axes.
  • πŸ”„ The 'justify-content' and 'align-items' properties are key for positioning elements along the main and cross axes respectively.
  • πŸ“Š The 'display: flex' property transforms the layout system, allowing for side-by-side positioning of elements.
  • πŸ”„ 'Flex-start', 'flex-end', and 'center' are basic values for aligning elements horizontally and vertically.
  • 🌐 The 'flex-direction' property controls the direction of the main axis, which can be row, row-reverse, or column.
  • πŸ”„ 'Flex-wrap' allows for responsive layouts by wrapping items to the next line when necessary.
  • πŸ”„ 'Flex-grow' and 'flex-shrink' properties enable elements to grow or shrink responsively within the container.
  • πŸ“ Minimum and maximum sizes can be combined with 'flex-grow' and 'flex-shrink' to define limits for resizing elements.
  • πŸ”„ The 'align-self' property allows for individual alignment of flex items on the cross axis, different from the container's 'align-items'.
  • 🌟 CSS Grid can sometimes achieve the same layout results with fewer lines of code, offering an alternative for complex layouts.

Q & A

  • What is the main benefit of using CSS Flexbox for layout design?

    -CSS Flexbox allows for the creation of responsive and flexible layouts with ease, enabling developers to align elements in various positions within a container and handle resizing behavior effectively.

  • How does understanding Flexbox improve one's approach to CSS coding?

    -Understanding Flexbox provides a superior grasp of layout dynamics in CSS, making developers more confident when dealing with common CSS problems such as centering elements or making a website responsive.

  • What is the initial step in creating a Flexbox layout?

    -The initial step is to define a reference point in HTML, which could be the body or a specific container, and then apply 'display: flex' to the parent element to initiate the Flexbox layout.

  • What are the two main axes in a Flexbox layout and why are they important?

    -The two main axes are the main axis and the cross axis. They are important because they control the flow and positioning of items within the Flexbox layout, determining how elements align and distribute space.

  • How can you horizontally center elements using Flexbox?

    -To horizontally center elements, apply 'display: flex' and 'justify-content: center' to the parent container.

  • What property is used to align elements vertically in a Flexbox layout?

    -The 'align-items' property is used to align elements vertically within a Flexbox layout.

  • What does the 'flex-direction' property do in Flexbox?

    -The 'flex-direction' property determines the direction of the main axis, which can be 'row' (default), 'row-reverse', 'column', or 'column-reverse', affecting the layout flow of the flex items.

  • How can you make a responsive layout that wraps elements to the next line when necessary?

    -Use the 'flex-wrap' property set to 'wrap' to create a responsive layout that allows elements to flow to the next line when there is not enough horizontal space.

  • What is the purpose of the 'flex-grow' property in Flexbox?

    -The 'flex-grow' property enables elements to grow and fill out available space within the parent container along the main axis, with higher values allowing elements to grow at a faster rate compared to others.

  • How can you prevent certain flex items from shrinking when the container size decreases?

    -Apply a 'flex-shrink' value of zero to specific flex items to prevent them from shrinking when the container size decreases.

  • What is the 'align-self' property used for in Flexbox?

    -The 'align-self' property is used to align a specific flex item differently from the alignment defined by 'align-items' on the cross axis, allowing for individual alignment control over flex items.

  • How can you create gaps between flex items?

    -The 'gap' property in Flexbox can be used to create consistent gaps between items. It can also be split into 'row-gap' and 'column-gap' for different horizontal and vertical spacing.

  • What is the recommended approach to center a div using CSS Grid compared to Flexbox?

    -In CSS Grid, you can center a div with even fewer lines of code by using 'display: grid' and 'place-content: center', which simplifies the process compared to the three lines required in Flexbox.

Outlines

00:00

🌟 Introduction to CSS Flexbox

This paragraph introduces the concept of CSS Flexbox as a solution to common layout issues such as overflowing elements and difficulty in creating responsive designs. The speaker shares their experience with Flexbox, emphasizing its transformative impact on their approach to CSS coding. The importance of understanding HTML as a foundation for Flexbox is highlighted, with the body or a specific container serving as the reference point for layout. The paragraph sets the stage for learning how to align elements within a parent element using Flexbox properties, starting with the 'display: flex' property and the axes concept, which includes the main axis and the cross axis. It also touches on the 'justify-content' and 'align-items' properties for horizontal and vertical alignment, and introduces the additional values these properties can take, such as 'space-between', 'space-around', and 'space-evenly', to create balanced gaps between elements.

05:02

πŸ”„ Advanced Flexbox Techniques

The second paragraph delves deeper into advanced Flexbox techniques, starting with the 'flex-direction' property, which controls the direction of the main axis and affects how elements are laid out and aligned. The speaker explains how changing the 'flex-direction' from 'row' to 'row-reverse' or 'column' alters the alignment behavior of 'justify-content' and 'align-items'. The paragraph also covers the 'gap' property for creating space between items and the 'flex-wrap' property for making layouts responsive by allowing items to wrap onto the next line when necessary. The 'align-content' property is introduced for aligning lines of items within a flex container when wrapping occurs. The speaker emphasizes the flexibility of Flexbox in creating complex layouts with minimal code.

10:02

πŸ“ Flexbox Sizing and Responsiveness

This paragraph focuses on the responsive sizing of elements within a Flexbox layout. The speaker discusses the 'flex-shrink' property, which controls how elements shrink when the container size is reduced, and demonstrates how to apply it to prevent certain elements from resizing. The 'flex-grow' property is introduced as a means to allow elements to grow and fill available space in the container. The paragraph explains how to apply different 'flex-grow' values to specific elements to control their growth behavior relative to others. The concept of using 'flex-grow' and 'flex-shrink' as multipliers is explored, along with the use of minimum and maximum sizes to define limits for resizing. The speaker also mentions the combination of these properties with media queries to create responsive designs that adapt to different screen sizes.

15:03

🎯 Fine-Tuning Flexbox Alignment

The fourth paragraph discusses fine-tuning the alignment of elements within a Flexbox layout. The 'align-self' property is introduced, allowing individual flex items to override the 'align-items' setting of the container. The speaker notes the limitation of 'align-self' to the cross-axis and the absence of a corresponding property for the main axis, suggesting the use of traditional CSS techniques like 'margin-right: auto' for certain layout scenarios. The paragraph also summarizes key takeaways from the discussion on Flexbox, emphasizing the ability to position elements anywhere within a container by considering the main and cross axes, wrapping elements with 'flex-wrap', and resizing elements with 'flex-grow' and 'flex-shrink'. The speaker teases the introduction of CSS Grid as a potentially more straightforward approach for certain layout tasks.

20:04

πŸš€ CSS Grid: Simplifying Layouts

In the final paragraph, the speaker introduces CSS Grid as an alternative to Flexbox for creating layouts, suggesting that it can simplify the process and reduce the amount of code needed. The paragraph contrasts the three-line approach of Flexbox for centering a div with the two-line method using CSS Grid, highlighting the efficiency of Grid for certain tasks. The speaker promotes their HTML and CSS complete course for learning both Flexbox and Grid through practical projects. The paragraph concludes with a call to action for viewers to learn more about web development and CSS layouts by following the provided link in the video description.

Mindmap

Keywords

πŸ’‘Flexbox

Flexbox is a layout model in CSS that allows for the arrangement of elements in a flexible and responsive manner. It is central to the video's theme, as it is the main subject being discussed. The script explains how Flexbox can be used to create responsive layouts and solve common CSS problems, such as centering a div, with just a few lines of code. Examples from the script include using 'display: flex' to enable Flexbox and aligning elements using 'justify-content' and 'align-items'.

πŸ’‘Responsive Layouts

Responsive layouts refer to web design approaches that ensure web pages render well on different devices and window or screen sizes. The video emphasizes the importance of responsive layouts and how Flexbox can facilitate their creation. The script mentions that with Flexbox, developers can easily make a website responsive by adjusting elements' sizes and positions dynamically based on the container's size.

πŸ’‘Centering Elements

Centering elements is a common design task in web development, and the script discusses how Flexbox simplifies this process. The video shows that by using 'display: flex' along with 'justify-content: center' and 'align-items: center', elements can be centered both horizontally and vertically within their parent container. This is a key example used to demonstrate the power and ease of use of Flexbox.

πŸ’‘Main Axis

In Flexbox, the main axis is the primary direction in which flex items are laid out. The script explains that understanding the main axis is crucial for aligning and distributing space among items within a flex container. The main axis is controlled by properties like 'justify-content', which can have values such as 'flex-start', 'flex-end', or 'center', as illustrated in the script when discussing the alignment of boxes.

πŸ’‘Cross Axis

The cross axis in Flexbox is perpendicular to the main axis and is used to align items on the axis that runs across the flex container. The script mentions that the cross axis is important for vertical alignment and is controlled by the 'align-items' property, which can also have values like 'flex-start', 'flex-end', or 'center', as used to center elements vertically in the video.

πŸ’‘Justify Content

The 'justify-content' property in Flexbox is used to align items along the main axis of a flex container. The video script provides examples of its use, such as 'justify-content: center' to center items horizontally. It is a fundamental concept in achieving various alignments within a flex layout.

πŸ’‘Align Items

The 'align-items' property in Flexbox is used to align items along the cross axis of a flex container. The script discusses how this property, with values like 'flex-start', 'flex-end', or 'center', can be used to achieve vertical alignment of items. It is a key concept in the video for centering elements within a flex container.

πŸ’‘Flex Direction

The 'flex-direction' property in Flexbox defines the direction of the main axis, which can be 'row', 'row-reverse', 'column', or 'column-reverse'. The script explains how changing this property affects the layout of flex items, allowing for different orientations such as left-to-right or top-to-bottom arrangements.

πŸ’‘Flex Wrap

The 'flex-wrap' property in Flexbox controls whether the flex items are forced into a single line or allowed to wrap onto additional lines. The script mentions 'flex-wrap: wrap' as a way to create a responsive layout that adjusts to the container's size by allowing items to flow to the next line when necessary.

πŸ’‘Gap

The 'gap' property in Flexbox creates space between items within a flex container. The script discusses how this property simplifies the creation of space between items without the need for manual margin adjustments. It is an example of how Flexbox can streamline layout design.

πŸ’‘Flex Grow

The 'flex-grow' property in Flexbox allows flex items to grow and fill up any available space along the main axis. The script explains how enabling 'flex-grow' causes items to expand and fill the container, which is useful for creating flexible and responsive designs. It also mentions that 'flex-grow' can have different values for different items, allowing for control over how much space each item occupies.

πŸ’‘Flex Shrink

The 'flex-shrink' property in Flexbox controls how flex items shrink when there isn't enough space in the container. The script discusses how 'flex-shrink' can be used to prevent items from resizing or to allow them to shrink proportionally, which is important for maintaining layout integrity when space is limited.

πŸ’‘Align Self

The 'align-self' property in Flexbox is used to align a specific item on the cross axis, independent of the 'align-items' setting for the flex container. The script mentions that 'align-self' can be used to override the default alignment for individual items, providing an example where the first box is aligned differently from the others.

πŸ’‘CSS Grid

CSS Grid is another layout system in CSS that is mentioned towards the end of the script as an alternative or complement to Flexbox for creating complex layouts. The video script suggests that while Flexbox is powerful, CSS Grid can offer even more control and simplicity in certain layout scenarios.

Highlights

Introduction to the power of CSS Flexbox for creating responsive layouts.

The transformative effect of understanding Flexbox on CSS coding practices.

Starting with HTML to define a reference point for layout.

Using 'display: flex' to enable Flexbox and change element positioning.

Explanation of the main and cross axes in Flexbox layout.

How to align elements using 'justify-content' and 'align-items' properties.

Centering elements horizontally and vertically with Flexbox.

The concept of 'flex-direction' and its impact on main axis direction.

Using 'flex-wrap' for responsive layouts that wrap elements to the next line.

Introduction of 'align-content' for aligning lines of wrapped elements.

The 'gap' property to create space between flex items.

Flexbox's ability to resize elements responsively using 'flex-shrink'.

The 'flex-grow' property to make elements grow and fill available space.

Combining 'flex-grow' and 'flex-shrink' with minimum and maximum sizes for control.

The 'align-self' property for individually aligning flex items on the cross axis.

Using Flexbox for complex layouts and responsive design with 'flex-wrap' and media queries.

Comparison between Flexbox and Grid layouts for centering elements with fewer lines of code.

Summary of key takeaways from Flexbox for positioning, wrapping, and resizing elements.

Invitation to learn more about CSS Grid and advanced layouts in the complete course.

Transcripts

play00:00

have you ever struggled with overflowing

play00:01

elements or weird resizing behavior in

play00:04

CSS and you just want to be able to

play00:05

create these awesome responsive layouts

play00:07

well then let me tell you today I have

play00:09

something really really special for you

play00:11

I did some research to find out what is

play00:12

the best piece of information we can

play00:14

teach you in CSS and here it is a

play00:16

complete CSS flexbox

play00:20

[Music]

play00:22

course I know from experience that

play00:25

understanding flexbox will get you to a

play00:26

level of superior understanding of how

play00:28

things work in CSS when I first heard

play00:30

about it years ago it completely changed

play00:32

the way I approach coding in CSS it will

play00:34

enable you to create beautiful and

play00:36

responsive layouts in any imaginable way

play00:39

you're going to feel so much more

play00:40

confident When approaching typical CSS

play00:42

problems like how to center a div or how

play00:44

can I make my website responsive with

play00:46

just a few lines of code it all starts

play00:48

with HTML where you have to define a

play00:50

reference point this could be the body

play00:52

or a specific container that you use for

play00:54

your layout every HTML element that

play00:56

holds any type of content can be a flex

play00:58

boox or GD container our first goal is

play01:01

finding out how to align elements

play01:03

anywhere inside its parent element at

play01:05

the top bottom left right center and

play01:07

every combination in between so we are

play01:09

going to answer the most popular

play01:10

beginner question how to center a diff

play01:12

in HTML we have these five diff

play01:14

container boxes they have some very

play01:16

basic Styles in CSS to align your boxes

play01:19

with a flex boox layout you need to

play01:21

address the parent element so we address

play01:23

the body and apply display

play01:26

Flex notice how this immediately changed

play01:29

the way the the boxes are positioned

play01:31

they used to be one below the other and

play01:33

now they are side by side the display

play01:35

property is a very important CSS

play01:37

property most elements have a display of

play01:39

block or inline per default block

play01:42

elements take up the entire width of the

play01:43

screen this results in elements being

play01:45

forced to the next line when we apply

play01:48

display Flex we leave this system behind

play01:50

and enter a new world here things work

play01:52

differently let's add a little border so

play01:55

that we can see the frame of our flexbox

play01:57

layout the most important Concept in

play01:59

flexbox are the axes along which items

play02:02

are positioned the main axis and the

play02:04

cross axis they control the flow of our

play02:06

flexbox layout to position your elements

play02:08

you have to keep both of these axes in

play02:10

mind don't worry I will keep showing

play02:12

them every once in a while to position

play02:14

your elements anywhere along the main

play02:16

axis you need the justify content

play02:18

property this property has three basic

play02:21

values Flex start is at the start of the

play02:24

main axis this is the default value

play02:27

which is why our boxes are here at the

play02:28

left side of our screen

play02:31

Flex end is at the end of the main axis

play02:34

this will align the boxes on the right

play02:36

side and Center is at the center of the

play02:39

main axis so you can use these two lines

play02:42

of code display flex and justify content

play02:44

Center to Center your elements

play02:46

horizontally by the way this video is a

play02:49

free sample of our HTML and CSS complete

play02:51

course so if you like the way we teach

play02:53

and you want to learn more about web

play02:54

development then get the full course

play02:56

right now now let's see how we can align

play02:58

elements vertically

play03:00

in this case we need to consider the

play03:02

cross axis to do this let's increase the

play03:05

size of the flexbox layout by applying a

play03:07

Min height of 800 pixels in the body now

play03:10

we can see that this black border is

play03:12

basically the frame of our flexbox

play03:14

layout to Center your elements on the

play03:16

cross axis we use the Align items

play03:18

property this property can have the same

play03:21

values as justify content so you have

play03:24

Flex start for top positioning meaning

play03:27

at the start of the Cross

play03:28

axis X end for bottom positioning now

play03:32

they are down here and of course Center

play03:35

to put them in the

play03:36

center and now we have finally answered

play03:39

how to center a div you just need these

play03:41

three lines of code to Center elements

play03:43

along the main and cross axis since

play03:46

flexbox is flexible this layout will

play03:48

work even if we change the size of the

play03:51

layout the boxes will always be in the

play03:53

center no matter if we change the height

play03:55

and width of the body but justify

play03:58

content and align items can have three

play04:00

additional and more complex values space

play04:02

between space around and space evenly

play04:06

space between will distribute the

play04:07

elements from left to right the first

play04:10

and last element will touch the edge of

play04:12

the layout depending on how big the

play04:14

container is the elements will have a

play04:16

bigger Gap in

play04:17

between space around Works similarly but

play04:20

here the first and last element do not

play04:22

touch the edge instead every element

play04:25

gets some space to the left and right as

play04:27

you can see even when I resize the

play04:29

entire website the spacing between the

play04:31

elements adjusts

play04:32

automatically but here we run into a

play04:34

slight inconsistency that just triggers

play04:36

my OCD since the right space of the Box

play04:39

adds up with the left space of the

play04:41

second box it will result in these

play04:43

spaces being twice as big as the spaces

play04:45

that we have to the edges if you don't

play04:48

want that you can use space evenly here

play04:51

all the spaces are the same size and now

play04:53

finally the gaps are perfectly balanced

play04:55

as all things should be and of course

play04:58

all of these values are completely

play04:59

flexible they adjust perfectly when the

play05:02

size of the container changes and that's

play05:04

the whole point of using Flex boox

play05:06

theoretically you could achieve the same

play05:08

thing centering a div and positioning

play05:10

your elements using only margins and

play05:11

padding but they are just not as

play05:13

flexible as a flexbox layout by the way

play05:16

you can use the same values on the Align

play05:18

items property as well but in this case

play05:20

it wouldn't make sense since we only

play05:22

have one row of content the boxes will

play05:25

all just be at the start of the Cross

play05:26

AIS but it is possible to use these

play05:29

values on the cross axis as you will

play05:31

learn later once the layouts get more

play05:32

complex for now let's learn about

play05:34

another interesting flexbox property

play05:36

which is flex direction to understand it

play05:39

easier let's remove align items and just

play05:41

use justify content Flex start for now

play05:44

the flex Direction property controls the

play05:46

direction of the main axis its default

play05:49

value is row which makes the main axis

play05:51

go from left to right you could also use

play05:54

row reverse to make it go from right to

play05:57

left looking at the numbers we can see

play06:00

that it starts counting from the right

play06:02

but this property also changed the way

play06:04

our justifi content value works now as

play06:07

the main axis goes from right to left

play06:09

the value Flex start is on the right

play06:11

side and flex end is on the left

play06:14

side you can also make the main axis go

play06:17

from top to bottom by applying Flex

play06:19

Direction

play06:20

column now the elements are no longer

play06:23

side by side but instead on top of each

play06:25

other since the direction of the main

play06:27

axis changed the direction of the Cross

play06:29

ax has changed as well now if you want

play06:32

to Center these boxes horizontally you

play06:33

no longer use Justified content but

play06:36

instead align items since justify

play06:38

content aligns the elements on the main

play06:40

axis which goes from top to bottom it

play06:42

doesn't make sense now to use it we need

play06:45

to use align items Center to Center them

play06:47

on the cross axis so I hope you

play06:50

understand that it is really important

play06:51

to remember the flex direction of your

play06:53

layout as this completely changes the

play06:55

way all the other properties work per

play06:58

default flexbox uses a Flex Direction

play07:00

row this will align the elements side by

play07:03

side but you're also going to want to

play07:05

change it to flex Direction column when

play07:07

you want your elements to be aligned

play07:09

vertically here is a very common use

play07:11

case scenario you want your entire

play07:13

website to stay the same everything is

play07:15

aligned vertically but you also want to

play07:18

Center everything horizontally then you

play07:20

would go to the body and apply display

play07:24

Flex Flex Direction

play07:28

column and and align items

play07:31

Center and with that every element will

play07:34

automatically be centered

play07:36

horizontally all right let's get back to

play07:38

our starting point display Flex there

play07:40

are a few more simple flexbox properties

play07:43

before it gets complicated the Gap

play07:45

property will create a gap between our

play07:47

items this property is very simple and

play07:50

you no longer need to use margins inside

play07:52

a flexbox layout for example let's give

play07:54

our boxes a gap of 20 pixels and as we

play07:57

can see now every box has a nice little

play07:59

Gap in

play08:00

between the flex wrap property can make

play08:03

your flexbox layout responsive using

play08:05

only one line of code you either have

play08:08

wrap or no wrap if you use flex wrap you

play08:12

will get a responsive layout that will

play08:14

align your items on the next line if we

play08:16

don't have enough space resizing the

play08:19

window will show how the elements flow

play08:21

to the next line when necessary if you

play08:23

use flex wrap no wrap that will not

play08:25

happen and instead the boxes will shrink

play08:28

together

play08:30

using a flex wrap of wrap will make the

play08:32

elements flow to the next

play08:34

line if we want this layout to be

play08:36

centered using justify content Center

play08:38

and align item Center we end up with

play08:41

this layout while the gap between the

play08:43

first line and the second line is so big

play08:45

will become clear once we have a few

play08:47

more boxes so let's say we have nine

play08:50

boxes in

play08:51

HTML just add a few boxes and number

play08:54

them

play08:55

properly then the flex box layout will

play08:58

have more line braks when when we use

play08:59

flx wrap the issue that we are running

play09:02

into is that we no longer have one main

play09:04

axis and one cross axis but every line

play09:07

has its own Main and cross axis in this

play09:10

case we have three of them the Align

play09:13

items property will only control the

play09:15

alignment along each individual cross

play09:17

axis Flex start puts the boxes at the

play09:19

start of each cross

play09:21

axis Flex end at the

play09:24

end and Center in the

play09:27

center but now we also need another CSS

play09:29

property to control the alignment of all

play09:31

the lines together for that we use align

play09:34

content align content has a default

play09:37

value of space around and this is why

play09:39

the gaps between the lines are so

play09:42

big we can also apply space between or

play09:44

space evenly to change

play09:46

that and of course we can also use the

play09:49

basic values Flex start Flex end and

play09:53

Center so the difference between align

play09:56

content and align items is align items

play09:58

will control the alignment along the

play10:00

cross axis of every flexbox line

play10:02

individually and align content will

play10:04

control the alignment of all lines

play10:06

together ultimately the perfectly

play10:08

centered layout in flexbox is when you

play10:10

Center all three of these properties and

play10:13

wrap the elements automatically when

play10:15

necessary justify content Center align

play10:17

item Center and align content Center by

play10:20

the way when we have a layout like this

play10:22

where are horizontal gaps and vertical

play10:24

gaps then you can actually split the Gap

play10:27

property into two different properties

play10:29

row Gap and column Gap you can use these

play10:32

to apply different values for the

play10:33

horizontal and vertical gaps let's say

play10:36

column Gap is 10 pixels and row Gap is

play10:39

20 pixels this way we applied different

play10:42

values for the horizontal and vertical

play10:43

gaps but most likely you will not need

play10:46

that and just use the normal Gap

play10:47

property to control both at the same

play10:50

time these properties are not so

play10:52

important on flexbox but they will

play10:53

become interesting when you have more

play10:55

complex layouts in Grid later in the

play10:57

course by now you should have a good

play10:59

understanding of alignment and flexbox

play11:01

using these flexbox properties and their

play11:03

different combinations of values you can

play11:05

create any alignment possibility you

play11:07

want and of course if you don't know by

play11:10

now you can also use flexbox in every

play11:11

other HTML element as well it does not

play11:14

have to be the body for example a little

play11:17

thinking exercise for beginners if we

play11:19

want to Center the numbers inside the

play11:20

boxes which are currently at the top

play11:22

left corner how would you do that how

play11:24

can you Center the numbers inside the

play11:26

boxes pause the video and try it out

play11:28

yourself

play11:30

the answer is pretty straightforward we

play11:32

do the same thing as earlier but now

play11:34

Inside the Box selector display Flex to

play11:37

enable flexbox justify content center

play11:40

for the main axis and align item center

play11:43

for the cross AIS and this way we

play11:46

centered the numbers inside the

play11:48

boxes obviously you can use all the

play11:50

different combinations we talked about

play11:52

with black star and flex and here as

play11:54

well now let's move on to something very

play11:56

powerful in flexbox you don't always

play11:59

have to wrap elements in Flex box you

play12:01

can also resize them responsively so our

play12:04

next goal is this layout where the boxes

play12:07

grow and drink

play12:08

responsively to do that let's go back to

play12:11

our layout of five boxes in HTML so

play12:14

remove a few boxes so that we have five

play12:16

boxes again in CSS we remove any Flex

play12:19

box properties except display flex and

play12:21

gap of 10 pixels this is as simple as it

play12:25

gets now since we have no flex rep

play12:28

applied the boxes will be resized

play12:29

automatically if the viewport gets too

play12:31

small this Behavior can be specified

play12:34

using Flex shrink and interestingly this

play12:37

is something that you apply on the flex

play12:39

items not on the flex container inside

play12:41

the boxes we can apply a flex rink of

play12:43

zero for example this will prevent the

play12:46

shrinking of the boxes they can no

play12:47

longer be resized and will overflow the

play12:49

container this is also the case for

play12:51

every non-flex box layout when you have

play12:54

the problem of overflowing elements like

play12:55

this then consider using Flex box to

play12:58

either wrap the elements to with the

play12:59

next line or enable flexx rink with a

play13:01

value of one to make them shrink

play13:03

automatically but remember Flex rink of

play13:06

one is the default value you can imagine

play13:08

this property like a switch that you can

play13:09

turn on and off now when we have Flex

play13:12

rink applied we can see how the boxes

play13:14

resize automatically currently we are

play13:16

addressing every box the same way using

play13:18

their class so every box has the same

play13:20

Flex rink value but you can also use

play13:22

different Flex rink values on each item

play13:25

so in HTML I give the first box an ID

play13:28

box one I want to style this one

play13:30

differently when I address this box in

play13:32

CSS I could give this box a flex ring of

play13:35

zero for example and the other ones

play13:37

still have a flex ring of

play13:39

one this will result in this Behavior

play13:42

every element rings when necessary

play13:44

except the first one it will stay the

play13:46

same you could use this intentionally if

play13:49

you don't want specific elements to

play13:50

shrink for example if you don't want to

play13:53

distort an image or icon then it is

play13:55

pretty useful to disable Flex ring on

play13:57

that element now Flex rink is pretty

play14:00

useful but actually what I use way more

play14:02

often is its counterpart Flex grow Flex

play14:06

grow enables elements to grow meaning to

play14:08

stretch along the main axis its default

play14:11

value is zero so elements do not grow

play14:13

per default but once you enable it Flex

play14:16

grow one all the flex items try to fill

play14:19

out the empty space inside their parent

play14:21

element this means if there's more empty

play14:23

space the elements will be bigger they

play14:25

grow Flex grow tries to fill out all the

play14:28

available space inside the parent

play14:29

element and of course you can address

play14:32

specific elements and apply a different

play14:34

grow behavior let's say for the boxes

play14:36

the flex grow is zero so they cannot

play14:38

grow but box number one can

play14:41

grow and as you can see we end up with

play14:44

this effect where only the first element

play14:46

will grow I used this exact behavior

play14:49

when I developed the to-do application

play14:51

here the to-do text grows as much as

play14:53

possible while the checkbox the delete

play14:55

button and all the other elements should

play14:57

not be able to grow this is is a very

play14:59

useful technique to resize specific

play15:01

elements depending on the screen size so

play15:03

you will use it very often when making a

play15:05

website responsive Flex grow and flex

play15:07

rink are not only a Boolean that you can

play15:09

turn on and off but they also work as a

play15:11

multiplier you can assign even higher

play15:13

numbers than one they only Mak sense if

play15:15

they are compared with each other for

play15:17

example let's give the boxes a flex grow

play15:19

of one and the first box a flex grow of

play15:22

five now every element has the ability

play15:25

to grow but the first one does it five

play15:27

times faster this El element will be

play15:29

bigger than the others but every element

play15:31

can potentially grow so you can

play15:33

basically apply different grow values to

play15:35

control how much of the empty space they

play15:37

should fill this does not mean that this

play15:40

box is five times bigger as we can see

play15:42

per default they are all the same size

play15:45

but only when there's new empty space to

play15:47

fill out then this element will Reserve

play15:49

five times more of that new space than

play15:51

the other elements and the same thing

play15:54

can be done for Flex shrink now the

play15:56

first box shrinks five times faster than

play15:58

the other

play15:59

and of course you can address every

play16:01

element on its own and apply a different

play16:03

value for Flex grow and flex rank but I

play16:05

never had a situation where this

play16:07

specific feature was being used since

play16:09

most times you just want to turn on and

play16:11

off the grow and Shrink ability for the

play16:12

elements in a balanced way where they

play16:14

all behave the same way this whole

play16:16

system of flex grow and flex rink

play16:18

becomes even more powerful when you

play16:19

combine it with minimum and maximum

play16:21

sizes such as minwidth and Max width

play16:25

because then you can Define where the

play16:27

flex rink and flex grow should stop

play16:29

you can say my boxes should be able to

play16:31

grow Flex grow of one but they should

play16:34

not become bigger than 300 pixels each

play16:36

Max with 300

play16:38

pixels now the elements grow grow grow

play16:41

grow grow up to the point where they

play16:43

reach the maximum size and the same for

play16:45

Flex shrink they can shrink but only

play16:48

until they reach a minimum width of

play16:50

let's say 100

play16:51

pixels this will make them shrink in the

play16:53

browser as expected but once they reach

play16:56

that size of 100 pixels the elements

play16:58

will overflow

play17:00

and of course overflowing elements is

play17:02

something that you should avoid but

play17:04

sometimes your elements should not get

play17:05

any smaller and need a minimum width to

play17:07

solve the overflowing problem remember

play17:09

that earlier we talked about Flex wrap

play17:12

we can't combine Flex rink and flex wrap

play17:14

by just using a media query normally we

play17:17

use flex rink and flex wrap is disabled

play17:20

with no wrap but once the screen size

play17:23

gets too small and the minwidth causes

play17:25

the element to overflow then we apply a

play17:28

flex wrap of wrap and the element wraps

play17:30

to the next line and this is a beautiful

play17:33

example of how easy it can be to make

play17:35

your website responsive you just use

play17:37

flex shrink and once you don't want it

play17:39

to shrink anymore then you use a flex

play17:42

wrap a special feature of flexbox that I

play17:44

don't really see that often being used

play17:46

is the Align self property this property

play17:50

works just as align items but you use it

play17:52

on the flex items that means let's say

play17:55

we have an align items of flex start and

play17:58

for some reason we want to isolate the

play18:00

first box and apply something else that

play18:02

is not Flex start a line self of flex

play18:05

end for example this will position only

play18:08

the first box at Flex end and everything

play18:10

else stays Flex start of course Center

play18:14

is also possible so you can use this

play18:16

property to align self one item

play18:19

differently on the cross axis but

play18:21

unfortunately I have not found a similar

play18:23

flexbox property for the main axis let's

play18:26

say we have a general Justified content

play18:28

of flex m and we want the first item to

play18:30

be Flex start it's only logical to

play18:32

assume that the property justify self

play18:35

works the same for the main axis but

play18:37

unfortunately it doesn't since justify

play18:39

self is actually a property that you use

play18:41

in CSS grid as you are going to learn

play18:43

later in the course if you want to

play18:45

achieve the desired layout where only

play18:47

the first element is on the left side

play18:48

and every other element is on the right

play18:51

then you would use the old school

play18:52

solution of margin right Auto this is a

play18:55

very useful technique for navigation

play18:57

bars here you want your company logo on

play18:59

the left side and everything else on the

play19:01

right side so just use margin right Auto

play19:05

now we already know a lot about CSS

play19:07

flexbox layouts but before we dive into

play19:09

grid layouts let's summarize the key

play19:11

takeaways of flexbox you can position

play19:13

everything anywhere inside a flex

play19:15

container if you consider the main and

play19:17

cross axis and their properties justify

play19:20

content and align items you can wrap

play19:22

elements to the next line using Flex

play19:25

wrap of wrap you can also resize

play19:27

elements using Flex grow and flex rink

play19:30

and apply different values for each

play19:31

element if that is what you want

play19:33

combining all of that with minimum and

play19:35

maximum sizes and media queries you can

play19:37

already achieve most layouts that you

play19:39

have in mind but if you want to create

play19:41

more complex layouts or you just want to

play19:43

simplify your code then let me introduce

play19:45

CSS grid because there are actually

play19:48

things that are easier in Grid and work

play19:50

with even fewer lines of code here is a

play19:52

common situation how to Cent a div an

play19:55

experienced flexbox guy would say just

play19:57

use display Flex justify content Center

play20:00

and Aline items Center and that's fine

play20:03

but a grid layout guy would say hold on

play20:06

three lines of code I can do it in two

play20:08

display grid Place content center now if

play20:11

you want to learn CSS Grid in flexbox in

play20:13

a practical way by building Advanced

play20:15

projects like this learning page that

play20:17

uses modern flexbox and grid sections

play20:19

then I strongly recommend that you get

play20:21

our HTML and CSS complete course using

play20:23

the first link in the video description

play20:25

my name is Fabian and this was coding to

play20:27

go the channel where you the most

play20:29

relevant coding Concept in just a few

play20:31

minutes

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
CSS FlexboxResponsive DesignWeb DevelopmentLayout CenteringFlexbox PropertiesCoding TutorialGrid LayoutsAlignment TechniquesFrontend WebFlexbox Course