Skip to content

Commit 49106bd

Browse files
CSS module: box alignment (#37566)
* CSS module: box alignment * CSS module: box alignment * related * Apply suggestions from code review Co-authored-by: Chris Mills <[email protected]> * Update files/en-us/web/css/css_box_alignment/index.md Co-authored-by: Chris Mills <[email protected]> * Update files/en-us/web/css/css_box_alignment/index.md Co-authored-by: Chris Mills <[email protected]> * edits per review * edits per review * link text * Fix suggestion snafu --------- Co-authored-by: Chris Mills <[email protected]>
1 parent e03cb03 commit 49106bd

File tree

14 files changed

+387
-382
lines changed

14 files changed

+387
-382
lines changed

files/en-us/glossary/baseline/typography/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ Other writing systems have different baselines. For example, Tibetan and similar
1414

1515
## See also
1616

17-
- [CSS box alignment](/en-US/docs/Web/CSS/CSS_box_alignment#types_of_alignment)
17+
- [CSS box alignment](/en-US/docs/Web/CSS/CSS_box_alignment/box_alignment#types_of_alignment)
1818
- [CSS inline layout](/en-US/docs/Web/CSS/CSS_inline_layout) module
1919
- [Baseline (Typography)](<https://en.wikipedia.org/wiki/Baseline_(typography)>) on Wikipedia

files/en-us/mozilla/firefox/releases/61/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ _No changes._
3636

3737
- CSS parsing has been parallelized ([Firefox bug 1346988](https://bugzil.la/1346988)).
3838
- Support for {{cssxref("font-variation-settings")}} and {{cssxref("font-optical-sizing")}} has been enabled by default ([Firefox bug 1447163](https://bugzil.la/1447163)).
39-
- The `grid-gap`, `grid-row-gap`, and `grid-column-gap` properties have been renamed to {{cssxref("gap")}}, {{cssxref("row-gap")}}, and {{cssxref("column-gap")}}, as they are no longer grid-specific ([Firefox bug 1398482](https://bugzil.la/1398482)). See [Box alignment; Gaps between boxes](/en-US/docs/Web/CSS/CSS_box_alignment#gaps_between_boxes) for additional details. The old names have been kept as aliases for web compatibility purposes.
39+
- The `grid-gap`, `grid-row-gap`, and `grid-column-gap` properties have been renamed to {{cssxref("gap")}}, {{cssxref("row-gap")}}, and {{cssxref("column-gap")}}, as they are no longer grid-specific ([Firefox bug 1398482](https://bugzil.la/1398482)). See [Box alignment; Gaps between boxes](/en-US/docs/Web/CSS/CSS_box_alignment/box_alignment#gaps_between_boxes) for additional details. The old names have been kept as aliases for web compatibility purposes.
4040
- The {{cssxref("flex-basis")}} `content` value is now supported ([Firefox bug 1105111](https://bugzil.la/1105111)).
4141
- Percentage values of {{cssxref("column-gap")}} are now supported in [CSS multi-column layout](/en-US/docs/Web/CSS/CSS_multicol_layout) ([Firefox bug 1398537](https://bugzil.la/1398537)).
4242
- The CSS {{cssxref(":host")}} pseudo-class is now supported; this selects a custom element from inside its shadow DOM ([Firefox bug 992245](https://bugzil.la/992245)).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,288 @@
1+
---
2+
title: CSS box alignment overview
3+
slug: Web/CSS/CSS_box_alignment/box_alignment
4+
page-type: guide
5+
---
6+
7+
{{CSSRef}}
8+
9+
The [CSS box alignment](/en-US/docs/Web/CSS/CSS_box_alignment) module specifies CSS features that relate to the alignment of boxes in the various CSS box layout models. The module aims to create a consistent method of alignment across all of CSS. The CSS box alignment properties provide full horizontal and vertical alignment capabilities.
10+
11+
This guide details the general concepts found in this module. Additional guides provide more information on box alignment in [flexbox](/en-US/docs/Web/CSS/CSS_box_alignment/Box_alignment_in_flexbox), [grid layout](/en-US/docs/Web/CSS/CSS_box_alignment/Box_alignment_in_grid_layout), [multiple-column layout](/en-US/docs/Web/CSS/CSS_box_alignment/Box_alignment_in_multi-column_layout), and [block, absolutely positioned and table layout](/en-US/docs/Web/CSS/CSS_box_alignment/Box_alignment_in_block_abspos_tables). Alignment of text is covered by the [CSS text](/en-US/docs/Web/CSS/CSS_text) and [CSS inline layout](/en-US/docs/Web/CSS/CSS_inline_layout) modules.
12+
13+
## Key concepts and terminology
14+
15+
The specification details some alignment terminology to make it easier to discuss these alignment properties outside their implementation within a particular layout method. There are also some key concepts which are common to all layout methods.
16+
17+
### Relationship to writing modes
18+
19+
Alignment is linked to writing modes in that when we align an item we do not consider whether we are aligning it to the physical dimensions of top, right, bottom and left. Instead we describe alignment in terms of the start and end of the particular dimension we are working with. This ensures that alignment works in the same way whichever writing mode the document has.
20+
21+
### Two dimensions of alignment
22+
23+
When using the box alignment properties you will align content on one of two axes — the inline (or main) axis, and the block (or cross) axis. The inline axis is the axis along which words in a sentence flow in the writing mode being used. For English, for example, the inline axis is horizontal. The block axis is the axis along which blocks, such as paragraph elements, are laid out; it runs across the Inline axis.
24+
25+
![Inline axis is the left / right, or horizontal, direction. Block axis is vertical, or top / bottom.](two-axes.png)
26+
27+
When aligning items on the inline axis you will use the properties that begin with `justify-`:
28+
29+
- {{cssxref("justify-items")}}
30+
- {{cssxref("justify-self")}}
31+
- {{cssxref("justify-content")}}
32+
33+
When aligning items on the block axis you will use the properties that begin with `align-`:
34+
35+
- {{cssxref("align-items")}}
36+
- {{cssxref("align-self")}}
37+
- {{cssxref("align-content")}}
38+
39+
Flexbox adds an additional complication in that the above is true when {{cssxref("flex-direction")}} is set to `row`. The properties are swapped when flexbox is set to `column`. Therefore, when working with flexbox it is easier to think about the main and cross axis rather than inline and block. The `justify-` properties are always used to align on the main axis, the `align-` properties on the cross axis.
40+
41+
### The alignment subject
42+
43+
The **{{Glossary("alignment subject")}}** is the thing that is being aligned. For `justify-self` or `align-self`, or when setting these values as a group with `justify-items` or `align-items`, this will be the margin box of the element that this property is being used on. The `justify-content` and `align-content` properties differ per layout method.
44+
45+
### The alignment container
46+
47+
The **{{Glossary("alignment container")}}** is the box the subject is being aligned inside. This will typically be the alignment subject's containing block. An alignment container may contain one or many alignment subjects.
48+
49+
The below image shows an alignment container with two alignment subjects inside.
50+
51+
![A box containing two rectangles of the same width but different heights. The two rectangles are top aligned, meaning they both have their top lines about 10px inside the top of the box in which they are contained.](align-container-subjects.png)
52+
53+
## Types of alignment
54+
55+
There are three different types of alignment that the specification details; these use keyword values.
56+
57+
- [Positional alignment](#positional-alignment)
58+
- [Baseline alignment](#baseline-alignment)
59+
- [Distributed alignment](#distributed-alignment)
60+
61+
### Positional alignment
62+
63+
**Positional alignment** is the position of an alignment subject with relation to its alignment container. The positional alignment keyword values are defined for positional alignment, and can be used as values for content alignment with `justify-content` and `align-content` and also for self alignment with `justify-self` and `align-self`.
64+
65+
- `center`
66+
- `start`
67+
- `end`
68+
- `self-start`
69+
- `self-end`
70+
- `flex-start` for flexbox only
71+
- `flex-end` for flexbox only
72+
- `left`
73+
- `right`
74+
75+
Other than the physical values of `left` and `right`, which relate to physical attributes of the screen, all of the other values, the {{cssxref("self-position")}} and {{cssxref("content-position")}} values, are logical values and relate to the writing mode of the content.
76+
77+
For example, when working in CSS grid layout, if you are working in English and set `justify-content` to `start` this will move the items in the inline dimension to the start, which will be the left as sentences in English start on the left-hand side of the page. If you were using Arabic, a right-to-left language, then the same value of `start` would result in the items moving to the right, as sentences in Arabic start on the right-hand side of the page.
78+
79+
![There are two boxes, each with 3 children of differing heights but similar widths. The first box has three children with the letters A, B, and C. These three boxes are all aligned to the left. The second box has three children with arabic letters in them. Those three boxes are all aligned to the right.](writing-mode-start.png)
80+
81+
Both have `justify-content: start`, but the location of the two starts is different because of the writing mode.
82+
83+
### Baseline alignment
84+
85+
**Baseline alignment** is the relationship among the baselines of multiple alignment subjects within an alignment context. The Baseline alignment {{cssxref("baseline-position")}} keywords are used to align the baselines of boxes across a group of alignment subjects. They can be used as values for content alignment with `justify-content` and `align-content` and self-alignment with `justify-self` and `align-self`.
86+
87+
- `baseline`
88+
- `first baseline`
89+
- `last baseline`
90+
91+
Baseline content alignment — specifying a baseline alignment value for `justify-content` or `align-content` — works in layout methods that lay items out in rows. The alignment subjects are baseline aligned against each other by adding padding inside the boxes.
92+
93+
Baseline self-alignment shifts the boxes to align by baseline by adding a margin outside the boxes. Self-alignment is done for singular boxes using `justify-self` or `align-self`, or for groups of boxes using `justify-items` and `align-items`.
94+
95+
### Distributed alignment
96+
97+
**Distributed alignment** defines alignment as a distribution of space among alignment subjects. The distributed alignment {{cssxref("content-distribution")}} keywords are used with the `align-content` and `justify-content` properties. These keywords define what happens to any additional space after alignment subjects have been displayed. The values are as follows:
98+
99+
- `stretch`
100+
- `space-between`
101+
- `space-around`
102+
- `space-evenly`
103+
104+
For example, in Flex Layout items are aligned with `flex-start` initially. Working in a horizontal top-to-bottom writing mode (with a language such as English), with `flex-direction` set to `row`, the items start on the far left, and any available space after displaying the items is placed after them.
105+
106+
![Three rectangles of different widths are inside a box. They are all aligned to the left side of the containing box, with about 10px between them, and 10px between the left side of the first rectangle and the parent container.](justify-content-start.png)
107+
108+
If you set `justify-content: space-between` on the flex container, the available space is now shared out and placed between the items.
109+
110+
![Three rectangles of different widths are inside a box. The first rectangle is aligned to the left side of the containing box, the third rectangle is aligned right, and the middle rectangle is equally spaced between the first and last.](justify-content-space-between.png)
111+
112+
For these keywords to take effect, space is required along the dimension in which you wish to align the items. With no space, there is nothing to distribute.
113+
114+
### Basic examples
115+
116+
The following examples demonstrate how some of the box alignment properties are applied in [Grid](/en-US/docs/Web/CSS/CSS_grid_layout) and [Flexbox](/en-US/docs/Web/CSS/CSS_flexible_box_layout).
117+
118+
#### CSS grid layout alignment example
119+
120+
In this grid layout example, there is extra space in the grid container after laying out the fixed-width tracks on the inline (main) axis. This space is distributed using {{cssxref("justify-content")}}. On the block (cross) axis, the alignment of the items inside their grid areas is controlled with {{cssxref("align-items")}}. The first item overrides the `align-items` value set on the group by setting {{cssxref("align-self")}} to `center`.
121+
122+
```html live-sample___grid-align-items
123+
<div class="box">
124+
<div>One</div>
125+
<div>Two</div>
126+
<div>Three <br />has <br />extra <br />text</div>
127+
<div>Four</div>
128+
<div>Five</div>
129+
<div>Six</div>
130+
</div>
131+
```
132+
133+
```css hidden live-sample___grid-align-items
134+
body {
135+
font: 1.2em sans-serif;
136+
}
137+
138+
.box {
139+
border: 2px dotted rgb(96 139 168);
140+
}
141+
142+
.box > * {
143+
padding: 20px;
144+
border: 2px solid rgb(96 139 168);
145+
border-radius: 5px;
146+
background-color: rgb(96 139 168 / 0.2);
147+
}
148+
```
149+
150+
```css live-sample___grid-align-items
151+
.box {
152+
display: grid;
153+
grid-template-columns: 120px 120px 120px;
154+
align-items: start;
155+
justify-content: space-between;
156+
}
157+
158+
.box :first-child {
159+
align-self: center;
160+
}
161+
```
162+
163+
{{EmbedLiveSample("grid-align-items", "", "200px")}}
164+
165+
#### Flexbox Alignment Example
166+
167+
In this example, three flex items are aligned on the main axis using `justify-content` and on the cross axis using `align-items`. The first item overrides the `align-items` set on the group by setting `align-self` to `center`.
168+
169+
```html live-sample___flex-align-items
170+
<div class="box">
171+
<div>One</div>
172+
<div>Two</div>
173+
<div>Three <br />has <br />extra <br />text</div>
174+
</div>
175+
```
176+
177+
```css hidden live-sample___flex-align-items
178+
body {
179+
font: 1.2em sans-serif;
180+
}
181+
182+
.box {
183+
border: 2px dotted rgb(96 139 168);
184+
}
185+
186+
.box > * {
187+
padding: 20px;
188+
border: 2px solid rgb(96 139 168);
189+
border-radius: 5px;
190+
background-color: rgb(96 139 168 / 0.2);
191+
}
192+
```
193+
194+
```css live-sample___flex-align-items
195+
.box {
196+
display: flex;
197+
align-items: flex-start;
198+
justify-content: space-between;
199+
}
200+
201+
.box :first-child {
202+
align-self: center;
203+
}
204+
```
205+
206+
{{EmbedLiveSample("flex-align-items")}}
207+
208+
## Overflow alignment
209+
210+
The {{cssxref("overflow-position")}} keywords `safe` and `unsafe` help define behavior when an alignment subject is larger than the alignment container. The `safe` keyword will align to `start` in the case of a specified alignment causing an overflow, the aim being to avoid "data loss" where part of the item is outside the boundaries of the alignment container and can't be scrolled to.
211+
212+
If you specify `unsafe` then the alignment will be honoured even if it would cause such data loss.
213+
214+
## Gaps between boxes
215+
216+
The box alignment specification also includes the `gap`, `row-gap`, and `column-gap` properties. These properties enable the setting of a consistent gap between items in a row or column, in any layout method which has items arranged in this way.
217+
218+
The `gap` property is a shorthand for `row-gap` and `column-gap`, which allows us to set these properties at once:
219+
220+
- {{cssxref("row-gap")}}
221+
- {{cssxref("column-gap")}}
222+
- {{cssxref("gap")}}
223+
224+
In the below example, a grid layout uses the `gap` shorthand to set a `10px` gap between row tracks, and a `2em` gap between column tracks.
225+
226+
```html live-sample___grid-gap
227+
<div class="box">
228+
<div>One</div>
229+
<div>Two</div>
230+
<div>Three</div>
231+
<div>Four</div>
232+
<div>Five</div>
233+
<div>Six</div>
234+
</div>
235+
```
236+
237+
```css hidden live-sample___grid-gap
238+
body {
239+
font: 1.2em sans-serif;
240+
}
241+
242+
.box {
243+
border: 2px dotted rgb(96 139 168);
244+
}
245+
246+
.box > * {
247+
padding: 20px;
248+
border: 2px solid rgb(96 139 168);
249+
border-radius: 5px;
250+
background-color: rgb(96 139 168 / 0.2);
251+
}
252+
```
253+
254+
```css live-sample___grid-gap
255+
.box {
256+
display: grid;
257+
grid-template-columns: 1fr 1fr 1fr;
258+
gap: 10px 2em;
259+
}
260+
261+
.box :first-child {
262+
align-self: center;
263+
}
264+
```
265+
266+
{{EmbedLiveSample("grid-gap")}}
267+
268+
Early grid implementations included `gap` properties prefixed with `grid-`. All browsers support the unprefixed properties, though you may see the following properties in a code-base: {{cssxref("row-gap", "grid-row-gap")}}, {{cssxref("column-gap", "grid-column-gap")}}, and {{cssxref("gap", "grid-gap")}}. The prefixed versions are aliases of the unprefixed ones.
269+
270+
Be aware that other things may increase the visual gap displayed, for example using the space distribution keywords or adding margins to items.
271+
272+
## Box-alignment by layout type
273+
274+
As the CSS box alignment properties are implemented differently depending on the specification they interact with, refer to the following guides for details of how to use the alignment properties with each layout type:
275+
276+
- [Box alignment in flexbox](/en-US/docs/Web/CSS/CSS_box_alignment/Box_alignment_in_flexbox)
277+
- [Box alignment in CSS grid layout](/en-US/docs/Web/CSS/CSS_box_alignment/Box_alignment_in_grid_layout)
278+
- [Box alignment in multiple-column layout](/en-US/docs/Web/CSS/CSS_box_alignment/Box_alignment_in_multi-column_layout)
279+
- [Box alignment for block, absolutely positioned and table layout](/en-US/docs/Web/CSS/CSS_box_alignment/Box_alignment_in_block_abspos_tables)
280+
281+
## See also
282+
283+
- [CSS display](/en-US/docs/Web/CSS/CSS_display) module
284+
- [CSS flex layout](/en-US/docs/Web/CSS/CSS_flexible_box_layout) module
285+
- [Basic concepts of flexbox](/en-US/docs/Web/CSS/CSS_flexible_box_layout/Basic_concepts_of_flexbox)
286+
- [Aligning items in a flex container](/en-US/docs/Web/CSS/CSS_flexible_box_layout/Aligning_items_in_a_flex_container)
287+
- [CSS grid layout](/en-US/docs/Web/CSS/CSS_grid_layout) module
288+
- [Box alignment in CSS grid layouts](/en-US/docs/Web/CSS/CSS_grid_layout/Box_alignment_in_grid_layout)

files/en-us/web/css/css_box_alignment/box_alignment_in_block_abspos_tables/index.md

+6-16
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ slug: Web/CSS/CSS_box_alignment/Box_alignment_in_block_abspos_tables
44
page-type: guide
55
---
66

7-
The [box alignment specification](/en-US/docs/Web/CSS/CSS_box_alignment) details how alignment works in various layout methods. In this page we explore how box alignment works in the context of block layout, including floated, positioned, and table elements. As this page aims to detail things which are specific to block layout and box alignment, it should be read in conjunction with the main [box alignment](/en-US/docs/Web/CSS/CSS_box_alignment) page, which details the common features of box alignment across layout methods.
7+
The [CSS box alignment](/en-US/docs/Web/CSS/CSS_box_alignment) module details how alignment works in various layout methods. In this guide, we explore how box alignment works in the context of block layout, including floated, positioned, and table elements. As this guide aims to detail things which are specific to block layout and box alignment, it should be read in conjunction with the [box alignment](/en-US/docs/Web/CSS/CSS_box_alignment/box_alignment) guide, which details the common features of box alignment across layout methods.
88

99
## align-content and justify-content
1010

@@ -75,19 +75,9 @@ For many use cases, turning the block container into a flex item will give you t
7575

7676
{{EmbedLiveSample("intro", "", "320px")}}
7777

78-
## Reference
78+
## See also
7979

80-
### CSS Properties
81-
82-
- {{cssxref("justify-content")}}
83-
- {{cssxref("align-content")}}
84-
- {{cssxref("justify-self")}}
85-
- {{cssxref("align-self")}}
86-
87-
### Glossary Entries
88-
89-
- [Alignment subject](/en-US/docs/Glossary/Alignment_Subject)
90-
- [Alignment container](/en-US/docs/Glossary/Alignment_Container)
91-
- [Fallback alignment](/en-US/docs/Glossary/Fallback_Alignment)
92-
93-
{{CSSRef}}
80+
- [Box alignment overview](/en-US/docs/Web/CSS/CSS_box_alignment/Box_alignment)
81+
- [Box alignment in flexbox](/en-US/docs/Web/CSS/CSS_box_alignment/Box_alignment_in_flexbox)
82+
- [Box alignment in CSS grid layout](/en-US/docs/Web/CSS/CSS_box_alignment/Box_alignment_in_grid_layout)
83+
- [Box alignment in multiple-column layout](/en-US/docs/Web/CSS/CSS_box_alignment/Box_alignment_in_multi-column_layout)

0 commit comments

Comments
 (0)