Main Axis Alignment
In Flexbox, justify-content is a property that controls the alignment of items along the main axis. The main axis is defined by the flex-direction property, and the justify-content property determines how items are positioned within the main axis.
There are several possible values for the justify-content property, including:
flex-start: Items are aligned to the start of the main axis. This is the default value.center: Items are centered along the main axis.flex-end: Items are aligned to the end of the main axis.space-between: Places items with space distributed evenly around the start and end of the list.space-around: Items are evenly distributed along the main axis, with equal amounts of space around each item.space-evenly: Spaces items such that they have equal amounts of space before and after them.
By using the justify-content property, you can control the alignment of items within a Flexbox container, making it easier to create flexible and responsive layouts. After all items have been calculated in size, the remaining space in the main axis of the container (either width or height, depending on the flex-direction) will be divided amongst the items using a specific algorithm controlled by the value of justify-content.
1. flex-start
The default setting for justify-content is flex-start, which aligns items to the beginning of the main axis.
2. flex-end
However, if you change the value of justify-content to flex-end, the elements will be aligned to the end of the main axis.
3. center
With justify-content set to center, items are positioned in the center along the main axis of the flex container. This value is often used when we want to center the elements within the container, both horizontally and vertically.
4. space-between
With justify-content set to space-between, the items in the flex container are evenly spaced out along the main axis with equal space between each item, as well as between the first and last item and the start and end of the main axis
5. space-around
When you set justify-content to space-around, it evenly distributes the items along the main axis, with equal amounts of space around each item. This means that there is an equal amount of space before the first item and after the last item, as well as between each adjacent item. This creates a visually balanced layout, with items spaced evenly apart.
6. space-evenly
7. The special case of margin
In the previous sections, we looked at how the justify-content property affects the distribution of remaining space in a flex container without considering the effect of margins on this distribution. In this section, we will examine the impact of margins on the distribution of remaining space in a flex container.
It’s crucial to keep in mind that in flexbox, margins between items inside a flex container do not overlap. This means that the margin of each item will be respected and kept separate.
To show this, let’s consider a flex container that has a vertical arrangement of items, also known as a column flex direction. If the first item has a bottom margin of 40px and the second item has a top margin of 30px, we can avoid margin collapse by setting the gap between items to 0. As a result, we can see that the two margins are kept separate.
Furthermore, flexbox handles margin: auto differently. When an item within a flex container has margin: auto assigned to it, CSS will divide the remaining main size space equally among the margin: auto values. For instance, if the first and second items have margin-left: auto, the remaining space will be divided into two parts.
If all three items have margin: auto, the remaining space will be divided into six parts.
It’s important to remember that margins are prioritized for distribution before any other elements. This means that if any of the flex items have margin: auto, the justify-content property often has no effect on the layout of the container.
In this lesson, we have already learned how CSS distributes leftover space between the main size of the container and the size of the items based on the margin and justify-content properties.
This raises some new questions:
- How does CSS handle leftover space in the cross size?
- Since the leftover space is dependent on the size of the items, which CSS properties will affect the size of the items?
We will deep dive into these questions in next lessons. Let joy me to learn about this part. I am very exiciting to guide you into this journey.
Let’s continue to explore and learn about these questions. Together, we will expand our knowledge of how CSS handles left over space in cross size and how the size of items is affected by different properties. Get ready for an exciting journey ahead!