CSS Flexbox
CSS Flexbox, otherwise known as the Flexible Box Layout, is an advanced layout model that enables space to be distributed across one dimension, either horizontally or vertically. It is a much simpler and flexible way of laying out pages and aligning space among items in a container, while also with unknown or dynamic dimensions.
Why Use Flexbox?
Flexbox is one of the most commonly used layout options in modern web development for many reasons. Flexbox allows for:
- Responsive layouts: Flexbox ensures elements behave predictably regardless of screen size. This makes it a very important layout model when considering responsive webpages.
- Alignment of items: Flexbox makes centering and aligning items horizontally and vertically much easier than older methods which used floats and positioning.
- Dynamic layouts: Flexbox works well with content of different sizes and viewport sizes, this makes it highly dynamic for building scalable websites.
Basic Structure of Flexbox
Flexbox consists of a
flex container and the
flex items inside it. The container is
defined by setting display: flex; (or
display: inline-flex; for inline layouts)
on the parent element. These items are placed automatically based on what you define to the
container and the children.
Basic Example
.container {
display: flex;
}
.item {
flex: 1;
}
In this example, all the
.item elements within the
.container will be distributed evenly across the container's width
and share the same amount of space.
Flexbox Properties
1. Flex Container Properties
There are several key properties that you can apply to the flex container:
-
display: Defines the container as a flex container. You can set it toflexorinline-flexfor inline behavior. -
flex-direction: Determines the direction in which the flex items are placed in the container.The default is row and possibilities include:-
row(default): items are placed in a row left to right -
row-reverse: Itemsare placed in a row right to left -
column: items are placed in a column top to bottom -
column-reverse: Items are placed in a column bottom to top
-
-
justify-content: Items are aligned along the horizontal axis (the main axis). The available values are:-
flex-start: items are placed at the beginning of the container -
flex-end: items are placed at the end of the container. -
center: Centers items horizontally. -
space-between: Distributes items with equal spacing between them. -
space-around: Distributes items with equal spacing around them.
-
-
align-items: Aligns items vertically (along the cross axis). Possible values:-
flex-start: Aligns items at the start of the container. -
flex-end: Aligns items at the end of the container. -
center: Centers items vertically. -
stretch: Stretches items to fill the container. -
baseline: Aligns items based on their text baseline.
-
-
flex-wrap: Controls whether items should wrap onto a new line if they don’t fit. Possible values:-
nowrap: Prevents wrapping. -
wrap: Allows items to wrap onto a new line. -
wrap-reverse: Wraps items in the opposite direction.
-
2. Flex Item Properties
The individual items in a flex container can also have their own unique properties:
-
flex-grow: Defines the ability of a flex item to grow relative to other flex items. It is a unitless number which indicates how the flex item will be proportionately larger than the other flex items. The default value is0. -
flex-shrink: Defines the ability for a flex item to shrink relative to other items when the container is too small. The default value is1. -
flex-basis: Defines the initial size of the flex item before it starts growing or shrinking. It can be set to a specific length (e.g.,200px) orauto(default). -
flex: A shorthand forflex-grow,flex-shrink, andflex-basis. For example:flex: 1 1 200px; -
align-self: Allows an individual item to override thealign-itemsvalue for its own alignment. Possible values:auto,flex-start,flex-end,center,baseline,stretch.
Example: Simple Flexbox Layout
Here’s an example of how to create a simple flexbox layout with three items:
.container {
display: flex;
justify-content: space-between;
align-items: center;
}
.item {
background-color: #4CAF50;
color: white;
padding: 20px;
margin: 10px;
}
Best Practices for Using Flexbox
-
Always define a
flex-directionThis will help you know exactly how to align the items. -
Use
flex-wrapThis will help you when your items overflow in a smaller screen. -
Leverage
align-itemsandjustify-contentYou have more flexibility for fixing the spacing and aligning items. - Keep it loose by not using widths. Let Flexbox do its thing.
Conclusion
Flexbox gives web developers a lot of new options for working with layout today. The options it gives you for aligning items, sharing space, and letting the layout respond makes it an essential tool for modern web development. Using its simple power is central to creating modern, flexible, responsive web projects.