Home > database >  Tailwind - keep header and left/right sidebar sticky on scroll
Tailwind - keep header and left/right sidebar sticky on scroll

Time:09-01

How do I keep my header, left/right sidebar sticky on scroll (on desktop, not mobile)?

It doesn't seem to be working with the fixed or sticky class, I've posted an example here: https://play.tailwindcss.com/Bj68nUJj1C.

<!-- Background color split screen for large screens -->
<div  aria-hidden="true"></div>
<div  aria-hidden="true"></div>
<div >
  <!-- Navbar -->
  <nav >
    <div >
      <div >
        <!-- Logo section -->
        <div >
          <div >
            <img  src="https://tailwindui.com/img/logos/workflow-mark.svg?color=indigo&shade=300" alt="Workflow" />
          </div>
        </div>

        <!-- Search section -->
        <div >
          <div >
            <label for="search" >Search projects</label>
            <div >
              <div >
                <!-- Heroicon name: mini/magnifying-glass -->
                <svg  xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
                  <path fill-rule="evenodd" d="M9 3.5a5.5 5.5 0 100 11 5.5 5.5 0 000-11zM2 9a7 7 0 1112.452 4.391l3.328 3.329a.75.75 0 11-1.06 1.06l-3.329-3.328A7 7 0 012 9z" clip-rule="evenodd" />
                </svg>
              </div>
              <input id="search" name="search"  placeholder="Search projects" type="search" />
            </div>
          </div>
        </div>
        <div >
          <!-- Mobile menu button -->
          <button type="button"  aria-controls="mobile-menu" aria-expanded="false">
            <span >Open main menu</span>
            <!--
              Icon when menu is closed.

              Heroicon name: outline/bars-3-center-left

              Menu open: "hidden", Menu closed: "block"
            -->
            <svg  xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true">
              <path stroke-linecap="round" stroke-linejoin="round" d="M3.75 6.75h16.5M3.75 12H12m-8.25 5.25h16.5" />
            </svg>
            <!--
              Icon when menu is open.

              Heroicon name: outline/x-mark

              Menu open: "block", Menu closed: "hidden"
            -->
            <svg  xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true">
              <path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12" />
            </svg>
          </button>
        </div>
        <!-- Links section -->
        <div >
          <div >
            <div >
              <a href="#" >Documentation</a>
              <a href="#" >Support</a>
            </div>
            <!-- Profile dropdown -->
            <div >
              <div>
                <button type="button"  id="user-menu-button" aria-expanded="false" aria-haspopup="true">
                  <span >Open user menu</span>
                  <img  src="https://images.unsplash.com/photo-1517365830460-955ce3ccd263?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=256&h=256&q=80" alt="" />
                </button>
              </div>

              <!--
                Dropdown menu, show/hide based on menu state.

                Entering: "transition ease-out duration-100"
                  From: "transform opacity-0 scale-95"
                  To: "transform opacity-100 scale-100"
                Leaving: "transition ease-in duration-75"
                  From: "transform opacity-100 scale-100"
                  To: "transform opacity-0 scale-95"
              -->
              <div  role="menu" aria-orientation="vertical" aria-labelledby="user-menu-button" tabindex="-1">
                <!-- Active: "bg-gray-100", Not Active: "" -->
                <a href="#"  role="menuitem" tabindex="-1" id="user-menu-item-0">View Profile</a>
                <a href="#"  role="menuitem" tabindex="-1" id="user-menu-item-1">Settings</a>
                <a href="#"  role="menuitem" tabindex="-1" id="user-menu-item-2">Logout</a>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </nav>

  <!-- 3 column wrapper -->
  <div >
    <!-- Left sidebar & main wrapper -->
    <div >
      <div >
        <div >
          <!-- Start left column area -->
          <div  style="min-height: 12rem">
            <div ></div>
          </div>
          <!-- End left column area -->
        </div>
      </div>

      <div >
        <div >
          <!-- Start main area-->
          <div  style="min-height: 36rem">
            <div ></div>
          </div>
          <!-- End main area -->
        </div>
      </div>
    </div>

    <div >
      <div >
        <!-- Start right column area -->
        <div  style="min-height: 16rem">
          <div ></div>
        </div>
        <!-- End right column area -->
      </div>
    </div>
  </div>
</div>

CodePudding user response:

You need to use top-X class together with sticky. For navbar sticky top-0 and for sidebar something like sticky top-20 should work.

Prefix it with corresponding breakpoint prefix to apply it only for bigger screens.

CodePudding user response:

I try to solve your problem, you can check here

CodePudding user response:

You can try using grid instead of flex and change your layout a bit to achieve this.

<script src="https://cdn.tailwindcss.com"></script>

<div  aria-hidden="true"></div>
<div  aria-hidden="true"></div>
<div >
  <!-- Navbar -->
  <nav >
    Nav
  </nav>

  <aside >
    <div >
      <!-- Start left column area -->
      <div  style="min-height: 12rem">
        <div >
          Aside
        </div>
      </div>
      <!-- End left column area -->
    </div>
  </aside>

  <main >
    <div >
      <!-- Start main area-->
      <div  style="min-height: 36rem">
        <div >
          Main
        </div>
      </div>
      <!-- End main area -->
    </div>
  </main>

  <aside >
    <div >
      <!-- Start right column area -->
      <div  style="min-height: 16rem">
        <div >
          Aside
        </div>
      </div>
      <!-- End right column area -->
    </div>
  </aside>
</div>

  • Related