Published on

6 Easy Ways To Build A Wysiwyg Editor With Tailwind CSS

Wysiwyg Editor

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 Wysiwyg Editor ui component

Get started with a custom wysiwyg editor components from flowbite built with tailwind css to add multiple actions to a textarea element flowbite.com/docs/forms/textarea/#wysiwyg-editor

Why use Tailwind CSS to build a Wysiwyg Editor ui component?

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

The preview of Wysiwyg Editor ui component

Free download of the Wysiwyg Editor's source code

The source code of Wysiwyg Editor ui component

<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/flowbite.min.css" />

<!-- This is an example component -->
<div class="max-w-2xl mx-auto">

	<form>
    <div class="mb-4 w-full bg-gray-50 rounded-lg border border-gray-200 dark:bg-gray-700 dark:border-gray-600">
        <div class="flex justify-between items-center py-2 px-3 border-b dark:border-gray-600">
            <div class="flex flex-wrap items-center divide-gray-200 sm:divide-x dark:divide-gray-600">
                <div class="flex items-center space-x-1 sm:pr-4">
                    <button type="button" class="p-2 text-gray-500 rounded cursor-pointer hover:text-gray-900 hover:bg-gray-100 dark:text-gray-400 dark:hover:text-white dark:hover:bg-gray-600">
                        <svg class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M8 4a3 3 0 00-3 3v4a5 5 0 0010 0V7a1 1 0 112 0v4a7 7 0 11-14 0V7a5 5 0 0110 0v4a3 3 0 11-6 0V7a1 1 0 012 0v4a1 1 0 102 0V7a3 3 0 00-3-3z" clip-rule="evenodd"></path></svg>
                    </button>
                    <button type="button" class="p-2 text-gray-500 rounded cursor-pointer hover:text-gray-900 hover:bg-gray-100 dark:text-gray-400 dark:hover:text-white dark:hover:bg-gray-600">
                        <svg class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M5.05 4.05a7 7 0 119.9 9.9L10 18.9l-4.95-4.95a7 7 0 010-9.9zM10 11a2 2 0 100-4 2 2 0 000 4z" clip-rule="evenodd"></path></svg>
                    </button>
                    <button type="button" class="p-2 text-gray-500 rounded cursor-pointer hover:text-gray-900 hover:bg-gray-100 dark:text-gray-400 dark:hover:text-white dark:hover:bg-gray-600">
                        <svg class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M4 3a2 2 0 00-2 2v10a2 2 0 002 2h12a2 2 0 002-2V5a2 2 0 00-2-2H4zm12 12H4l4-8 3 6 2-4 3 6z" clip-rule="evenodd"></path></svg>
                    </button>
                    <button type="button" class="p-2 text-gray-500 rounded cursor-pointer hover:text-gray-900 hover:bg-gray-100 dark:text-gray-400 dark:hover:text-white dark:hover:bg-gray-600">
                        <svg class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M12.316 3.051a1 1 0 01.633 1.265l-4 12a1 1 0 11-1.898-.632l4-12a1 1 0 011.265-.633zM5.707 6.293a1 1 0 010 1.414L3.414 10l2.293 2.293a1 1 0 11-1.414 1.414l-3-3a1 1 0 010-1.414l3-3a1 1 0 011.414 0zm8.586 0a1 1 0 011.414 0l3 3a1 1 0 010 1.414l-3 3a1 1 0 11-1.414-1.414L16.586 10l-2.293-2.293a1 1 0 010-1.414z" clip-rule="evenodd"></path></svg>
                    </button>
                    <button type="button" class="p-2 text-gray-500 rounded cursor-pointer hover:text-gray-900 hover:bg-gray-100 dark:text-gray-400 dark:hover:text-white dark:hover:bg-gray-600">
                        <svg class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM7 9a1 1 0 100-2 1 1 0 000 2zm7-1a1 1 0 11-2 0 1 1 0 012 0zm-.464 5.535a1 1 0 10-1.415-1.414 3 3 0 01-4.242 0 1 1 0 00-1.415 1.414 5 5 0 007.072 0z" clip-rule="evenodd"></path></svg>
                    </button>
                </div>
                <div class="flex flex-wrap items-center space-x-1 sm:pl-4">
                    <button type="button" class="p-2 text-gray-500 rounded cursor-pointer hover:text-gray-900 hover:bg-gray-100 dark:text-gray-400 dark:hover:text-white dark:hover:bg-gray-600">
                        <svg class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M3 4a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zm0 4a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zm0 4a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zm0 4a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1z" clip-rule="evenodd"></path></svg>
                    </button>
                    <button type="button" class="p-2 text-gray-500 rounded cursor-pointer hover:text-gray-900 hover:bg-gray-100 dark:text-gray-400 dark:hover:text-white dark:hover:bg-gray-600">
                        <svg class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M11.49 3.17c-.38-1.56-2.6-1.56-2.98 0a1.532 1.532 0 01-2.286.948c-1.372-.836-2.942.734-2.106 2.106.54.886.061 2.042-.947 2.287-1.561.379-1.561 2.6 0 2.978a1.532 1.532 0 01.947 2.287c-.836 1.372.734 2.942 2.106 2.106a1.532 1.532 0 012.287.947c.379 1.561 2.6 1.561 2.978 0a1.533 1.533 0 012.287-.947c1.372.836 2.942-.734 2.106-2.106a1.533 1.533 0 01.947-2.287c1.561-.379 1.561-2.6 0-2.978a1.532 1.532 0 01-.947-2.287c.836-1.372-.734-2.942-2.106-2.106a1.532 1.532 0 01-2.287-.947zM10 13a3 3 0 100-6 3 3 0 000 6z" clip-rule="evenodd"></path></svg>       
                    </button>
                    <button type="button" class="p-2 text-gray-500 rounded cursor-pointer hover:text-gray-900 hover:bg-gray-100 dark:text-gray-400 dark:hover:text-white dark:hover:bg-gray-600">
                        <svg class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M6 2a1 1 0 00-1 1v1H4a2 2 0 00-2 2v10a2 2 0 002 2h12a2 2 0 002-2V6a2 2 0 00-2-2h-1V3a1 1 0 10-2 0v1H7V3a1 1 0 00-1-1zm0 5a1 1 0 000 2h8a1 1 0 100-2H6z" clip-rule="evenodd"></path></svg>     
                    </button>
                    <button type="button" class="p-2 text-gray-500 rounded cursor-pointer hover:text-gray-900 hover:bg-gray-100 dark:text-gray-400 dark:hover:text-white dark:hover:bg-gray-600">
                        <svg class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M3 17a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zm3.293-7.707a1 1 0 011.414 0L9 10.586V3a1 1 0 112 0v7.586l1.293-1.293a1 1 0 111.414 1.414l-3 3a1 1 0 01-1.414 0l-3-3a1 1 0 010-1.414z" clip-rule="evenodd"></path></svg>
                    </button>
                </div>
            </div>
            <button type="button" data-tooltip-target="tooltip-fullscreen" class="p-2 text-gray-500 rounded cursor-pointer sm:ml-auto hover:text-gray-900 hover:bg-gray-100 dark:text-gray-400 dark:hover:text-white dark:hover:bg-gray-600">
                <svg class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M3 4a1 1 0 011-1h4a1 1 0 010 2H6.414l2.293 2.293a1 1 0 11-1.414 1.414L5 6.414V8a1 1 0 01-2 0V4zm9 1a1 1 0 010-2h4a1 1 0 011 1v4a1 1 0 01-2 0V6.414l-2.293 2.293a1 1 0 11-1.414-1.414L13.586 5H12zm-9 7a1 1 0 012 0v1.586l2.293-2.293a1 1 0 111.414 1.414L6.414 15H8a1 1 0 010 2H4a1 1 0 01-1-1v-4zm13-1a1 1 0 011 1v4a1 1 0 01-1 1h-4a1 1 0 010-2h1.586l-2.293-2.293a1 1 0 111.414-1.414L15 13.586V12a1 1 0 011-1z" clip-rule="evenodd"></path></svg>
            </button>
            <div id="tooltip-fullscreen" role="tooltip" class="inline-block absolute invisible z-10 py-2 px-3 text-sm font-medium text-white bg-gray-900 rounded-lg shadow-sm opacity-0 transition-opacity duration-300 tooltip dark:bg-gray-700">
                Show full screen
                <div class="tooltip-arrow" data-popper-arrow></div>
            </div>
        </div>
        <div class="py-2 px-4 bg-white rounded-b-lg dark:bg-gray-800">
            <label for="editor" class="sr-only">Publish post</label>
            <textarea id="editor" rows="8" class="block px-0 w-full text-sm text-gray-800 bg-white border-0 dark:bg-gray-800 focus:ring-0 dark:text-white dark:placeholder-gray-400" placeholder="Write an article..." required></textarea>
        </div>
    </div>
    <button type="submit" class="inline-flex items-center px-5 py-2.5 text-sm font-medium text-center text-white bg-blue-700 rounded-lg focus:ring-4 focus:ring-blue-200 dark:focus:ring-blue-900 hover:bg-blue-800">
        Publish post
    </button>
    </form>

	<p class="mt-5">This wysiwyg editor component is part of a larger, open-source library of Tailwind CSS components. Learn
		more
		by going to the official <a class="text-blue-600 hover:underline"
			href="https://flowbite.com/docs/getting-started/introduction/" target="_blank">Flowbite Documentation</a>.
	</p>
    <script src="https://unpkg.com/[email protected]/dist/flowbite.js"></script>
</div>

How to build a Wysiwyg Editor with Tailwind CSS?

Install tailwind css of verion 3.0.18

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

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

All the unility class needed to build a Wysiwyg Editor component

  • max-w-2xl
  • mx-auto
  • mb-4
  • w-full
  • bg-gray-50
  • border-gray-200
  • dark:bg-gray-700
  • dark:border-gray-600
  • flex
  • py-2
  • px-3
  • border-b
  • flex-wrap
  • sm:pr-4
  • p-2
  • text-gray-500
  • hover:text-gray-900
  • hover:bg-gray-100
  • dark:text-gray-400
  • w-5
  • h-5
  • sm:pl-4
  • sm:ml-auto
  • inline-block
  • absolute
  • invisible
  • z-10
  • text-sm
  • text-white
  • bg-gray-900
  • px-4
  • bg-white
  • dark:bg-gray-800
  • block
  • px-0
  • text-gray-800
  • border-0
  • dark:text-white
  • inline-flex
  • px-5
  • py-2.5
  • text-center
  • bg-blue-700
  • hover:bg-blue-800
  • mt-5
  • text-blue-600

46 steps to build a Wysiwyg Editor component with Tailwind CSS

  1. Set the maximum width/height of an element using the max-w-2xl utilities.

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

  3. Control the margin on bottom side of an element to 1rem using the mb-4 utilities.

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

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

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

  7. Control the background color of an element to gray-700 using the dark:bg-gray-700 utilities in dark theme.

  8. Control the border color of an element to gray-600 using the dark:border-gray-600 utilities in dark theme.

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

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

  11. Control the horizontal padding of an element to 0.75rem using the px-3 utilities.

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

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

  14. Control the padding on right side of an element to 1rem at only small screen sizes using the sm:pr-4 utilities.

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

  16. Control the text color of an element to gray-500 using the text-gray-500 utilities.

  17. Control the text color of an element to gray-900 on hover using the hover:text-gray-900 utilities.

  18. Control the background color of an element to gray-100 using the hover:bg-gray-100 utilities on hover.

  19. Control the text color of an element to gray-400 in dark theme using the dark:text-gray-400 utilities.

  20. Use w-5 to set an element to a fixed width(1.25rem).

  21. Use h-5 to set an element to a fixed height(1.25rem).

  22. Adjust the left padding of an element to 1rem at only small screen sizes using the sm:pl-4 utilities class

  23. Control the margin on left side of an element to auto at only small screen sizes using the sm:ml-auto utilities.

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

  25. 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.

  26. Use invisible to hide an element, but still maintain its place in the DOM, affecting the layout of other elements.

  27. Control the stack order (or three-dimensional positioning) of an element to 10 in Tailwind, regardless of order it has been displayed, using the z-10 utilities.

  28. Control the text color of an element to sm using the text-sm utilities.

  29. Control the text color of an element to white using the text-white utilities.

  30. Control the background color of an element to gray-900 using the bg-gray-900 utilities.

  31. Control the horizontal padding of an element to 1rem using the px-4 utilities.

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

  33. Control the background color of an element to gray-800 using the dark:bg-gray-800 utilities in dark theme.

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

  35. Control the horizontal padding of an element to 0rem using the px-0 utilities.

  36. Control the text color of an element to gray-800 using the text-gray-800 utilities.

  37. Control the border color of an element to 0rem using the border-0 utilities.

  38. Control the text color of an element to white in dark theme using the dark:text-white utilities.

  39. Use inline-flex to create an inline flex container that flows with text.

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

  41. Control the vertical padding of an element to 2.5 using the py-2.5 utilities.

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

  43. Control the background color of an element to blue-700 using the bg-blue-700 utilities.

  44. Control the background color of an element to blue-800 using the hover:bg-blue-800 utilities on hover.

  45. Control the margin on top side of an element to 1.25rem using the mt-5 utilities.

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

Conclusion

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