HTML and CSS

CSS flex property | Display Flex

The flex container becomes flexible by setting the display property to flex:

CSS Flexbox
CSS Flexbox

The flex container properties are:

  1. flex-direction
  2. flex-wrap
  3. flex-flow
  4. justify-content
  5. align-items
  6. align-content

A Flexible Layout must have a parent element with the display property set to flex. Direct child elements(s) of the flexible container automatically becomes flexible items.

<div class="flexContainer">
  <div>1</div>
  <div>2</div>
  <div>3</div>
</div>
----------------------------
.flex-container {
    display: flex;
}
.flexContainer > div {
  background-color: #f1f1f1;
  margin: 5px;
  padding: 5px;
  font-size: 14px;
}

The flex-direction Property

The flex-direction property defines in which direction the container wants to stack the flex items.

The column value stacks the flex items vertically (from top to bottom):

flex items vertically
from top to bottom

The column-reverse value stacks the flex items vertically (from bottom to top):

flex items vertically
from bottom to top

The row value stacks the flex items horizontally (from left to right):

flex items horizontally
from left to right

The row-reverse value stacks the flex items horizontally (from right to left):

flex items horizontally
from right to left

The flex-wrap Property

The flex-wrap property specifies whether the flex items should wrap or not. The examples below have 18 flex items, to better demonstrate the flex-wrap property.

The wrap value specifies that the flex items will wrap if necessary:

flex wrap
flex-wrap

The nowrap value specifies that the flex items will not wrap (this is default):

nowrap
nowrap 

The wrap-reverse value specifies that the flexible items will wrap if necessary, in reverse order:

wrap-reverse
wrap-reverse

The flex-flow Property

The flex-flow property is a shorthand property for setting both the flex-direction and flex-wrap properties.

flex-flow
flex-flow

The justify-content Property

The justify-content property is used to align the flex items:

justify-content
justify-content

flex-start

The flex-start value aligns the flex items at the beginning of the container (this is default): The “align-items: flex-start;” aligns the flex items at the top of the container:

flex-start
flex-start

flex-end

The flex-end value aligns the flex items at the end of the container:

flex-end
flex-end

space-around

The space-around value displays the flex items with space before, between, and after the lines:

space-around
space-around

space-between

The space-between value displays the flex items with space between the lines:

space-between
space-between

The align-items Property

The align-items property is used to align the flex items. The “align-items: center;” aligns the flex items in the middle of the container:

align-items
align-items

flex-end

The flex-end value aligns the flex items at the bottom of the container: The align-items Property The “align-items: flex-end;” aligns the flex items at the bottom of the container:

flex-end
flex-end

stretch

The stretch value stretches the flex items to fill the container (this is default): The “align-items: stretch;” stretches the flex items to fill the container (this is default):

stretch
stretch

space-between

The space-between value displays the flex lines with equal space between them: The align-content Property The “align-content: space-between;” displays the flex lines with equal space between them:

space-between
space-between

center

The center value displays display the flex lines in the middle of the container: The align-content Property The “align-content: center;” displays the flex lines in the middle of the container:

center
center

justify-content

Set both the justify-content and align-items properties to center, and the flex item will be perfectly centered:

justify-content
justify-content

justify-content-between

Set both the justify-content-between and align-items properties to center, and the flex item will be perfectly centered:

justify-content-between
justify-content-between

Learn HTML/CSS from W3 School Website