files/en-us/web/css/reference/properties/justify-items/index.md
The CSS justify-items property defines the default {{CSSxRef("justify-self")}} for all items of the box, giving them all a default way of justifying each box along the appropriate axis.
{{InteractiveExample("CSS Demo: justify-items")}}
justify-items: stretch;
justify-items: center;
justify-items: start;
justify-items: end;
<section class="default-example" id="default-example">
<div class="example-container">
<div class="transition-all" id="example-element">
<div>One</div>
<div>Two</div>
<div>Three</div>
</div>
</div>
</section>
#example-element {
border: 1px solid #c5c5c5;
display: grid;
grid-template-columns: 1fr 1fr;
grid-auto-rows: 40px;
grid-gap: 10px;
width: 220px;
}
#example-element > div {
background-color: rgb(0 0 255 / 0.2);
border: 3px solid blue;
}
The effect of this property is dependent of the layout mode we are in:
/* Basic keywords */
justify-items: normal;
justify-items: stretch;
/* Positional alignment */
justify-items: center; /* Pack items around the center */
justify-items: start; /* Pack items from the start */
justify-items: end; /* Pack items from the end */
justify-items: flex-start; /* Equivalent to 'start'. Note that justify-items is ignored in flexbox layouts. */
justify-items: flex-end; /* Equivalent to 'end'. Note that justify-items is ignored in flexbox layouts. */
justify-items: self-start;
justify-items: self-end;
justify-items: left; /* Pack items from the left */
justify-items: right; /* Pack items from the right */
justify-items: anchor-center;
/* Baseline alignment */
justify-items: baseline;
justify-items: first baseline;
justify-items: last baseline;
/* Overflow alignment (for positional alignment only) */
justify-items: safe center;
justify-items: unsafe center;
/* Legacy alignment */
justify-items: legacy left;
justify-items: legacy right;
justify-items: legacy center;
/* Global values */
justify-items: inherit;
justify-items: initial;
justify-items: revert;
justify-items: revert-layer;
justify-items: unset;
This property can take one of four different forms:
normal or stretch.baseline keyword, plus optionally one of first or last.center, start, end, flex-start, flex-end, self-start, self-end, left, or right. Plus optionally safe or unsafe.legacy keyword, followed by one of left, right, or center.normal
start.start on replaced absolutely-positioned boxes, and as stretch on all other absolutely-positioned boxes.stretch, except for boxes with an aspect ratio or an intrinsic size where it behaves like start.start
end
flex-start
start.flex-end
end.self-start
self-end
center
left
start.right
start.baseline, first baseline, last baseline
first baseline is start, the one for last baseline is end.stretch
auto-sized items have their size increased equally (not proportionally), while still respecting the constraints imposed by {{CSSxRef("max-height")}}/{{CSSxRef("max-width")}} (or equivalent functionality), so that the combined size exactly fills the alignment container.anchor-center
anchor-center.safe
start.unsafe
legacy
justify-self: auto value, the legacy keyword is not considered by the descend, only the left, right, or center value associated to it.{{CSSInfo}}
{{csssyntax}}
In this example, we have a 2 x 2 grid layout. Initially the grid container is given a justify-items value of stretch (the default), which causes the grid items to stretch across the entire width of their cells.
If you hover or tab onto the grid container however, it is given a justify-items value of center, which causes the grid items to span only as wide as their content width, and align in the center of their cells.
<article class="container" tabindex="0">
<span>First child</span>
<span>Second child</span>
<span>Third child</span>
<span>Fourth child</span>
</article>
html {
font-family: "Helvetica", "Arial", sans-serif;
letter-spacing: 1px;
}
article {
background-color: red;
display: grid;
grid-template-columns: 1fr 1fr;
grid-auto-rows: 40px;
grid-gap: 10px;
margin: 20px;
width: 300px;
justify-items: stretch;
}
article:hover,
article:focus {
justify-items: center;
}
article span {
background-color: black;
color: white;
margin: 1px;
text-align: center;
}
article,
span {
padding: 10px;
border-radius: 7px;
}
{{EmbedLiveSample('Basic_demonstration', '100%', 200)}}
{{Specifications}}
{{Compat}}