CSS Star Count Button
A GitHub-style Star button with an attached counter that toggles a filled star when checked.
<label class="btn-star">
<input type="checkbox" aria-label="Star this repository">
<span class="btn-star-main"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="m12 3 2.7 5.6 6.1.9-4.4 4.3 1 6.1L12 17l-5.4 2.9 1-6.1-4.4-4.3 6.1-.9z"/></svg>Star</span>
<span class="btn-star-count">12.4k</span>
</label>
.btn-star {
position: relative;
display: inline-flex;
border: 1px solid #d0d7de;
border-radius: 8px;
background: #f6f8fa;
color: #24292f;
font: 600 14px/1 system-ui, sans-serif;
cursor: pointer;
user-select: none;
}
.btn-star input {
position: absolute;
inset: 0;
margin: 0;
opacity: 0;
cursor: pointer;
}
.btn-star-main {
display: inline-flex;
align-items: center;
gap: 6px;
padding: 8px 12px;
}
.btn-star svg {
width: 16px;
height: 16px;
fill: none;
stroke: #57606a;
stroke-width: 2;
stroke-linejoin: round;
transition: fill 0.15s ease, stroke 0.15s ease, transform 0.15s ease;
}
.btn-star-count {
padding: 8px 10px;
border-left: 1px solid #d0d7de;
font-variant-numeric: tabular-nums;
}
.btn-star:hover {
background: #eef1f4;
}
.btn-star input:checked + .btn-star-main svg {
fill: #e3b341;
stroke: #e3b341;
transform: scale(1.15);
}
.btn-star:has(input:focus-visible) {
outline: 2px solid #0969da;
outline-offset: 2px;
}
About this CSS Star Count Button
This CSS Star Count Button is 57 lines of pure CSS (1.0 KB) with no JavaScript and no dependencies. It uses transitions, transforms, Flexbox and the :has() selector. It works in current versions of Chrome, Edge, Safari and Firefox. It works in any stack: plain HTML, React, Vue, Svelte, Astro, WordPress or Webflow.
CSS features used
- transitions
- transforms
- Flexbox
- the :has() selectorChrome 105+, Safari 15.4+, Firefox 121+.
- the :checked state (no JavaScript)
- keyboard focus styles
Customize it
The colors in this snippet. Search and replace them to match your brand:
#d0d7de#f6f8fa#24292f#57606a#eef1f4#e3b341#0969da
How to use this star count button
- Copy the HTML and paste it where you want the element to appear.
- Copy the CSS into your stylesheet, or inside a
<style>tag. - Keep the
.btn-starclass name, or rename it in both places. Then tweak colors, sizes and timing to match your design. - Using React or Next.js? Change
classtoclassNamein the HTML. The CSS works as is, including in Tailwind projects (put it in your global stylesheet).
Building with an AI tool?
“Copy for AI” copies a short prompt for Cursor, Claude Code, Lovable, Bolt or v0. Your AI tool opens the Markdown version of this snippet and reads the HTML, CSS and setup notes there. If your tool can't open links, with the code instead. More ways to use copy CSS with AI.