Css flexbox for Beginners, Learn Basic concept About Css Flexbox In 5 Minutes
Hello i will be talking on a quick beginners guide on how you can master Css Flexbox for Absolute Beginners AND for advance css designers too.
First Below is the list of all i will be covering in this teaching, just read to the end to benefit every thing covered.
- Creating a Flex Container
- Defining Flex Direction
- Align and Justify Flex Items
- Wrapping
- Expanding Flex Items
- Additional Method to Access Flex Items just stay tuned.
Before Proceeding I will love to give you a backgroung history of Css FlexBox and how it came about. this was found from an online resource named
CSS Tricks All credits to him about the background of flexbox.
The Flexbox Layout (Flexible Box) module (a W3C Candidate Recommendation as of October 2017) aims at providing a more efficient way to lay out, align and distribute space among items in a container, even when their size is unknown and/or dynamic (thus the word "flex").
The main idea behind the flex layout is to give the container the ability to alter its items' width/height (and order) to best fill the available space (mostly to accommodate to all kind of display devices and screen sizes). A flex container expands items to fill available free space, or shrinks them to prevent overflow.
Most importantly, the flexbox layout is direction-agnostic as opposed to the regular layouts (block which is vertically-based and inline which is horizontally-based). While those work well for pages, they lack flexibility (no pun intended) to support large or complex applications (especially when it comes to orientation changing, resizing, stretching, shrinking, etc.).
Note: Flexbox layout is most appropriate to the components of an application, and small-scale layouts, while the Grid layout is intended for larger scale layouts.
Basics & Terminology
Since flexbox is a whole module and not a single property, it involves a lot of things including its whole set of properties. Some of them are meant to be set on the container (parent element, known as "flex container") whereas the others are meant to be set on the children (said "flex items").
If "regular" layout is based on both block and inline flow directions, the flex layout is based on "flex-flow directions". Please have a look at this figure from the specification, explaining the main idea behind the flex layout.
A diagram explaining flexbox terminology. The size across the main axis of flexbox is called the main size, the other direction is the cross size. Those sizes have a main start, main end, cross start, and cross end. Items will be laid out following either the main axis (from main-start to main-end) or the cross axis (from cross-start to cross-end).
main axis - The main axis of a flex container is the primary axis along which flex items are laid out. Beware, it is not necessarily horizontal; it depends on the flex-direction property (see below).
main-start | main-end - The flex items are placed within the container starting from main-start and going to main-end.
main size - A flex item's width or height, whichever is in the main dimension, is the item's main size. The flex item's main size property is either the ‘width’ or ‘height’ property, whichever is in the main dimension.
cross axis - The axis perpendicular to the main axis is called the cross axis. Its direction depends on the main axis direction.
cross-start | cross-end - Flex lines are filled with items and placed into the container starting on the cross-start side of the flex container and going toward the cross-end side.
cross size - The width or height of a flex item, whichever is in the cross dimension, is the item's cross size. The cross size property is whichever of ‘width’ or ‘height’ that is in the cross dimension.
Note that in css flexbox they are three main component that we should take notice of and they are :-
- Flex Container
- Flex Items
- Direction of Flow
Creating A flexbox In CSS
To define and access a container as a flex container you can use display: flex;. If no additional rules are set, all direct children will be considered flex items and will be laid horizontally, from left to right. The width of flex items automatically adjusts to fit inside the container. With the help of these CSS property your getting set to create a flex website.
display : flex;
Defining Flex Direction
On Defining the direction of our Flex items they are many different ways we can do that. To put all flex items in one column, add flex-direction: column; to your CSS. so on this section every thing about defining direction will be defined.
.flexcontainer { display: flex; flex-direction: column; } To reverse the column order of flex items, add flex-direction: column-reverse; to your CSS.
.flexcontainer { display: flex; flex-direction: column-reverse; } To reverse the row order of flex items, add flex-direction: row-reverse; to your CSS.
.flexcontainer { display: flex; flex-direction: row-reverse; }
Align and Justify Flex Items
This defines the alignment along the main axis. It helps distribute extra free space left over when either all the flex items on a line are inflexible, or are flexible but have reached their maximum size. It also exerts some control over the alignment of items when they overflow the line.
If you want flex items aligned to the right:
.flexcontainer { display: flex; justify-content: flex-end; } If you want flex items centered:
.flexcontainer { display: flex; justify-content: center; } If you want flex items separated:
.flexcontainer { display: flex; justify-content: space-between; } To center flex items both horizontally and vertically:
.flexcontainer { display: flex; justify-content: center; align-items: center; }
Wrapping
Resized to fit into one column or one row when a flex container is not large enough, flex items are not able to wrap by default. By using flex-wrap: wrap;, any flex items causing overflow will be wrapped to the next line.
To allow wrapping:
.flexcontainer { display: flex; flex-wrap: wrap; } To allow reverse wrapping (overflow item(s) are sent to the line above):
.flexcontainer { display: flex; flex-wrap: wrap-reverse; } To control the positioning of flex items when there is wrapping (replace * with stretch, space-around, space-between, center, flex-end, or flex-start):
.flexcontainer { display: flex; flex-wrap: wrap; align-content: *; }
Expanding Flex Items
When there is leftover space within a flex container, flex-grow can be used to dictate how each item fits in the remaining space.
To grow a flex item (default size is 0):
.flexcontainer { display: flex; } .flexitem1 { flex-grow: 1; border: 3px solid; } Only working when there is space remaining in a container, flex-grow equals the ratio of a container’s width/height that an item should stretch to fit to use up all leftover container space, relative to the size of other children in the container. Similarly, flex-shrink only works when flex items are overflowing the flex container due to insufficient space.
To shrink a flex item (default size is 0):
.flexcontainer { display: flex; } .flexitem1 { flex-shrink: 1; border: 3px solid; } To set the precise size of each flex item (default value is flex-basis: auto;):
.flexcontainer { display: flex; } .flexitem1 { flex-basis: 25%; /percentage value/ } .flexitem2 { flex-basis: 50px; /absolute value/ } .flexitem3 { flex-basis: 5px; } To use flex-grow, flex-basis and flex-shrink at the same time:
.flexcontainer { display: flex; } .flexitem1 { flex: 2 1 100px; } .flexitem2 { 5 1 5%; } .flexitem3 { flex: 1 5 5%; }
### Additional Method to Access Flex Items
Throughout the tutorial flex items were access directly via CSS, example:
.flexitem1 { flex-shrink: 1; }
However, you may want to name all flex items the same, create groups of items, etc. To make your code more usable, you can access flexbox items by their order number (place the order number of the item you want to alter in parenthesis):
.flexitem1:nth-child(1) { flex-shrink: 1; }
Conclusion:
Hope This tutorial give you a good head start to building of good css Responsive website and you can check on my posts for more tutorial on css and html.
Thanks to Jill Platts For Her Article on Medium and Also thanks the CSS-Tricks for Doing justice to the issues of Css FLexbox by his Article.
THANK YOU TOO FOR READING....