Align Items in CSSThe align-items property is used within a flex container to control how its flex items are aligned vertically along the cross-axis, which is perpendicular to the main axis of the flex container. The main axis is determined by the flex-direction property (either horizontal or vertical). Syntax:Properties of Align-items in CSSAmong the essential properties of Flexbox, align-items are a fundamental attribute that enables the precise control of vertical alignment within flex containers. The layout is governed by two main axes: the main axis and the cross-axis. The main axis is determined by the flex-direction property, which can be either row or column-based. The cross-axis is perpendicular to the main axis and serves as the basis for vertical alignment when utilizing the align-items property. The align-items property is specifically employed to control how flex items are aligned vertically along the cross-axis within a flex container. It offers a range of values, each influencing the alignment behaviour: 1. Stretch (Default): The default value, stretch, causes flex items to fill the entire cross-axis of the container. If no explicit height is set on the items, they stretch to match the height of the tallest item. This behaviour is beneficial when aiming for uniformity in item heights. 2. Flex-start: When align-items is set to flex-start, the items align at the start of the cross-axis. In other words, they are aligned to the top of the container. This alignment mode is beneficial when dealing with vertical headers or navigation bars. 3. Flex-end: In contrast to flex-start, flex-end aligns the items at the end of the cross-axis, akin to aligning them at the bottom of the container. This is valuable for creating footers or placing content at the bottom of a container. 4. Center: The centre value centres the items along the cross-axis, giving the layout a balanced and aesthetically pleasing appearance. This mode is suitable for the vertical alignment of items within content sections. 5. Baseline: Baseline alignment aligns the items based on their textual baselines. This is particularly useful when dealing with text elements of varying sizes, ensuring that the text appears consistent across items. Code:In this example, we have a flex container with three flex items inside. The align-items: center; property in the .flex-container selector aligns the flex items vertically at the center of the cross-axis. The height property ensures that the container has a specified height of 300px. Each flex item has padding and a border to make them visually distinct. You can experiment with different values for the align-items property (such as flex-start, flex-end, baseline, or stretch) to see how they affect the vertical alignment of the items within the container. Uses of Align-items
Drawbacks:
Next TopicCSS Border Width
|