Justify content justify items в чем разница
Перейти к содержимому

Justify content justify items в чем разница

  • автор:

The Difference between Justify Content, Justify Items, Align Content and Align Items as they pertain to flexboxes…

TheCodeLessTravelled

I have most recently completed my very first webpage. (Feel free to have a gander) Now I know I’m not alone in my many a tantrum and frustration in learning the finer points of CSS. One large point of contention for myself were these 4 so similarly named commands above. Justify and align in the English dictionary are synonyms. Content and Items are also synonyms. These phrases literally mean the same thing when spoken in a normal conversation!! But in CSS they all have very different meanings and how many times I experimentally interchanged them until I got my desired result, I could not count.

But after much ado I can now share my knowledge on their actual differences!

Justify Content: Firstly this is only used on items in a flexbox, and only makes a difference if these items do not take up all the space in the flexbox! It moves the items in the box horizontally (if the flexbox is set to a row)

The main values for this can be set to:
flex-start — moves all items to the start of the box (left)
flex-end — moves all items to the end of the box (right)
center — centers the items in the middle of the box
space-between — put the first item at the start of the box with the last item at the end and everything else spaced equally inbetween
space-around — every item will be equally spaced with a space at the beginning and end as well.

Justify-Items: This sets the default value for justify-self for all items in the box, and when used in a flexbox it is ignored.

Align-Items: This is more of a vertical alignment (when the flexbox is set to row). It defines how individual items should arrange themselves inside a flexbox.

The main values for this can be set to:
stretch — stretches all the items in the box to fill the space top to bottom
center — positions all items in the middle of the container
flex-start — arranges all items at the top of the container
flex-end — arranges all items at the bottom of the container

Align-Content: This is similar to align-items only it only works once the items wrap! It tells flex-lines how to organise themselves, but utterly useless if they are just the one line!

So to summarise:
Justify-content — only works if the box isn’t full and puts items at the start and end of box.

Justify-items — useless on flexboxes

Align-items — puts items up and down in the box

Align-content — same as align-items but only works once the items wrap!

What is difference between justify-self, justify-items and justify-content in CSS grid?

I’m really confused. When looking for online resources and documentation, most of the documentation of these properties seem to reference Flex-box, not grid. And I don’t know how applicable the documentation for the equivalent properties in Flex-box is to grid.

justify-items — Aligns the content inside a grid item along the row axis

justify-content — This property aligns the grid along the row axis

justify-self — Aligns the content inside a grid item along the row axis

But I still don’t understand what the difference between them is. So, I have 3 questions I want to clarify.

  1. Is the justify-items property in Flex-box the same as the justify-items property in Grid? or are they different somehow? (In other words, can I reuse Flex-box documentation for Grid)
  2. What do (justify-)content, self and items do?
  3. How are (justify-)content, self and items different?

Any clarification would be greatly appreciated.

Edit: Since everyone keeps giving me Flex-box resources, I am asking about css-grid, NOT flex-box.

5 Answers 5

To answer your questions:

1

As reiallenramos mentioned, «The justify-self and justify-items properties are not implemented in flexbox. This is due to the one-dimensional nature of flexbox, and that there may be multiple items along the axis, making it impossible to justify a single item. To align items along the main, inline axis in flexbox you use the justify-content property.» — MDN

2-3

This screen shot from W3 does an excellent job of showing what they do and the differences between them. enter image description here

Good To Knows:

For more information and example, I would suggest you check out:

And for some inspiration:

L Bahr's user avatar

Key differences between justify-content , justify-items and justify-self in CSS Grid:

  • The justify-content property controls the alignment of grid columns. It is set on the grid container. It does not apply to or control the alignment of grid items.
  • The justify-items property controls the alignment of grid items. It is set on the grid container.
  • The justify-self property overrides justify-items on individual items. It is set on grid items and inherits the value of justify-items , by default.

Example

Here’s a 2×3 grid.

  • 2 columns, each 100px wide
  • 3 rows, each 50px tall

The container is:

  • 500px wide
  • 250px tall

enter image description here

justify-content

The justify-content property aligns columns within the container.

enter image description here

With justify-content: space-between both columns are pinned to the edges. The grid items shift only because they exist inside those columns. They are otherwise unaffected.

Note that this property works only when there is free space in the container. If any of the columns were sized with fr , then all free space would be consumed, and justify-content would have no effect.

justify-items

The justify-items property aligns grid items within their tracks (not the entire container)

enter image description here

With justify-items: center the grid items are centered within their columns.

justify-self

The justify-self property overrides justify-items on individual items.

enter image description here

align-content , align-items and align-self

These properties do the same as their justify-* counterparts, but in the perpendicular direction.

Spec Reference

10.3. Row-axis Alignment: the justify-self and justify-items properties

Grid items can be aligned in the inline dimension by using the justify-self property on the grid item or justify-items property on the grid container.

10.4. Column-axis Alignment: the align-self and align-items properties

Grid items can also be aligned in the block dimension (perpendicular to the inline dimension) by using the align-self property on the grid item or align-items property on the grid container.

10.5. Aligning the Grid: the justify-content and align-content properties

If the grid’s outer edges do not correspond to the grid container’s content edges (for example, if no columns are flex-sized), the grid tracks are aligned within the content box according to the justify-content and align-content properties on the grid container.

(emphasis added)

CSS Box Alignment Module

Is the justify-items property in Flex-box the same as the justify-items property in Grid?

Although the Flex and Grid specs provide their own definitions for keyword alignment properties, such as justify-items and align-content , the W3C is in the process of phasing out alignment properties for individual box models and implementing their new Box Alignment Module, which seeks to define a set of alignment properties for use across all box models.

From the flexbox spec:

1.2. Module interactions

The CSS Box Alignment Module extends and supercedes the definitions of the alignment properties ( justify-content , align-items , align-self , align-content ) introduced here.

There are similar references in the Grid spec.

OK, I think I figured it out thanks to Michael_B. My confusion came from the fact that sometimes different properties would randomly not change anything about the layout of the grid.

justify-content allows you to position the grid within it’s grid container. This is why the justify-content property will have no effect if the grid-container is the same size as the grid. (Which is always the case if you use fr units). This is also why it can have the values: space-around, space-between and space-evenly (In addition to start, end, centre and stretch), which will break up the grid and place the grid items within the grid container. This is a property of the grid container.

justify-items allows you to set a default position for content placed in the grid’s grid items (A grid item being a box in the grid, as defined in Michael_B’s post). This is a property of the grid container.

justify-self allows you to override the default position of content in an individual cell. This will override the position set by justify-items. Hence, if you use justify-self on all children of the container, setting justify-items on the grid container will have no effect. This is a property of the content inside a grid item.

Note: If you make a grid-item a grid itself, (In other words, the content inside a grid item is a grid) then you can position it within the outer grid item using either the justify-self property or the justify-content property on the inner grid’s grid container, since the inner-grid’s grid container is one of the outer grid’s grid items’s content.

CSS Flexbox Ultimate Guide

Laying out elements in CSS is something that used to be pretty difficult to do. We were forced to use annoying hacks like floating elements with clearfixes, but then flexbox was created. Flexbox revolutionized CSS layouts and is by far one of the most defining features of CSS to come out in the last 10 years. In this article I will teach you exactly what flexbox is, how you can use it, and all the advanced nuances you need to understand.

If you prefer to learn visually, check out the video version of this article.

What Is Flexbox?

Flexbox is a way to layout elements in CSS and is broken into a two main components. The flexbox container and flexbox items. The flexbox container is the parent element that contains all the flexbox items as its children. The flexbox container is where you define all your properties about the flexbox layout and then on the individual items you can make additional tweaks. Let’s take a look at how we would get started with a simple flexbox example. If we have an element with children inside of it all we need to do is set the display property of the parent element to flex and we will have a flex container. display: flex

As you can see by default when we specify a display of flex on an element we are setting that element as the flex container. All the direct children in that element are then considered flex items. By default if you specify no other properties the flex items will lay themselves out on one line from left to right taking up only as much space as they need. Items will also automatically shrink down to fit within the flex container if they would normally overflow.

Now this is the absolute basics of flexbox and on its own isn’t really useful so next we need to talk about how to handle the layout of items in the container.

Flexbox Layout

Flexbox was the first CSS layout method that worked in a completely different way than normal CSS. Instead of worrying about block/inline elements flexbox worries about a main axis and a cross axis.

By default the main axis (shown in green) goes horizontally across the container and the cross axis goes vertically. This means that any layout method that deals with the main axis will layout elements horizontally while the cross axis will layout elements vertically. Let’s look at how we can layout elements along the main axis first.

justify-content

For all these examples we will assume that the flex-items have a width of 20%.

flex-start (default)

Places all items at the start of the main axis which is the left side of the axis by default. This is also the default behavior of justify-content .

flex-end

Places all items at the end of the main axis which is the right side of the axis by default.

center

Places all items in the center of the main axis. This is one of the easiest ways to center elements in CSS.

space-between

This takes all the extra space inside the container and evenly spreads it between each element to space them as far apart as possible from one another while filling the full container.

space-around

This is very similar to space-between , but it also adds space between the outside of the container and the first/last element. The amount of space between the outside of the container and the first/last element is exactly half the amount of space between elements.

space-evenly

This is very similar to space-around , but the space between the outside of the container and the first/last element is the same as the space between elements instead of half the size.

With all of these properties you can easily lay elements out along the main axis to fit any need. Now let’s talk about how to lay out element on the cross axis.

align-items

For all these examples we will assume that the flex-items all have a width of 20%, but that the elements are all different heights.

stretch (default)

This will stretch all items to fill the full height of the cross axis unless they have a specific height set. In our example I set the height of the first child to initial which is essentially the same as if we had never set a height on the first child. By default when a div has no height it will just be the height of the content inside of it, but as you can see below the first child fills the full height of the container since it is stretching to fill the full height. The second element is not stretching, though, since we set a specific height of 100px on it. This is the default behavior of align-items .

flex-start

This works the same as flex-start for justify-content , but will start at the top of the cross axis by default.

flex-end

This works the same as flex-end for justify-content , but will start at the bottom of the cross axis by default.

center

This works the same as center for justify-content , but will center based on the cross axis.

Now this covers all the ways you can layout elements along the main and cross axis, but there is one more important thing you need to know about flexbox axes. They can actually be swapped. There is a property called flex-direction which determines the orientation of the main and cross axis.

flex-direction

This property allows us to determine which direction each axis corresponds to as well as where the axis start.

row (default)

The default direction is row. This means the main axis is horizontal while the cross axis is vertical. This also means the main axis starts on the left while the cross axis starts at the top.

row-reverse

Similar to row we have row-reverse . This direction does not swap the main/cross axis, but it does swap where the main axis starts. The main axis now starts on the right while the cross axis does not change and still starts at the top. You will see below that our items start on the right side of the container and are ordered right to left since we are using the reverse ordering.

column

The column direction completely swaps our axes so now the main axis is vertical and the cross axis is horizontal. This means that if you use justify-content you will be laying out elements in the vertical direction and align-items will work in the horizontal direction.

column-reverse

The column-reverse direction is essentially the same as column , but it reverses the direction of the main axis so now the items start at the bottom of the container.

Now that is a lot of stuff we just covered related to layout, but really the main things you need to understand are justify-content , align-items , flex-direction , and how each of those effect the main/cross axis.

Flex Item Layout

So far everything we have covered has had to deal with the layout of the entire flex container. We can actually take this a step further, though, and have specific layouts for each item in the flex container. This is done via the align-self property.

As you can see from the above example we set the align-self property of the first child to flex-end and it is now aligned at the bottom of our cross axis even though the flex container as a whole has an align-items of flex-start .

This align-self property can be used with any of the align-items values to place each item in the container exactly where you want them.

One important thing to note, though, is that there is no way to do justify-self since all justification is taken care of by the parent only.

Sizing Flex Items

Now we come to what is probably the most confusing part of flexbox which is sizing the individual items, but I promise you I will make it as easy as possible to understand.

The real power of flexbox is not in its layout properties (even though, those are amazing), but in its ability to resize items based on the size of other elements on your page. This is done via 3 different properties flex-grow , flex-shrink , and flex-basis . First I want to talk about flex-grow since it is the most common property you will use.

flex-grow

The flex-grow property is a property you define on a flex item and it tells the item how much of the extra space that item is allowed to take to fill its container. By default this property is set to 0 which means the item does not get any extra space. Let’s first look at a flex container where none of the items have flex-grow set.

As you can see each item is taking up only its width and the rest of the space in the container is unfilled. This is the default behavior of flexbox, but if you want one or more items to fill the remaining space you need flex-grow .

By setting a flex-grow of 1 we are telling the second element that it should get 1 part of the extra space and since no other items have a flex grow that 1 part of extra space is the entirety of the extra space.

In this example we set both the first and second element to a flex-grow of 1 so now each of those element will receive 1 part of the remaining space. To determine how much space that is we just add up all the flex-grow numbers for each item in the container (1 + 1 = 2) and then divide the flex-grow of each item by that number. Since our flex-grow for each item is 1 each item will get 1/2 of the remaining space added to it.

We can make more complex layouts, though, by giving some elements more or less of the available space.

In this example we are saying the first element should get 2 parts of the remaining space while the second element only gets 1 part. When we do the math we will see that the first element gets 2/3 of the remaining space while the second element only gets 1/3 the remaining space.

At first glance you may think this code is saying that the first element should be twice the size of the second element but flex grow only cares about the remaining space after all elements are added to the container. Since by default our 3 elements take up 60% of the container size the remaining space to divide between the elements is only 40% of the container size. We can actually modify how this remaining space is calculated, though, by using flex-basis

flex-basis

The flex-basis property tells our flex container how much space the item is taking up in the container. By default this is set to auto which means it just uses the width property of the element to calculate this.

In the above code we set the flex-basis of the first element to 40% so now instead of taking up 20% of the width (which is what we have defined as the width of the element) it will instead take up 40% of the container size. This now means there is less space remaining in the container to divide up with flex-grow .

This property is really only useful when you want to change the width of elements in the flex container so they work better with flex-grow . For example let’s say you want to create 3 equal size elements in your container, but they all have different widths to start with that you cannot change.

You may think just setting flex-grow to 1 would make them all the same size, but since they start out at different widths they will end up at different widths since the amount of extra space added to the elements is exactly the same for each independent of their size.

To get around this issue you need to give all elements the same flex-basis so they all start at the same size and grow at the same rate. Generally you will use 0 for the flex-basis since it will ensure all elements are always the same size no matter how small/large they start.

Now the last sizing property we have to talk about is flex-shrink which we have actually already seen in action.

flex-shrink

Way back at the start of this article I mentioned how if the flex items in a container overflow the container they will automatically shrink down to fill the correct size. This is because by default flex-shrink is set to 1 on all flex items.

Even though each element should be 50% of the container they shrink down evenly so that each element is only 33% of the container size. If we wanted to prevent one of the children from shrinking we could set the flex-shrink to 0.

As you can see the first element stays 50% of the container size and does not shrink while the other two elements shrink down to ensure all items can fit in the container.

We can also make it so that some items shrink more than other.

By setting flex-shrink to 2 we are saying that the first element should lose 2 parts of the overflown space while the other two elements each only lose 1 part since they are set to a flex-shrink of 1 by default. This works exactly the same as flex-grow when it comes to proportions, but flex-shrink deals with the overflown space outside the container while flex-grow deals with the space left over inside the container.

For the most part this is not really a property that you will have to mess with much since you usually only care about growing items and the default of shrinking when overflown is usually what you want.

Also, an important thing to note is that flex-shrink is pretty smart and will ensure that if you have one really large item and one really small item that they will shrink in a way that the large item shrinks more so that the small items doesn’t shrink so small that it disappears.

Now that is all you need to know about sizing flex items, but what happens if you want to ensure items don’t shrink and that they can wrap to a new line as needed.

Flex Wrapping

When you are working in flexbox you are usually working with just one line of items, but sometimes in rare occasions you may want to handle wrapping when you have more items than you can fit on one line.

Since our items have a width of 50% normally they would shrink to ensure they all fit on one line, but we set the flex-wrap property to wrap which means that if an item would overflow the container it is instead put on a new line. This is fine if you are using wrap to ensure that even if your list of items grows to be too large they still will look good, but if you are using wrapping to create a two dimensional layout you are using flexbox wrong. Instead you should look towards using CSS grid. You can check out my CSS grid tutorial video for more information on CSS grid.

If you want to stop a flex container from wrapping you need to set the wrap property to nowrap .

Advanced Wrapping Layout

If you use flex-wrap in a column layout then the items will wrap onto new columns instead of rows.

Another important thing to know is that now that there are multiple rows/columns you can use align-content to layout how the different rows/columns interact.

In a normal flexbox layout you can use justify-content to space the items on the main axis and align-content is essentially the same thing, but for spacing out elements on the cross axis. This property will only matter when flex-wrap is set to wrap and the content is wrapping.

For example in a normal flexbox layout the align-content property is set to normal which means that no specific alignment is set. If we want we can use all the fancy alignments available to justify-content within align-content .

As you can see all the items are positioned at the bottom of the container which is the end of the cross axis. If we were to use a column layout instead then align-content would position the elements horizontally.

Advanced Flexbox Properties

Now this covers all the basic and even intermediate properties for flexbox so now I want to cover a few advanced/niches flexbox properties.

Adding Gaps

One thing that we haven’t covered so far is the ability to add gaps between elements in a flexbox container. You could try to do this with margins/padding, but the easier way is to use the gap property. This property will add space between each item based on the value we pass to gap .

As you can see each of our items above has a gap of 10px between them. This gap also works for multiline flex containers too.

As you can see each of our rows/columns has a 10px gap.

We can also go a step further and define only a row gap or only a column gap.

Ordering Elements

By default we can order elements in normal order or reverse order by using flex-direction , but we can also order individual elements with the order property.

As you can see by specifying the order property of our elements we can determine the exact order of them. By default each element has an order of 0 and since flexbox will layout out all elements with the same order based on their order in the HTML you will notice that if you don’t define any order all elements will stay in the same order they are in the HTML.

In our example we specified an order of 2 for our first item and then an order of 1 for our other two items. When flexbox lays out the items it starts with the lowest order first, which in our case is 1. Since our second and third element both have the same order they will be ordered based on their HTML order so the second element will come first and the third element will come second. Finally our first element has an order of 2 which is large than 1 so it will be put as our last element.

Now this may seem like a cool trick that allows you to really change how your page works, but I recommend never using the order property. The reason for this is because it does not work well with screen readers since screen readers always read based on the HTML order. So for example in our above scenario a person looking at the screen will see the numbers in the order 2, 3, 1 while a screen reader will see them in the order 1, 2, 3 since that is the order they appear in the HTML.

Flex Shorthand

We already talked about the 3 main properties used to size a flex item, flex-grow , flex-shrink , and flex-basis . Usually you will only need to define one of these at a time, but if you want to define multiple at once you can use the flex shorthand property which defines all 3 properties.

The above code is the same as:

The way this property works is the first value is passed to flex-grow , the second value is passed to flex-shrink , and the third property is passed to flex-basis . If you only define the first property then it will just set the flex-grow property. CSS is smart enough, though, that if you pass a width value, such as 10px, as the only property to flex then it will set just the flex-basis .

When using the flex shorthand flex-grow will default to 0 unless defined, flex-shrink will default to 1, and flex-basis will default to 0. This is important to note since flex-basis is normally auto , but if you use the flex shorthand it will default flex-basis to 0 unless defined.

Conclusion

And finally we come to the end of the ultimate guide to flexbox. I know this was a ton of information to cover, but hopefully at least some of it stuck. I know it is impossible to memorize all this at once, but you can continually come back to this article for quick refreshers on what each property does. I also am planning to create a cheat sheet soon that will cover all flexbox properties/values with nice drawings to go along with them. If that is something you are interested in make sure you sign up for my newsletter at the top of the page to be notified when I finish the cheat sheet.

CSS Box Alignment Module Level 3

This module contains the features of CSS relating to the alignment of boxes within their containers in the various CSS box layout models: block layout, table layout, flex layout, and grid layout. (The alignment of text and inline-level content is defined in [CSS-TEXT-3] and [CSS-INLINE-3].)

CSS is a language for describing the rendering of structured documents (such as HTML and XML) on screen, on paper, etc.

https://amdy.su/wp-admin/options-general.php?page=ad-inserter.php#tab-8

Status of this document

This section describes the status of this document at the time of its publication. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at https://www.w3.org/TR/.

This document was published by the CSS Working Group as a Working Draft using the Recommendation track. Publication as a Working Draft does not imply endorsement by W3C and its Members.

This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress.

Please send feedback by filing issues in GitHub (preferred), including the spec code “css-align” in the title, like this: “[css-align] …summary of comment…”. All issues and comments are archived. Alternately, feedback can be sent to the (archived) public mailing list www-style@w3.org.

This document was produced by a group operating under the W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.

The following features are at-risk, and may be dropped during the CR period:

  • [ first | last ]? baseline
  • the <overflow-position> keywords
  • the scrollable-area safety trigger into safe mode when no <overflow-position> is specified
  • the legacy keyword for justify-items
  • the effect of the box alignment properties on absolutely-positioned boxes with auto offsets

“At-risk” is a W3C Process term-of-art, and does not necessarily imply that the feature is in danger of being dropped or delayed. It means that the WG believes the feature may have difficulty being interoperably implemented in a timely manner, and marking it as such allows the WG to drop the feature if necessary when transitioning to the Proposed Rec stage, without having to publish a new Candidate Rec without the feature first.

1. Introduction

CSS Levels 1 and 2 allowed for the alignment of text via text-align and the alignment of blocks by balancing auto margins. However, except in table cells, vertical alignment was not possible. As CSS adds further capabilities, the ability to align boxes in various dimensions becomes more critical. This module attempts to create a cohesive and common box alignment model to share among all of CSS.

Note: The alignment of text and inline-level content is defined in [CSS-TEXT-3] and [CSS-INLINE-3].

Note: This specification is not intended to change any of the behavior defined in CSS2.1 when the properties defined here are set to their initial values. If implementors or anyone else notices a discrepancy, please report this to the CSSWG as an error.

This section (above) is not normative.

1.1. Module Interactions

This module adds some new alignment capabilities to the block layout model described in [CSS2] chapters 9 and 10, redefines how overconstrained block-level box margins are resolved, and defines the interaction of these new alignment properties with the alignment of table cell content using vertical-align, as defined in [CSS2] chapter 17.

The interaction of these properties with Grid Layout [CSS-GRID-1] and Flexible Box Layout [CSS-FLEXBOX-1] is defined in their respective modules. The property definitions here supersede those in [CSS-FLEXBOX-1] (which have a smaller, earlier subset of permissible values).

No properties in this module apply to the ::first-line or ::first-letter pseudo-elements.

1.2. Value Definitions

This specification follows the CSS property definition conventions from [CSS2] using the value definition syntax from [CSS-VALUES-3]. Value types not defined in this specification are defined in CSS Values & Units [CSS-VALUES-3] . Combination with other CSS modules may expand the definitions of these value types.

In addition to the property-specific values listed in their definitions, all properties defined in this specification also accept the CSS-wide keywords as their property value. For readability they have not been repeated explicitly.

1.3. Partial Implementations

Since it is expected that support for the features in this module will be deployed in stages corresponding to the various layout models affected, it is hereby clarified that the rules for partial implementations that require treating as invalid any unsupported feature apply to any alignment keyword which is not supported across all layout modules to which it applies for layout models in which the implementation supports the property in general.

For example, if an implementation supports align-self in [CSS-GRID-1] and [CSS-FLEXBOX-1], then it must treat start as invalid unless it is supported in both grid and flex containers. However if that same implementation does not support align-self for block-level elements at all, then a lack of implementation of align-self: start does not trigger this requirement to treat it as invalid.

2. Overview of Alignment Properties

The in CSS are a set of 6 properties that control alignment of boxes within other boxes. They can be described along two axises:

  • which dimension they apply to (main/inline vs. cross/block), and
  • whether they control the position of the box within its parent, or the box’s content within itself.

Note: This specification uses the terms “justify” and “align” to distinguish between alignment in the main/inline and cross/block dimensions, respectively. The choice is somewhat arbitrary, but having the two terms allows for a consistent naming scheme that works across all of CSS’s layout models (including CSS Flexbox 1 § 2 Flex Layout Box Model and Terminology)

The following table summarizes the box alignment properties and the display types they can apply to.

Note: The *-items properties don’t affect the element itself. When set on a container, they specify the interpretation of any *-self: auto used on children of the container element.

3. Alignment Terminology

Since this module defines alignment properties for all layout modes in CSS, some abstract terminology is introduced:

The alignment subject is the thing or things being aligned by the property. For justify-self and align-self, the alignment subject is the margin box of the box the property is set on, and assumes the writing mode of that box. For justify-content and align-content, the alignment subject is defined by the layout mode and refers to some aspect of its contents; it also assumes the writing mode of the box the property is set on. The alignment container is the rectangle that the alignment subject is aligned within. This is defined by the layout mode, but is usually the alignment subject ’s containing block, and assumes the writing mode of the box establishing the containing block. Some alignments can only be fulfilled in certain situations or are limited in how much space they can consume; for example, space-between can only operate when there is more than one alignment subject, and baseline alignment, once fulfilled, might not be enough to absorb all the excess space. In these cases a fallback alignment takes effect (as defined below) to fully consume the excess space.

4. Alignment Keywords

All of the alignment properties use a common set of keyword values, which are defined in this section. Keywords fall into three categories:

positional alignment These keywords define alignment as an absolute position within the alignment container. baseline alignment These keywords define alignment as a relationship among the baselines of multiple alignment subjects within an alignment context. distributed alignment These keywords define alignment as a distribution of space among alignment subjects.

4.1. Positional Alignment: the center, start, end, self-start, self-end, flex-start, flex-end, left, and right keywords

The keywords specify a position for an alignment subject with respect to its alignment container.

Values have the following meanings:

(self, content) Centers the alignment subject within its alignment container. (self, content) Aligns the alignment subject to be flush with the alignment container’s start edge in the appropriate axis. (self, content) Aligns the alignment subject to be flush with the alignment container’s end edge in the appropriate axis. (self) Aligns the alignment subject to be flush with the edge of the alignment container corresponding to the alignment subject ’s start side in the appropriate axis. (self) Aligns the alignment subject to be flush with the edge of the alignment container corresponding to the alignment subject ’s end side in the appropriate axis. (self, content) Only used in flex layout. [CSS-FLEXBOX-1] Aligns the alignment subject to be flush with the edge of the alignment container corresponding to the flex container’s main-start or cross-start side, as appropriate.

When used outside of a flex formatting context, this value behaves as start. That is, on boxes that are not flex items (or pretending to be flex items , such as when determining the static position of an absolutely-positioned box that is a child of a flex container), this value behaves as start when used in the self-alignment properties, and on boxes that are not flex containers , this value behaves as start when used in the content-distribution properties.

(self, content) Only used in flex layout. Aligns the alignment subject to be flush with the edge of the alignment container corresponding to the flex container’s main-end or cross-end side, as appropriate.

When used outside of a flex formatting context, this value behaves as end. That is, on boxes that are not flex items (or pretending to be flex items , such as when determining the static position of an absolutely-positioned box that is a child of a flex container), this value behaves as end when used in the self-alignment properties, and on boxes that are not flex containers , this value behaves as end when used in the content-distribution properties.

(only justify-*) Aligns the alignment subject to be flush with the alignment container’s line-left or physical left edge, whichever is in the appropriate axis.

If the property’s axis is not parallel with either left↔right axis, this value behaves as start. Currently, the only case where the property’s axis is not parallel with either left↔right axis is in a column flexbox.

(only justify-*) Aligns the alignment subject to be flush with the alignment container’s line-right or physical right edge, whichever is in the appropriate axis.

If the property’s axis is not parallel with either left↔right axis, this value behaves as start. Currently, the only case where the property’s axis is not parallel with either left↔right axis is in a column flexbox.

Two grammar terms are used to denote certain subsets of these values:

<self-position> This set is used by justify-self and align-self to align the box within its alignment container, and also by justify-items and align-items (to specify default values for justify-self and align-self ). <content-position> This set is used by justify-content and align-content to align the box’s contents within itself.

Note: left and right are excluded from <self-position> and <content-position>, despite being valid positional alignment values for justify-content/justify-self/justify-items, because they are not allowed in the align-* properties. They are instead explicitly included in the justify-* properties’ grammars.

The start and end keywords are flow-relative: they use the writing mode to determine which side to align to. start vs end in the inline axis (typically † justify-*)

The left and right keywords are absolute (not flow-relative).

The start and end keywords have meaning in both the inline and block axes: start always orients to the start of the text (top left for left-to-right/top-to-bottom languages like English) while end always orients to the end of the text.

The start and end keywords use the writing mode of the alignment container, to help keep things consistent. But if alignment using the alignment subject’s writing mode is needed, the self-start and self-end keywords can be used.

start vs self-start on the individual items The behavior of the alignment keywords is analogous in vertical writing modes: start and end are relative to the start/end of the text in the relevant axis. The left and right keywords are interpreted as line-left and line-right, relative to the “left” and “right” sides of LTR/RTL text. start vs end in the inline axis (typically † justify-*) left vs right start vs end in the block axis (typically † align-*) start vs self-start on the individual items

Flexbox, on the other hand, has justify-* align things in the main axis and align-* align things in the cross axis. This depends on the value of flex-direction: when flex-direction is row or row-reverse, it matches the other layout modes (inline axis with justify-* , block axis with align-* ); when flex-direction is column or column-reverse, it has the opposite correspondence.

Make it easier to understand the dual-axis nature of «start» and «end» wrt orthogonal flows.

4.2. Baseline Alignment: the baseline keyword and first/last modifiers

is a form of positional alignment that aligns multiple alignment subjects within a shared alignment context (such as cells within a row) by matching up their alignment baselines. If the position of the alignment subjects within a baseline-sharing group is not fully constrained by baseline alignment (i.e., they could be shifted within their respective alignment containers while maintaining baseline-alignment), they are fallback-aligned insofar as possible while preserving their baseline alignment .

The baseline alignment keywords are represented with the <baseline-position> grammar term:

The first and last values give a box a : either “first” or “last”, respectively, defaulting to “first”.

Values have the following meanings:

Computes to first baseline, defined below. Specifies participation in : aligns the alignment baseline of the box’s first baseline set with the corresponding baseline of its baseline-sharing group. See § 9.3 Aligning Boxes by Baseline for more details.

Specifies participation in : aligns the alignment baseline of the box’s last baseline set with the corresponding baseline of its baseline-sharing group.̣ See § 9.3 Aligning Boxes by Baseline for more details.

When specified for align-content, these values trigger baseline content-alignment, shifting the content of the box within its content box, and may also affect the sizing of the box itself. See § 5.4 Baseline Content-Alignment.

When specified for align-self/justify-self, these values trigger baseline self-alignment, shifting the entire box within its container, which may affect the sizing of its container. See § 6.4 Baseline Self-Alignment.

When baseline self-alignment is specified on a box, content distribution is performed first, then the resulting box and its contents are self-aligned However, if the box also has baseline content-alignment in the same axis or if its content-distribution property in the same axis is normal, its used content-distribution property in that axis is start or safe end for a baseline alignment preference of its baseline self-alignment of “first” or “last”, respectively.

Add example images here.

If a box does not belong to a shared alignment context, then the fallback alignment is used. For example, align-content: baseline on a block box falls back to start alignment. The fallback alignment is also used to align the baseline-sharing group within its alignment container.

Note: Because they are equivalent, and baseline is shorter, the CSSOM serializes first baseline as baseline . See CSSOM § 6.7.2 Serializing CSS Values.

Note: For the somewhat-related vertical-align property, due to inconsistent design decisions in CSS2.1, baseline is not equivalent to first baseline as an inline-level box’s baseline alignment preference depends on display. (E.g., inline-block uses its last baseline by default, while inline-table uses its first baseline by default.)

4.3. Distributed Alignment: the stretch, space-between, space-around, and space-evenly keywords

The values are used by justify-content and align-content to disperse a container’s extra space among its alignment subjects.

When space cannot be distributed in this way, these values behave as their fallback alignment. Each distribution value has an associated default fallback alignment . (A future level of this module may allow the fallback alignment to be specified explicitly.)

The alignment subjects are evenly distributed in the alignment container. The first alignment subject is placed flush with the start edge of the alignment container , the last alignment subject is placed flush with the end edge of the alignment container , and the remaining alignment subjects are distributed so that the spacing between any two adjacent alignment subjects is the same.

The default fallback alignment for this value is flex-start. (For layout modes other than flex layout, flex-start is identical to start.)

The alignment subjects are evenly distributed in the alignment container, with a half-size space on either end. The alignment subjects are distributed so that the spacing between any two adjacent alignment subjects is the same, and the spacing before the first and after the last alignment subject is half the size of the other spacing.

The default fallback alignment for this value is safe center .

The alignment subjects are evenly distributed in the alignment container, with a full-size space on either end. The alignment subjects are distributed so that the spacing between any two adjacent alignment subjects , before the first alignment subject , and after the last alignment subject is the same.

The default fallback alignment for this value is safe center .

If the combined size of the alignment subjects is less than the size of the alignment container, any auto -sized alignment subjects have their size increased equally (not proportionally), while still respecting the constraints imposed by max-height/max-width (or equivalent functionality), so that the combined size exactly fills the alignment container .

The default fallback alignment for this value is flex-start. (For layout modes other than flex layout, flex-start is identical to start.)

These values are represented with the <content-distribution> grammar term:

4.4. Overflow Alignment: the safe and unsafe keywords and scroll safety limits

When the alignment subject is larger than the alignment container, it will overflow. Some alignment modes, if honored in this situation, may cause data loss: for example, if the contents of a sidebar are centered, when they overflow they may send part of their boxes past the viewport’s start edge, which can’t be scrolled to.

To control this situation, an mode can be explicitly specified. “Unsafe” alignment honors the specified alignment mode in overflow situations, even if it causes data loss, while “safe” alignment changes the alignment mode in overflow situations in an attempt to avoid data loss. The default behavior is to contain the alignment subject within the scrollable area, though at the time of writing this safety feature is not yet implemented.

If the size of the alignment subject overflows the alignment container, the alignment subject is instead aligned as if the alignment mode were start. Regardless of the relative sizes of the alignment subject and alignment container, the given alignment value is honored. (no value specified) If the overflow alignment isn’t explicitly specified, the default overflow alignment is a blend of “safe” and “unsafe” in that an overflowing alignment subject is allowed to overflow its alignment container, but if this would cause it to also overflow the scrollable overflow area of its nearest ancestor scroll container (thus extending into the “unscrollable” region), then its overflow in that direction is limited by biasing any remaining overflow to the opposite side.

It may not be Web-compatible to implement the “smart” default behavior (though we hope so, and believe it to be likely), so UAs should pass any feedback on this point to the WG. UAs that have not implemented the “smart” default behavior must behave as unsafe.

5. Content Distribution: Aligning a Box’s Contents Within Itself

controls alignment of the box’s content within its content box. It is specified by the align-content and justify-content (and their place-content shorthand).

5.1. The justify-content and align-content Properties

Aligns the contents of the box as a whole (as the alignment subject) within the box itself (as the alignment container): along the inline/row/main axis of the box (for justify-content) or the block/column/cross axis of the box (for align-content). Values other than are defined in § 4 Alignment Keywords, above.

For all layout modes, the alignment subject and alignment container both assume the writing mode of the box the *-content property is set on.

5.1.1. Block Containers (Including Table Cells)

For table cells, the behavior of align-content: normal depends on the computed value of vertical-align: top makes it behave as start and bottom makes it behave as end; otherwise middle makes it behave as center, and all other values make it behave as baseline. [CSS2]

normal otherwise behaves as start.

5.1.2. Multicol Containers

In the case of multi-column containers with a non-auto column-width, justify-content values other than normal or stretch cause the columns to take their specified column-width rather than stretching to fill the container. The column boxes are then aligned as specified by justify-content .

5.1.3. Flex Containers
5.1.4. Grid Containers

5.2. Content-Distribution Shorthand: the place-content property

This shorthand property sets both the align-content and justify-content properties in one declaration. The first value is assigned to align-content . The second value is assigned to justify-content ; if omitted, it is copied from the first value, unless that value is a <baseline-position> in which case it is defaulted to start.

5.3. Overflow and Scroll Positions

When the content-distribution properties are set on a scroll container with an overflowing alignment subject, rather than shifting the layout positions of its content, they instead change its initial scroll position so that the initially-visible content of the scroll container satisfies the expected alignment of the alignment subject and alignment container.

Note: The presence of scrollbars can change the size of the scroll container’s content box, and thus the size of the alignment container and/or alignment subject.

Issue: Replace this image with a proper SVG.

None of this changes how scroll coordinates are assigned: the origin is still where it would be in a start-aligned container, it just might be initially positioned outside the scrollport.

Note: The alignment subject is not necessarily identical to the scrollable overflow area: content overflowing the alignment subject , such as an absolutely-positioned or out-of-flow element, grows the scrollable overflow area but not the alignment subject , thus an end-aligned scroll container might not be initially scrolled all the way to the bottom.

Overflow is not part of the alignment subject, even for a scroll container.

Replace this image too.

5.4. Baseline Content-Alignment

The content of boxes participating in row-like layout contexts (shared alignment contexts) can be baseline-aligned to each other. effectively increases the padding on the box to align the alignment baseline of its contents with that of other baseline-aligned boxes in its baseline-sharing group.

Baseline content-alignment can only apply if the align-content axis is parallel with the box’s block axis (i.e. it does not apply to “column” flex containers); otherwise the fallback alignment is used.

The set of boxes that participate in baseline content-alignment depends on the layout model:

Table Cells: A (non-replaced) table cell participates in first/last baseline content-alignment in its startmost/endmost row if its inline axis is parallel to that of the table itself and its computed align-content is first baseline (last baseline).

Note: Baseline content-alignment of cells sharing a column is not supported; however this may be added in a future level if there is sufficient demand and implementer interest.

Flex Items: A non-replaced flex item participates in first/last baseline content-alignment in its flex line if its computed align-content is first baseline/last baseline and its inline axis is parallel to the main axis. Grid Items: A non-replaced grid item participates in first/last baseline content-alignment in its startmost/endmost row or column (whichever is parallel to its inline axis) and if its computed align-content is first baseline/last baseline.

Additionally, in order to participate in baseline content-alignment it must also have a , to guarantee that the box lines up the relevant edge with other boxes in its baseline-sharing group. That is, the box’s start (end) margin edge must be intended to align— and actually align— to the corresponding edge of its containing block when its baseline alignment preference is “first” (“last”). It otherwise takes its fallback alignment.

When is a self-alignment preference coordinated?

A box’s start margin edge is aligned to the corresponding edge of its containing block and it has a coordinated self-alignment preference for a “first” baseline alignment preference when, in the relevant axis:

There are no auto margins and the relevant self-alignment property either is or aligns identically to stretch or self-start; or

There is only an auto end-edge margin, which absorbs any positive free space and disables the effects of any self-alignment property, and its margin box does not overflow its containing block under circumstances that would cause it to effectively end-align instead (such as having a containing block with an opposite writing mode).

A box’s end margin edge is aligned to the corresponding edge of its containing block and it has a coordinated self-alignment preference for a “last” baseline alignment preference when, in the relevant axis:

There are no auto margins and the relevant self-alignment property either is or aligns identically to self-end and its self-alignment is what would result from an unsafe overflow alignment; or

There is only an auto start-edge margin, which absorbs any positive free space and disables the effects of any self-alignment property and its margin box does not overflow its containing block under circumstances that would cause it to effectively start-align instead.

See § 9.3 Aligning Boxes by Baseline for additional details. Baseline content-alignment can increase the intrinsic size of the box.

6. Self-Alignment: Aligning the Box Within Its Parent

controls alignment of the box within its containing block. It is specified by the align-self and justify-self (and their place-self shorthand).

6.1. Inline-Axis (or Main-Axis) Alignment: the justify-self property

Justifies the box (as the alignment subject) within its containing block (as the alignment container) along the inline/row/main axis of the alignment container : the box’s outer edges are aligned within its alignment container as described by its alignment value. Values have the following meanings:

Behaves as normal if the box has no parent, or when determining the actual position of an absolutely positioned box. It behaves as the computed justify-items value of the parent box (minus any legacy keywords) otherwise (including when determining the static position of an absolutely positioned box).

Represents the “default” alignment for the layout mode. Its behavior depends on the layout mode, as described below.

When the box’s computed width/height (as appropriate to the axis) is auto and neither of its margins (in the appropriate axis) are auto , sets the box’s used size to the length necessary to make its outer size as close to filling the alignment container as possible while still respecting the constraints imposed by min-height/min-width/max-height/max-width.

Unless otherwise specified, this value falls back to flex-start generally, and to self-start or self-end if the box has also specified first baseline or last baseline baseline content-alignment (respectively) in the same axis.

Note: The stretch keyword can cause elements to shrink, to fit their container.

Values other than stretch cause a width/height of auto to be treated as fit-content.

Note: auto margins, because they effectively adjust the size of the margin area, take precedence over justify-self.

6.1.1. Block-Level Boxes

This property does not apply to floats.

6.1.2. Absolutely-Positioned Boxes

This section describes the effect of justify-self on how the margin box of an absolutely-positioned box is positioned with respect to its (absolute-positioning) containing block.

For replaced absolutely-positioned boxes, the normal keyword behaves as start.

For all other absolutely-positioned boxes, normal behaves as stretch. Additionally, if neither inset in this axis is auto, but width (height, for vertical writing modes) is auto, auto margins are treated as zero.

(In non-normal cases, auto margins are prioritized over the self-alignment properties, as usual.)

Note: This somewhat bizarre behavior is required for compatibility with the behavior specified in CSS 2.1.

Values other than stretch or normal cause non-replaced absolutely-positioned boxes to use fit-content sizing for calculating auto sizes in the affected axis.

Note that stretch does cause replaced absolutely-positioned boxes to fill their containing block just as non-replaced ones do.

Note: If only one inset property is auto, the computations in CSS2 section 10.3.7 fully determine its size and position, and justify-self has no effect. (If both are auto , then the box is statically-positioned, see above, and can be aligned within the static-position rectangle.)

6.1.3. Table Cells

This property does not apply to table cells, because their position and size is fully constrained by table layout.

6.1.4. Flex Items

This property does not apply to flex items, because there is more than one item in the main axis. See flex for stretching and justify-content for main-axis alignment. [CSS-FLEXBOX-1]

6.1.5. Grid Items

6.2. Block-Axis (or Cross-Axis) Alignment: the align-self property

Aligns the box (as the alignment subject) within its containing block (as the alignment container) along the block/column/cross axis of the alignment container : the box’s outer edges are aligned within its alignment container as described by its alignment value. Values have the following meanings:

Behaves as normal if the box has no parent, or when determining the actual position of an absolutely positioned box. It behaves as the computed align-items value of the parent box (minus any legacy keywords) otherwise (including when determining the static position of an absolutely positioned box).

Represents the “default” alignment for the layout mode, as defined below.

Note: auto margins, because they effectively adjust the size of the margin area, take precedence over align-self.

6.2.1. Block-Level Boxes

The align-self property does not apply to block-level boxes (including floats), because there is more than one item in the block axis.

6.2.2. Absolutely-Positioned Boxes

This section describes the effect of align-self on how the margin box of an absolutely-positioned box is positioned with respect to its (absolute-positioning) containing block.

For replaced absolutely-positioned boxes, the normal keyword behaves as start.

For all other absolutely-positioned boxes, normal behaves as stretch. Additionally, if neither inset in this axis is auto, but width (height, for vertical writing modes) is auto, auto margins are treated as zero.

(In non-normal cases, auto margins are prioritized over the self-alignment properties, as usual.)

Note: This somewhat bizarre behavior is required for compatibility with the behavior specified in CSS 2.1.

Values other than stretch or normal cause non-replaced absolutely-positioned boxes to use fit-content sizing for calculating auto sizes in the affected axis.

Note that stretch does cause replaced absolutely-positioned boxes to fill their containing block just as non-replaced ones do.

Note: If only one inset property is auto, the computations in CSS2 section 10.6.4 fully determine its size and position, and align-self has no effect. (If both are auto , then the box is statically-positioned, see above, and can be aligned within the static-position rectangle.)

6.2.3. Table Cells

This property does not apply to table cells, because their position and size is fully constrained by table layout.

6.2.4. Flex Items
6.2.5. Grid Items

6.3. Self-Alignment Shorthand: the place-self property

This shorthand property sets both the align-self and justify-self properties in a single declaration. The first value is assigned to align-self . The second value is assigned to justify-self ; if omitted, it is copied from the first value.

6.4. Baseline Self-Alignment

Boxes participating in row-like layout contexts (shared alignment contexts) can be baseline-aligned to each other. effectively increases the margins on the box to align its alignment baseline with that of other baseline-aligned boxes in its baseline-sharing group.

The set of boxes that participate in baseline self-alignment depends on the layout model:

Flex Items: A flex item participates in first/last baseline self-alignment in its flex line if its computed align-self is first baseline/last baseline. See [CSS-FLEXBOX-1] for details. Grid Items: A grid item participates in first/last baseline self-alignment in its startmost/endmost row or column if its align-self or justify-self property (respectively) computes to first baseline/last baseline.

See § 9.3 Aligning Boxes by Baseline for exact details. Baseline self-alignment can increase the intrinsic size contribution of the box.

6.5. Effects on Sizing of Absolutely Positioned Boxes with Static-Position Insets

For absolutely-positioned boxes whose inline-axis offsets are both auto, the available space for calculating the inline size is also affected by alignment.

Note: In [CSS2], the available space is keyed off of the direction property of the static-position containing block. (See CSS2§10.3.7 and CSS2§10.3.8.) Fundamentally these rules set one of the auto insets (by default, the start-edge inset) to the corresponding edge of the static-position rectangle and the other to the corresponding edge of the containing block (i.e. set the inset to zero). Just as the self-alignment properties replace the containing block ’s direction lookup for placement, they also replace this lookup for sizing, as specified here.

Thus, when interpreting the rules in CSS2§10.3.7 and CSS2§10.3.8, wherever the direction property of the static-position containing block is referenced, instead reference the value of the align-self or justify-self property (whichever is defined to apply to the relevant axis), treating left-equivalent alignment as defined for ltr and right-equivalent alignment as defined for rtl. Treat normal as start and any distributed alignment value as its fallback alignment.

In the case of center alignment, the available space for the box is double the distance between the center of the static-position rectangle and the closest edge of the containing block in the relevant axis.

Instead of always sizing within the available space between the inline-start static position and the inline-end containing block edge as specified in [CSS2], an absolutely-positioned element with auto insets will be sized with reference to the static-position rectangle’s edge(s) most appropriate to its specified self-alignment.

Note: The align-self/justify-self properties can also modify additional aspects of sizing: for example, justify-self: stretch will replace “shrink-to-fit” (fit-content) sizing into the available space with stretch-fit sizing (consuming all of the available space ). This is an independent effect from the available space adjustment here.

Note: This only affect how available space is calculated for sizing the absolutely-positioned box; its alignment is as specified in previous sections.

7. Default Alignment

The align-items and justify-items properties (and their place-items shorthand) set the default align-self and justify-self behavior of the element’s child boxes.

7.1. Inline-Axis (or Main-Axis) Alignment: the justify-items property

This property specifies the default justify-self for all of the child boxes (including anonymous boxes) participating in this box’s formatting context. Values have the following meanings:

This keyword causes the value to effectively inherit into descendants.

If the legacy keyword appears on its own (without an accompanying left, right, or center keyword): if the inherited value of justify-items includes the legacy keyword, this value computes to the inherited value ; otherwise it computes to normal .

When justify-self:auto references the value of justify-items, only the alignment keyword, not the legacy keyword, is referenced by it. It exists to implement the legacy alignment behavior of HTML’s <center> element and align attribute.

Other values have no special handling and are merely referenced by justify-self.

7.2. Block-Axis (or Cross-Axis) Alignment: the align-items property

This property specifies the default align-self for all of the child boxes (including anonymous boxes) participating in this box’s formatting context.

Values have no special handling and are merely referenced by align-self.

7.3. Self-Alignment Shorthand: the place-items property

This shorthand property sets both the align-items and justify-items properties in a single declaration. The first value is assigned to align-items . The second value is assigned to justify-items ; if omitted, it is copied from the first value.

8. Gaps Between Boxes

While margin and padding can be used to specify visual spacing around individual boxes, it’s sometimes more convenient to globally specify spacing between adjacent boxes within a given layout context, particularly when the spacing is different between boxes as opposed to between the first/last box and the container’s edge.

The gap property, and its row-gap and column-gap sub-properties, provide this functionality for multi-column, flex, and grid layout.

8.1. Row and Column Gutters: the row-gap and column-gap properties

These properties specify fixed-length between items in the container, adding space between them— in a manner similar to the space-between keyword of the content-distribution properties, but of a fixed size instead of as a fraction of remaining space. The column-gap property specifies spacing between “columns”, separating boxes in the container’s inline axis similar to inline-axis margin; while row-gap indicates spacing between “rows”, separating boxes in the container’s block axis.

Values have the following meanings:

Specifies a gap between “rows” or “columns”, as defined by the layout modes to which it applies; see subsections below for details.

Negative values are invalid. For percentages, see § 8.3 Percentages In gap Properties.

The normal represents a used value of 1em on multi-column containers, and a used value of 0px in all other contexts.

Gutters effect a minimum spacing between items: additional spacing may be added by justify-content/align-content. Such additional space effectively increases the size of these gutters.

The exact handling of these properties varies by layout container:

multi-column containers column-gap specifies the gutter between adjacent column boxes. See [CSS-MULTICOL-1] for details on how this affects the layout of multicol elements. row-gap does not currently apply. flex containers When applied to the main axis (e.g. column-gap in a row flex container), indicates minimum spacing between items (as if an additional fixed-size margin were inserted between adjacent flex items in a single line).

When applied to the cross axis (e.g. row-gap in a row flex container), indicates minimum spacing between adjacent flex lines.

Note: Table boxes do not use the gap properties to specify separation between their cells. Instead, they use the border-spacing property, which has slightly different functionality: it inherits, and it also specifies the additional spacing between the outermost cells and the border of the table (similar to space-evenly rather than space-between).

8.2. Gap Shorthand: the gap property

This property is a shorthand that sets row-gap and column-gap in one declaration. If <‘column-gap’> is omitted, it’s set to the same value as <‘row-gap’> .

Note: The gap property is only one component of the visible “gutter” or “alley” created between boxes. Margins, padding, or the use of distributed alignment may increase the visible separation between boxes beyond what is specified in gap .

8.3. Percentages In gap Properties

In general, gaps introduced by the gap properties are intended to act like an empty item/track/etc with the gap’s size; in other words, an author should be able to reproduce the effects of gap by just inserting additional empty items/tracks/etc into the container.

gap always resolves percentages against the corresponding size of the content box of the element. When this size is definite, the behavior is well-defined and consistent across layout modes. But since different layout modes treat cyclic percentage sizes for items/tracks/etc differently, gap does as well:

As in the min size properties and margins/paddings [CSS-SIZING-3], cyclic percentage sizes resolve against zero for determining intrinsic size contributions, but resolve against the box’s content box when laying out the box’s contents.

Cyclic percentage sizes resolve against zero in all cases.

8.4. Legacy Gap Properties: the grid-row-gap, grid-column-gap, and grid-gap properties

The Grid Layout module was originally written with its own set of gutter properties, before all such properties were unified into the existing row-gap/column-gap naming. For compatibility with legacy content, those legacy property names must be supported as aliases:

must be treated as a shorthand for the row-gap property

must be treated as a shorthand for the column-gap property

must be treated as a shorthand for the gap property

In all three cases, the legacy properties must take the same grammar as the property they are aliasing, and just «forward» the value to their aliased property.

9. Baseline Alignment Details

Boxes in a baseline-sharing group are aligned to each other using their alignment baselines. For example, in horizontal writing modes, specifying align-content: baseline on table cells in the same row will align the baselines of their first formatted lines. This section defines exactly how baseline alignment is performed in consideration of the myriad baselines and writing modes that exist in internationalized modern CSS.

A is a set of baselines (alphabetic, central, etc.) associated with a common baseline table. Typically, a typesetting tradition will use only one of these, but different writing systems use different baselines, and mixing writing systems can result in using more than one within a single line. Refer to CSS Writing Modes 3 § 4.1 Introduction to Baselines for more information on baselines and writing modes.

9.1. Determining the Baselines of a Box

Each box, for a given axis, has potentially a (and ) that nominally corresponds to the baseline set of the first/last line of text within the box. The , which is the baseline used to align the box in its alignment context, is one of the baselines in its baseline set , usually the dominant baseline associated with the shared alignment context . (See the dominant-baseline and alignment-baseline properties in [CSS-INLINE-3].)

The first and last baseline sets of a box are determined differently based on the layout model, as follows:

line box The first/last baseline set of a line box is generated from the dominant baseline and the font settings of its root inline box. block containers The first/last baseline set of a block container is taken from the first/last in-flow line box in the block container or the first/last in-flow block-level child in the block container that contributes a set of first/last baselines, whichever comes first/last. If there is no such line box or child, then the block container has no baseline set .

However, for legacy reasons a block-level or inline-level scroll container always has a last baseline set, whose baselines all correspond to its block-end margin edge.

multi-column containers The first baseline set of a multi-column container is the first baseline set of the column or multi-column spanner with the highest (block-start–most) baseline corresponding to the multi-column container ‘s alignment baseline. If there is no such line box or child, then the multi-column container has no first baseline set .

The last baseline set is analogous, but uses the last baseline set and lowest (block-end–most) baseline.

tables The first/last baseline set of a table box is the first/last baseline set of its first/last row.

When finding the first/last baseline set of an inline-block, any baselines contributed by table boxes must be skipped. (This quirk is a legacy behavior from [CSS2].)

table rows If any cells in the row participate in first baseline/last baseline alignment along the inline axis, the first/last baseline set of the row is generated from their shared alignment baseline and the row’s first available font, after alignment has been performed. Otherwise, the first/last baseline set of the row is synthesized from the lowest and highest content edges of the cells in the row. [CSS2] flex containers See Flex Baselines in [CSS-FLEXBOX-1]. grid containers See Grid Baselines in [CSS-GRID-1].

To for a box from a single baseline, use the baseline table from the font settings and first available font of that box, and align that baseline set to the given single baseline.

If a box that participates in baseline alignment has no baseline set, then its alignment baseline is synthesized according to the rules of the formatting context in which it participates. To from a rectangle (or two parallel lines), synthesize the alphabetic baseline from the line-under line, and the central baseline by averaging the positions of the two edges or lines. See CSS Inline Layout 3 § Appendix A: Synthesizing Alignment Metrics for rules on synthesizing additional baselines.

Note: The edges used to synthesize baselines from a box depend on their formatting context: inline-level boxes synthesize from their margin edges [CSS-INLINE-3], table cells synthesize from their content edges [CSS2], and grid and flex items synthesize from their border edges [CSS-GRID-1] [CSS-FLEXBOX-1].

In general, the writing mode of the box, shape, or other object being aligned is used to determine the line-under and line-over edges for synthesis. However, when that writing mode ’s block flow direction is parallel to the axis of the alignment context, an axis-compatible writing mode must be assumed:

If the box establishing the alignment context has a block flow direction that is orthogonal to the axis of the alignment context , use its writing mode.

Otherwise, assume either horizontal-tb or vertical-lr writing-mode, whichever is orthogonal to the box’s own writing-mode .

For the purposes of finding the baselines of a box, it and all its in-flow descendants with a scrolling mechanism (see the overflow property) must be considered as if scrolled to their initial scroll position. Additionally, if the position of a scroll container’s first/last baseline is outside its border edge, that baseline’s position is clamped to the border edge.

9.2. Baseline Alignment Grouping

A is composed of boxes that participate in baseline alignment together. This is possible only if they both:

  • Share an alignment context along an axis perpendicular to the axis they’re being baseline-aligned in. (For example, grid items with align-self: baseline are baseline-aligning along the grid’s block axis, and therefore participate with other items in their row.)
  • Have compatible baseline alignment preferences (i.e., the baselines that want to align are on the same side of the alignment context).

Note: Boxes participating in baseline content-alignment and boxes participating in baseline self-alignment can be part of the same baseline-sharing group, and can thus align to each other. The difference between the two methods is in where extra space is inserted to perform the alignment (inside or outside the box’s own border).

Boxes share an , along a particular axis, and established by a particular box, when they are:

  • table cells in the same row, along the table’s row (inline) axis, established by the row box
  • grid items in the same row, along the grid’s row (inline) axis, established by the grid container
  • grid items in the same column, along the grid’s column (block) axis, established by the grid container
  • flex items in the same flex line, along the flex container’s main axis, established by the flex container

Note: Conceptually, the inline-level boxes in a line box also share a self-alignment context and participate in a baseline-sharing group; however they only baseline-align in response to the vertical-align property, not any of the properties defined in this module. See [CSS-INLINE-3].

If a box spans multiple shared alignment contexts, then it participates in first/last baseline alignment within its start-most/end-most shared alignment context along that axis. For example, a table cell spanning three rows participates in first-baseline alignment with the table cells in the first row that it spans, or alternatively in last-baseline alignment with the table cells in the last row that it spans.

9.3. Aligning Boxes by Baseline

Given a set of alignment subjects and their baselines that all belong to a single baseline-sharing group, the alignment subjects are baseline-aligned as follows:

Generate the baseline-sharing group’s baseline table from the first available font of the group’s alignment context and overlay also the mirror of this baseline table by aligning their central baselines. These are the baseline “grids” to which the alignment subjects will align.

Align each alignment subject by its specified alignment baseline to the group’s baseline table or its mirror, whichever matches the alignment subject’s line orientation. Unless otherwise specified (e.g. via the alignment-baseline property), the alignment baseline is the dominant baseline of the alignment context.

Position the aligned baseline-sharing group within the alignment container according to its fallback alignment. The fallback alignment of a baseline-sharing group is the fallback alignment of its items as resolved to physical directions.

For first/last baseline content-alignment, then add the minimum necessary extra space between the alignment container’s start/end content edge and the alignment subject’s edge to align the start/end margin edges of all the alignment containers in the alignment context while maintaining baseline alignment within the baseline-sharing group.

Appendix A: Static Position Terminology

When both inset properties in a given axis are auto on an absolutely positioned box, CSS2 uses its static position to resolve its size and position. See Cascading Style Sheets Level 2 Revision 1 (CSS 2.1) Specification § #abs-non-replaced-width. The box alignment properties modify these calculations, just as they do the sizing and positioning calculations in other layout modes. These modifications refer to a , whose edges represent the static position of the box from each side of its containing block.

The static-position rectangle and the static positions to which it corresponds are defined by the layout model of its “hypothetical box”:

Block Layout The static positions of a block-level box are defined in [CSS2] Chapter 10. The static-position rectangle is a zero-thickness rectangle spanning between the inline-axis sides of the box’s static-position containing block (see CSS2§10.3.7); and positioned at its block-start static position (see CSS2§10.6.4). Inline Layout The static positions of an inline-level box are defined in [CSS2] Chapter 10. The static-position rectangle is a zero-thickness rectangle spanning between the line-over/line-under sides of the line box that would have contained its “hypothetical box” (see CSS2§10.3.7); and positioned at its inline-start static position . Flex Layout The static-position rectangle of the child of a flex container corresponds to the content edges of the flex container . See CSS Flexbox 1 § 4.1 Absolutely-Positioned Flex Children. Grid Layout By default, the static-position rectangle of the child of a grid container corresponds to the content edges of the grid container . However, if that grid container also establishes the box’s actual containing block, then the grid area specified by the grid-placement properties establishes its static-position rectangle instead. See the static position of a grid container child in [CSS-GRID-1].

10. Changes

  • Define the baselines of a multi-column container. (Issue 7856, Issue 7639)
  • Use the writing mode of the static-position containing block when determining the static position. (Issue 7599, Issue 7612; Changes)
  • Remove definition of special behavior for including padding in scrollable overflow for non-normal alignments since this is now defined unconditionally in [CSS-OVERFLOW-3]. (Issue 129)
  • Minor clarifications:

    Define fallback alignment of a baseline-sharing group as the shared fallback alignment of its individual items.

    • Allowed reordering of [ first | last ] and baseline components of <baseline-position>. (Issue 5235)
    • Make fallback alignment of space-around and space-evenly include safe. (Issue 5088)
    • Various corrections to normal alignment and percentage gaps to match respective layout specs.
    • Corrections to special legacy handling of the last baseline set of scroll containers. (Issue 3611)
    • Clarify that a box with no baseline set synthesizes its alignment baseline as needed. (Issue 3611)
    • Clarified that only non-replaced boxes are affected by content distribution. (Issue 4545)
    • Clarified when a box has a coordinated self-alignment preference that enables baseline content-alignment, particularly with respect to auto margins. (Issue 5923)
    • Better define how baseline self-alignment and content-alignment interact. (Issue 6389)
    • Clarify how baseline-sharing groups determine their fallback alignment. (Issue 7645)
    • Improved the center self-alignment of absolutely-positioned elements. Issue 4659
    • Fixed inconsistent fallback of baseline alignment in mixed writing modes. Issue 3454
    • Minor editorial fixes and clarifications.

    Changes since the 30 August 2018 Working Draft consist of some minor clarifications.

    • Better definition of how the alignment properties interact with absolutely-positioned boxes with auto offsets. See § 6.1.2 Absolutely-Positioned Boxes, § 6.2.2 Absolutely-Positioned Boxes, § 6.5 Effects on Sizing of Absolutely Positioned Boxes with Static-Position Insets, and Appendix A: Static Position Terminology.
    • More detailed definition of how the content-distribution properties work on scroll containers. See § 5.3 Overflow and Scroll Positions.
    • Noted CSS2’s special rules about the baselines of inline-block scroll containers.
    • Defined that percentage gaps compute against zero when calculating intrinsic sizes, and resolve during layout. (Issue 509)
    • Miscellaneous minor clarifications.
    • Added more illustrations! Many thanks to Melanie Richards for working through a difficult diagramming problem.
    • Fixed errors in property definition table of gap shorthand.
    • Defining percentage gaps to resolve to zero when the containing block’s size depends on the gaps. (Issue 509, Issue 2297)
    • Fixed errors in handling left and right in the grammars.
    • Fixed discrepancy in space-align fallback alignment. (Issue 2316)
    • Tightened up handling of baseline alignment in cases where the box edges don’t align. (Issue 1556, Issue 1611)
    • Miscellaneous clarifications.

    Changes since the 20 July 2017 Working Draft include:

    • Added the row-gap and column-gap properties and gap shorthand, applying them to both grid layout and flex layout to replace the grid-specific grid-row-gap/grid-column-gap/grid-gap properties.

    Changes since the 15 May 2017 Working Draft include:

    • Dropped the left and right keywords from align-self and align-content. They may be re-introduced in a later level if there is demand. (Issue 1403)
    • Assigned end fallback alignment to stretch cases when combined with last baseline content-alignment.
    • Disabled baseline alignment across cells in a table column, due to implementation complexity.
    • A large number of minor corrections and clarifications reported by David Baron.

    Changes since the 7 April 2017 Working Draft include:

    • Adding a section to clarify handling of partial implementations. (Issue 1167)
    • Dropped <baseline-position> value of justify-content since it can’t do anything anyway. (Issue 1184)
    • Deferred ability to specify fallback alignments to Level 4. (Issue 1002)
    • Fixed position of <overflow-position> wrt <content-position>. (Issue 1001)
    • Fixed rules for handling baseline-aligned boxes being aligned along their block axis. (Issue 1038)
    • Renamed justify-items: auto to justify-items: legacy . (Issue 1318)
    • Clamped baselines of scrollable boxes to the border edge, rather than margin edge. (Issue 766)

    11. Privacy Considerations

    As a simple layout spec, this introduces no new privacy considerations.

    12. Security Considerations

    As a simple layout spec, this introduces no new security considerations.

    Acknowledgments

    Special thanks goes to David Baron, Javier Fernandez, Markus Mielke, Alex Mogilevsky, and the participants in the CSSWG’s March 2008 F2F alignment discussions for their contributions to the alignment model described herein, and to Melanie Richards for her illustrations of the various alignment keywords.

    Conformance

    Document conventions

    Conformance requirements are expressed with a combination of descriptive assertions and RFC 2119 terminology. The key words “MUST”, “MUST NOT”, “REQUIRED”, “SHALL”, “SHALL NOT”, “SHOULD”, “SHOULD NOT”, “RECOMMENDED”, “MAY”, and “OPTIONAL” in the normative parts of this document are to be interpreted as described in RFC 2119. However, for readability, these words do not appear in all uppercase letters in this specification.

    All of the text of this specification is normative except sections explicitly marked as non-normative, examples, and notes. [RFC2119]

    Examples in this specification are introduced with the words “for example” or are set apart from the normative text with class=»example» , like this:

    This is an example of an informative example.

    Informative notes begin with the word “Note” and are set apart from the normative text with class=»note» , like this:

    Note, this is an informative note.

    Advisements are normative sections styled to evoke special attention and are set apart from other normative text with <strong > , like this: UAs MUST provide an accessible alternative.

    Conformance classes

    Conformance to this specification is defined for three conformance classes:

    style sheet A CSS style sheet. renderer A UA that interprets the semantics of a style sheet and renders documents that use them. authoring tool A UA that writes a style sheet.

    A style sheet is conformant to this specification if all of its statements that use syntax defined in this module are valid according to the generic CSS grammar and the individual grammars of each feature defined in this module.

    A renderer is conformant to this specification if, in addition to interpreting the style sheet as defined by the appropriate specifications, it supports all the features defined by this specification by parsing them correctly and rendering the document accordingly. However, the inability of a UA to correctly render a document due to limitations of the device does not make the UA non-conformant. (For example, a UA is not required to render color on a monochrome monitor.)

    An authoring tool is conformant to this specification if it writes style sheets that are syntactically correct according to the generic CSS grammar and the individual grammars of each feature in this module, and meet all other conformance requirements of style sheets as described in this module.

    Partial implementations

    So that authors can exploit the forward-compatible parsing rules to assign fallback values, CSS renderers must treat as invalid (and ignore as appropriate) any at-rules, properties, property values, keywords, and other syntactic constructs for which they have no usable level of support. In particular, user agents must not selectively ignore unsupported component values and honor supported values in a single multi-value property declaration: if any value is considered invalid (as unsupported values must be), CSS requires that the entire declaration be ignored.

    Implementations of Unstable and Proprietary Features

    To avoid clashes with future stable CSS features, the CSSWG recommends following best practices for the implementation of unstable features and proprietary extensions to CSS.

    Non-experimental implementations

    Once a specification reaches the Candidate Recommendation stage, non-experimental implementations are possible, and implementors should release an unprefixed implementation of any CR-level feature they can demonstrate to be correctly implemented according to spec.

    To establish and maintain the interoperability of CSS across implementations, the CSS Working Group requests that non-experimental CSS renderers submit an implementation report (and, if necessary, the testcases used for that implementation report) to the W3C before releasing an unprefixed implementation of any CSS features. Testcases submitted to W3C are subject to review and correction by the CSS Working Group.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *