Skip to content

Tailwind CSS - Flex Basis

In Tailwind CSS, flex-basis utilities control the initial or “ideal” size of a flex item before any remaining space in the container is distributed. It’s a foundational property for creating sophisticated and responsive layouts with Flexbox. Think of it as suggesting a size; the item might grow or shrink from this basis depending on other flex properties like flex-grow and flex-shrink.

Core Concept: The Flexbox Sizing Algorithm

Section titled “Core Concept: The Flexbox Sizing Algorithm”

To effectively use flex-basis, it’s helpful to understand its role in the Flexbox layout algorithm:

  1. Determine Flex Basis: The browser first looks at the flex-basis of each item. If it’s auto, the item’s basis is its content size. If it’s a specific value (like basis-1/4 or basis-32), that value is used.
  2. Distribute Free Space: The browser then compares the total basis of all items to the available space in the container.
    • If there’s extra space, it’s distributed among items according to their flex-grow factor.
    • If there’s not enough space, items shrink according to their flex-shrink factor.

Tailwind’s basis-* utilities give you precise control over step 1.

Tailwind provides a comprehensive set of basis-* utilities based on its default spacing and sizing scale. This includes fixed values (rem, px), percentages, and keywords.

ClassCSS Properties
basis-0flex-basis: 0px;
basis-1flex-basis: 0.25rem; /* 4px */
basis-2flex-basis: 0.5rem; /* 8px */
basis-4flex-basis: 1rem; /* 16px */
basis-autoflex-basis: auto;
basis-pxflex-basis: 1px;
basis-1/2flex-basis: 50%;
basis-1/3flex-basis: 33.333333%;
basis-2/3flex-basis: 66.666667%;
basis-1/4flex-basis: 25%;
basis-2/4flex-basis: 50%;
basis-3/4flex-basis: 75%;
basis-fullflex-basis: 100%;

Note: The table above shows a representative sample. Tailwind includes many more numeric steps (basis-3, basis-5, etc.) and fractional values (basis-1/5, basis-2/5, etc.) that align with its spacing scale. You can find the full list in the official Tailwind CSS documentation.

While you can use the Tailwind Play CDN for quick demos, real-world projects benefit from a build-process setup. This enables customization, optimization, and the full power of Tailwind.

Terminal window
# 1. Install Tailwind CSS
npm install -D tailwindcss
npx tailwindcss init
# 2. Configure your template paths in tailwind.config.js
// tailwind.config.js
module.exports = {
content: ["./src/**/*.{html,js}"],
theme: {
extend: {},
},
plugins: [],
}
# 3. Add the Tailwind directives to your main CSS file
/* src/input.css */
@tailwind base;
@tailwind components;
@tailwind utilities;
# 4. Start the build process
npx tailwindcss -i ./src/input.css -o ./dist/output.css --watch

Here, we create a common layout with a fixed-width sidebar and a main content area that fills the remaining space. We use basis-64 to set the sidebar’s initial width and flex-1 (a shorthand for flex-grow: 1) on the main content area to make it expand.

<!-- Link to your compiled CSS file -->
<link href="/dist/output.css" rel="stylesheet">
<body class="bg-slate-100 text-slate-800 p-8">
<h3 class="text-2xl font-bold mb-4">Product Page Layout</h3>
<div class="flex flex-row gap-8 bg-white p-4 rounded-lg shadow-md">
<!-- Sidebar: Has a fixed basis and will not grow or shrink -->
<aside class="basis-64 flex-shrink-0 bg-slate-200 p-4 rounded">
<h4 class="font-semibold">Filters</h4>
<p class="text-sm mt-2">Category filters go here.</p>
</aside>
<!-- Main Content: Grows to fill available space -->
<main class="flex-1 bg-slate-50 p-4 rounded">
<h4 class="font-semibold">Products</h4>
<p class="text-sm mt-2">Product grid goes here.</p>
</main>
</div>
</body>

A key strength of Tailwind is responsive design. In this example, we create a three-column layout. On small screens (< md), each item takes the full width. On medium screens and up (md:), they form a three-column layout where the middle item is twice as wide as the others.

<link href="/dist/output.css" rel="stylesheet">
<body class="bg-gray-100 p-8">
<h3 class="text-2xl font-bold mb-4">Responsive Tiered Pricing</h3>
<div class="flex flex-wrap md:flex-nowrap gap-4">
<!-- On mobile, basis-full makes it a full-width block. -->
<!-- On desktop, basis-1/4 sets its width. -->
<div class="basis-full md:basis-1/4 bg-white p-6 rounded-lg shadow text-center">
<h4 class="font-bold text-lg">Basic</h4>
<p class="mt-2">$10/month</p>
</div>
<!-- The featured plan is wider on desktop -->
<div class="basis-full md:basis-1/2 bg-indigo-500 text-white p-6 rounded-lg shadow-xl text-center">
<h4 class="font-bold text-lg">Pro</h4>
<p class="mt-2">$25/month</p>
</div>
<div class="basis-full md:basis-1/4 bg-white p-6 rounded-lg shadow text-center">
<h4 class="font-bold text-lg">Enterprise</h4>
<p class="mt-2">Contact Us</p>
</div>
</div>
</body>
  • Use gap for Spacing: Instead of adding margins to flex children (mr-4), apply a gap-4 utility to the parent flex container. This is cleaner and handles spacing more consistently, especially in wrapping layouts.
  • Parent Must be flex: The basis-* utilities have no effect unless the parent element has display: flex (or inline-flex).
  • basis vs. width: Use basis-* when you want an item’s size to be part of the flexible layout calculation. Use w-* when you want to set a rigid width that flex-grow or flex-shrink should not influence (unless flex-basis is auto).
  • Combine with grow and shrink: flex-basis is most powerful when combined with flex-grow (grow or flex-1) and flex-shrink (shrink or shrink-0) to control how items adapt to available space.