Published on

How To Make A Input Number / Counter With Tailwind CSS From Scratch

Input Number  / Counter

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 Input Number / Counter ui component

A custom number input including the javascript to make it work -- can be used as a counter or in any forms

Why use Tailwind CSS to create a Input Number / Counter ui component?

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

The preview of Input Number / Counter ui component

Free download of the Input Number / Counter's source code

The source code of Input Number / Counter ui component

<div class="custom-number-input h-10 w-32">
  <label for="custom-input-number" class="w-full text-gray-700 text-sm font-semibold">Counter Input
  </label>
  <div class="flex flex-row h-10 w-full rounded-lg relative bg-transparent mt-1">
    <button data-action="decrement" class=" bg-gray-300 text-gray-600 hover:text-gray-700 hover:bg-gray-400 h-full w-20 rounded-l cursor-pointer outline-none">
      <span class="m-auto text-2xl font-thin"></span>
    </button>
    <input type="number" class="outline-none focus:outline-none text-center w-full bg-gray-300 font-semibold text-md hover:text-black focus:text-black  md:text-basecursor-default flex items-center text-gray-700  outline-none" name="custom-input-number" value="0"></input>
  <button data-action="increment" class="bg-gray-300 text-gray-600 hover:text-gray-700 hover:bg-gray-400 h-full w-20 rounded-r cursor-pointer">
    <span class="m-auto text-2xl font-thin">+</span>
  </button>
</div>

<style>
  input[type='number']::-webkit-inner-spin-button,
  input[type='number']::-webkit-outer-spin-button {
    -webkit-appearance: none;
    margin: 0;
  }

  .custom-number-input input:focus {
    outline: none !important;
  }

  .custom-number-input button:focus {
    outline: none !important;
  }
</style>

<script>
  function decrement(e) {
    const btn = e.target.parentNode.parentElement.querySelector(
      'button[data-action="decrement"]'
    );
    const target = btn.nextElementSibling;
    let value = Number(target.value);
    value--;
    target.value = value;
  }

  function increment(e) {
    const btn = e.target.parentNode.parentElement.querySelector(
      'button[data-action="decrement"]'
    );
    const target = btn.nextElementSibling;
    let value = Number(target.value);
    value++;
    target.value = value;
  }

  const decrementButtons = document.querySelectorAll(
    `button[data-action="decrement"]`
  );

  const incrementButtons = document.querySelectorAll(
    `button[data-action="increment"]`
  );

  decrementButtons.forEach(btn => {
    btn.addEventListener("click", decrement);
  });

  incrementButtons.forEach(btn => {
    btn.addEventListener("click", increment);
  });
</script>

How to create a Input Number / Counter with Tailwind CSS?

Install tailwind css of verion 1.2.0

Use the link html tag to import the stylesheet of Tailwind CSS of the version 1.2.0

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

All the unility class needed to create a Input Number / Counter component

  • h-10
  • w-32
  • w-full
  • text-gray-700
  • text-sm
  • flex
  • flex-row
  • relative
  • bg-transparent
  • mt-1
  • bg-gray-300
  • text-gray-600
  • hover:text-gray-700
  • hover:bg-gray-400
  • h-full
  • w-20
  • m-auto
  • text-2xl
  • text-center
  • text-md
  • hover:text-black
  • focus:text-black
  • md:text-basecursor-default

23 steps to create a Input Number / Counter component with Tailwind CSS

  1. Use h-10 to set an element to a fixed height(2.5rem).

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

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

  4. Control the text color of an element to gray-700 using the text-gray-700 utilities.

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

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

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

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

  9. Control the background color of an element to transparent using the bg-transparent utilities.

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

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

  12. Control the text color of an element to gray-600 using the text-gray-600 utilities.

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

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

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

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

  17. Control the margin on all sides of an element to auto using the m-auto utilities.

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

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

  20. Control the text color of an element to md using the text-md utilities.

  21. Control the text color of an element to black on hover using the hover:text-black utilities.

  22. Control the text color of an element to black on focus using the focus:text-black utilities.

  23. Control the text color of an element to basecursor-default at only medium screen sizes using the md:text-basecursor-default utilities.

Conclusion

The above is a step-by-step tutorial on how to use Tailwind CSS to create a Input Number / Counter components, learn and follow along to implement your own components.