Published on

What You Need To Create A Toast With Tailwind CSS

Toast

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 Toast ui component

Use this tailwind css toast component to show message alerts and push notifications to your users flowbite.com/docs/components/toast/

Why use Tailwind CSS to create a Toast ui component?

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

The preview of Toast ui component

Free download of the Toast's source code

The source code of Toast ui component

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

	<div id="toast-default"
		class="flex items-center w-full max-w-xs p-4 text-gray-500 bg-white rounded-lg shadow dark:text-gray-400 dark:bg-gray-800"
		role="alert">
		<div
			class="inline-flex items-center justify-center flex-shrink-0 w-8 h-8 text-blue-500 bg-blue-100 rounded-lg dark:bg-blue-800 dark:text-blue-200">
			<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.395 2.553a1 1 0 00-1.45-.385c-.345.23-.614.558-.822.88-.214.33-.403.713-.57 1.116-.334.804-.614 1.768-.84 2.734a31.365 31.365 0 00-.613 3.58 2.64 2.64 0 01-.945-1.067c-.328-.68-.398-1.534-.398-2.654A1 1 0 005.05 6.05 6.981 6.981 0 003 11a7 7 0 1011.95-4.95c-.592-.591-.98-.985-1.348-1.467-.363-.476-.724-1.063-1.207-2.03zM12.12 15.12A3 3 0 017 13s.879.5 2.5.5c0-1 .5-4 1.25-4.5.5 1 .786 1.293 1.371 1.879A2.99 2.99 0 0113 13a2.99 2.99 0 01-.879 2.121z"
					clip-rule="evenodd"></path>
			</svg>
		</div>
		<div class="ml-3 text-sm font-normal">Set yourself free.</div>
		<button type="button" class="ml-auto -mx-1.5 -my-1.5 bg-white text-gray-400 hover:text-gray-900 rounded-lg focus:ring-2 focus:ring-gray-300 p-1.5 hover:bg-gray-100 inline-flex h-8 w-8 dark:text-gray-500 dark:hover:text-white dark:bg-gray-800 dark:hover:bg-gray-700" data-collapse-toggle="toast-default" aria-label="Close">
        <span class="sr-only">Close</span>
        <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.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z" clip-rule="evenodd"></path></svg>
    </button>
	</div>

	<p class="mt-5">This toast 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>
</div>

How to create a Toast 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 create a Toast component

  • max-w-2xl
  • mx-auto
  • flex
  • w-full
  • max-w-xs
  • p-4
  • text-gray-500
  • bg-white
  • dark:text-gray-400
  • dark:bg-gray-800
  • inline-flex
  • flex-shrink-0
  • w-8
  • h-8
  • text-blue-500
  • bg-blue-100
  • dark:bg-blue-800
  • dark:text-blue-200
  • w-5
  • h-5
  • ml-3
  • text-sm
  • ml-auto
  • -mx-1.5
  • -my-1.5
  • text-gray-400
  • hover:text-gray-900
  • p-1.5
  • hover:bg-gray-100
  • dark:text-gray-500
  • mt-5
  • text-blue-600

32 steps to create a Toast 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. Use flex to create a block-level flex container.

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

  5. Set the maximum width/height of an element using the max-w-xs utilities.

  6. Control the padding on all sides of an element to 1rem using the p-4 utilities.

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

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

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

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

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

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

  13. Use w-8 to set an element to a fixed width(2rem).

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

  15. Control the text color of an element to blue-500 using the text-blue-500 utilities.

  16. Control the background color of an element to blue-100 using the bg-blue-100 utilities.

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

  18. Control the text color of an element to blue-200 in dark theme using the dark:text-blue-200 utilities.

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

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

  21. Control the margin on left side of an element to 0.75rem using the ml-3 utilities.

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

  23. Control the margin on left side of an element to auto using the ml-auto utilities.

  24. Control the horizontal margin of an element to 1.5 using the -mx-1.5 utilities.

  25. Control the vertical margin of an element to 1.5 using the -my-1.5 utilities.

  26. Control the text color of an element to gray-400 using the text-gray-400 utilities.

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

  28. Control the padding on all sides of an element to 1.5 using the p-1.5 utilities.

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

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

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

  32. 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 create a Toast components, learn and follow along to implement your own components.