Published on

Learn How To Make A Dark mode toggle With Tailwind CSS from the Pros

Dark mode toggle

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 Dark mode toggle ui component

Dark mode toggle

Why use Tailwind CSS to make a Dark mode toggle ui component?

  • It can make the building process of Dark mode toggle ui component faster and more easily.
  • Enables building complex responsive layouts and components freely.
  • Minimum lines of CSS code in Dark mode toggle component file.

The preview of Dark mode toggle ui component

Free download of the Dark mode toggle's source code

The source code of Dark mode toggle ui component

<style>
    .toggle-checkbox:checked {
        right: 0;
        border: none;
        background-color: rgb(129 124 214);
        background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' className='h-10 w-10' fill='%23FFF' viewBox='0 0 24 24' stroke='rgb(129 124 214)' > <path strokeLinecap='round' strokeLinejoin='round' d='M20.354 15.354A9 9 0 018.646 3.646 9.003 9.003 0 0012 21a9.003 9.003 0 008.354-5.646z' /> </svg>");
    }
    .toggle-checkbox:checked + .toggle-label {
        background-color: rgb(76 73 188);
    }   
</style>
<div class='h-screen flex items-center justify-center'>
    <div
            class="relative inline-block w-12 mr-2 align-middle select-none transition duration-200 ease-in"
    >
        <input
                type="checkbox"
                name="toggle"
                id="toggle"
                class="bg-yellow-300 border-yellow-500 mr-1 focus:ring-transparent toggle-checkbox absolute block w-6 h-6 rounded-full border-2 appearance-none cursor-pointer"
        />
        <label
                for="toggle"
                class="toggle-label block h-8 -ml-1 -mt-1 rounded-full bg-green-400 cursor-pointer"
        ></label>
    </div>
</div>

How to make a Dark mode toggle with Tailwind CSS?

Install tailwind css of verion 2.2.4

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

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

All the unility class needed to make a Dark mode toggle component

  • relative
  • inline-block
  • w-12
  • mr-2
  • bg-yellow-300
  • border-yellow-500
  • mr-1
  • absolute
  • block
  • w-6
  • h-6
  • border-2
  • h-8
  • -ml-1
  • -mt-1
  • bg-green-400

16 steps to make a Dark mode toggle component with Tailwind CSS

  1. Use relative to position an element according to the normal flow of the document.

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

  3. Use w-12 to set an element to a fixed width(3rem).

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

  5. Control the background color of an element to yellow-300 using the bg-yellow-300 utilities.

  6. Control the border color of an element to yellow-500 using the border-yellow-500 utilities.

  7. Control the margin on right side of an element to 0.25rem using the mr-1 utilities.

  8. Use absolute to position an element outside of the normal flow of the document, causing neighboring elements to act as if the element doesn’t exist.

  9. Use inline utilities to put the element on its own line and fill its parent.

  10. Use w-6 to set an element to a fixed width(1.5rem).

  11. Use h-6 to set an element to a fixed height(1.5rem).

  12. Control the border color of an element to 0.5rem using the border-2 utilities.

  13. Use h-8 to set an element to a fixed height(2rem).

  14. Control the margin on left side of an element to -0.25rem using the -ml-1 utilities.

  15. Control the margin on top side of an element to -0.25rem using the -mt-1 utilities.

  16. Control the background color of an element to green-400 using the bg-green-400 utilities.

Conclusion

The above is a step-by-step tutorial on how to use Tailwind CSS to make a Dark mode toggle components, learn and follow along to implement your own components.