From 24545e8e953cd48f8dd682f1c534e537deb41430 Mon Sep 17 00:00:00 2001 From: Estelle Weyl Date: Mon, 17 Jun 2024 06:56:40 -0700 Subject: [PATCH] CSS property update: align-items (#34142) * CSS property update: align-items * Edits based of @bsmth's feedback * grid size * flex values outside of flex * Update index.md --- files/en-us/web/css/align-items/index.md | 99 +++++++++++++++++------- 1 file changed, 71 insertions(+), 28 deletions(-) diff --git a/files/en-us/web/css/align-items/index.md b/files/en-us/web/css/align-items/index.md index 149f9c34050872b..38496656f5b46cd 100644 --- a/files/en-us/web/css/align-items/index.md +++ b/files/en-us/web/css/align-items/index.md @@ -7,12 +7,12 @@ browser-compat: css.properties.align-items {{CSSRef}} -The [CSS](/en-US/docs/Web/CSS) **`align-items`** property sets the {{cssxref("align-self")}} value on all direct children as a group. In Flexbox, it controls the alignment of items on the {{glossary("Cross Axis")}}. In Grid Layout, it controls the alignment of items on the Block Axis within their {{glossary("Grid Areas", "grid area")}}. - -The interactive example below demonstrates some of the values for `align-items` using grid layout. +The [CSS](/en-US/docs/Web/CSS) **`align-items`** property sets the {{cssxref("align-self")}} value on all direct children as a group. In flexbox, it controls the alignment of items on the {{glossary("cross axis")}}. In grid layout, it controls the alignment of items on the block axis within their {{glossary("grid areas")}}. {{EmbedInteractiveExample("pages/css/align-items.html")}} +The interactive example below demonstrates some of the values for `align-items` using grid and flex layout. + ## Syntax ```css @@ -57,29 +57,51 @@ align-items: unset; - For grid items, this keyword leads to a behavior similar to the one of `stretch`, except for boxes with an {{glossary("aspect ratio")}} or an intrinsic sizes where it behaves like `start`. - The property doesn't apply to block-level boxes, and to table cells. -- `flex-start` - - : Used in flex layout only, aligns the flex items flush against the flex container's main-start or cross-start side. -- `flex-end` - - : Used in flex layout only, aligns the flex items flush against the flex container's main-end or cross-end side. - `center` + - : The flex items' margin boxes are centered within the line on the cross-axis. If the cross-size of an item is larger than the flex container, it will overflow equally in both directions. + - `start` + - : The items are packed flush to each other toward the start edge of the alignment container in the appropriate axis. + - `end` + - : The items are packed flush to each other toward the end edge of the alignment container in the appropriate axis. + - `self-start` + - : The items are packed flush to the edge of the alignment container's start side of the item, in the appropriate axis. + - `self-end` + - : The items are packed 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 [flex container baselines](https://drafts.csswg.org/css-flexbox-1/#flex-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. + - `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. +There are also two values that were defined for flexbox, as they are base on [flex model axes](/en-US/docs/Learn/CSS/CSS_layout/Flexbox#the_flex_model) concepts, that work in grid layouts as well: + +- `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 {{CSSInfo}} @@ -90,33 +112,34 @@ align-items: unset; ## Examples +In this example we have a container with six children. A {{htmlelement("select")}} dropdown menu enables toggling the {{cssxref("display")}} of the container between `grid` and `flex`. A second menu enables changing the value of the container's `align-items` property. + ### CSS +We style a the container and items in a manner that ensures we have two lines or rows or items. We defined `.flex` and `.grid` classes, which will be applied to the container with JavaScript. They set the {{cssxref("display")}} value of the container, and change its background and border colors providing an additional indicator that the layout has changed. The six flex items each have a different background color, with the 4th item being two lines long and the 6th item having an enlarged font. + ```css -#container { +.flex, +.grid { height: 200px; - width: 240px; - align-items: center; /* Can be changed in the live sample */ - background-color: #8c8c8c; + width: 500px; + align-items: initial; /* Change the value in the live sample */ + border: solid 5px transparent; + gap: 3px; } .flex { display: flex; flex-wrap: wrap; + background-color: #8c8c9f; + border-color: magenta; } .grid { display: grid; - grid-template-columns: repeat(auto-fill, 50px); -} - -div > div { - box-sizing: border-box; - border: 2px solid #8c8c8c; - width: 50px; - display: flex; - align-items: center; - justify-content: center; + grid-template-columns: repeat(auto-fill, 100px); + background-color: #9f8c8c; + border-color: slateblue; } #item1 { @@ -149,7 +172,9 @@ div > div { min-height: 50px; font-size: 30px; } +``` +```css hidden select { font-size: 16px; } @@ -157,20 +182,33 @@ select { .row { margin-top: 10px; } + +div > div { + box-sizing: border-box; + border: 2px solid #fff; + width: 100px; + display: flex; + align-items: center; + justify-content: center; +} ``` ### HTML +We include a container {{htmlelement("div")}} with six nested `
` children. The HTML for the form and the JavaScript that changes the container's class have been hidden for the sake of brevity. + ```html
1
2
3
-
4
+
4
line 2
5
6
+``` +```html hidden