csstailwind-cssstickytailwind-ui

Tailwind: Sticky while scrolling


I'm attempting to create a layout where I have an image that sticks to the left side of the viewport while scrolling until the content ends, at which point it should scroll to the next section.

However, despite trying various approaches, I can't seem to get the image to stick properly.

<div className="relative py-24 overflow-hidden bg-gray-900 sm:py-32">
      <div className="px-6 mx-auto max-w-7xl lg:px-8">
        <div className="grid max-w-2xl grid-cols-1 mx-auto gap-x-8 gap-y-16 sm:gap-y-20 lg:mx-0 lg:max-w-none lg:grid-cols-2">
          <div className="lg:ml-auto lg:pl-4 lg:pt-4">
            <div className="lg:max-w-lg">
              <h2 className="text-base font-semibold leading-7 text-indigo-600">Deploy faster</h2>
              <p className="mt-2 text-3xl font-bold tracking-tight text-gray-100 sm:text-4xl">A better workflow</p>
              <p className="mt-6 text-lg leading-8 text-gray-400">
                Lorem ipsum, dolor sit amet consectetur adipisicing elit. Maiores impedit perferendis suscipit eaque,
                iste dolor cupiditate blanditiis ratione.
              </p>
              <dl className="max-w-xl mt-10 space-y-8 text-base leading-7 text-gray-400 lg:max-w-none">
                {features.map((feature) => (
                  <div key={feature.name} className="relative pl-9">
                    <dt className="inline font-semibold text-gray-100">
                      <feature.icon className="absolute w-5 h-5 text-indigo-600 left-1 top-1" aria-hidden="true" />
                      {feature.name}
                    </dt>{' '}
                    <dd className="inline">{feature.description}</dd>
                  </div>
                ))}
              </dl>
              <dl className="max-w-xl mt-10 space-y-8 text-base leading-7 text-gray-400 lg:max-w-none">
                {features.map((feature) => (
                  <div key={feature.name} className="relative pl-9">
                    <dt className="inline font-semibold text-gray-100">
                      <feature.icon className="absolute w-5 h-5 text-indigo-600 left-1 top-1" aria-hidden="true" />
                      {feature.name}
                    </dt>{' '}
                    <dd className="inline">{feature.description}</dd>
                  </div>
                ))}
              </dl>
            </div>
          </div>
          <div className="flex items-start justify-end lg:order-first lg:sticky lg:top-4">
            <img
              src="https://tailwindui.com/img/component-images/dark-project-app-screenshot.png"
              alt="Product screenshot"
              className="w-[48rem] max-w-none rounded-xl shadow-xl ring-1 ring-gray-400/10 sm:w-[57rem]"
              width={2432}
              height={1442}
            />
          </div>
        </div>
      </div>
    </div>

I tried adding stick top-4 to the div enclosing the image but still it doesnt seem to be sticky when scroll.

enter image description here

Link: https://play.tailwindcss.com/Ur3gB0pIcj


Solution

  • First, you'd need to remove the overflow: hidden from the root <div>. This contributes to the sticky behavior not working (as per the MDN documentation):

    Note that a sticky element "sticks" to its nearest ancestor that has a "scrolling mechanism" (created when overflow is hidden, scroll, auto, or overlay), even if that ancestor isn't the nearest actually scrolling ancestor.

    Thus, we could consider adding lg:overflow-visible to the root <div> so that the <body> becomes the nearest ancestor.

    Furthermore, the sticky element would only be sticky until its bottom edge meets the bottom edge of its parent element. At the moment, the sticky element is the same height as the parent, so no stickiness occurs:

    enter image description here

    We can "shrink-wrap" the height of the sticky element to the content, thus allowing some stickiness to show by applying align-self: start via the lg:self-start Tailwind utility class:

    <script src="https://cdn.tailwindcss.com/3.4.3"></script>
    
    <div class="relative py-24 overflow-hidden bg-gray-900 sm:py-32 lg:overflow-visible">
      <div class="px-6 mx-auto max-w-7xl lg:px-8">
        <div
          class="grid max-w-2xl grid-cols-1 mx-auto gap-x-8 gap-y-16 sm:gap-y-20 lg:mx-0 lg:max-w-none lg:grid-cols-2"
        >
          <div class="lg:ml-auto lg:pl-4 lg:pt-4">
            <div class="lg:max-w-lg">
              <h2 class="text-base font-semibold leading-7 text-indigo-600">
                Deploy faster
              </h2>
              <p
                class="mt-2 text-3xl font-bold tracking-tight text-gray-100 sm:text-4xl"
              >
                A better workflow
              </p>
              <p class="mt-6 text-lg leading-8 text-gray-400">
                Lorem ipsum, dolor sit amet consectetur adipisicing elit. Maiores
                impedit perferendis suscipit eaque, iste dolor cupiditate blanditiis
                ratione.
              </p>
              <dl
                class="max-w-xl mt-10 space-y-8 text-base leading-7 text-gray-400 lg:max-w-none"
              >
                <div class="relative pl-9">
                  <dt class="inline font-semibold text-gray-100">
                    <svg
                      xmlns="http://www.w3.org/2000/svg"
                      viewBox="0 0 20 20"
                      fill="currentColor"
                      aria-hidden="true"
                      data-slot="icon"
                      class="absolute w-5 h-5 text-indigo-600 left-1 top-1"
                    >
                      <path
                        fill-rule="evenodd"
                        d="M5.5 17a4.5 4.5 0 0 1-1.44-8.765 4.5 4.5 0 0 1 8.302-3.046 3.5 3.5 0 0 1 4.504 4.272A4 4 0 0 1 15 17H5.5Zm3.75-2.75a.75.75 0 0 0 1.5 0V9.66l1.95 2.1a.75.75 0 1 0 1.1-1.02l-3.25-3.5a.75.75 0 0 0-1.1 0l-3.25 3.5a.75.75 0 1 0 1.1 1.02l1.95-2.1v4.59Z"
                        clip-rule="evenodd"
                      ></path></svg
                    >Push to deploy.
                  </dt>
                  <dd class="inline">
                    Lorem ipsum, dolor sit amet consectetur adipisicing elit.
                    Maiores impedit perferendis suscipit eaque, iste dolor
                    cupiditate blanditiis ratione.
                  </dd>
                </div>
                <div class="relative pl-9">
                  <dt class="inline font-semibold text-gray-100">
                    <svg
                      xmlns="http://www.w3.org/2000/svg"
                      viewBox="0 0 20 20"
                      fill="currentColor"
                      aria-hidden="true"
                      data-slot="icon"
                      class="absolute w-5 h-5 text-indigo-600 left-1 top-1"
                    >
                      <path
                        fill-rule="evenodd"
                        d="M10 1a4.5 4.5 0 0 0-4.5 4.5V9H5a2 2 0 0 0-2 2v6a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2v-6a2 2 0 0 0-2-2h-.5V5.5A4.5 4.5 0 0 0 10 1Zm3 8V5.5a3 3 0 1 0-6 0V9h6Z"
                        clip-rule="evenodd"
                      ></path></svg
                    >SSL certificates.
                  </dt>
                  <dd class="inline">
                    Anim aute id magna aliqua ad ad non deserunt sunt. Qui irure qui
                    lorem cupidatat commodo.
                  </dd>
                </div>
                <div class="relative pl-9">
                  <dt class="inline font-semibold text-gray-100">
                    <svg
                      xmlns="http://www.w3.org/2000/svg"
                      viewBox="0 0 20 20"
                      fill="currentColor"
                      aria-hidden="true"
                      data-slot="icon"
                      class="absolute w-5 h-5 text-indigo-600 left-1 top-1"
                    >
                      <path
                        d="M4.632 3.533A2 2 0 0 1 6.577 2h6.846a2 2 0 0 1 1.945 1.533l1.976 8.234A3.489 3.489 0 0 0 16 11.5H4c-.476 0-.93.095-1.344.267l1.976-8.234Z"
                      ></path>
                      <path
                        fill-rule="evenodd"
                        d="M4 13a2 2 0 1 0 0 4h12a2 2 0 1 0 0-4H4Zm11.24 2a.75.75 0 0 1 .75-.75H16a.75.75 0 0 1 .75.75v.01a.75.75 0 0 1-.75.75h-.01a.75.75 0 0 1-.75-.75V15Zm-2.25-.75a.75.75 0 0 0-.75.75v.01c0 .414.336.75.75.75H13a.75.75 0 0 0 .75-.75V15a.75.75 0 0 0-.75-.75h-.01Z"
                        clip-rule="evenodd"
                      ></path></svg
                    >Database backups.
                  </dt>
                  <dd class="inline">
                    Ac tincidunt sapien vehicula erat auctor pellentesque rhoncus.
                    Et magna sit morbi lobortis.
                  </dd>
                </div>
              </dl>
              <dl
                class="max-w-xl mt-10 space-y-8 text-base leading-7 text-gray-400 lg:max-w-none"
              >
                <div class="relative pl-9">
                  <dt class="inline font-semibold text-gray-100">
                    <svg
                      xmlns="http://www.w3.org/2000/svg"
                      viewBox="0 0 20 20"
                      fill="currentColor"
                      aria-hidden="true"
                      data-slot="icon"
                      class="absolute w-5 h-5 text-indigo-600 left-1 top-1"
                    >
                      <path
                        fill-rule="evenodd"
                        d="M5.5 17a4.5 4.5 0 0 1-1.44-8.765 4.5 4.5 0 0 1 8.302-3.046 3.5 3.5 0 0 1 4.504 4.272A4 4 0 0 1 15 17H5.5Zm3.75-2.75a.75.75 0 0 0 1.5 0V9.66l1.95 2.1a.75.75 0 1 0 1.1-1.02l-3.25-3.5a.75.75 0 0 0-1.1 0l-3.25 3.5a.75.75 0 1 0 1.1 1.02l1.95-2.1v4.59Z"
                        clip-rule="evenodd"
                      ></path></svg
                    >Push to deploy.
                  </dt>
                  <dd class="inline">
                    Lorem ipsum, dolor sit amet consectetur adipisicing elit.
                    Maiores impedit perferendis suscipit eaque, iste dolor
                    cupiditate blanditiis ratione.
                  </dd>
                </div>
                <div class="relative pl-9">
                  <dt class="inline font-semibold text-gray-100">
                    <svg
                      xmlns="http://www.w3.org/2000/svg"
                      viewBox="0 0 20 20"
                      fill="currentColor"
                      aria-hidden="true"
                      data-slot="icon"
                      class="absolute w-5 h-5 text-indigo-600 left-1 top-1"
                    >
                      <path
                        fill-rule="evenodd"
                        d="M10 1a4.5 4.5 0 0 0-4.5 4.5V9H5a2 2 0 0 0-2 2v6a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2v-6a2 2 0 0 0-2-2h-.5V5.5A4.5 4.5 0 0 0 10 1Zm3 8V5.5a3 3 0 1 0-6 0V9h6Z"
                        clip-rule="evenodd"
                      ></path></svg
                    >SSL certificates.
                  </dt>
                  <dd class="inline">
                    Anim aute id magna aliqua ad ad non deserunt sunt. Qui irure qui
                    lorem cupidatat commodo.
                  </dd>
                </div>
                <div class="relative pl-9">
                  <dt class="inline font-semibold text-gray-100">
                    <svg
                      xmlns="http://www.w3.org/2000/svg"
                      viewBox="0 0 20 20"
                      fill="currentColor"
                      aria-hidden="true"
                      data-slot="icon"
                      class="absolute w-5 h-5 text-indigo-600 left-1 top-1"
                    >
                      <path
                        d="M4.632 3.533A2 2 0 0 1 6.577 2h6.846a2 2 0 0 1 1.945 1.533l1.976 8.234A3.489 3.489 0 0 0 16 11.5H4c-.476 0-.93.095-1.344.267l1.976-8.234Z"
                      ></path>
                      <path
                        fill-rule="evenodd"
                        d="M4 13a2 2 0 1 0 0 4h12a2 2 0 1 0 0-4H4Zm11.24 2a.75.75 0 0 1 .75-.75H16a.75.75 0 0 1 .75.75v.01a.75.75 0 0 1-.75.75h-.01a.75.75 0 0 1-.75-.75V15Zm-2.25-.75a.75.75 0 0 0-.75.75v.01c0 .414.336.75.75.75H13a.75.75 0 0 0 .75-.75V15a.75.75 0 0 0-.75-.75h-.01Z"
                        clip-rule="evenodd"
                      ></path></svg
                    >Database backups.
                  </dt>
                  <dd class="inline">
                    Ac tincidunt sapien vehicula erat auctor pellentesque rhoncus.
                    Et magna sit morbi lobortis.
                  </dd>
                </div>
              </dl>
            </div>
          </div>
          <div
            class="flex items-start justify-end lg:order-first lg:sticky lg:top-0 lg:self-start"
          >
            <img
              src="https://tailwindui.com/img/component-images/dark-project-app-screenshot.png"
              alt="Product screenshot"
              class="w-[48rem] max-w-none rounded-xl shadow-xl ring-1 ring-gray-400/10 sm:w-[57rem]"
              width="2432"
              height="1442"
            />
          </div>
        </div>
      </div>
    </div>