Flexbox CSS
Flexbox CSS
The main idea behind CSS flexbox is to give the container ability to change its
item's size automatically to best fit in available space.
Here are 3 images that show how HTML containers arranged using flexbox
automatically fits themselves according to device width.
1. CSS flex-direction
Flexbox module provides a property called flex-direction which specify what
direction the children are laid out in.
flex-direction has 4 different values:
I. row: It aligns flexbox items from left to right. It is the default value
of flex-direction.
II. row-reverse: It aligns flexbox items from the right end of the
container to the left end.
III. column: It aligns flexbox items from top to bottom in the flexbox
container.
IV. column-reverse: It aligns flexbox items from bottom to top in the
flexbox or container.
.container {
display: flex;
flex-direction: row; /* or row-reverse | columns | column-reverse */
}
2. CSS flex-wrap
flex-wrap property is used to define whether the flex items are aligned in a single
row or the items can flow to multiple rows.
flex-wrap property set values of wrapping items. There are three values that
could be set for flex-wrap property nowrap, wrap and wrap-reverse.
.flow2 {
flex-flow: row wrap;
}
.container {
display: flex;
width: 250px;
background: #7E89B4;
border: 2px solid #4B5681;
}
.container div {
margin: 6px;
color: white;
font-size: 18px;
font-family: sans-serif;
padding: 10px 15px;
background: #353C5A;
}
</style>
</head>
<body>
<h2>Container items no wraping <code>flex-warp:nowrap</code>
property.</h2>
<p>The "flow1" defines item to flow in column and to reverse wrap.</p>
<div class="container flow1">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>10</div>
</div>
<p>The "flow2" defines item to flow in row and to wrap items.</p>
<div class="container flow2">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>10</div>
</div>
</body>
</html>
4. justify-content CSS
The justify-content property aligns flex items along the X-axis (horizontally) also
known as the main axis in the flexbox. All flex line's items align horizontally
according to justify-content value.
The justify-content property has 6 different values flex-start, flex-
end, center, space-around, space-evenly and space-between.
I. stretch - It stretches the flex items along the container to fill the
container but min-width and max-width are applied to items if
mentioned. It is the default value of the align-item property.
II. flex-start - It aligns flex items to the top of the container.
III. flex-end - It aligns flex items to the bottom of the container.
IV. center - It aligns flex items in the center of the container.
V. baseline - It aligns flex items in such a way that their baseline
aligns.
6. align content CSS
The align-content property works the same as the align-content property along Y-
axis but the only difference in both is that instead of aligning flex items align-
content aligns the flex container's line as a whole
The align-content is multiline property. Its effect is not visible when there is
only one row in the flexbox.
The align-content property has 6 different values flex-start, flex-
end, center, space-around, space-between and stretch.
1. Flexbox order
The order CSS property of flexbox defines the order of an item in flexbox. Items
of the container are sorted according to their order value in ascending order and
then by their source code order.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>CSS - flexbox order</title>
<style>
.container {
display: flex;
border: 2px solid #bd1e60;
margin: 20px 0;
}
#target1 {
order: 0;
background-color: #bd1e60;
}
#target2 {
order: 1;
background-color: #bd1e60;
}
#target3 {
order: -1;
background-color: #bd1e60;
}
#target4 {
order: 5;
background-color: #bd1e60;
}
.container div {
margin: 6px;
color: white;
font-size: 18px;
font-family: sans-serif;
padding: 10px 15px;
border-radius: 5px;
background-color: rgb(39, 170, 164);
}
</style>
</head>
<body>
<h2>order: Integer_value</h2>
<p>order property orders the container's item according to their order
value in ascending order.</p>
<div class="container">
<div>1 <sub>order:0</sub></div>
<div>2 <sub>order:0</sub></div>
<div id="target1">3 <sup>(Target
element)</sup><sub>order:0</sub></div>
<div>4 <sub>order:0</sub></div>
<div>5 <sub>order:0</sub></div>
</div>
<div class="container">
<div>1 <sub>order:0</sub></div>
<div>2 <sub>order:0</sub></div>
<div id="target2">3 <sup>(Target
element)</sup><sub>order:1</sub></div>
<div>4 <sub>order:0</sub></div>
<div>5 <sub>order:0</sub></div>
</div>
<div class="container">
<div>1 <sub>order:0</sub></div>
<div>2 <sub>order:0</sub></div>
<div id="target3">3 <sup>(Target element)</sup><sub>order:-
1</sub></div>
<div>4 <sub>order:0</sub></div>
<div>5 <sub>order:0</sub></div>
</div>
<div class="container">
<div style="order:15;background:#4bc505">1 <sub>order:15</sub></div>
<div style="order:3;background:#ad6903">2 <sub>order:3</sub></div>
<div id="target4">3 <sup>(Target
element)</sup><sub>order:5</sub></div>
<div style="order:4;background:#cccf1d">4 <sub>order:4</sub></div>
<div>5 <sub>order:0</sub></div>
</div>
</body>
</html>
2. flex-grow CSS
The flex-grow CSS property defines a flex grow factor of any specific
item's main size, where the main size is either height or width of the
container depending on the flex-direction property.
The flex-grow property defines how the remaining space in the
container will be assigned to the item, where the remaining space is the
size of the container which remains after all items fit in.
The flex-grow value is always positive or 0, the negative value is not
supported.
flex grow example
Suppose you have 5 items of equal width 50px in a flex container with a
width of 400px, and for simplicity suppose there is no margin between
the items.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>CSS - flex grow</title>
<style>
.container {
display: flex;
border: 2px solid #bd1e60;
width: 400px;
margin: 20px 0;
}
#target1 {
flex-grow: 2;
background: #bd1e60;
}
#target2 {
flex-grow: 1;
background: #bd1e60;
}
#target3 {
flex-grow: 0.5;
background: #bd1e60;
}
.container div {
margin: 1px;
color: white;
font-size: 18px;
font-family: sans-serif;
padding: 10px 15px;
background-color: rgb(39, 170, 164);
}
</style>
</head>
<body>
<h2>flex-grow: number</h2>
<p>flex-grow set a flex grow factor to the item to set remaing size of
container to the item.</p>
<div class="container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
</div>
<div class="container">
<div>1</div>
<div>2</div>
<div id="target1">3</div>
<div>4</div>
<div>5</div>
</div>
<div class="container">
<div>1</div>
<div>2</div>
<div id="target3">3</div>
<div>4</div>
<div>5</div>
</div>
<div class="container">
<div>1</div>
<div>2</div>
<div id="target1">3</div>
<div id="target2">4</div>
<div>5</div>
</div>
</body>
</html>
3. flex-basis
The flex-basis CSS property is used to set the size of the flex item.
In case both flex-basis and width (or height) are defined for an
element then flex-basis has priority.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>CSS - flex basis</title>
<style>
.container {
display: flex;
border: 2px solid #bd1e60;
}
#target1 {
flex-basis: 150px;
background: #bd1e60;
}
.container div {
margin: 1px;
padding: 10px 15px;
color: white;
font-family: sans-serif;
background-color: #27aaa4;
}
</style>
</head>
<body>
<h2>flex-basis: size</h2>
<p>flex-basis set the initial size of a flex item.</p>
<div class="container">
<div>1</div>
<div>2</div>
<div>3</div>
<div id="target1">4</div>
<div>5</div>
</div>
</body>
</html>
4. flex-shrink
The flex-shrink CSS property is used to set the flex-shrink factor for a
flex item.
It determines how much a flex item will shrink relative to the rest of the
items in the container when the container is full.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>CSS - flex shrink</title>
<style>
.container {
display: flex;
width: 280px;
border: 2px solid #bd1e60;
}
#target1 {
flex-shrink: 5;
background: #bd1e60;
}
.container div {
margin: 1px;
flex-basis: 40px;
padding: 10px 0;
color: white;
font-family: sans-serif;
background-color: #27aaa4;
}
</style>
</head>
<body>
<h2>flex-shrink: number</h2>
<p>flex-shrink set shrink factor for a flex item.</p>
<div class="container">
<div>1</div>
<div>2</div>
<div>3</div>
<div id="target1">4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
</div>
</body>
</html>
5. flex
This is a shorthand property for flex-grow, flex-basis and flex-
shrink.
/* 1 value, unitless number: flex-grow */
flex: 1;
/* 1 value, width/height: flex-basis */
flex: 15px;
flex: 10%;
6. align-self
The align-self property overwrites the align-items value for individual
items. It aligns the item within the container on the Y-axis.
.container1 {
display: flex;
align-items: center;
}
#target1 {
align-self: self-end;
}
.container2 {
display: flex;
align-items: flex-start;
}
#target2 {
align-self: center;
}