Tillitsdone
down Scroll to discover

CSS align-items A Comprehensive Guide

CSS align-items is a powerful property for aligning flex and grid items.

Learn how to use it with options like center, start, end, and stretch.

Enhance your web design skills with practical examples and browser compatibility details.
thumbnail

Introduction

The CSS align-items property is a powerful tool for web developers and designers. It helps align flex items within a container and is particularly useful in both flexbox and grid layouts. In flexbox, align-items controls the alignment of items on the cross axis, while in grid layouts, it manages the alignment of items on the block axis within their grid areas.

By setting the align-items property, you can define how the contents of a flex or grid container are aligned. This includes options like stretching items to fill the container, centering items, and aligning items to the start or end of the container.

In this article, we’ll explore the various values of align-items, provide practical examples, and discuss browser compatibility. Whether you’re a seasoned web developer or just starting out, understanding how to use align-items effectively can greatly enhance your web design skills.

Specification

The align-items property is defined in several CSS specifications:

  • CSS Box Alignment Module Level 3: This module outlines the alignment properties for CSS, providing a standardized way to align items within containers.
  • CSS Flexible Box Layout Module Level 1: This module specifically addresses the behavior of flex containers and their items, including the align-items property.

These specifications ensure that align-items is consistently implemented and supported across various web platforms, making it a reliable tool for creating flexible and responsive layouts.

Syntax

The CSS align-items property has a straightforward syntax:

align-items: value;

Here are some of the values you can use with the align-items property:

  • Basic Keywords: normal, stretch
  • Positional Alignment: center, start, end, flex-start, flex-end, self-start, self-end, anchor-center
  • Baseline Alignment: baseline, first baseline, last baseline
  • Overflow Alignment: safe center, unsafe center
  • Global Values: inherit, initial, revert, revert-layer, unset

Each of these values specifies a different way to align items within a container, giving you flexibility in how your layout behaves.

Values

The align-items property can take a variety of values, each affecting the alignment of items within a container in different ways. Here’s a breakdown of the main values you can use:

normal:

  • The behavior of this keyword depends on the layout mode. In flex items, it behaves as stretch. For grid items, it behaves similarly to stretch, except for boxes with an aspect ratio or intrinsic sizes, where it behaves like start.

center:

  • The items are centered within the line on the cross-axis. If an item’s size is larger than the flex container, it will overflow equally in both directions.

start:

  • The items are aligned flush to each other toward the start edge of the alignment container in the appropriate axis.

end:

  • The items are aligned flush to each other toward the end edge of the alignment container in the appropriate axis.

self-start:

  • The items are aligned flush to the edge of the alignment container’s start side of the item, in the appropriate axis.

self-end:

  • The items are aligned flush to the edge of the alignment container’s end side of the item, in the appropriate axis.

baseline, first baseline, last baseline:

  • All flex items are aligned such that their baselines align. The item with the largest distance between its cross-start margin edge and its baseline is flushed with the cross-start edge of the line.

stretch:

  • If the items are smaller than the alignment container, auto-sized items will be equally enlarged to fill the container, respecting the items’ width and height limits.

anchor-center:

  • In the case of anchor-positioned elements, aligns the items to the center of the associated anchor element in the block direction.

safe:

  • Used alongside an alignment keyword. If the chosen keyword means that the item overflows the alignment container causing data loss, the item is instead aligned as if the alignment mode were start.

unsafe:

  • Used alongside an alignment keyword. Regardless of the relative sizes of the item and alignment container and whether overflow which causes data loss might happen, the given alignment value is honored.

Additionally, there are two values defined specifically for flexbox layouts that also work in grid layouts:

flex-start:

  • Used in flex layout only, aligns the flex items flush against the flex container’s main-start or cross-start side. When used outside of a flex formatting context, this value behaves as start.

flex-end:

  • Used in flex layout only, aligns the flex items flush against the flex container’s main-end or cross-end side. When used outside of a flex formatting context, this value behaves as end.

Formal Definition and Syntax

The align-items property is defined with specific initial values, inheritance, and computed values to ensure consistent behavior across different browsers and rendering engines. Here’s a detailed look at its formal definition:

Initial Valuenormal
Applies toall elements
Inheritedno
Computed Valueas specified
Animation Typediscrete

The formal syntax for the align-items property is structured to include various alignment options, ensuring flexibility and control over the layout:

align-items =
normal |
stretch |
<baseline-position> |
[ <overflow-position>? <self-position> ] |
anchor-center
<baseline-position> =
[ first | last ]? && baseline
<overflow-position> =
unsafe | safe
<self-position> =
center | start | end | self-start | self-end | flex-start | flex-end

Examples

Let’s explore some interactive examples using both flex and grid layouts. These examples will demonstrate various values of align-items and their effects on the alignment of items within a container.

Flexbox and Grid Alignment

HTML:

<div id="flex-container" class="flex">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
</div>
<div id="grid-container" class="grid">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
</div>
<div class="controls">
<label for="align-items">align-items (Flex): </label>
<select id="align-items">
<option value="normal">normal</option>
<option value="flex-start">flex-start</option>
<option value="flex-end">flex-end</option>
<option value="center" selected>center</option>
<option value="baseline">baseline</option>
<option value="stretch">stretch</option>
<option value="start">start</option>
<option value="end">end</option>
<option value="self-start">self-start</option>
<option value="self-end">self-end</option>
<option value="first baseline">first baseline</option>
<option value="last baseline">last baseline</option>
<option value="safe center">safe center</option>
<option value="unsafe center">unsafe center</option>
</select>
<label for="align-items-grid">align-items (Grid): </label>
<select id="align-items-grid">
<option value="normal">normal</option>
<option value="start">start</option>
<option value="end">end</option>
<option value="center" selected>center</option>
<option value="baseline">baseline</option>
<option value="stretch">stretch</option>
<option value="self-start">self-start</option>
<option value="self-end">self-end</option>
<option value="first baseline">first baseline</option>
<option value="last baseline">last baseline</option>
<option value="safe center">safe center</option>
<option value="unsafe center">unsafe center</option>
</select>
</div>

CSS:

.flex {
display: flex;
flex-wrap: wrap;
align-items: center; /* Change this value in the live sample */
border: solid 5px transparent;
gap: 3px;
height: 200px;
width: 500px;
background-color: #8c8c9f;
}
.grid {
display: grid;
grid-template-columns: repeat(auto-fill, 100px);
align-items: center; /* Change this value in the live sample */
border: solid 5px transparent;
gap: 3px;
height: 200px;
width: 500px;
background-color: #9f8c8c;
}
.item {
background-color: #8cffa0;
min-height: 50px;
width: 100px;
display: flex;
align-items: center;
justify-content: center;
font-size: 18px;
}
.controls {
margin-top: 10px;
}

JavaScript:

const alignItemsSelect = document.getElementById("align-items");
const flexContainer = document.getElementById("flex-container");
alignItemsSelect.addEventListener("change", (evt) => {
flexContainer.style.alignItems = evt.target.value;
});
const alignItemsGridSelect = document.getElementById("align-items-grid");
const gridContainer = document.getElementById("grid-container");
alignItemsGridSelect.addEventListener("change", (evt) => {
gridContainer.style.alignItems = evt.target.value;
});

These examples illustrate how the align-items property can be used to control the alignment of items within flex and grid containers. By changing the values in the dropdown menus, you can see the immediate effect on the layout, helping you understand the practical application of this CSS property.

Browser Compatibility

The align-items property is widely supported across modern web browsers, ensuring that you can use it confidently in your web development projects. Here’s a breakdown of browser compatibility for the align-items property:

  • Chrome: Full support since version 21.0 (July 2012).
  • Firefox: Full support since version 20.0 (April 2013).
  • Internet Explorer / Edge: Supported in Internet Explorer since version 11.0 (October 2013). Full support in Microsoft Edge.
  • Opera: Full support since version 12.1 (November 2012).
  • Safari: Full support since version 9.0 (September 2015).
  • iOS Safari: Full support since version 7.0, with additional improvements in later versions.
  • Android Chrome: Full support across all versions.
  • Android Firefox: Full support across all versions.
  • Android Browser: Supported since version 92.
  • Opera Mobile: Full support since version 12.1.

Understanding the formal definition and syntax of the align-items property is crucial for web developers aiming to create precise and responsive layouts. This detailed knowledge ensures that you can effectively control the alignment of items within a container, leading to better web design outcomes.

Conclusion

The align-items property is a versatile tool for aligning items within flex and grid containers. By understanding its values and how to use them, you can create dynamic and responsive web layouts. The examples provided demonstrate the practical application of this property, and the wide browser compatibility ensures that you can use it with confidence in your projects. Whether you’re a beginner or an experienced developer, mastering the align-items property will enhance your web design skills and help you create more engaging and functional websites.

See Also

To learn more about CSS alignment properties and related topics, check out these helpful resources:

These resources will help you master CSS alignment properties and create complex, responsive layouts. Whether you’re using flexbox, grid, or other layout systems, understanding these properties is key to effective web design and development.

icons/css-4.svg CSS Blogs
CSS3 is the latest version of Cascading Style Sheets, offering advanced styling features like animations, transitions, shadows, gradients, and responsive design.
icons/logo-tid.svg

Talk with CEO

Ready to bring your web/app to life or boost your team with expert Thai developers?
Contact us today to discuss your needs, and let’s create tailored solutions to achieve your goals. We’re here to help at every step!
🖐️ Contact us
Let's keep in Touch
Thank you for your interest in Tillitsdone! Whether you have a question about our services, want to discuss a potential project, or simply want to say hello, we're here and ready to assist you.
We'll be right here with you every step of the way.
Contact Information
rick@tillitsdone.com+66824564755
Find All the Ways to Get in Touch with Tillitsdone - We're Just a Click, Call, or Message Away. We'll Be Right Here, Ready to Respond and Start a Conversation About Your Needs.
Address
9 Phahonyothin Rd, Khlong Nueng, Khlong Luang District, Pathum Thani, Bangkok Thailand
Visit Tillitsdone at Our Physical Location - We'd Love to Welcome You to Our Creative Space. We'll Be Right Here, Ready to Show You Around and Discuss Your Ideas in Person.
Social media
Connect with Tillitsdone on Various Social Platforms - Stay Updated and Engage with Our Latest Projects and Insights. We'll Be Right Here, Sharing Our Journey and Ready to Interact with You.
We anticipate your communication and look forward to discussing how we can contribute to your business's success.
We'll be here, prepared to commence this promising collaboration.
Frequently Asked Questions
Explore frequently asked questions about our products and services.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.