Kaigai Blog living abroad in my twenties

【My Study Note】Flexbox

CSS Programming

Properties for the parent


These are the property for the parent element.

flex-direction

.container {
  flex-direction: row | row-reverse | column | column-reverse;
}
  • row (default): left to right
  • row-reverse: right to left
  • column: same as row but top to bottom
  • column-reverse: same as row-reverse but bottom to top

flex-wrap

.container {
  flex-wrap: nowrap | wrap | wrap-reverse;
}
  • nowrap (default): all flex items will be on one line
  • wrap: flex items will wrap onto multiple lines, from top to bottom.
  • wrap-reverse: flex items will wrap onto multiple lines from bottom to top.

flex-flow

.container {
  flex-flow: column wrap;
}

This is a shorthand for the flex-direction and flex-wrap properties.

justify-content

.container {
  justify-content: flex-start | flex-end | center | space-between | space-around | space-evenly;
}

align-items

.container {
  align-items: stretch | flex-start | flex-end | center | baseline;
}

align-content

.container {
  align-content: flex-start | flex-end | center | space-between | space-around | space-evenly;
}

gap, row-gap, column-gap

.container {
  /* Specify both of them */
  gap: 10px;
  /* row-gap column gap */
  gap: 10px 20px;
  /* Specify separately */
  row-gap: 10px;
  column-gap: 20px;
}

Properties for the children

These are the properties for the child element.

Order

.item {
  order: 5; /* default is 0 */
}

By default, flex items are laid out in the source order. However, the order property controls the order in which they appear in the flex container.

flex-grow

header {
    flex-grow: 1;
}
main {
    flex-grow: 2;
}
footer {
    flex-grow: 1;
}

The flex-grow property specifies how much the item will grow relative to the rest of the flexible items inside the same container.

flex-shrink

/* default 1 */
.item {
  flex-shrink: 3;
}

The flex-shrink property specifies how the item will shrink relative to the rest of the flexible items inside the same container.

flex-basis

/* default auto */
main{
  flex-basis: 100px;
}

The flex-basis property specifies the initial length of a flexible item.

flex

.item {
  flex: none | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ]
}

This is the shorthand for flex-grow, flex-shrink and flex-basis combined.

align-self

.item {
  align-self: auto | flex-start | flex-end | center | baseline | stretch;
}

This allows the default alignment (or the one specified by align-items) to be overridden for individual flex items.