Published on

Practical Guide: Create A Pan-Zoom Control With Tailwind CSS

Pan-Zoom Control

What is Tailwind CSS?

Tailwind CSS is a utility-first CSS framework that focuses on creating personalized user interfaces quickly. It can gives you all the building blocks you are able to create personalized designs without having to fight to override irritating opinionated styles. Also, Tailwind CSS is a highly configurable, low-level CSS framework.

The description of Pan-Zoom Control ui component

Custom pan-zoom canvas control template

Why use Tailwind CSS to build a Pan-Zoom Control ui component?

  • It can make the building process of Pan-Zoom Control ui component faster and more easily.
  • Enables building complex responsive layouts and components freely.
  • Minimum lines of CSS code in Pan-Zoom Control component file.

The preview of Pan-Zoom Control ui component

Free download of the Pan-Zoom Control's source code

The source code of Pan-Zoom Control ui component

<!-- MDI Icons -->
<link rel="stylesheet" href="https://cdn.materialdesignicons.com/6.5.95/css/materialdesignicons.min.css">


<h1 class="text-center text-3xl font-bold my-20">
    < Canvas Container >
</h1>

<div class="flex">
    <span class="text-center font-bold my-20 mx-auto">
        Pan-zoom icons required MDI (npm i @mdi/font)

        <hr class="my-4">

        <a href="https://egoistdeveloper.github.io/twcss-to-sass-playground/" target="_blank" class="text-blue-600">
            Convetert to SASS
        </a>
    </span>
</div>

<!-- Panzoom Area -->
<div class="flex fixed right-2 bottom-2 cursor-default">
    <!-- Minimap -->
    <div class="ngn-minimap">
        <div class="flex-initial relative
            bg-white bg-opacity-80
            rounded shadow
            p-2 py-5 mr-2">
            <div class="h-full w-full">
                <div class="bg-gray-50 border border-gray-200 
                    py-24 px-5">
                    Add some minimap
                </div>
            </div>
        </div>
    </div>

    <!-- Pan Controls -->
    <div class="flex-initial bg-white bg-opacity-90
        rounded shadow p-2 py-2
        cursor-default flex flex-col
        hover:bg-opacity-100
        lg:bottom-3 lg:right-3
        transition duration-200 ease-in-out">

    <!-- Pan Only -->
    <button class="w-9 h-9 min-w-min
            rounded-full text-lg mb-2
            bg-gray-300 bg-opacity-30
            border border-gray-300 border-opacity-30
            hover:bg-yellow-400 hover:bg-opacity-50
            outline-none hover:shadow-md
            transition duration-200 ease-in-out"
        title="Pan-only Mode">
        <i class="mdi mdi-focus-field"></i>
    </button>

    <!-- Pan Reset -->
    <button class="w-9 h-9 min-w-min
            rounded-full text-lg mb-2
            bg-gray-300 bg-opacity-30
            border border-gray-300 border-opacity-30
            hover:bg-yellow-400 hover:bg-opacity-50
            outline-none hover:shadow-md
            transition duration-200 ease-in-out"
        title="Reset Pan View">
        <i class="mdi mdi-image-filter-center-focus"></i>
    </button>

    <!-- Zoom Controls-->
    <div class="flex flex-col">
        <button class="w-9 h-9 min-w-min
            rounded-none rounded-t-full text-lg
            bg-gray-300 bg-opacity-30
            border border-gray-300 border-opacity-30
            hover:bg-yellow-400 hover:bg-opacity-50
            outline-none hover:shadow-md
            transition duration-200 ease-in-out
            mb-0 border
            border-gray-300 border-opacity-30
            hover:border-yellow-200"
        title="Zoom In">
        <i class="mdi mdi-minus"></i>
        </button>

        <div class="bg-gray-300 bg-opacity-30
            border border-b border-gray-300 border-opacity-30
            font-medium py-2 cursor-default
            w-9 h-9 min-w-min
            text-xs text-center
            inline-block align-baseline">
            123%
        </div>

        <button class="w-9 h-9 min-w-min
            rounded-none rounded-b-full text-lg
            bg-gray-300 bg-opacity-30
            border border-gray-300 border-opacity-30
            hover:bg-yellow-400 hover:bg-opacity-50
            outline-none hover:shadow-md
            transition duration-200 ease-in-out
            mb-0 border
            border-gray-300 border-opacity-30
            hover:border-yellow-200"
        title="Zoom Out">
        <i class="mdi mdi-plus"></i>
        </button>
    </div>

    <!-- Show Minimap -->
    <button class="w-9 h-9 min-w-min
            rounded-full text-lg mt-2
            bg-gray-300 bg-opacity-30
            border border-gray-300 border-opacity-30
            hover:bg-yellow-400 hover:bg-opacity-50
            outline-none hover:shadow-md
            transition duration-200 ease-in-out"
        title="Show Mini Map">
        <i class="mdi mdi-map-outline"></i>
    </button>
    </div>
</div>

How to build a Pan-Zoom Control with Tailwind CSS?

Install tailwind css of verion 2.2.19

Use the script html tag to import the script of Tailwind CSS of the version 2.2.19

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

All the unility class needed to build a Pan-Zoom Control component

  • text-center
  • text-3xl
  • my-20
  • flex
  • mx-auto
  • my-4
  • text-blue-600
  • fixed
  • right-2
  • bottom-2
  • flex-initial
  • bg-white
  • p-2
  • py-5
  • mr-2
  • h-full
  • w-full
  • bg-gray-50
  • border-gray-200
  • py-24
  • px-5
  • lg:bottom-3
  • w-9
  • h-9
  • text-lg
  • bg-gray-300
  • border-gray-300
  • hover:bg-yellow-400
  • flex-col
  • mb-0
  • hover:border-yellow-200
  • border-b
  • py-2
  • text-xs
  • inline-block

35 steps to build a Pan-Zoom Control component with Tailwind CSS

  1. Control the text color of an element to center using the text-center utilities.

  2. Control the text color of an element to 3xl using the text-3xl utilities.

  3. Control the vertical margin of an element to 5rem using the my-20 utilities.

  4. Use flex to create a block-level flex container.

  5. Control the horizontal margin of an element to auto using the mx-auto utilities.

  6. Control the vertical margin of an element to 1rem using the my-4 utilities.

  7. Control the text color of an element to blue-600 using the text-blue-600 utilities.

  8. Use fixed to position an element relative to the browser window.

  9. Use the right-2 utilities to set the right position of a positioned element to 0.5rem.

  10. Use the bottom-2 utilities to set the bottom position of a positioned element to 0.5rem.

  11. Use flex to create a block-level flex container.

  12. Control the background color of an element to white using the bg-white utilities.

  13. Control the padding on all sides of an element to 0.5rem using the p-2 utilities.

  14. Control the vertical padding of an element to 1.25rem using the py-5 utilities.

  15. Control the margin on right side of an element to 0.5rem using the mr-2 utilities.

  16. Use h-full to set an element’s height to 100% of its parent, as long as the parent has a defined height.

  17. Use w-full to set an element to a 100% based width.

  18. Control the background color of an element to gray-50 using the bg-gray-50 utilities.

  19. Control the border color of an element to gray-200 using the border-gray-200 utilities.

  20. Control the vertical padding of an element to 6rem using the py-24 utilities.

  21. Control the horizontal padding of an element to 1.25rem using the px-5 utilities.

  22. Use the lg:bottom-3 utilities to set the bottom position of a positioned element to 0.75rem at only large screen sizes.

  23. Use w-9 to set an element to a fixed width(2.25rem).

  24. Use h-9 to set an element to a fixed height(2.25rem).

  25. Control the text color of an element to lg using the text-lg utilities.

  26. Control the background color of an element to gray-300 using the bg-gray-300 utilities.

  27. Control the border color of an element to gray-300 using the border-gray-300 utilities.

  28. Control the background color of an element to yellow-400 using the hover:bg-yellow-400 utilities on hover.

  29. Use flex to create a block-level flex container.

  30. Control the margin on bottom side of an element to 0rem using the mb-0 utilities.

  31. Control the border color of an element to yellow-200 using the hover:border-yellow-200 utilities on hover.

  32. Control the border color of an element to b using the border-b utilities.

  33. Control the vertical padding of an element to 0.5rem using the py-2 utilities.

  34. Control the text color of an element to xs using the text-xs utilities.

  35. Use inline-block utilities to wrap the element to prevent the text inside from extending beyond its parent.

Conclusion

The above is a step-by-step tutorial on how to use Tailwind CSS to build a Pan-Zoom Control components, learn and follow along to implement your own components.