Touch Hitbox
The touch hitbox utility for Tailwind CSS
Installation
npx shadcn@latest add @caprice/touch-hitbox@caprice-ui/tailwindInstall the following dependencies:
npm install @caprice-ui/tailwindCopy and paste the following code into your project.
/** * @utility touch-hitbox * Expands the interactive area of an element to meet minimum touch target guidelines (≥44px on mobile). * * - Ensures accessible, forgiving hit targets for touch and pointer input. * - Does not visually alter the element, only increases the clickable/tappable area. * - Use on small buttons, icons, or controls to improve usability and meet accessibility standards. * * Reference: https://x.com/frederic_ooo/status/1952756133732483543 */@utility touch-hitbox { position: relative; &:before { content: ""; position: absolute; display: block; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 100%; height: 100%; min-height: 44px; min-width: 44px; z-index: 9999; }}Usage
Import the utility into your global.css file:
@import "tailwindcss";
@import "@caprice-ui/tailwind/utility/touch-hitbox";Add the utility classes to your markup / components:
<button type="button" class="touch-hitbox">
<Icon size="size-4">
</button>Additionally, you can use the pointer-coarse: variant to apply the expanded touch target only on touchscreen devices:
// [!code word:pointer-coarse:touch-hitbox]
<button type="button" class="pointer-coarse:touch-hitbox">
<Icon size="size-4">
</button>