Back to Tailwindcss

Align Items

src/docs/align-items.mdx

latest8.1 KB
Original Source

import { ApiTable } from "@/components/api-table.tsx"; import { Example } from "@/components/example.tsx"; import { Figure } from "@/components/figure.tsx"; import { ResponsiveDesign } from "@/components/content.tsx"; import { Stripes } from "@/components/stripes.tsx";

export const title = "align-items"; export const description = "Utilities for controlling how flex and grid items are positioned along a container's cross axis.";

<ApiTable rows={[ ["items-start", "align-items: flex-start;"], ["items-end", "align-items: flex-end;"], ["items-end-safe", "align-items: safe flex-end;"], ["items-center", "align-items: center;"], ["items-center-safe", "align-items: safe center;"], ["items-baseline", "align-items: baseline;"], ["items-baseline-last", "align-items: last baseline;"], ["items-stretch", "align-items: stretch;"], ]} />

Examples

Stretch

Use the items-stretch utility to stretch items to fill the container's cross axis:

<Figure> <Example> { <div className="grid grid-cols-1"> <Stripes border className="col-start-1 row-start-1 rounded-lg" /> <div className="col-start-1 row-start-1 flex w-full items-stretch gap-4 rounded-lg text-center font-mono text-sm leading-6 font-bold text-white"> <div className="flex flex-1 items-center justify-center rounded-lg bg-cyan-500 py-4">01</div> <div className="flex flex-1 items-center justify-center rounded-lg bg-cyan-500 py-12">02</div> <div className="flex flex-1 items-center justify-center rounded-lg bg-cyan-500 py-8">03</div> </div> </div> } </Example>
html
<!-- [!code classes:items-stretch] -->
<div class="flex items-stretch ...">
  <div class="py-4">01</div>
  <div class="py-12">02</div>
  <div class="py-8">03</div>
</div>
</Figure>

Start

Use the items-start utility to align items to the start of the container's cross axis:

<Figure> <Example> { <div className="grid grid-cols-1"> <Stripes border className="col-start-1 row-start-1 rounded-lg" /> <div className="col-start-1 row-start-1 flex w-full items-start gap-4 rounded-lg text-center font-mono text-sm leading-6 font-bold text-white"> <div className="flex flex-1 items-center justify-center rounded-lg bg-pink-500 py-4">01</div> <div className="flex flex-1 items-center justify-center rounded-lg bg-pink-500 py-12">02</div> <div className="flex flex-1 items-center justify-center rounded-lg bg-pink-500 py-8">03</div> </div> </div> } </Example>
html
<!-- [!code classes:items-start] -->
<div class="flex items-start ...">
  <div class="py-4">01</div>
  <div class="py-12">02</div>
  <div class="py-8">03</div>
</div>
</Figure>

Center

Use the items-center utility to align items along the center of the container's cross axis:

<Figure> <Example> { <div className="grid grid-cols-1"> <Stripes border className="col-start-1 row-start-1 rounded-lg" /> <div className="col-start-1 row-start-1 flex w-full items-center gap-4 rounded-lg text-center font-mono text-sm leading-6 font-bold text-white"> <div className="flex flex-1 items-center justify-center rounded-lg bg-violet-500 py-4">01</div> <div className="flex flex-1 items-center justify-center rounded-lg bg-violet-500 py-12">02</div> <div className="flex flex-1 items-center justify-center rounded-lg bg-violet-500 py-8">03</div> </div> </div> } </Example>
html
<!-- [!code classes:items-center] -->
<div class="flex items-center ...">
  <div class="py-4">01</div>
  <div class="py-12">02</div>
  <div class="py-8">03</div>
</div>
</Figure>

End

Use the items-end utility to align items to the end of the container's cross axis:

<Figure> <Example> { <div className="grid grid-cols-1"> <Stripes border className="col-start-1 row-start-1 rounded-lg" /> <div className="col-start-1 row-start-1 flex w-full items-end gap-4 rounded-lg text-center font-mono text-sm leading-6 font-bold text-white"> <div className="flex flex-1 items-center justify-center rounded-lg bg-sky-500 py-4">01</div> <div className="flex flex-1 items-center justify-center rounded-lg bg-sky-500 py-12">02</div> <div className="flex flex-1 items-center justify-center rounded-lg bg-sky-500 py-8">03</div> </div> </div> } </Example>
html
<!-- [!code classes:items-end] -->
<div class="flex items-end ...">
  <div class="py-4">01</div>
  <div class="py-12">02</div>
  <div class="py-8">03</div>
</div>
</Figure>

Baseline

Use the items-baseline utility to align items along the container's cross axis such that all of their baselines align:

<Figure> <Example> { <div className="grid grid-cols-1"> <Stripes border className="col-start-1 row-start-1 rounded-lg" /> <div className="col-start-1 row-start-1 flex w-full items-baseline gap-4 rounded-lg text-center font-mono text-sm leading-6 font-bold text-white"> <div className="flex flex-1 items-center justify-center rounded-lg bg-blue-500 pt-2 pb-6">01</div> <div className="flex flex-1 items-center justify-center rounded-lg bg-blue-500 pt-8 pb-12">02</div> <div className="flex flex-1 items-center justify-center rounded-lg bg-blue-500 pt-12 pb-4">03</div> </div> </div> } </Example>
html
<!-- [!code classes:items-baseline] -->
<div class="flex items-baseline ...">
  <div class="pt-2 pb-6">01</div>
  <div class="pt-8 pb-12">02</div>
  <div class="pt-12 pb-4">03</div>
</div>
</Figure>

Last baseline

Use the items-baseline-last utility to align items along the container's cross axis such that all of their baselines align with the last baseline in the container:

<Figure> <Example padding={false}> { <div className="mx-auto grid max-w-md divide-y divide-gray-100 border-x border-x-gray-200 text-gray-700 dark:divide-gray-800 dark:border-x-gray-800 dark:bg-gray-950/10 dark:text-gray-300"> <div className="grid grid-cols-[1fr_auto] items-baseline-last gap-x-4 px-4 py-6"> <div className="grid grid-cols-[auto_1fr] gap-x-4 max-sm:grid-cols-1">
      <div className="font-semibold text-gray-900 sm:col-start-2 dark:text-white">Spencer Sharp</div>
      <p className="text-sm sm:col-start-2">Working on the future of astronaut recruitment at Space Recruit.</p>
    </div>
    <a
      href="#"
      className="font-mono text-xs font-medium text-gray-400 underline hover:text-blue-500 dark:text-gray-500"
    >
      spacerecruit.com
    </a>
  </div>
  <div className="grid grid-cols-[1fr_auto] items-baseline-last gap-x-4 px-4 py-6">
    <div className="grid grid-cols-[auto_1fr] gap-x-4 max-sm:grid-cols-1">
      
      <div className="font-semibold text-gray-900 sm:col-start-2 dark:text-white">Alex Reed</div>
      <p className="text-sm sm:col-start-2">A multidisciplinary designer.</p>
    </div>
    <a
      href="#"
      className="font-mono text-xs font-medium text-gray-400 underline hover:text-blue-500 dark:text-gray-500"
    >
      alex-reed.com
    </a>
  </div>
</div>

} </Example>

html
<!-- [!code classes:items-baseline-last] -->
<div class="grid grid-cols-[1fr_auto] items-baseline-last">
  <div>
    
    <h4>Spencer Sharp</h4>
    <p>Working on the future of astronaut recruitment at Space Recruit.</p>
  </div>
  <p>spacerecruit.com</p>
</div>
</Figure>

This is useful for ensuring that text items align with each other, even if they have different heights.

Responsive design

<ResponsiveDesign property="align-items" defaultClass="flex items-stretch" featuredClass="items-center" />