Here are a quick list of advantages and disadvantages:
Advantages:
- Fill the gap where HTML lack of ability to compose complex layout
- Work well with existing HTML layout or any grid systems like Bootstrap
- Easy to understand its containership concepts
- Create vertical layouts become an easy task
- Dynamic changeable horizontal/vertical layout with the media query
- Ability to apply to any HTML elements: semantic and non-semantic
Disadvantages:
- Older browsers will not support
- Different containership concepts need to learn
Flexbox is flexible in a way that it also works with all the HTML elements, including semantic [tags define its content] ones, such as <form>, <table> and <img>. And the magic actually occurs in the CSS portion of the code when you declare this attribute, display: flex;, in the container element as shown below:
<style>
.container {
display: flex;
}
</style>
In HTML:
<div class="container"></div>
Another two CSS attributes are flex-direction and justify-content. The flex-direction property specifies how flex items are arranged in the main container. It can be arranged in rows or columns counting from left to right or top to bottom. In addition, it could also be stacked from right to left as "row-reverse", or bottom to up as "column-reverse". The justify-content property specifies the space between or the alignment of the flex items relative to x-axis and y-axis and its flexibility in its size to have fixed length or fill in the rest of space. The default value is stretch.
In many occasions, a vertical layout could be a difficult task to do since it does not have a good way of setting it. One must know the exact size of the window from the top-level container all the way down into the child elements. With today's various mobile devices and its range of dimension, Flexbox can make this issue a piece of cake. This is how we use Flexbox to create the vertical layout. By changing the flex-direct attribute to column and add height to 100%, and add overflow to auto, we can see that the content are stacked vertically according to their height of the flex items. Please note that we need to set height to 100% for html, body attributes so that the entire browser window can fill the full area of the content.
html,body {
height: 100%;
overflow: hidden;
}
.main-container {
display: flex;
flex-direction: column;
height: 100%;
overflow: auto;
}
.box-left {
/*width: 300px; if want fixed width*/
flex: 3;
background-color:red;
}
.box-mid {
flex: 2;
background-color:yellow;
}
.box-right {
flex: 4; /* resizable */
background-color:green;
display: flex;
flex-direction: column;
align-items: center; /*x-axis*/
justify-content: center; /*y-axis*/
}
</style>
<div class="main-container">
<nav class="box-left">
left
</nav>
<div class="box-mid">
middle
</div>
<article class="box-right">
right
</article>
</div>
Nested Flex Containers
Flexbox Adjusts Itself in the Responsive Design
@media (max-width: 768px) { /* iPad*/
.container-main {
flex-direction: column;
}
}
For Device Widths For Responsive Design:
device_widths_for_responsive_design.png |