CSS Flexbox - Overview



What is Flexbox?



• A CSS3 layout mode that provides and easy and clean way to arrange items within a container
• No floats!
• Responsive and mobile friendly
• Positioning child elements is MUCH easier
• Flex container's margin do not collapse with the margins of its content
• Order of elements can be easily changed without editing the source HTML

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Flexible Box Model Concept



• The ability to alter item width and height to best fit in its containers available free space
• Flexbox is direction-agnostic
• Built for small-scale layouts while the upcoming “Grid” specification is for more large scale

images/223-1.png

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Main Flex Properties



PropertiesValuesNotesScreenshot
displayflex | inline-flexPut elements in a class to a flexbox
flex-directionrow | columnDisplay elements vertically or horizontally
flex-wrapwrap | no warp | wrapreverse
flex-basis<length>Specify width of element in % of the container
justify-contentflex-start | flex-end | centeralign left (default) | right | center
space-between | space-aroundY
align-selfflex-start | flex-end | center
align-itemsflex-start | flex-end | centerdefault: align-items: <stretch>
align-contentflex-start | flex-end | center
flex-grow<number>
flex-shrink<number>
flex<integer>define relative size (base=1) of elements
order<integer>change order of elements


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Display Elements Side by Side



images/223-2.png

/* Display elements side by side */
.container-1{
display:flex; /* Put elements in a class to a flexbox */
}


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Display Elements Vertically



images/223-3.png

/* Display elements vertically */
.container-1{
display:flex;
flex-direction:column;
}


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Display Elements Evenly



images/223-4.png

/* Display Element Evenly */
.box-1{
    flex:1;
}
  
  .box-2{
    flex:1;
}
  
  .box-3{
    flex:1;
}


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Display Elements in Proportion



images/223-5.png

/* Display Element in Proportion */
.box-1{
    flex:2;
}
  
  .box-2{
    flex:1;
}
  
  .box-3{
    flex:1;
}


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Change Order of Display



images/223-6.png

/* Change order of display */
.box-1{
flex:2;
order:2;
}
  
.box-2{
flex:1;
order:1;
}
  
.box-3{
flex:1;
order: 3;
}


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Change the Height



• By default, all elements within a container are of the same height
• To override this, use align-items: flex-start

images/223-7.png

• As shown above, Box One is much shorter because it has less content

/* Change the Height*/
.container-1{
display:flex;
align-items:flex-start; /* move towards the top */
/* align-items:flex-end; */ /* move towards the bottom */
/* align-items:center; */ /* move towards the center */
}


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Specify Margins and Widths



images/223-8.png

• Create another class .container-2 and put its elements in a flexbox

.container-2{
display:flex;  
}


/* Specify width */
.container-2-box{
width20%;
/* or */
flex-basis: 20%;  
}


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Align Elements Within a Container



images/223-9.png
justify-content: space-between: Put margins between elements so they can spread across

images/223-10.png
justify-content: space-around: Put margins on the side as well

/* Align elements evenly within the container */
.container-2{
display:flex;  
justify-content:space-between;
/* justify-content:flex-start; */  /* align left (default)*/
/* justify-content:flex-end; */  /* align right */
/* justify-content:center; */  /* align center */
}


.container-2-box{
width20%;  
}


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Set Minimum Width to Display Columns



• On small screen, we don't want flexbox to diplay too many columns
• For example, we don't want to have a 3-column layout on a smartphone
• We can use media query to achieve that. Below the specified width, all column displays will change to vertically display

images/223-11.png
images/223-12.png

/* Set minimum width to display columns */
@media(min-width:468px){
/* Put containers 1 and 2 inside */
}


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Wrap



• Add container-3 with 6 boxes for demo purose

/* Add container-3 */
.container-1 div, .container-2 div, .container-3 div{
border:1px #ccc solid;
padding:10px;
}


images/223-13.png

• If we narrow the window, at a certain point, some elements will move to the next line

/* Set wrap */
.container-3{
display:flex;  
flex-wrap:wrap;
justify-content:space-between; /* add margin */
}

.container-3-box{
flex-basis:12%; /* set width */
}




Index