🪟 Windows TippsWinZip(17.09.2026 um 08:30 Uhr)
🪟 Windows ServerDomänen-Trust weg nach Windows-Update - IT-Administrator.de(17.09.2026 um 08:17 Uhr)
🪟 Windows TippsWinZip(17.09.2026 um 08:30 Uhr)
🪟 Windows ServerDomänen-Trust weg nach Windows-Update - IT-Administrator.de(17.09.2026 um 08:17 Uhr)
🔧 Programmierung 🕛 vor 1 Jahr 6 Min Lesezeit
0

Create an Engaging Voting Widget with Tailwind CSS

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

With the upcoming American elections, it's a great time to practice your voting skills and get ready for the polls with this JavaScript-free widget!



In this article, we will explore how to create a simple voting widget using Tailwind CSS to customize a radio button without the need for JavaScript. This widget will allow users to upvote or downvote and display the current score, enhancing user engagement on your site.



icon by








Step 2: Code Explanation






2.1 Extend Tailwind with Our Animation



First, we will extend the Tailwind CSS default configuration to incorporate our animation. The sprite will be set as a background image, and we will create a simple @keyframes rule to position the background. Then we will create the sprite animation using it.




CODE
tailwind.config = {
theme: {
extend: {
keyframes: {
sprite: {
from: {
backgroundPosition: "left"
},
to: {
backgroundPosition: "right"
}
}
},
animation: {
"vote-sprite": "sprite .8s steps(23) 1 forwards"
}
}
}
}






If you use a different number of frames in your sprite, adjust the number of steps and duration of the animation accordingly.






2.2 Radio inputs



Three radio inputs represent the voting options:




  1. Downvote: To vote down.


  2. Blank: To cancel the vote.


  3. Upvote: To vote up.




Each input is hidden using the hidden class and we will be used as peers to change the style of next elements depending on the states of the radio inputs.




CODE
<input id="down-radio" class="peer/down hidden" type="radio" name="vote" value="down">
<input id="blank-radio" class="peer/blank hidden" type="radio" name="vote" value="blank" checked="checked">
<input id="up-radio" class="peer/up hidden" type="radio" name="vote" value="up">









2.3 Labels



The labels are used to activate the radio inputs when clicked. We will have four labels used as buttons for voting actions: one label for checking downvote, one label for canceling the downvote checking blank, and the same for upvoting.



They utilize Tailwind classes for appearance and behavior management:




  • The first label is displayed by default and and points to downvote. Its background is the static image.


  • The second label is shown when downvote is checked and it contains the animation animation that runs each time we vote.



  • The same principle applies for upvoting.


    CODE
    <!-- Downvote -->
    <label class="peer/down-btn aspect-square cursor-pointer w-12 peer-checked/down:invisible" for="down-radio" style="background-image: url('https://cdn.hashnode.com/res/hashnode/image/upload/v1728932385888/51fc087e-ca44-4f12-869c-4e50409390b2.png');" aria-label="Downvote"></label>
    <label class="absolute aspect-square bg-no-repeat bg-cover cursor-pointer left-0 opacity-0 pointer-events-none w-12 transition-opacity peer-hover/down-btn:opacity-100 peer-checked/down:animate-vote-sprite peer-checked/down:pointer-events-auto peer-checked/down:opacity-100 peer-checked/down:hover:opacity-80" for="blank-radio" style="background-image: url('https://cdn.hashnode.com/res/hashnode/image/upload/v1728932412479/c91aef3d-6588-4883-b503-2db3b6594ba0.png');" aria-label="Remove downvote"></label>

    <!-- Upvote -->
    <label class="peer/up-btn aspect-square cursor-pointer rotate-180 w-12 peer-checked/up:invisible" for="up-radio" style="background-image: url('https://cdn.hashnode.com/res/hashnode/image/upload/v1728932385888/51fc087e-ca44-4f12-869c-4e50409390b2.png');" aria-label="Upvote"></label>
    <label class="absolute aspect-square bg-no-repeat bg-cover cursor-pointer opacity-0 pointer-events-none right-0 w-12 transition-opacity peer-hover/up-btn:opacity-100 peer-checked/up:animate-vote-sprite peer-checked/up:pointer-events-auto peer-checked/up:opacity-100 peer-checked/up:hover:opacity-80" for="blank-radio" style="background-image: url('https://cdn.hashnode.com/res/hashnode/image/upload/v1728932423782/2e233a8b-db52-431f-9f1b-7458214ac960.png');" aria-label="Remove upvote"></label>








2.4 Vote Counter



The central section displays the vote score. Using transition and translate classes, we can animate score changes when a user votes. The wrapper masks other values with overflow-hidden.




CODE
<div class="group flex items-center h-8 mx-1 overflow-hidden text-2xl">
<div class="flex flex-col font-semibold items-center transition duration-700 translate-y-0 peer-checked/up:group-[]:-translate-y-8 peer-checked/down:group-[]:translate-y-8">
<span class="text-red-700">-1</span>
<span>0</span>
<span class="text-green-800">1</span>
</div>
</div>









2.5 Wrapping in Flexbox



We wrap everything in an inline-flex container to ensure elements are well placed and aligned. We also noticed that clicking on the buttons was selecting some text on mobile devices so we recommend using select-none to remove the problem.






3. Try It Out










CODE
<div class="gap-0 h-16 inline-flex items-center relative select-none">
<input id="down-radio" class="peer/down hidden" type="radio" name="vote" value="down">
<input id="blank-radio" class="peer/blank hidden" type="radio" name="vote" value="blank" checked="checked">
<input id="up-radio" class="peer/up hidden" type="radio" name="vote" value="up">

<label class="peer/down-btn aspect-square cursor-pointer w-12 peer-checked/down:invisible" for="down-radio" style="background-image: url('https://cdn.hashnode.com/res/hashnode/image/upload/v1728932385888/51fc087e-ca44-4f12-869c-4e50409390b2.png');" aria-label="Downvote"></label>
<label class="absolute aspect-square bg-no-repeat bg-cover cursor-pointer left-0 opacity-0 pointer-events-none w-12 transition-opacity peer-hover/down-btn:opacity-100 peer-checked/down:animate-vote-sprite peer-checked/down:pointer-events-auto peer-checked/down:opacity-100 peer-checked/down:hover:opacity-80" for="blank-radio" style="background-image: url('https://cdn.hashnode.com/res/hashnode/image/upload/v1728932412479/c91aef3d-6588-4883-b503-2db3b6594ba0.png');" aria-label="Remove downvote"></label>
<div class="group flex items-center h-8 mx-1 overflow-hidden text-2xl">
<div class="flex flex-col font-semibold items-center transition duration-700 translate-y-0 peer-checked/up:group-[]:-translate-y-8 peer-checked/down:group-[]:translate-y-8">
<span class="text-red-700">-1</span>
<span>0</span>
<span class="text-green-800">1</span>
</div>
</div>
<label class="peer/up-btn aspect-square cursor-pointer rotate-180 w-12 peer-checked/up:invisible" for="up-radio" style="background-image: url('https://cdn.hashnode.com/res/hashnode/image/upload/v1728932385888/51fc087e-ca44-4f12-869c-4e50409390b2.png');" aria-label="Upvote"></label>
<label class="absolute aspect-square bg-no-repeat bg-cover cursor-pointer opacity-0 pointer-events-none right-0 w-12 transition-opacity peer-hover/up-btn:opacity-100 peer-checked/up:animate-vote-sprite peer-checked/up:pointer-events-auto peer-checked/up:opacity-100 peer-checked/up:hover:opacity-80" for="blank-radio" style="background-image: url('https://cdn.hashnode.com/res/hashnode/image/upload/v1728932423782/2e233a8b-db52-431f-9f1b-7458214ac960.png');" aria-label="Remove upvote"></label>
</div>









Conclusion



This simple voting widget, built solely with HTML and Tailwind CSS, provides a smooth user experience without requiring JavaScript. By customizing styles and integrating this code, you can easily allow users to interact with your content effectively. For more information on using Tailwind CSS, check out the official documentation.

Vollständiger Original-Artikel
Den kompletten Beitrag mit allen Details direkt auf dev.to lesen.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
Windows 11 startet nicht: So findet ihr die Ursache und behebt sie
1 Quelle
Belegen Sie die Copilot-Taste neu und starten Sie damit Ihre Lieblings-App
1 Quelle
WinZip
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Create an Engaging Voting Widget with Tailwind CSS

Thematisch verwandte Begriffe: Create, Engaging, Voting, Widget · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...