|

CSS 27 💻 flex-basis, flex and flex-flow properties

These three properties give you precise control over initial sizing, space distribution, and direction/wrapping in Flexbox. They’re essential for building sophisticated, responsive layouts.


Overview of Properties

PropertyDescriptionType
flex-basisSets the initial main size of a flex itemLonghand
flexShorthand for flex-grow, flex-shrink, flex-basisShorthand
flex-flowShorthand for flex-direction and flex-wrapShorthand

1. flex-basis

The flex-basis property specifies the initial main size of a flex item before any free space is distributed.

.item1 {
    flex-basis: 100px; /* Starts at 100px before growing/shrinking */
}

Values

ValueDescriptionExample
autoBased on content or width/height (default)flex-basis: auto;
lengthFixed size (px, em, rem)flex-basis: 100px;
percentagePercentage of containerflex-basis: 20%;
contentBased on content sizeflex-basis: content;
max-contentMaximum content sizeflex-basis: max-content;
min-contentMinimum content sizeflex-basis: min-content;
fit-contentFits content within limitsflex-basis: fit-content(200px);

Examples

/* Fixed size */
.item1 {
    flex-basis: 100px; /* Starts at 100px */
}

/* Percentage of container */
.item3 {
    flex-basis: 20%; /* Starts at 20% of container width */
}

/* Relative to font size */
.item4 {
    flex-basis: 5em; /* Starts at 5em */
}

/* Content-based */
.item {
    flex-basis: content; /* Size based on content */
}

Important: flex-basis is applied before flex-grow and flex-shrink come into play. It’s the starting point for space distribution.


2. flex (Shorthand)

The flex property is a shorthand for flex-grow, flex-shrink, and flex-basis.

.item {
    flex: 2 1 100px; /* grow: 2, shrink: 1, basis: 100px */
}

Syntax

flex: <flex-grow> <flex-shrink> <flex-basis>;
flex: <flex-grow> <flex-basis>;  /* shrink defaults to 1 */
flex: <flex-grow>;               /* shrink: 1, basis: 0 */

Common Values

ValueEquivalentDescription
flex: none;0 0 autoFixed size, no grow/shrink
flex: auto;1 1 autoBased on content, can grow/shrink
flex: 1;1 1 0%Equal distribution, ignores content
flex: 2;2 1 0%Grows 2x more than flex: 1
flex: 1 1 10em;1 1 10emGrow 1x, shrink 1x, base 10em
flex: 2 1 100px;2 1 100pxGrow 2x, shrink 1x, base 100px
flex: 1 2 auto;1 2 autoGrow 1x, shrink 2x, base auto

Special Values

ShorthandEquivalentDescription
flex: none0 0 autoNo flexibility at all
flex: auto1 1 autoFully flexible based on content
flex: initial0 1 autoDefault flex behavior

Two-Value Syntax

If you provide two values and both are integers, the second is flex-shrink:

flex: 2 2;    /* grow: 2, shrink: 2, basis: 0% */

If one is an integer and the other is a length, the integer is flex-grow and the length is flex-basis:

flex: 1 30px; /* grow: 1, shrink: 1, basis: 30px */

3. flex-flow (Shorthand)

The flex-flow property is a shorthand for flex-direction and flex-wrap.

.container {
    flex-flow: row wrap; /* direction: row, wrap: wrap */
}

Values

PartValues
flex-directionrow, row-reverse, column, column-reverse
flex-wrapnowrap, wrap, wrap-reverse

Examples

/* Row direction with wrapping */
.container {
    flex-flow: row wrap;
}

/* Column direction without wrapping */
.container {
    flex-flow: column nowrap;
}

/* Row reverse with wrapping */
.container {
    flex-flow: row-reverse wrap;
}

/* Only direction (wrap defaults to nowrap) */
.container {
    flex-flow: column;
}

/* Only wrap (direction defaults to row) */
.container {
    flex-flow: wrap;
}

Complete Example

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>flex-basis, flex, and flex-flow Properties</title>
    <style>
        *, *::before, *::after {
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            background: #f8f9fa;
            line-height: 1.6;
        }

        h1 {
            color: #007bff;
            border-bottom: 3px solid #007bff;
            padding-bottom: 10px;
        }

        h2 {
            color: #28a745;
            border-left: 4px solid #28a745;
            padding-left: 15px;
            margin-top: 30px;
        }

        h3 {
            color: #333;
            margin-top: 20px;
        }

        section {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            margin: 20px 0;
        }

        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            padding: 15px;
            border-radius: 8px;
            overflow-x: auto;
            font-family: 'Courier New', monospace;
            font-size: 0.9rem;
            line-height: 1.8;
            margin: 10px 0;
        }

        .reference-table {
            width: 100%;
            border-collapse: collapse;
            margin: 15px 0;
        }

        .reference-table th,
        .reference-table td {
            padding: 10px;
            border: 1px solid #ddd;
            text-align: left;
        }

        .reference-table th {
            background: #007bff;
            color: white;
        }

        .reference-table tr:nth-child(even) {
            background: #f8f9fa;
        }

        .note {
            font-size: 0.9rem;
            color: #6c757d;
            margin-top: 5px;
        }

        .highlight {
            background: #ffc107;
            color: #333;
            padding: 2px 6px;
            border-radius: 4px;
            font-weight: bold;
        }

        /* ====== FLEX CONTAINERS ====== */
        .container {
            display: flex;
            border: 2px solid #333;
            width: 500px;
            height: 100px;
            padding: 5px;
            margin: 15px 0;
            background: #e9ecef;
            border-radius: 8px;
            gap: 5px;
        }

        .container-small {
            width: 300px;
        }

        .container-tall {
            height: 350px;
        }

        .item {
            border: 2px solid #dc3545;
            height: 50px;
            border-radius: 6px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-weight: bold;
            font-size: 0.8rem;
            padding: 5px;
            background: #007bff;
        }

        .item:nth-child(even) {
            background: #28a745;
        }

        .item:nth-child(3n) {
            background: #6c5ce7;
        }

        /* flex-basis demos */
        .basis-100px {
            flex-basis: 100px;
        }

        .basis-20p {
            flex-basis: 20%;
        }

        .basis-5em {
            flex-basis: 5em;
        }

        .basis-auto {
            flex-basis: auto;
        }

        /* flex shorthand demos */
        .flex-1 {
            flex: 1;
        }

        .flex-2 {
            flex: 2;
        }

        .flex-none {
            flex: none;
        }

        .flex-auto {
            flex: auto;
        }

        .flex-2-1-100 {
            flex: 2 1 100px;
        }

        .flex-1-2-auto {
            flex: 1 2 auto;
        }

        .flex-1-1-5em {
            flex: 1 1 5em;
        }

        /* flex-flow demos */
        .flow-row-wrap {
            flex-flow: row wrap;
        }

        .flow-column-nowrap {
            flex-flow: column nowrap;
        }

        .flow-row-reverse-wrap {
            flex-flow: row-reverse wrap;
        }

        .item-flow {
            border: 2px solid #dc3545;
            flex: 0 0 100px;
            height: 60px;
            border-radius: 6px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-weight: bold;
            font-size: 0.8rem;
            background: #007bff;
        }

        .item-flow:nth-child(even) {
            background: #28a745;
        }

        /* Side-by-side comparison */
        .comparison-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 20px;
        }

        .comparison-item {
            text-align: center;
        }

        .comparison-item h4 {
            margin-bottom: 5px;
            color: #007bff;
        }

        /* Visual explanation */
        .visual-explanation {
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 20px;
            flex-wrap: wrap;
            margin: 20px 0;
        }

        .visual-box {
            text-align: center;
        }

        .visual-box .bar {
            height: 30px;
            border-radius: 4px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 0.8rem;
            font-weight: bold;
            margin: 5px 0;
        }

        .visual-box .bar.basis-100 {
            background: #007bff;
            width: 100px;
        }

        .visual-box .bar.basis-50 {
            background: #28a745;
            width: 50px;
        }

        .visual-box .bar.basis-150 {
            background: #dc3545;
            width: 150px;
        }
    </style>
</head>
<body>

    <h1>flex-basis, flex, and flex-flow Properties</h1>

    <!-- ====== 1. FLEX-BASIS ====== -->
    <section>
        <h2>1. flex-basis</h2>
        <p>Specifies the <strong>initial main size</strong> of a flex item before any space is distributed.</p>

        <div class="container">
            <div class="item basis-100px">basis: 100px</div>
            <div class="item basis-20p">basis: 20%</div>
            <div class="item basis-5em">basis: 5em</div>
        </div>

        <p class="note">Each item starts at its <code>flex-basis</code> value. The remaining space is distributed based on <code>flex-grow</code>.</p>

        <div class="code-block">
            .item1 { flex-basis: 100px; }  /* Starts at 100px */
            .item2 { flex-basis: 20%; }    /* Starts at 20% of container */
            .item3 { flex-basis: 5em; }    /* Starts at 5em */
            .item4 { flex-basis: auto; }   /* Based on content (default) */
        </div>
    </section>

    <!-- ====== 2. FLEX SHORTHAND ====== -->
    <section>
        <h2>2. flex (Shorthand)</h2>
        <p>Combines <code>flex-grow</code>, <code>flex-shrink</code>, and <code>flex-basis</code>.</p>

        <h3>flex: 2 1 100px | 1 2 auto | 1 1 5em</h3>
        <div class="container">
            <div class="item flex-2-1-100">flex: 2 1 100px</div>
            <div class="item flex-1-2-auto">flex: 1 2 auto</div>
            <div class="item flex-1-1-5em">flex: 1 1 5em</div>
        </div>

        <h3>flex: 1 | 2 | none | auto</h3>
        <div class="container">
            <div class="item flex-1">flex: 1</div>
            <div class="item flex-2">flex: 2</div>
            <div class="item flex-none">flex: none</div>
            <div class="item flex-auto">flex: auto</div>
        </div>

        <div class="code-block">
            /* Longhand version */
            .item {
                flex-grow: 2;
                flex-shrink: 1;
                flex-basis: 100px;
            }

            /* Shorthand version */
            .item {
                flex: 2 1 100px;
            }

            /* Common shorthand values */
            .item { flex: 1; }      /* grow: 1, shrink: 1, basis: 0% */
            .item { flex: 2; }      /* grow: 2, shrink: 1, basis: 0% */
            .item { flex: none; }   /* grow: 0, shrink: 0, basis: auto */
            .item { flex: auto; }   /* grow: 1, shrink: 1, basis: auto */
            .item { flex: 1 30px; } /* grow: 1, shrink: 1, basis: 30px */
        </div>
    </section>

    <!-- ====== 3. FLEX-FLOW ====== -->
    <section>
        <h2>3. flex-flow (Shorthand)</h2>
        <p>Combines <code>flex-direction</code> and <code>flex-wrap</code>.</p>

        <h3>flex-flow: row wrap</h3>
        <div class="container container-small container-tall flow-row-wrap">
            <div class="item-flow">1</div>
            <div class="item-flow">2</div>
            <div class="item-flow">3</div>
            <div class="item-flow">4</div>
        </div>

        <h3>flex-flow: column nowrap</h3>
        <div class="container container-small container-tall flow-column-nowrap" style="width: auto; height: 300px;">
            <div class="item-flow" style="flex: 0 0 50px;">1</div>
            <div class="item-flow" style="flex: 0 0 50px;">2</div>
            <div class="item-flow" style="flex: 0 0 50px;">3</div>
            <div class="item-flow" style="flex: 0 0 50px;">4</div>
        </div>

        <h3>flex-flow: row-reverse wrap</h3>
        <div class="container container-small container-tall flow-row-reverse-wrap">
            <div class="item-flow">1</div>
            <div class="item-flow">2</div>
            <div class="item-flow">3</div>
            <div class="item-flow">4</div>
        </div>

        <div class="code-block">
            /* Row direction with wrapping */
            .container { flex-flow: row wrap; }

            /* Column direction without wrapping */
            .container { flex-flow: column nowrap; }

            /* Row reverse with wrapping */
            .container { flex-flow: row-reverse wrap; }

            /* Only direction (wrap defaults to nowrap) */
            .container { flex-flow: column; }

            /* Only wrap (direction defaults to row) */
            .container { flex-flow: wrap; }
        </div>
    </section>

    <!-- ====== 4. SIDE-BY-SIDE COMPARISON ====== -->
    <section>
        <h2>4. Side-by-Side Comparison</h2>

        <h3>flex-basis vs flex-grow</h3>
        <div class="comparison-grid">
            <div class="comparison-item">
                <h4>flex-basis: 100px | 100px | 100px</h4>
                <div class="container" style="width: 100%;">
                    <div class="item basis-100px">100px</div>
                    <div class="item basis-100px">100px</div>
                    <div class="item basis-100px">100px</div>
                </div>
                <p class="note">All start at 100px, no growth</p>
            </div>

            <div class="comparison-item">
                <h4>flex: 1 | 1 | 1</h4>
                <div class="container" style="width: 100%;">
                    <div class="item flex-1">flex: 1</div>
                    <div class="item flex-1">flex: 1</div>
                    <div class="item flex-1">flex: 1</div>
                </div>
                <p class="note">All grow equally to fill space</p>
            </div>

            <div class="comparison-item">
                <h4>flex: 1 | 2 | 1</h4>
                <div class="container" style="width: 100%;">
                    <div class="item flex-1">flex: 1</div>
                    <div class="item flex-2">flex: 2</div>
                    <div class="item flex-1">flex: 1</div>
                </div>
                <p class="note">Middle grows twice as much</p>
            </div>
        </div>
    </section>

    <!-- ====== 5. VISUAL EXPLANATION ====== -->
    <section>
        <h2>5. Visual Explanation: flex-basis</h2>
        <p>Think of <code>flex-basis</code> as the starting point before any space is distributed.</p>

        <div class="visual-explanation">
            <div class="visual-box">
                <div class="bar basis-50">50px</div>
                <p class="note">flex-basis: 50px</p>
            </div>
            <div class="visual-box">
                <div class="bar basis-100">100px</div>
                <p class="note">flex-basis: 100px</p>
            </div>
            <div class="visual-box">
                <div class="bar basis-150">150px</div>
                <p class="note">flex-basis: 150px</p>
            </div>
        </div>

        <div class="code-block">
            /* flex-basis sets the starting size */
            .item1 { flex-basis: 50px; }
            .item2 { flex-basis: 100px; }
            .item3 { flex-basis: 150px; }

            /* Then flex-grow distributes remaining space */
            .item { flex-grow: 1; }
        </div>
    </section>

    <!-- ====== 6. REFERENCE TABLES ====== -->
    <section>
        <h2>6. Reference Tables</h2>

        <h3>flex-basis Values</h3>
        <table class="reference-table">
            <tr>
                <th>Value</th>
                <th>Description</th>
                <th>Example</th>
            </tr>
            <tr>
                <td><code>auto</code></td>
                <td>Based on content or width/height (default)</td>
                <td><code>flex-basis: auto;</code></td>
            </tr>
            <tr>
                <td><code>length</code></td>
                <td>Fixed size (px, em, rem)</td>
                <td><code>flex-basis: 100px;</code></td>
            </tr>
            <tr>
                <td><code>percentage</code></td>
                <td>Percentage of container</td>
                <td><code>flex-basis: 20%;</code></td>
            </tr>
            <tr>
                <td><code>content</code></td>
                <td>Based on content size</td>
                <td><code>flex-basis: content;</code></td>
            </tr>
            <tr>
                <td><code>max-content</code></td>
                <td>Maximum content size</td>
                <td><code>flex-basis: max-content;</code></td>
            </tr>
            <tr>
                <td><code>min-content</code></td>
                <td>Minimum content size</td>
                <td><code>flex-basis: min-content;</code></td>
            </tr>
            <tr>
                <td><code>fit-content</code></td>
                <td>Fits content within limits</td>
                <td><code>flex-basis: fit-content(200px);</code></td>
            </tr>
        </table>

        <h3>flex Shorthand Values</h3>
        <table class="reference-table">
            <tr>
                <th>Shorthand</th>
                <th>Equivalent</th>
                <th>Description</th>
            </tr>
            <tr>
                <td><code>flex: none;</code></td>
                <td><code>0 0 auto</code></td>
                <td>Fixed size, no grow/shrink</td>
            </tr>
            <tr>
                <td><code>flex: auto;</code></td>
                <td><code>1 1 auto</code></td>
                <td>Based on content, can grow/shrink</td>
            </tr>
            <tr>
                <td><code>flex: 1;</code></td>
                <td><code>1 1 0%</code></td>
                <td>Equal distribution, ignores content</td>
            </tr>
            <tr>
                <td><code>flex: 2;</code></td>
                <td><code>2 1 0%</code></td>
                <td>Grows 2x more than flex: 1</td>
            </tr>
            <tr>
                <td><code>flex: 1 1 10em;</code></td>
                <td><code>1 1 10em</code></td>
                <td>Grow 1x, shrink 1x, base 10em</td>
            </tr>
            <tr>
                <td><code>flex: 2 1 100px;</code></td>
                <td><code>2 1 100px</code></td>
                <td>Grow 2x, shrink 1x, base 100px</td>
            </tr>
            <tr>
                <td><code>flex: 1 2 auto;</code></td>
                <td><code>1 2 auto</code></td>
                <td>Grow 1x, shrink 2x, base auto</td>
            </tr>
        </table>

        <h3>flex-flow Values</h3>
        <table class="reference-table">
            <tr>
                <th>Value</th>
                <th>Description</th>
                <th>Example</th>
            </tr>
            <tr>
                <td><code>row wrap</code></td>
                <td>Row direction with wrapping</td>
                <td><code>flex-flow: row wrap;</code></td>
            </tr>
            <tr>
                <td><code>column nowrap</code></td>
                <td>Column direction without wrapping</td>
                <td><code>flex-flow: column nowrap;</code></td>
            </tr>
            <tr>
                <td><code>row-reverse wrap</code></td>
                <td>Row reverse with wrapping</td>
                <td><code>flex-flow: row-reverse wrap;</code></td>
            </tr>
            <tr>
                <td><code>column</code></td>
                <td>Column direction (wrap defaults to nowrap)</td>
                <td><code>flex-flow: column;</code></td>
            </tr>
            <tr>
                <td><code>wrap</code></td>
                <td>Wrapping (direction defaults to row)</td>
                <td><code>flex-flow: wrap;</code></td>
            </tr>
        </table>
    </section>

    <!-- ====== 7. BEST PRACTICES ====== -->
    <section>
        <h2>7. Best Practices</h2>

        <div style="background: #d4edda; padding: 20px; border-radius: 8px; border-left: 4px solid #28a745;">
            <h3 style="margin-top: 0; color: #155724;">✅ Do This:</h3>
            <ul>
                <li>Use <code>flex: 1</code> for equal-width items that fill available space</li>
                <li>Use <code>flex: none</code> for fixed-size items that shouldn't grow or shrink</li>
                <li>Use <code>flex-basis</code> to set a starting size before distribution</li>
                <li>Use <code>flex-flow</code> to combine direction and wrapping</li>
                <li>Use the shorthand properties for cleaner, more readable code</li>
            </ul>
        </div>

        <div style="background: #f8d7da; padding: 20px; border-radius: 8px; border-left: 4px solid #dc3545; margin-top: 15px;">
            <h3 style="margin-top: 0; color: #721c24;">❌ Don't Do This:</h3>
            <ul>
                <li>Don't confuse <code>flex-basis</code> with <code>width</code> — they behave differently</li>
                <li>Don't use <code>flex: 1</code> when you want items to be based on their content (use <code>flex: auto</code>)</li>
                <li>Don't forget that <code>flex-basis: auto</code> looks at <code>width</code>/<code>height</code> first</li>
                <li>Don't write all three longhand properties when <code>flex</code> shorthand works</li>
            </ul>
        </div>
    </section>

</body>
</html>

Quick Reference

PropertyDescriptionExample
flex-basisInitial main sizeflex-basis: 100px;
flexShorthand for grow, shrink, basisflex: 2 1 100px;
flex-flowShorthand for direction and wrapflex-flow: row wrap;

flex-basis Values

ValueDescription
autoBased on content or width/height (default)
lengthFixed size (px, em, rem)
percentagePercentage of container
contentBased on content size
max-contentMaximum content size
min-contentMinimum content size
fit-content(limit)Fits content within limits

flex Shorthand Values

ShorthandEquivalentDescription
flex: none;0 0 autoFixed size, no grow/shrink
flex: auto;1 1 autoBased on content
flex: 1;1 1 0%Equal distribution
flex: 2;2 1 0%Grows 2x more
flex: 1 1 10em;1 1 10emGrow 1x, shrink 1x, base 10em
flex: 2 1 100px;2 1 100pxGrow 2x, shrink 1x, base 100px

flex-flow Values

ValueDescription
row wrapRow direction with wrapping
column nowrapColumn direction without wrapping
row-reverse wrapRow reverse with wrapping
columnColumn direction (wrap defaults to nowrap)
wrapWrapping (direction defaults to row)

Best Practices

Do This:

/* Equal-width items */
.item {
    flex: 1; /* grow: 1, shrink: 1, basis: 0% */
}

/* Fixed-size item */
.sidebar {
    flex: none; /* grow: 0, shrink: 0, basis: auto */
}

/* Content-based item */
.content {
    flex: auto; /* grow: 1, shrink: 1, basis: auto */
}

/* Custom sizing */
.item {
    flex: 2 1 200px; /* grow: 2, shrink: 1, basis: 200px */
}

/* Direction + wrap */
.container {
    flex-flow: row wrap;
}

Don’t Do This:

/* Don't confuse flex-basis with width */
.item {
    flex-basis: 100px; /* Initial size before distribution */
    width: 100px;      /* Ignored in flex context (mostly) */
}

/* Don't use flex: 1 when you want content-based sizing */
.item {
    flex: 1; /* Ignores content, uses 0% basis */
    /* Use flex: auto for content-based sizing */
}

/* Don't write longhand when shorthand works */
.item {
    flex-grow: 1;
    flex-shrink: 1;
    flex-basis: 0%;
    /* Just write: flex: 1; */
}

Pro Tip: flex-basis is the starting point for a flex item before any space is distributed. Think of it as “how big should this item be before we start sharing the extra space?” Use flex: 1 for equal-width items that ignore content size, and flex: auto for items that respect their content size. The flex shorthand is almost always cleaner than writing the three longhand properties separately. And flex-flow is a convenient way to set both direction and wrapping in one line!


Stop using slow, ad-bloated tool sites! 🤮

🔎 Search “KandZ Tools” on Google to use many professional utilities for free.

KandZ.me is the ultimate minimalist hub for:
✅ Finance (Mortgage, Interest, Inflation)
✅ Tech (Base64, JSON, Dev Suite, IP)
✅ Health (BMI, BMR, TDEE)
✅ Productivity (Timer, Workspace, QR)

⚡️ Fast & Private
🔒 No data leaves your device
💎 100% Free

🔗 Use it now: https://tools.kandz.me
🔖 Bookmark it—you’ll need it later!