Flex

    Initial

    Use to allow a flex item to shrink but not grow, taking into account its initial size:

    Use .flex-1 to allow a flex item to grow and shrink as needed, ignoring its initial size:

    Flex - 图2

    1. <div class="flex bg-gray-200">
    2. <div class="flex-1 text-gray-700 text-center bg-gray-400 px-4 py-2 m-2">
    3. Short
    4. </div>
    5. <div class="flex-1 text-gray-700 text-center bg-gray-400 px-4 py-2 m-2">
    6. Medium length
    7. <div class="flex-1 text-gray-700 text-center bg-gray-400 px-4 py-2 m-2">
    8. Significantly larger amount of content
    9. </div>
    10. </div>

    Auto

    Use .flex-auto to allow a flex item to grow and shrink, taking into account its initial size:

    Use .flex-none to prevent a flex item from growing or shrinking:

    Flex - 图4

    1. <div class="flex bg-gray-200">
    2. <div class="flex-1 text-gray-700 text-center bg-gray-400 px-4 py-2 m-2">
    3. Item that can grow or shrink if needed
    4. </div>
    5. Item that cannot grow or shrink
    6. </div>
    7. <div class="flex-1 text-gray-700 text-center bg-gray-400 px-4 py-2 m-2">
    8. Item that can grow or shrink if needed
    9. </div>
    10. </div>

    Responsive

    To control how a flex item both grows and shrinks at a specific breakpoint, add a {screen}: prefix to any existing utility class. For example, use md:flex-1 to apply the flex-1 utility at only medium screen sizes and above.

    all

    Flex - 图6

    sm

    md

    Flex - 图8

    lg

    Item that can grow or shrink if needed

    Responsive flex item

    Item that can grow or shrink if needed

    By default Tailwind provides four flex utilities. You change, add, or remove these by editing the theme.flex section of your Tailwind config.

    1. // tailwind.config.js
    2. theme: {
    3. flex: {
    4. '1': '1 1 0%',
    5. auto: '1 1 auto',
    6. - initial: '0 1 auto',
    7. none: 'none',
    8. + '2': '2 2 0%',
    9. }
    10. }
    11. }

    By default, only responsive variants are generated for flex utilities.

    You can control which variants are generated for the flex utilities by modifying the flex property in the variants section of your tailwind.config.js file.

    For example, this config will also generate hover and focus variants:

    If you don’t plan to use the flex utilities in your project, you can disable them entirely by setting the flex property to false in the corePlugins section of your config file:

    1. // tailwind.config.js
    2. module.exports = {
    3. corePlugins: {
    4. // ...
    5. + flex: false,
    6. }

      Flex Grow →