Skip to content

Quick Start Guide — Build Your First CSS Variant

Create your first type-safe component variant in three steps.

  1. Define your variant

    Create a button component with color and size variants:

    import { cv } from 'css-variants'
    const button = cv({
    base: 'font-semibold rounded-lg transition-colors',
    variants: {
    color: {
    primary: 'bg-blue-600 text-white hover:bg-blue-700',
    secondary: 'bg-gray-200 text-gray-900 hover:bg-gray-300',
    danger: 'bg-red-600 text-white hover:bg-red-700',
    },
    size: {
    sm: 'px-3 py-1.5 text-sm',
    md: 'px-4 py-2 text-base',
    lg: 'px-6 py-3 text-lg',
    },
    },
    defaultVariants: {
    color: 'primary',
    size: 'md',
    },
    })
  2. Use the variant

    Call the variant function with your desired options:

    // Use defaults
    button()
    // => 'font-semibold rounded-lg transition-colors bg-blue-600 text-white hover:bg-blue-700 px-4 py-2 text-base'
    // Override specific variants
    button({ color: 'danger', size: 'lg' })
    // => 'font-semibold rounded-lg transition-colors bg-red-600 text-white hover:bg-red-700 px-6 py-3 text-lg'
    // Mix defaults with overrides
    button({ size: 'sm' })
    // => 'font-semibold rounded-lg transition-colors bg-blue-600 text-white hover:bg-blue-700 px-3 py-1.5 text-sm'
  3. Add runtime overrides (optional)

    You can add additional classes at runtime using the className prop:

    button({ color: 'primary', className: 'mt-4 w-full' })
    // => 'font-semibold rounded-lg ... bg-blue-600 ... px-4 py-2 ... mt-4 w-full'

How to Use css-variants in React, Vue, Solid.js

Section titled “How to Use css-variants in React, Vue, Solid.js”
import type { ComponentProps } from 'react'
type ButtonProps = Parameters<typeof button>[0] & ComponentProps<'button'>
function Button({ color, size, children, ...props }: ButtonProps) {
return (
<button className={button({ color, size })} {...props}>
{children}
</button>
)
}
// Usage
<Button color="danger" size="lg">Delete Account</Button>