Flexbox - Flexible Box Layout Model
→ Flexbox is a layout mechanism
- it allows you to arrange the "Content" in the box, along one axis.
→ Flex items move as a group on either one axis.
⟶ but on the other axis items can move individually or as a group.
Flex-box is one dimensional, you can control alignment in one axis, a row or a column, not both.
Think of your webpage as a 2-dimensional plain, with 2 axis:
X-axis or Main axis ⟶ (inline flow)
Y-axis or Cross axis ⟶ (block flow)
Flex divides the boxes in the layout in 2 parts:
Both have different set of properties.
D.I.Y
.flex-container {
display: flex;
border: 1px solid black;
padding: 10px;
margin: 10px;
}
Set the display of the box as "flex".
This box is the flex-container.
This is the box where your "content" is present.
D.I.Y
.flex-container {
display: flex;
flex-direction: column; /* row | row-reverse | column | column-reverse */
border: 1px solid black;
padding: 10px;
margin: 10px;
}
row: horizontal, left-to-right, default,
column: vertical, top-to-bottom
row-reverse: horizontal, right-to-left
column-reverse: vertical, bottom-to-top
D.I.Y
Establishes which direction will be regarded as the Main-axis.
.flex-container {
display: flex;
flex-direction: column; /* row | row-reverse | column | column-reverse */
flex-wrap: wrap; /* nowrap | wrap | wrap-reverse */
border: 1px solid black;
padding: 10px;
margin: 10px;
}
no-wrap: items will not wrap, default,
wrap: items will wrap, top to bottom,
wrap-reverse: items will wrap, bottom to top,
D.I.Y
.flex-container {
display: flex;
/*
* flex-direction: column; // row | row-reverse | column | column-reverse
* flex-wrap: wrap; // nowrap | wrap | wrap-reverse
*/
/*OR*/
flex-flow: column wrap;
border: 1px solid black;
padding: 10px;
margin: 10px;
}
shorthand for the flex-direction
and flex-wrap
properties
D.I.Y
Arranges items along the Main axis
Note: Here we are assuming the main-axis as x-axis
(flex-direction: row;
).
D.I.Y
It handles arrangement of items on the cross-axis.
Affects only one line of items, if the items wrap to next line, on the next line also they are arranged as per this rule.
D.I.Y
D.I.Y
If flex-direction is set to row,
Main-axis is X-axis, inline-flow
justify-content works along X-axis,
align-items works along Y-axis.
Now align-content, will also arrange items on Y-axis, but only those which have been wrapped on the next line.
.flex-container {
display: flex;
gap: 10px;
gap: 10px 20px; /* row-gap column gap */
row-gap: 10px;
column-gap: 20px;
}
It applies spacing, but only between items not on the outer edges.
.flex-container {
display: flex;
gap: 10px;
gap: 10px 20px; /* row-gap column gap */
row-gap: 10px;
column-gap: 20px;
}
D.I.Y
The flex-items properties that apply to the children (flex-items) of the flex container directly.
0
, items along the main-axis, lower to higher order value,flex-grow flex-shrink flex-basis
align-items
, for one item.The problem with having background images, with CSS:
Solution:
the HTML's <img>
tag
<img
class="image"
src="https://framerusercontent.com/images/gPrYLwg38BGyWaAHDpE9nIvAa1Y.jpeg"
alt="MDN Logo"
/>
D.I.Y
Create the following layout, and add gaps to them.
D.I.Y
Create the following layout, and add gaps to them.
D.I.Y
Create the following layout, and add gaps to them.
D.I.Y
Create the following layout, and add gaps to them.