Published on

6 Critical Skills To Build A Todo list With Tailwind CSS Remarkably Well

Todo list

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 Todo list ui component

Alpinejs todo list inspired by things

Why use Tailwind CSS to build a Todo list ui component?

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

The preview of Todo list ui component

Free download of the Todo list's source code

The source code of Todo list ui component

<script src="https://cdn.jsdelivr.net/gh/alpinejs/[email protected]/dist/alpine.js" defer></script>
<style>@import url(https://cdnjs.cloudflare.com/ajax/libs/MaterialDesign-Webfont/5.3.45/css/materialdesignicons.min.css);</style>

<div class="min-w-screen min-h-screen bg-gray-800 flex items-center justify-center px-5 py-5">
    <div class="w-full mx-auto rounded-lg border border-gray-700 p-8 lg:py-12 lg:px-14 text-gray-300" style="max-width: 800px" x-data="app()" x-init="addItem()">
        <div class="mb-10">
            <h1 class="text-2xl font-bold"><i class="mdi mdi-star text-yellow-300 text-3xl leading-none align-bottom"></i> Todos</h1>
        </div>
        <div class="mb-10">
            <template x-if="todos.length">
                <ul @click.away="defocusItems()" class="-mx-1">
                    <template x-for="(item,index) in todos">
                        <li @click="focusItem(index)" @dblclick="openItem(index)" class="px-2 py-2 rounded transition-all flex text-md" :class="{'bg-indigo-800':item.focused,'bg-gray-700 shadow-lg px-4 py-4 my-10 -mx-2':item.open,'mb-1 cursor-pointer':!item.open}" :key="index">
                            <div class="flex-none w-10 leading-none">
                                <input type="checkbox" :checked="item.checked" @click="item.checked=!item.checked">
                            </div>
                            <div class="flex-grow max-w-full">
                                <div class="w-full leading-none">
                                    <h3 class="text-md leading-none truncate w-full pr-10" :class="item.title.length?'text-gray-300':'text-gray-500'" x-text="item.title||'New todo...'" x-show="!item.open"></h3>
                                    <input type="text" x-show="item.open" class="text-md w-full bg-transparent text-gray-300 leading-none focus:outline-none mb-2" x-model="item.title" :id="`titleField${index}`" placeholder="New todo..."/>
                                </div>
                                <div class="w-full" x-show="item.open">
                                    <textarea class="text-md w-full bg-transparent text-gray-300 leading-tight focus:outline-none" rows="10" x-model="item.notes" placeholder="Notes"></textarea>
                                </div>
                                <div class="w-full flex justify-end" x-show="item.open">
                                    <button class="p-1 -mr-1 focus:outline-none hover:text-red-300" @click="removeItem(index)"><i class="mdi mdi-trash-can-outline"></i></button>
                                </div>
                            </div>
                        </li>
                    </template>
                </ul>
            </template>
            <template x-if="!todos.length">
                <p class="text-gray-500">No todos</p>
            </template>
        </div>
        <div class="flex justify-center">
            <button class="py-1 px-10 border border-gray-800 hover:border-gray-700 rounded leading-none focus:outline-none text-xl" @click="addItem()"><i class="mdi mdi-plus"></i></button>
        </div>
    </div>
</div>

<script>
function app(){
    return {
        todos: [],
        focusItem: function(index) {
            if ( !this.todos[index].open ) {
                for(let i = 0; i < this.todos.length; i++){
                    this.todos[i].open = false;
                    this.todos[i].focused = i == index;
                }
            }
        },
        openItem: function(index) {
            for(let i = 0; i < this.todos.length; i++){
                this.todos[i].focused = false;
                this.todos[i].open = i == index;
            }
            setTimeout(()=>document.getElementById(`titleField${index}`).focus(),10);
        },
        defocusItems: function() {
            for(let i = 0; i < this.todos.length; i++){
                this.todos[i].focused = false;
                this.todos[i].open = false;
            }
        },
        removeItem: function(index) {
            this.todos = this.todos.filter((todo, i) => {
                return i == index ? false : true;
            });
            setTimeout(()=>this.defocusItems(),10);
        },
        addItem: function() {
            this.todos.push({
                title: '',
                notes: '',
                checked: false,
                focused: false,
                open: false
            });
            setTimeout(()=>this.openItem(this.todos.length-1),10);
        }
    }
}
</script>

<!-- BUY ME A BEER AND HELP SUPPORT OPEN-SOURCE RESOURCES -->
<div class="flex items-end justify-end fixed bottom-0 right-0 mb-4 mr-4 z-10">
    <div>
        <a title="Buy me a beer" href="https://www.buymeacoffee.com/scottwindon" target="_blank" class="block w-16 h-16 rounded-full transition-all shadow hover:shadow-lg transform hover:scale-110 hover:rotate-12">
            <img class="object-cover object-center w-full h-full rounded-full" src="https://i.pinimg.com/originals/60/fd/e8/60fde811b6be57094e0abc69d9c2622a.jpg"/>
        </a>
    </div>
</div>

How to build a Todo list with Tailwind CSS?

Install tailwind css of verion 2.0.2

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

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

All the unility class needed to build a Todo list component

  • min-w-screen
  • min-h-screen
  • bg-gray-800
  • flex
  • px-5
  • py-5
  • w-full
  • mx-auto
  • border-gray-700
  • p-8
  • lg:py-12
  • lg:px-14
  • text-gray-300
  • mb-10
  • text-2xl
  • text-yellow-300
  • text-3xl
  • -mx-1
  • px-2
  • py-2
  • text-md
  • px-4
  • py-4
  • my-10
  • flex-none
  • w-10
  • flex-grow
  • max-w-full
  • pr-10
  • bg-transparent
  • mb-2
  • p-1
  • -mr-1
  • hover:text-red-300
  • text-gray-500
  • py-1
  • px-10
  • border-gray-800
  • hover:border-gray-700
  • text-xl
  • fixed
  • bottom-0
  • right-0
  • mb-4
  • mr-4
  • z-10
  • block
  • w-16
  • h-16
  • h-full

50 steps to build a Todo list component with Tailwind CSS

  1. Set the minimum width/height of an element using the min-w-screen utilities.

  2. Set the minimum width/height of an element using the min-h-screen utilities.

  3. Control the background color of an element to gray-800 using the bg-gray-800 utilities.

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

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

  6. Control the vertical padding of an element to 1.25rem using the py-5 utilities.

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

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

  9. Control the border color of an element to gray-700 using the border-gray-700 utilities.

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

  11. Control the vertical padding of an element to 3rem at only large screen sizes using the lg:py-12 utilities.

  12. Control the horizontal padding of an element to 3.5rem at only large screen sizes using the lg:px-14 utilities.

  13. Control the text color of an element to gray-300 using the text-gray-300 utilities.

  14. Control the margin on bottom side of an element to 2.5rem using the mb-10 utilities.

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

  16. Control the text color of an element to yellow-300 using the text-yellow-300 utilities.

  17. Control the text color of an element to 3xl using the text-3xl utilities.

  18. Control the horizontal margin of an element to -0.25rem using the -mx-1 utilities.

  19. Control the horizontal padding of an element to 0.5rem using the px-2 utilities.

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

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

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

  23. Control the vertical padding of an element to 1rem using the py-4 utilities.

  24. Control the vertical margin of an element to 2.5rem using the my-10 utilities.

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

  26. Use w-10 to set an element to a fixed width(2.5rem).

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

  28. Set the maximum width/height of an element using the max-w-full utilities.

  29. Control the padding on right side of an element to 2.5rem using the pr-10 utilities.

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

  31. Control the margin on bottom side of an element to 0.5rem using the mb-2 utilities.

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

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

  34. Control the text color of an element to red-300 on hover using the hover:text-red-300 utilities.

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

  36. Control the vertical padding of an element to 0.25rem using the py-1 utilities.

  37. Control the horizontal padding of an element to 2.5rem using the px-10 utilities.

  38. Control the border color of an element to gray-800 using the border-gray-800 utilities.

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

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

  41. Use fixed to position an element relative to the browser window.

  42. Use the bottom-0 utilities to set the bottom position of a positioned element to 0rem.

  43. Use the right-0 utilities to set the right position of a positioned element to 0rem.

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

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

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

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

  48. Use w-16 to set an element to a fixed width(4rem).

  49. Use h-16 to set an element to a fixed height(4rem).

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

Conclusion

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