htmlcssflexboxtailwind-css

basis-[800px] prevent element from shrinking in flex layout


I have the following element:

    <div className='mx-auto flex flex-col-reverse gap-5 md:flex-row md:items-start'>
      <div className='flex min-w-0 shrink grow-0 basis-[800px] flex-col'>
        ...
      </div>
        <StickyPanel className='flex min-w-0 shrink-[3] grow basis-0 flex-col gap-2'>
          <RightBar />
        </StickyPanel>
      
    </div>

My expectation was:

Unfortunately, only the second element shrinks while the first one have alway 800px. When I switch to % basis values like basis-[70%] and basis-[30%] then it works and both elements shrinks down. How to fix that? What am I missing?


Solution

  • The behavior you're actually seeing is the <StickyPanel> growing less as the viewport shrinks in width. The calculations of the layout is as follows:

    Thus, if you want a shrinking ratio, you should set an appropriate basis on <StickyPanel>, say 200px and remove the shrink-[3]. This is because it's easier to reason with one variable, flex-basis, than adding flex-shrink ratios aswell into the mix.

    <script src="https://unpkg.com/@tailwindcss/browser@4.1.10"></script>
    
    <div class='mx-auto flex flex-col-reverse gap-5 md:flex-row md:items-start'>
      <div class='flex min-w-0 shrink grow-0 basis-[800px] flex-col'>
        ...
      </div>
      <div class='flex min-w-0 grow basis-[200px] flex-col gap-2'>
        <RightBar>RightBar</RightBar>
      </div>
    </div>