How to Build an Interactive Voting Poll Card with Pure CSS Bars & LocalStorage Persistence
An interactive voting poll widget tracks selected radio or checkbox inputs, calculates relative percentages across total votes, dynamically assigns CSS variables (`--pct: 64%`), and animates progress fills using `width: var(--pct)` with `transition: width 0.6s cubic-bezier(0.16, 1, 0.3, 1)`.
Step 1: Structure the Poll Markup with Accessible Radio Groups
Use fieldset and legend tags to ensure screen readers announce the poll question and option choices properly.
<fieldset class="poll-widget" id="pollWidget">
<legend class="poll-question">Which CSS layout model do you use most frequently?</legend>
<div class="poll-options">
<label class="poll-option" data-id="grid">
<input type="radio" name="pollChoice" value="grid">
<div class="poll-bar" style="--pct: 0%;"></div>
<span class="poll-label">CSS Grid</span>
<span class="poll-pct">0%</span>
</label>
<label class="poll-option" data-id="flexbox">
<input type="radio" name="pollChoice" value="flexbox">
<div class="poll-bar" style="--pct: 0%;"></div>
<span class="poll-label">CSS Flexbox</span>
<span class="poll-pct">0%</span>
</label>
<label class="poll-option" data-id="subgrid">
<input type="radio" name="pollChoice" value="subgrid">
<div class="poll-bar" style="--pct: 0%;"></div>
<span class="poll-label">Subgrid & Multi-Track</span>
<span class="poll-pct">0%</span>
</label>
</div>
<footer class="poll-footer">
<span class="poll-total-votes" id="pollTotal">0 votes</span>
<button type="button" class="poll-vote-btn" id="submitVoteBtn" disabled>Vote</button>
</footer>
</fieldset>
Step 2: Style Layered Progress Fills with CSS Variables
Position an absolute progress fill behind the option text that animates from 0% to the target percentage upon voting.
.poll-widget {
background: #0f172a;
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 16px;
padding: 24px;
max-width: 440px;
color: #fff;
}
.poll-question {
font-size: 1.15rem;
font-weight: 600;
margin-bottom: 16px;
}
.poll-option {
position: relative;
display: flex;
justify-content: space-between;
align-items: center;
padding: 14px 18px;
margin-bottom: 10px;
border-radius: 10px;
background: rgba(255, 255, 255, 0.04);
cursor: pointer;
overflow: hidden;
border: 1px solid transparent;
transition: border-color 0.2s;
}
.poll-option:has(input:checked) {
border-color: #6366f1;
}
.poll-bar {
position: absolute;
top: 0; left: 0; bottom: 0;
width: var(--pct, 0%);
background: linear-gradient(90deg, rgba(99,102,241,0.25), rgba(168,85,247,0.35));
transition: width 0.65s cubic-bezier(0.16, 1, 0.3, 1);
pointer-events: none;
border-radius: 8px;
}
.poll-label, .poll-pct {
position: relative;
z-index: 2;
font-size: 0.95rem;
font-variant-numeric: tabular-nums;
}
Step 3: Compute Percentages and Store Vote in LocalStorage
Calculate relative proportions, animate the bar widths, and disable further submissions if user already voted.
const pollKey = 'css_poll_voted_demo';
const counts = { grid: 45, flexbox: 78, subgrid: 18 };
function renderResults(userChoice) {
const total = Object.values(counts).reduce((a, b) => a + b, 0);
document.getElementById('pollTotal').textContent = `${total.toLocaleString()} votes`;
document.querySelectorAll('.poll-option').forEach(opt => {
const id = opt.dataset.id;
const pct = Math.round((counts[id] / total) * 100) || 0;
opt.querySelector('.poll-bar').style.setProperty('--pct', `${pct}%`);
opt.querySelector('.poll-pct').textContent = `${pct}%`;
opt.querySelector('input').disabled = true;
if (id === userChoice) opt.classList.add('user-voted');
});
document.getElementById('submitVoteBtn').style.display = 'none';
}
document.getElementById('submitVoteBtn').addEventListener('click', () => {
const selected = document.querySelector('input[name="pollChoice"]:checked');
if (!selected) return;
counts[selected.value] = (counts[selected.value] || 0) + 1;
localStorage.setItem(pollKey, selected.value);
renderResults(selected.value);
});
Next-Level Addons: Elevating Your Implementation
Take this technique further by combining it with complementary design token studios and animation tools:
Here's my golden rule when implementing this in production: test across multiple devices and browsers early. Keep your DOM hierarchy shallow, optimize assets with modern formats (like WebP/AVIF), and verify keyboard navigation for accessibility.
Frequently Asked Questions
Practical answers to common implementation questions, browser quirks, and performance tips.
How do you prevent duplicate voting on client-side polls?
Store the poll completion key in localStorage with the poll ID. For mission-critical security, hash the client IP and browser fingerprint on an edge worker or server endpoint.
Can the CSS poll support multi-select checkboxes?
Yes, change input types from radio to checkbox and calculate percentages based on total respondents rather than total checks.
Why use CSS custom properties for the bar width instead of inline style width?
CSS custom properties (`--pct`) decouple logic from presentation, allowing the same property to animate background gradients, SVG stroke offsets, or clip-path masks.
How does tabular-nums help font rendering in poll widgets?
font-variant-numeric: tabular-nums ensures all digits have identical widths, preventing layout jitter when percentages count up from 0% to 100%.
Is this poll accessible to screen readers?
Yes, using fieldset/legend semantics with native input elements ensures full keyboard accessibility and screen reader support.