Flex

Utilities for controlling how flex items both grow and shrink.

Class reference

Class
Properties
.flex-1flex: 1 1 0%;
.flex-autoflex: 1 1 auto;
.flex-initialflex: 0 1 auto;
.flex-noneflex: none;

Initial

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

Flex - 图1

  1. <div class="flex bg-gray-200">
  2. <div class="flex-initial text-gray-700 text-center bg-gray-400 px-4 py-2 m-2">
  3. Short
  4. </div>
  5. <div class="flex-initial text-gray-700 text-center bg-gray-400 px-4 py-2 m-2">
  6. Medium length
  7. </div>
  8. </div>
  9. <div class="flex bg-gray-200">
  10. <div class="flex-initial text-gray-700 text-center bg-gray-400 px-4 py-2 m-2">
  11. Short
  12. </div>
  13. <div class="flex-initial text-gray-700 text-center bg-gray-400 px-4 py-2 m-2">
  14. Medium length
  15. </div>
  16. <div class="flex-initial text-gray-700 text-center bg-gray-400 px-4 py-2 m-2">
  17. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Qui ad labore ipsam, aut rem quo repellat esse tempore id, quidem
  18. </div>
  19. </div>

Flex 1

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>
  8. <div class="flex-1 text-gray-700 text-center bg-gray-400 px-4 py-2 m-2">
  9. Significantly larger amount of content
  10. </div>
  11. </div>

Auto

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

Flex - 图3

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

None

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. <div class="flex-none text-gray-800 text-center bg-gray-500 px-4 py-2 m-2">
  6. Item that cannot grow or shrink
  7. </div>
  8. <div class="flex-1 text-gray-700 text-center bg-gray-400 px-4 py-2 m-2">
  9. Item that can grow or shrink if needed
  10. </div>
  11. </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.

For more information about Tailwind’s responsive design features, check out the Responsive Design documentation.

Flex - 图5

all

Flex - 图6

sm

Flex - 图7

md

Flex - 图8

lg

Flex - 图9

xl

  1. <div class="flex ...">
  2. <!-- ... -->
  3. <div class="flex-none sm:flex-1 md:flex-auto lg:flex-initial xl:flex-1 ...">
  4. Responsive flex item
  5. </div>
  6. <!-- ... -->
  7. </div>

Item that can grow or shrink if needed

Responsive flex item

Item that can grow or shrink if needed

Customizing

Flex Values

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. module.exports = {
  3. theme: {
  4. flex: {
  5. '1': '1 1 0%',
  6. auto: '1 1 auto',
  7. - initial: '0 1 auto',
  8. + inherit: 'inherit',
  9. none: 'none',
  10. + '2': '2 2 0%',
  11. }
  12. }
  13. }

Responsive and pseudo-class variants

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:

  1. // tailwind.config.js
  2. module.exports = {
  3. variants: {
  4. // ...
  5. - flex: ['responsive'],
  6. + flex: ['responsive', 'hover', 'focus'],
  7. }
  8. }

Disabling

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. }
  7. }

← Flex Wrap   Flex Grow →