Published on

Best Ways To Create A Progress Slider Component With Tailwind CSS

Progress Slider Component

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 Progress Slider Component ui component

Tailwind + alpinejs progress slider example. also see on codepen.io/egoistdeveloper/pen/joyrngq

Why use Tailwind CSS to build a Progress Slider Component ui component?

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

The preview of Progress Slider Component ui component

Free download of the Progress Slider Component's source code

The source code of Progress Slider Component ui component

<!-- AlpineJS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/alpinejs/3.8.1/cdn.min.js" defer></script>

<style>
        /* Slider */

        .x-slider * {
            width: 350px !important;
        }

        .x-slider input::-webkit-slider-thumb {
            width: 20px;
            height: 20px;
            border-radius: 50%;
            background-color: #636363;
            border: 5px solid #24c55d;
            box-shadow: 0px 0px 40px -4px rgba(0, 0, 0, 0.76);
            transition: all 0.1s ease-in-out;
            outline: none;
            appearance: none;
        }

        .x-slider input::-webkit-slider-thumb:hover {
            border-color: #1bb350;
        }
</style>

<!-- Story Bar Container -->
<div class="flex flex-col items-center justify-center mt-52">
    <!-- App -->
    <div class="bg-white p-10 rounded-md shadow-lg" x-cloak x-data="appData()" x-init="appInit()">
        <!-- Player -->
        <div class="relative">
            <!-- Player Slider Container -->
            <div class="x-slider flex relative my-3">
                <!-- Player Slider -->
                <div class="x-slider inline justify-center rounded-md">
                    <!-- Range Input -->
                    <input class="absolute bg-transparent rounded-md
                        z-10 h-1 w-80 outline-0 appearance-none" type="range"
                        step="1" min='0' max="100" value="0"
                        x-model="sliderValue"
                        x-on:change.debounce="sliderProgressTime = sliderProgressToTime(sliderValue, songLength)">

                    <!-- Slider Placeholder -->
                    <div class="absolute z-0 w-80 h-0.5 mt-0.5 rounded-md bg-gray-500/70"></div>

                    <!-- Progress -->
                    <div class="absolute z-0 w-80 h-1.5 rounded-md bg-green-600
                        bg-gradient-to-r from-emerald-700 to-green-500"
                        x-bind:style="'width: ' + (sliderValue * 3.5) + 'px !important; '"></div>
                    <!-- x-slider is width 350px -->
                </div>
            </div>

            <!-- Duration Container -->
            <div class="flex flex-row flex-grow justify-start px-0.5">
                <!-- Current Time -->
                <div class="text-gray-500/90 text-sm select-none" x-text="sliderProgressTime"></div>

                <!-- Grow Space -->
                <div class="grow"></div>

                <!-- Total Time -->
                <div class="text-gray-400/90 text-sm select-none pr-1" x-text="secondsToDuration(songLength)"></div>
            </div>
        </div>
    </div>

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

<script>
    function appData() {
        return {
            sliderValue: 50,
            songLength: 250,
            sliderProgressTime: '00:00',

            appInit() {
                this.sliderProgressTime = this.sliderProgressToTime(this.sliderValue, this.songLength);
            },

            /*
            * Convert slider value to time
            */
            secondsToDuration(seconds) {
                var minutes = Math.floor(seconds / 60).toFixed(0),
                    seconds = Math.floor(seconds % 60).toFixed(0);

                return minutes + ":" + (seconds < 10 ? "0" : "") + seconds;
            },

            /*
            * Slider progress value to time
            */
            sliderProgressToTime(sliderValue, songLength) {
                return this.secondsToDuration((songLength / 100) * sliderValue);
            }
        }
    }
</script>

How to build a Progress Slider Component 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 Progress Slider Component component

  • flex
  • flex-col
  • mt-52
  • bg-white
  • p-10
  • relative
  • my-3
  • inline
  • absolute
  • bg-transparent
  • z-10
  • h-1
  • w-80
  • z-0
  • h-0.5
  • mt-0.5
  • bg-gray-500/70
  • h-1.5
  • bg-gradient-to-r
  • flex-row
  • flex-grow
  • justify-start
  • px-0.5
  • text-gray-500/90
  • text-sm
  • text-gray-400/90
  • pr-1
  • text-center
  • my-20
  • text-blue-600

30 steps to build a Progress Slider Component component with Tailwind CSS

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

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

  3. Control the margin on top side of an element to 13rem using the mt-52 utilities.

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

  5. Control the padding on all sides of an element to 2.5rem using the p-10 utilities.

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

  7. Control the vertical margin of an element to 0.75rem using the my-3 utilities.

  8. Use inline utilities to control the flow of text inside the element to wrap normally.

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

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

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

  12. Use h-1 to set an element to a fixed height(0.25rem).

  13. Use w-80 to set an element to a fixed width(20rem).

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

  15. Use h-0.5 to set an element to a fixed height(0.5).

  16. Control the margin on top side of an element to 0.5 using the mt-0.5 utilities.

  17. Control the background color of an element to gray-500/70 using the bg-gray-500/70 utilities.

  18. Use h-1.5 to set an element to a fixed height(1.5).

  19. Control the background color of an element to gradient-to-r using the bg-gradient-to-r utilities.

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

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

  22. Use justify-start to justify items against the start of the container’s main axis.

  23. Control the horizontal padding of an element to 0.5 using the px-0.5 utilities.

  24. Control the text color of an element to gray-500/90 using the text-gray-500/90 utilities.

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

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

  27. Control the padding on right side of an element to 0.25rem using the pr-1 utilities.

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

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

  30. 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 Progress Slider Component components, learn and follow along to implement your own components.