Filters
Overview
Filter utilities help you apply CSS filter effects to elements including blur, brightness, contrast, grayscale, hue rotation, and more. These utilities also include backdrop filter variants for applying effects to the area behind an element.
Why Use Filter Utilities?
Filter utilities help you:
- Create visual effects: Apply blur, brightness, and contrast adjustments
- Build frosted glass effects: Use backdrop filters for modern UI patterns
- Combine multiple filters: CSS custom properties allow stacking filter effects
- Support accessibility: Adjust contrast and color for better readability
useBlurUtility
The useBlurUtility() function creates utility classes for applying blur effects.
import { styleframe } from "styleframe";
import { useBlurUtility } from "@styleframe/theme";
const s = styleframe();
useBlurUtility(s, {
none: '0',
sm: '4px',
default: '8px',
md: '12px',
lg: '16px',
xl: '24px',
'2xl': '40px',
'3xl': '64px',
});
export default s;
._blur\:none {
--tw-blur: blur(0);
filter: var(--tw-blur) var(--tw-brightness) var(--tw-contrast) var(--tw-grayscale) var(--tw-hue-rotate) var(--tw-invert) var(--tw-saturate) var(--tw-sepia) var(--tw-drop-shadow);
}
._blur\:sm {
--tw-blur: blur(4px);
filter: var(--tw-blur) var(--tw-brightness) var(--tw-contrast) var(--tw-grayscale) var(--tw-hue-rotate) var(--tw-invert) var(--tw-saturate) var(--tw-sepia) var(--tw-drop-shadow);
}
._blur {
--tw-blur: blur(8px);
filter: var(--tw-blur) var(--tw-brightness) var(--tw-contrast) var(--tw-grayscale) var(--tw-hue-rotate) var(--tw-invert) var(--tw-saturate) var(--tw-sepia) var(--tw-drop-shadow);
}
._blur\:md {
--tw-blur: blur(12px);
filter: var(--tw-blur) var(--tw-brightness) var(--tw-contrast) var(--tw-grayscale) var(--tw-hue-rotate) var(--tw-invert) var(--tw-saturate) var(--tw-sepia) var(--tw-drop-shadow);
}
/* ... more sizes */
<div class="_blur:sm">Slightly blurred</div>
<img class="_blur:lg" src="background.jpg" alt="Blurred background">
<div class="_blur:none">No blur</div>
useBrightnessUtility
The useBrightnessUtility() function creates utility classes for adjusting element brightness.
import { styleframe } from "styleframe";
import { useBrightnessUtility } from "@styleframe/theme";
const s = styleframe();
useBrightnessUtility(s, {
'0': '0',
'50': '.5',
'75': '.75',
'90': '.9',
'95': '.95',
'100': '1',
'105': '1.05',
'110': '1.1',
'125': '1.25',
'150': '1.5',
'200': '2',
});
export default s;
._brightness\:0 {
--tw-brightness: brightness(0);
filter: var(--tw-blur) var(--tw-brightness) var(--tw-contrast) var(--tw-grayscale) var(--tw-hue-rotate) var(--tw-invert) var(--tw-saturate) var(--tw-sepia) var(--tw-drop-shadow);
}
._brightness\:50 {
--tw-brightness: brightness(.5);
filter: var(--tw-blur) var(--tw-brightness) var(--tw-contrast) var(--tw-grayscale) var(--tw-hue-rotate) var(--tw-invert) var(--tw-saturate) var(--tw-sepia) var(--tw-drop-shadow);
}
._brightness\:100 {
--tw-brightness: brightness(1);
filter: var(--tw-blur) var(--tw-brightness) var(--tw-contrast) var(--tw-grayscale) var(--tw-hue-rotate) var(--tw-invert) var(--tw-saturate) var(--tw-sepia) var(--tw-drop-shadow);
}
._brightness\:125 {
--tw-brightness: brightness(1.25);
filter: var(--tw-blur) var(--tw-brightness) var(--tw-contrast) var(--tw-grayscale) var(--tw-hue-rotate) var(--tw-invert) var(--tw-saturate) var(--tw-sepia) var(--tw-drop-shadow);
}
<img class="_brightness:50" src="image.jpg" alt="Darkened image">
<img class="_brightness:125" src="image.jpg" alt="Brightened image">
useContrastUtility
The useContrastUtility() function creates utility classes for adjusting element contrast.
import { styleframe } from "styleframe";
import { useContrastUtility } from "@styleframe/theme";
const s = styleframe();
useContrastUtility(s, {
'0': '0',
'50': '.5',
'75': '.75',
'100': '1',
'125': '1.25',
'150': '1.5',
'200': '2',
});
export default s;
._contrast\:0 {
--tw-contrast: contrast(0);
filter: var(--tw-blur) var(--tw-brightness) var(--tw-contrast) var(--tw-grayscale) var(--tw-hue-rotate) var(--tw-invert) var(--tw-saturate) var(--tw-sepia) var(--tw-drop-shadow);
}
._contrast\:100 {
--tw-contrast: contrast(1);
filter: var(--tw-blur) var(--tw-brightness) var(--tw-contrast) var(--tw-grayscale) var(--tw-hue-rotate) var(--tw-invert) var(--tw-saturate) var(--tw-sepia) var(--tw-drop-shadow);
}
._contrast\:150 {
--tw-contrast: contrast(1.5);
filter: var(--tw-blur) var(--tw-brightness) var(--tw-contrast) var(--tw-grayscale) var(--tw-hue-rotate) var(--tw-invert) var(--tw-saturate) var(--tw-sepia) var(--tw-drop-shadow);
}
<img class="_contrast:75" src="image.jpg" alt="Low contrast">
<img class="_contrast:150" src="image.jpg" alt="High contrast">
useGrayscaleUtility
The useGrayscaleUtility() function creates utility classes for applying grayscale effects.
import { styleframe } from "styleframe";
import { useGrayscaleUtility } from "@styleframe/theme";
const s = styleframe();
useGrayscaleUtility(s, {
'0': '0',
default: '100%',
});
export default s;
._grayscale\:0 {
--tw-grayscale: grayscale(0);
filter: var(--tw-blur) var(--tw-brightness) var(--tw-contrast) var(--tw-grayscale) var(--tw-hue-rotate) var(--tw-invert) var(--tw-saturate) var(--tw-sepia) var(--tw-drop-shadow);
}
._grayscale {
--tw-grayscale: grayscale(100%);
filter: var(--tw-blur) var(--tw-brightness) var(--tw-contrast) var(--tw-grayscale) var(--tw-hue-rotate) var(--tw-invert) var(--tw-saturate) var(--tw-sepia) var(--tw-drop-shadow);
}
<img class="_grayscale" src="color-image.jpg" alt="Black and white">
<img class="_grayscale:0" src="image.jpg" alt="Full color (removes grayscale)">
More Filter Utilities
useHueRotateUtility
Rotates the hue of an element's colors.
useHueRotateUtility(s, {
'0': '0deg',
'15': '15deg',
'30': '30deg',
'60': '60deg',
'90': '90deg',
'180': '180deg',
});
useInvertUtility
Inverts the colors of an element.
useInvertUtility(s, {
'0': '0',
default: '100%',
});
useSaturateUtility
Adjusts the saturation of an element's colors.
useSaturateUtility(s, {
'0': '0',
'50': '.5',
'100': '1',
'150': '1.5',
'200': '2',
});
useSepiaUtility
Applies a sepia tone effect.
useSepiaUtility(s, {
'0': '0',
default: '100%',
});
useDropShadowUtility
Applies a drop shadow effect (useful for non-rectangular elements like SVGs).
useDropShadowUtility(s, {
sm: '0 1px 1px rgb(0 0 0 / 0.05)',
default: '0 1px 2px rgb(0 0 0 / 0.1), 0 1px 1px rgb(0 0 0 / 0.06)',
md: '0 4px 3px rgb(0 0 0 / 0.07), 0 2px 2px rgb(0 0 0 / 0.06)',
lg: '0 10px 8px rgb(0 0 0 / 0.04), 0 4px 3px rgb(0 0 0 / 0.1)',
xl: '0 20px 13px rgb(0 0 0 / 0.03), 0 8px 5px rgb(0 0 0 / 0.08)',
'2xl': '0 25px 25px rgb(0 0 0 / 0.15)',
none: '0 0 #0000',
});
Backdrop Filter Utilities
Backdrop filter utilities apply filter effects to the area behind an element.
useBackdropBlurUtility
import { styleframe } from "styleframe";
import { useBackdropBlurUtility } from "@styleframe/theme";
const s = styleframe();
useBackdropBlurUtility(s, {
none: '0',
sm: '4px',
default: '8px',
md: '12px',
lg: '16px',
xl: '24px',
'2xl': '40px',
'3xl': '64px',
});
export default s;
._backdrop-blur\:none {
--tw-backdrop-blur: blur(0);
backdrop-filter: var(--tw-backdrop-blur) var(--tw-backdrop-brightness) var(--tw-backdrop-contrast) var(--tw-backdrop-grayscale) var(--tw-backdrop-hue-rotate) var(--tw-backdrop-invert) var(--tw-backdrop-opacity) var(--tw-backdrop-saturate) var(--tw-backdrop-sepia);
}
._backdrop-blur {
--tw-backdrop-blur: blur(8px);
backdrop-filter: var(--tw-backdrop-blur) var(--tw-backdrop-brightness) var(--tw-backdrop-contrast) var(--tw-backdrop-grayscale) var(--tw-backdrop-hue-rotate) var(--tw-backdrop-invert) var(--tw-backdrop-opacity) var(--tw-backdrop-saturate) var(--tw-backdrop-sepia);
}
<div class="_backdrop-blur:md _bg:white/50">
Frosted glass panel
</div>
<nav class="_backdrop-blur:lg">
Blurred navigation background
</nav>
Other Backdrop Utilities
useBackdropBrightnessUtility()- Adjust backdrop brightnessuseBackdropContrastUtility()- Adjust backdrop contrastuseBackdropGrayscaleUtility()- Apply backdrop grayscaleuseBackdropHueRotateUtility()- Rotate backdrop hueuseBackdropInvertUtility()- Invert backdrop colorsuseBackdropOpacityUtility()- Adjust backdrop opacityuseBackdropSaturateUtility()- Adjust backdrop saturationuseBackdropSepiaUtility()- Apply backdrop sepia
Examples
Frosted Glass Card
import { styleframe } from "styleframe";
import { useBackdropBlurUtility } from "@styleframe/theme";
import { useBackgroundColorUtility } from "@styleframe/theme";
const s = styleframe();
const { selector } = s;
useBackdropBlurUtility(s, {
md: '12px',
lg: '16px',
});
useBackgroundColorUtility(s, {
'white/50': 'rgb(255 255 255 / 0.5)',
'white/80': 'rgb(255 255 255 / 0.8)',
});
// Create a frosted glass card style
selector('.glass-card', {
backdropFilter: 'blur(16px)',
backgroundColor: 'rgb(255 255 255 / 0.7)',
borderRadius: '0.5rem',
border: '1px solid rgb(255 255 255 / 0.3)',
});
export default s;
._backdrop-blur\:md {
--tw-backdrop-blur: blur(12px);
backdrop-filter: var(--tw-backdrop-blur) var(--tw-backdrop-brightness) var(--tw-backdrop-contrast) var(--tw-backdrop-grayscale) var(--tw-backdrop-hue-rotate) var(--tw-backdrop-invert) var(--tw-backdrop-opacity) var(--tw-backdrop-saturate) var(--tw-backdrop-sepia);
}
._backdrop-blur\:lg {
--tw-backdrop-blur: blur(16px);
backdrop-filter: var(--tw-backdrop-blur) var(--tw-backdrop-brightness) var(--tw-backdrop-contrast) var(--tw-backdrop-grayscale) var(--tw-backdrop-hue-rotate) var(--tw-backdrop-invert) var(--tw-backdrop-opacity) var(--tw-backdrop-saturate) var(--tw-backdrop-sepia);
}
._bg\:white\/50 { background-color: rgb(255 255 255 / 0.5); }
._bg\:white\/80 { background-color: rgb(255 255 255 / 0.8); }
.glass-card {
backdrop-filter: blur(16px);
background-color: rgb(255 255 255 / 0.7);
border-radius: 0.5rem;
border: 1px solid rgb(255 255 255 / 0.3);
}
Disabled State with Grayscale
import { styleframe } from "styleframe";
import { useGrayscaleUtility, useOpacityUtility } from "@styleframe/theme";
const s = styleframe();
const { modifier } = s;
// Create disabled modifier
const disabled = modifier('disabled', ({ declarations }) => ({
'&:disabled': declarations,
}));
useGrayscaleUtility(s, {
default: '100%',
});
useOpacityUtility(s, {
'50': '0.5',
});
// Apply grayscale on disabled
useGrayscaleUtility(s, { default: '100%' }, [disabled]);
export default s;
._grayscale {
--tw-grayscale: grayscale(100%);
filter: var(--tw-blur) var(--tw-brightness) var(--tw-contrast) var(--tw-grayscale) var(--tw-hue-rotate) var(--tw-invert) var(--tw-saturate) var(--tw-sepia) var(--tw-drop-shadow);
}
._opacity\:50 { opacity: 0.5; }
._disabled\:grayscale:disabled {
--tw-grayscale: grayscale(100%);
filter: var(--tw-blur) var(--tw-brightness) var(--tw-contrast) var(--tw-grayscale) var(--tw-hue-rotate) var(--tw-invert) var(--tw-saturate) var(--tw-sepia) var(--tw-drop-shadow);
}
Best Practices
- Use backdrop-filter for glass effects: Combine backdrop blur with semi-transparent backgrounds
- Consider performance: Filter effects can be expensive; avoid animating complex filters
- Test browser support: Backdrop filters may not work in all browsers; provide fallbacks
- Combine thoughtfully: Multiple filters can compound effects; test combinations carefully
- Use drop-shadow for SVGs: Drop shadows work on the actual shape, unlike box-shadow
- Respect user preferences: Consider reducing motion and effects for users who prefer reduced motion
FAQ
filter applies effects to the element itself and its contents. backdrop-filter applies effects to the area behind the element, creating effects like frosted glass.drop-shadow() follows the actual shape of an element (including transparency in images and SVGs), while box-shadow only applies to the rectangular bounding box. Use drop-shadow for non-rectangular shapes.filter property that references all the variables.