Cross Axis Alignment
1. Align Items
The `justify-content CSS property provides us with a way to control the alignment of flex items along the main axis. This property allows us to easily adjust the position of the items within the container and ensure that they are aligned as desired.
Similarly, to align flex items along the cross axis, we can use the align-items property. This property offers greater control over the layout by allowing us to adjust the alignment along the perpendicular axis. The possible values for this property include:
stretch: This value stretches items to fill the entire height or width of the container along the cross axis. This is the default value for align-items.flex-start: This value aligns items to the start of the cross axis.flex-end: This value aligns items to the end of the cross axis.center: This value aligns items to the center of the cross axis.baseline: This value aligns items along their baselines, making it a common alignment choice for text items.
It is important to note that the effects of align-items are dependent on the value of flex-direction. For example, when flex-direction is set to row, align-items only affects the height of items and not their width, whereas when flex-direction is set to column, align-items affects the width of items and not their height.
1.1. Stretch
1.2. Start
1.3. End
1.4. Center
1.5. Baseline
1.5.1 Baseline with row direction
If we use the baseline alignment in CSS for a group of items with different heights, CSS will select the tallest item as the reference point and align all the other items based on its baseline.
For instance, in this example, item 2 is the tallest with a height of 200px, and its text is centered. Thus, when using baseline alignment in CSS, all the items will align with the baseline of item 2, causing item 1 and item 3 to align with item 2 as well.
.item1 { height: 40px; } .item2 { height: 200px; } .item3 { height: 100px; }
1.5.2. Baseline with column direction
When the flex-direction is changed to column, all the items align based on their left edge. If each item has a different margin on the left, they will still align so that their left edges are in the same column.
Let ‘s add more margin on each items
.item1 { margin-left: 60px; } .item2 { margin-left: 20px; } .item3 { margin-left: 100px; }
In this case, since item 3 has largest margin left, CSS adding a space with width is 40px (100px - 60px) to the left of item 1, and space with width 80px (100px - 20px) to the left of item 2.