CSS justify-self Property
The CSS justify-self property controls how an individual element is aligned along the inline axis within its available alignment area.
It is commonly used in CSS Grid to align a specific item at the start, center, or end of its grid area, or to stretch it across the available inline space. Unlike justify-items, which establishes the default alignment for items in a container, justify-self allows each item to have its own alignment.
justify-self Syntax
The justify-self property accepts alignment keywords that determine the position of an individual element within its alignment area.
Basic Syntax
selector {
justify-self: center;
}
Common Alignment Values
.item {
justify-self: auto;
}
.item {
justify-self: normal;
}
.item {
justify-self: start;
}
.item {
justify-self: end;
}
.item {
justify-self: center;
}
.item {
justify-self: stretch;
}
Self-Relative Alignment
.item {
justify-self: self-start;
}
.item {
justify-self: self-end;
}
Flex-Relative Alignment
.item {
justify-self: flex-start;
}
.item {
justify-self: flex-end;
}
Physical Alignment
.item {
justify-self: left;
}
.item {
justify-self: right;
}
Anchor Alignment
.item {
justify-self: anchor-center;
}
Overflow Alignment
.item {
justify-self: safe center;
}
.item {
justify-self: unsafe center;
}
CSS-Wide Keywords
.item {
justify-self: initial;
}
.item {
justify-self: inherit;
}
.item {
justify-self: unset;
}
.item {
justify-self: revert;
}
.item {
justify-self: revert-layer;
}
The initial value is auto. For a grid item, this normally uses the justify-items value established by its grid container.
Property Values
The following values determine how an individual item is aligned within its available inline-axis space.
| Value | Description |
|---|---|
auto |
Uses the applicable parent container's justify-items alignment. |
normal |
Uses the default alignment behavior for the item's layout context. |
start |
Aligns the item toward the start of its alignment area. |
end |
Aligns the item toward the end of its alignment area. |
center |
Centers the item within its alignment area. |
stretch |
Stretches an eligible auto-sized item along the alignment axis. |
self-start |
Aligns the item toward its own start side, accounting for its writing mode. |
self-end |
Aligns the item toward its own end side, accounting for its writing mode. |
flex-start |
Uses flex-start alignment semantics, behaving like start outside flex layout. |
flex-end |
Uses flex-end alignment semantics, behaving like end outside flex layout. |
left |
Aligns toward the physical left side where applicable. |
right |
Aligns toward the physical right side where applicable. |
anchor-center |
Centers an eligible anchor-positioned element relative to its anchor along the relevant axis. |
safe |
Modifies positional alignment to avoid certain unsafe overflow situations. |
unsafe |
Allows the requested positional alignment even when overflow occurs. |
initial |
Resets the property to auto. |
inherit |
Uses the computed value from the parent element. |
unset |
Resets the property to auto because it is not inherited. |
revert |
Reverts the property according to cascade origin rules. |
revert-layer |
Reverts the property according to cascade layer rules. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | auto |
| Inherited | No |
| Applies to | All elements |
| Computed value | As specified |
| Animation type | Discrete |
| Percentages | Not applicable |
| Primary alignment axis | Inline axis |
| Common layout | CSS Grid |
The property does not accept lengths or percentages directly. It uses alignment keywords to determine the item's position within its alignment area.
How It Works
The justify-self property positions an individual item within its alignment area after the browser determines the item's size and the space available to it.
Grid Container
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
.container .item {
justify-self: center;
}
Each matching item is centered along the inline axis within its own grid area.
Different Alignments
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
.item-one {
justify-self: start;
}
.item-two {
justify-self: center;
}
.item-three {
justify-self: end;
}
The three items can use different alignments without changing the grid track structure.
Important Consideration
The alignment is relative to the item's own alignment area, not necessarily the entire container. A grid item spanning multiple tracks uses the area assigned to that item.
auto Value
The auto value is the initial value of justify-self. It normally resolves to the default item alignment established by the parent container.
Using the Container Alignment
.container {
display: grid;
justify-items: center;
}
.container .item {
justify-self: auto;
}
The item uses the container's center alignment.
Overriding the Container Alignment
.container {
display: grid;
justify-items: center;
}
.container .special {
justify-self: end;
}
The special item aligns toward the inline-end edge rather than using the container's center alignment.
Important Consideration
auto does not mean that the browser chooses an arbitrary position. It follows the applicable default alignment rules, including the parent container's justify-items value.
normal Value
The normal value uses the default self-alignment behavior defined for the layout context.
Grid Item
.container {
display: grid;
}
.container .item {
justify-self: normal;
}
For most ordinary grid items, normal behaves like stretch.
Images and Intrinsic Aspect Ratios
.container {
display: grid;
}
.container img {
justify-self: normal;
max-width: 100%;
height: auto;
}
Grid items with intrinsic aspect ratios may use start alignment rather than stretching, helping preserve their natural proportions.
Important Consideration
The normal and auto values are not identical. auto normally uses the parent's justify-items value, while normal requests the default behavior for the item's layout context.
start and end Values
The start and end values align an item toward the logical start or end of its alignment area.
Start Alignment
.item {
justify-self: start;
}
End Alignment
.item {
justify-self: end;
}
Grid Example
.container {
display: grid;
grid-template-columns: repeat(2, 1fr);
}
.container .first {
justify-self: start;
}
.container .second {
justify-self: end;
}
In a typical left-to-right horizontal writing mode, the first item aligns left within its grid area and the second aligns right.
Important Consideration
Logical start and end alignment adapt to writing modes and direction. They should not be treated as permanent synonyms for left and right.
center Value
The center value positions an item in the middle of its alignment area along the inline axis.
Center a Grid Item
.item {
justify-self: center;
}
Center a Featured Card
.card-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 20px;
}
.card-grid .featured {
width: 180px;
justify-self: center;
}
Center an Image
.image-grid {
display: grid;
}
.image-grid img {
max-width: 100%;
height: auto;
justify-self: center;
}
Important Consideration
Center alignment is most visible when the item is smaller than its alignment area. If the item already fills that area, there may be no free space in which to center it.
stretch Value
The stretch value expands an eligible auto-sized item to fill the available inline-axis space within its alignment area.
Stretch a Grid Item
.item {
justify-self: stretch;
}
Auto Width
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
.container .item {
width: auto;
justify-self: stretch;
}
The item can expand to occupy the available inline space.
Explicit Width
.item {
width: 120px;
justify-self: stretch;
}
The explicit width generally prevents the item from stretching to fill its entire alignment area.
Maximum Width
.item {
width: auto;
max-width: 250px;
justify-self: stretch;
}
The item can stretch while respecting its maximum width.
Important Consideration
Stretch alignment is subject to the item's sizing rules, including intrinsic dimensions and minimum and maximum size constraints.
self-start and self-end Values
The self-start and self-end values align an item according to its own writing mode and direction.
Self-Start Alignment
.item {
justify-self: self-start;
}
Self-End Alignment
.item {
justify-self: self-end;
}
Mixed Writing Directions
.container {
display: grid;
direction: ltr;
}
.container .rtl-item {
direction: rtl;
justify-self: self-start;
}
The right-to-left item uses its own inline-start side for alignment.
Important Consideration
Use start and end when alignment should follow the container's alignment axis. Use self-start and self-end when the item's own writing direction should determine the aligned side.
flex-start and flex-end Values
The flex-start and flex-end values are part of the CSS alignment syntax, but justify-self does not control the main-axis alignment of flex items.
flex-start
.item {
justify-self: flex-start;
}
flex-end
.item {
justify-self: flex-end;
}
Grid Behavior
.container {
display: grid;
}
.container .item {
justify-self: flex-end;
}
Outside flex layout, flex-start and flex-end behave like start and end.
Important Consideration
Although these values are valid alignment keywords, justify-self has no effect on flex items. Use justify-content or auto margins for main-axis positioning in Flexbox.
left and right Values
The left and right values provide physical alignment along the horizontal axis.
Left Alignment
.item {
justify-self: left;
}
Right Alignment
.item {
justify-self: right;
}
Grid Example
.container {
display: grid;
direction: rtl;
}
.container .item {
justify-self: left;
}
The item aligns toward the physical left side even though the container uses right-to-left direction.
Important Consideration
When the alignment axis is not parallel to the physical left-to-right axis, left and right behave like start. Logical values are generally preferable for adaptable layouts.
anchor-center Value
The anchor-center value is associated with CSS Anchor Positioning. It can align an eligible positioned element relative to its anchor along the relevant axis.
Anchor Positioning Example
.anchor {
anchor-name: --reference;
}
.tooltip {
position: absolute;
position-anchor: --reference;
position-area: bottom;
justify-self: anchor-center;
}
In supporting browsers, the positioned element can be centered relative to the anchor along the applicable alignment axis.
Fallback Alignment
.tooltip {
justify-self: center;
justify-self: anchor-center;
}
The earlier declaration provides a fallback for browsers that do not recognize anchor-center, although the complete anchor-positioning layout also requires compatible positioning support.
Important Consideration
anchor-center is intended for anchor-positioned elements, not ordinary grid item centering. Check current browser compatibility before relying on it.
safe and unsafe Keywords
The safe and unsafe keywords modify positional alignment when an item may overflow its alignment area.
Safe Center Alignment
.item {
justify-self: safe center;
}
If centering would cause unsafe overflow, the browser can fall back toward start alignment.
Unsafe Center Alignment
.item {
justify-self: unsafe center;
}
This requests center alignment even when the item extends beyond its alignment area.
Safe End Alignment
.item {
justify-self: safe end;
}
Important Consideration
Overflow alignment changes the selected alignment behavior. It does not reduce an oversized item's width or replace the overflow property.
justify-self in Grid
CSS Grid is the most common layout context for justify-self. Each grid item can have its own inline-axis alignment within its assigned grid area.
Three-Column Grid
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 20px;
}
.container .first {
justify-self: start;
}
.container .second {
justify-self: center;
}
.container .third {
justify-self: end;
}
Override Container Alignment
.container {
display: grid;
justify-items: stretch;
}
.container .featured {
justify-self: center;
}
The featured item uses center alignment while other eligible items continue to stretch.
Spanning Multiple Columns
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
.container .featured {
grid-column: 1 / 3;
justify-self: center;
}
The featured item is centered within the grid area spanning the first two columns.
Important Consideration
The property does not change grid placement or track sizing. It aligns the item within the area determined by the grid layout.
justify-self in Flexbox
The justify-self property does not affect the alignment of flex items. Flexbox distributes main-axis space using container alignment and auto margins.
Ineffective Flexbox Declaration
.container {
display: flex;
}
.container .item {
justify-self: end;
}
This does not move the flex item toward the end of the main axis.
Use Auto Margins
.container {
display: flex;
}
.container .item {
margin-inline-start: auto;
}
In a typical horizontal row flex layout, the auto margin pushes the item toward the inline-end side.
Use justify-content
.container {
display: flex;
justify-content: flex-end;
}
This aligns the flex items as a group toward the main-end side.
Use align-self
.container {
display: flex;
}
.container .item {
align-self: center;
}
The align-self property controls the individual flex item's cross-axis alignment.
Important Consideration
Flexbox does not use justify-self for individual main-axis alignment. Use auto margins when one flex item needs to move independently along that axis.
Block Layout
The CSS Box Alignment specification defines inline-axis self-alignment for applicable boxes in block layout. Support for these behaviors has expanded beyond Grid, but browser implementation details can vary.
Block Element Alignment
.item {
width: 200px;
justify-self: center;
}
In supporting block layout implementations, an eligible block-level element can be centered within its containing block.
Traditional Margin Alternative
.item {
width: 200px;
margin-inline: auto;
}
Auto inline margins provide a widely established way to center a block-level element with a definite width.
Grid Alternative
.container {
display: grid;
}
.container .item {
justify-self: center;
}
Important Consideration
Do not assume that every browser applies justify-self identically in block and grid layouts. Use an established fallback when broad compatibility is required.
Positioned Elements
The justify-self property can participate in the alignment of absolutely positioned elements, depending on their positioning constraints and available space.
Absolute Positioning
.container {
position: relative;
width: 400px;
height: 200px;
}
.container .item {
position: absolute;
inset-inline: 0;
width: 100px;
justify-self: center;
}
When the positioning constraints establish an available inline-axis alignment area, justify-self can control the element's alignment within it.
End Alignment
.container .item {
position: absolute;
inset-inline: 0;
width: 100px;
justify-self: end;
}
Anchor Positioning
.anchor {
anchor-name: --menu-anchor;
}
.menu {
position: absolute;
position-anchor: --menu-anchor;
position-area: bottom;
justify-self: anchor-center;
}
Important Consideration
For absolutely positioned elements, the effect depends on the inset values, sizing rules, and the resulting positioning area. Alignment does not replace the positioning properties that establish that area.
Writing Modes and Direction
The justify-self property aligns elements along the inline axis. The physical direction of this axis depends on writing-mode and direction.
Left-to-Right Layout
.container {
display: grid;
direction: ltr;
}
.container .item {
justify-self: start;
}
In a horizontal writing mode, the item aligns toward the left side of its grid area.
Right-to-Left Layout
.container {
display: grid;
direction: rtl;
}
.container .item {
justify-self: start;
}
In a horizontal writing mode, the item aligns toward the right side.
Vertical Writing Mode
.container {
display: grid;
writing-mode: vertical-rl;
}
.container .item {
justify-self: start;
}
The inline axis runs vertically, so the item is aligned vertically within its grid area.
Writing Mode Comparison
| Writing Mode | Direction | Inline-Start Side |
|---|---|---|
horizontal-tb |
ltr |
Left |
horizontal-tb |
rtl |
Right |
vertical-rl |
ltr |
Top |
vertical-rl |
rtl |
Bottom |
vertical-lr |
ltr |
Top |
vertical-lr |
rtl |
Bottom |
Important Consideration
Logical alignment values are useful for multilingual interfaces because they adapt to the writing mode and text direction rather than relying on fixed physical sides.
Item Sizing and Alignment
The visible result of justify-self depends on the item's size relative to its available alignment area.
Fixed-Width Item
.container {
display: grid;
grid-template-columns: 1fr;
}
.container .item {
width: 150px;
justify-self: center;
}
The item is centered within its grid area when sufficient space is available.
Auto Width
.container .item {
width: auto;
justify-self: stretch;
}
An eligible auto-sized item can stretch across its available inline space.
Maximum Width
.container .item {
width: 100%;
max-width: 300px;
justify-self: center;
}
The item can fill smaller grid areas while remaining centered when the area exceeds its maximum width.
Minimum Width
.container .item {
min-width: 180px;
justify-self: center;
}
The minimum width can cause overflow if the grid area becomes narrower than the item's minimum size.
Important Consideration
Alignment does not override explicit sizing constraints. Use sizing properties to control the item's dimensions and alignment properties to control its position.
Auto Margins
Auto margins can absorb available space in an item's alignment area and take precedence over the alignment specified by justify-self.
Auto Inline Margins
.container {
display: grid;
}
.container .item {
width: 150px;
margin-inline: auto;
justify-self: end;
}
The auto margins can absorb available inline space, causing the item to be centered rather than visibly end-aligned.
Start Auto Margin
.container .item {
width: 150px;
margin-inline-start: auto;
}
In a typical horizontal left-to-right layout, the auto start margin pushes the item toward the inline-end side.
Use Alignment Without Auto Margins
.container .item {
width: 150px;
margin-inline: 0;
justify-self: end;
}
Important Consideration
When debugging unexpected alignment, check whether auto margins are consuming the available space before the self-alignment value takes effect.
justify-self vs. justify-items
Both properties control inline-axis item alignment, but they are applied to different elements.
| Property | Purpose |
|---|---|
justify-items |
Sets the default alignment for applicable child items. |
justify-self |
Sets the alignment of one individual item. |
Container Default
.container {
display: grid;
justify-items: center;
}
Individual Override
.container {
display: grid;
justify-items: center;
}
.container .special {
justify-self: end;
}
The special item uses end alignment, while other items continue using the container's center alignment.
Important Consideration
Use justify-items when most items need the same alignment and justify-self when an individual item needs different positioning.
justify-self vs. justify-content
The justify-self and justify-content properties control different aspects of inline-axis alignment in Grid.
| Property | Purpose |
|---|---|
justify-self |
Aligns one item within its grid area. |
justify-content |
Aligns or distributes the grid tracks within the grid container. |
Align a Grid Item
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
.container .item {
justify-self: center;
}
Align Grid Tracks
.container {
display: grid;
grid-template-columns: repeat(3, 100px);
justify-content: center;
}
Combine Both
.container {
display: grid;
grid-template-columns: repeat(3, 120px);
justify-content: center;
}
.container .featured {
justify-self: end;
}
The grid tracks are centered as a group, while the featured item aligns toward the end of its own grid area.
justify-self and align-self
In Grid, justify-self controls inline-axis alignment and align-self controls block-axis alignment.
| Property | Grid Alignment Axis |
|---|---|
justify-self |
Inline axis |
align-self |
Block axis |
Center Along Both Axes
.container {
display: grid;
grid-template-columns: 1fr;
grid-auto-rows: 200px;
}
.container .item {
justify-self: center;
align-self: center;
}
Different Alignment Values
.container .item {
justify-self: end;
align-self: start;
}
In a typical left-to-right horizontal writing mode, the item aligns toward the right and top edges of its grid area.
Important Consideration
The inline and block axes depend on the writing mode. They are not always equivalent to horizontal and vertical alignment.
place-self Shorthand
The place-self shorthand combines align-self and justify-self in one declaration.
Two Values
.item {
place-self: start end;
}
This is equivalent to:
.item {
align-self: start;
justify-self: end;
}
One Value
.item {
place-self: center;
}
This is equivalent to:
.item {
align-self: center;
justify-self: center;
}
Override justify-self
.item {
place-self: center;
justify-self: end;
}
The later declaration changes the inline-axis alignment to end while retaining align-self: center.
Important Consideration
When two values are provided, the first sets align-self and the second sets justify-self. When only one value is supplied, it generally applies to both properties, subject to shorthand grammar rules.
Inheritance
The justify-self property is not inherited. Its initial value is auto.
Parent Alignment
.parent {
display: grid;
justify-items: center;
}
Grid items with justify-self: auto use the applicable alignment established by the parent grid container.
Explicit Inheritance
.child {
justify-self: inherit;
}
This explicitly inherits the parent's computed justify-self value.
Reset to Initial Value
.item {
justify-self: initial;
}
This resets the property to auto.
Using unset
.item {
justify-self: unset;
}
Because the property is not inherited, unset behaves like initial.
Important Consideration
Using auto to follow the parent's justify-items value is not the same as CSS inheritance. The two properties participate in the alignment system through their defined layout behavior.
justify-self Example
This example demonstrates four common justify-self values. Each grid has three columns, and the highlighted item in the middle column changes its alignment while the surrounding items remain in place.
HTML Example
<div class="justify-self-demo">
<div class="demo-row">
<h3>start</h3>
<div class="demo-grid">
<div class="demo-item">1</div>
<div class="demo-item special start">2</div>
<div class="demo-item">3</div>
</div>
</div>
<div class="demo-row">
<h3>center</h3>
<div class="demo-grid">
<div class="demo-item">1</div>
<div class="demo-item special center">2</div>
<div class="demo-item">3</div>
</div>
</div>
<div class="demo-row">
<h3>end</h3>
<div class="demo-grid">
<div class="demo-item">1</div>
<div class="demo-item special end">2</div>
<div class="demo-item">3</div>
</div>
</div>
<div class="demo-row">
<h3>stretch</h3>
<div class="demo-grid">
<div class="demo-item">1</div>
<div class="demo-item special stretch">2</div>
<div class="demo-item">3</div>
</div>
</div>
</div>
CSS Example
.justify-self-demo .demo-row {
margin-bottom: 20px;
}
.justify-self-demo h3 {
margin: 0 0 8px;
font-size: 1rem;
}
.justify-self-demo .demo-grid {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 10px;
padding: 10px;
border: 2px solid #2563eb;
background-color: #eff6ff;
}
.justify-self-demo .demo-item {
display: flex;
align-items: center;
justify-content: center;
width: 45px;
height: 45px;
background-color: #64748b;
color: #fff;
font-weight: bold;
}
.justify-self-demo .special {
background-color: #2563eb;
}
.justify-self-demo .start {
justify-self: start;
}
.justify-self-demo .center {
justify-self: center;
}
.justify-self-demo .end {
justify-self: end;
}
.justify-self-demo .stretch {
width: auto;
justify-self: stretch;
}
start
center
end
stretch
The highlighted middle item demonstrates how justify-self changes one item's position without changing the alignment of the other grid items.
Try a Container Default
.justify-self-demo .demo-grid {
justify-items: center;
}
The surrounding items now use center alignment, while the highlighted items continue using their individual justify-self values.
Try Right-to-Left Direction
.justify-self-demo .demo-grid {
direction: rtl;
}
The logical start and end positions change to follow the right-to-left inline direction.
Common Mistakes
Using justify-self on Flex Items
.container {
display: flex;
}
.container .item {
justify-self: center;
}
This does not center the flex item. Use appropriate Flexbox alignment properties or auto margins.
Applying justify-self to the Container Instead of the Item
.container {
display: grid;
justify-self: center;
}
This does not center the container's child grid items. Use justify-items on the grid container or justify-self on individual grid items.
Using Distributed Alignment Values
.item {
justify-self: space-between;
}
This is invalid. Values such as space-between and space-evenly belong to justify-content.
Expecting Stretch to Override Fixed Widths
.item {
width: 150px;
justify-self: stretch;
}
The fixed width generally prevents the item from stretching across the available alignment area.
Confusing auto with normal
auto normally follows the parent's justify-items value. normal requests the layout's default self-alignment behavior.
Ignoring Auto Margins
Auto margins can consume available space and prevent the expected self-alignment from becoming visible.
Assuming start Always Means Left
Logical alignment follows the writing mode and direction, so the inline-start side is not always the physical left side.
Confusing Item Alignment with Grid Track Alignment
justify-self aligns one item within its grid area. It does not move the grid tracks within the container.
Important Considerations
Keep the following points in mind when using justify-self:
- The property controls the inline-axis alignment of an individual item.
- It is most commonly used with CSS Grid.
- The initial value is
auto. - The property is not inherited.
autonormally uses the applicable parent'sjustify-itemsvalue.normaluses the default alignment behavior for the layout context.startandendfollow the logical alignment axis.self-startandself-endaccount for the item's own writing direction.centercenters the item within its alignment area.stretchexpands eligible auto-sized items.- Explicit widths and sizing constraints can limit stretching.
leftandrightprovide physical alignment where applicable.flex-startandflex-endbehave like start and end outside flex layout.justify-selfdoes not affect flex items.safeandunsafemodify overflow alignment behavior.anchor-centersupports specialized anchor-positioning alignment.- Auto margins can take precedence over self-alignment.
justify-itemsestablishes the default alignment for applicable child items.justify-contentaligns grid tracks as a group.align-selfcontrols block-axis alignment in Grid.place-selfcombinesalign-selfandjustify-self.- Writing mode and direction affect the physical orientation of the inline axis.
- The property animates discretely rather than interpolating between alignment values.
- Support outside Grid may depend on the browser and layout context.
Browser Support
The CSS justify-self property is widely supported in modern browsers, including Chrome, Edge, Firefox, Safari, and Opera, particularly for CSS Grid layouts.
Support for newer values such as anchor-center and for alignment in certain block or positioned layouts may vary. Check compatibility before relying on these behaviors in production.
For current compatibility information, visit Can I Use? and search for justify-self.
Specifications
The justify-self property is defined in the CSS Box Alignment Module Level 3 specification.
CSS Box Alignment Module Level 3: justify-self
The specification defines individual self-alignment, logical alignment values, and overflow alignment across supported layout modes.
For additional documentation and examples, consult MDN Web Docs.
