UnoCSS preset for fluid sizing with UnoCSS philosophy in mind. A modern approach to @media
queries.
f-pt-min-32 f-pt-max-64
will be 32px
in mobiles and 64px
in desktops with a smooth transition for screens with width between 320px and 1920px.
- 🔥 Highly customizable via utilities.
- 📏 Supports
container
queries. - 💅 A well-tested default configuration.
- 🙈 Works with
attribufify
preset.
Learn about Fluid Sizing in CSS:
pnpm i -D unocss-preset-fluid-sizing unocss
// uno.config.ts
import { defineConfig } from 'unocss'
import { presetFluidSizing } from 'unocss-preset-fluid-sizing'
export default defineConfig({
presets: [
// ...
presetFluidSizing({/* options */}),
],
})
<!-- Using default theme that comes with the preset -->
<div class="f-p-2xs f-text-xl" />
<!-- Using utilities -->
<div class="f-p f-p-min-32 f-p-max-48" />
<div class="f-text f-p-text-8 f-p-text-12" />
You can play with the preset in the Playground
This preset provides a set of utilities for all CSS properties that accept a spacing value like padding
, margin
, gap
, width
and font-size
. You need to attach the prefix f-
to the utility name in order to use it.
Sets the minimum and maximum value for the utility:
f-pt-32/64
will set thepadding-top
to32px
when the screen width is smaller than64px
.
Same as above but more explicit.
<div class="f-pt f-pt-min-32 f-pt-max-64"></div>
f-pt-min-32
will set thepadding-top
to32px
.f-pt-max-64
will set thepadding-top
to64px
.
If using
Attributify preset
remember to add the self referencing char(~
) to the utility attribute likef-pt="~ min-32 max-64"
. Same asflex
orgrid
attributes.
Sets the minimum screen width for the utility. By default it is 320px
. You can change it with the option minContainerWidth
in the preset.
Sets the maximum screen width for the utility. By default it is 1920px
. You can change it with the option maxContainerWidth
in the preset.
Instead of using 100vw
to compute the value, it uses 100cqw
which is the width of the closest container.
Warning
This utility is experimental and may not work as expected.
Sets the base unit for the utility which by default is 1px
. You can also change the default base unit by passing the option defaultBaseUnit
to the preset. See CSS Units for more information.
You can also use $
to store the calculation of the fluid size in a CSS variable. For example f-$myvar f-$myvar-min-8 f-$myvar-max-12
will set the padding to the value of the CSS variable --myvar
. Then you can use the CSS variable in your CSS like --other-value: calc(var(--f-myvar) * 1.5);
.
Warning
Due to limitations, the name of the variable can only contain letters, but not numbers nor dashes.
✅ f-$myvar
is correct.
❌ f-$my-var
is not.
By default the preset includes a theme with predefined values for spacing and font sizes that I have been using in my projects and in general is what you will need. This will save you time as you can do f-p-2xs
instead of f-p f-p-min-8 f-p-max-12
or f-mt-lg
instead of f-mt-min-48 f-mt-max-72
.
You can check the theme in theme.ts.
You can disable the theme by passing the option disableTheme
to the preset and adding your own shortcuts.
Check the interface FluidSizingOptions
for the full list of options.
- UnoCSS Fluid Preset. Uses JS to compute the value like
f-pt-32-64
which results in a limited API. - UnoCSS preset quickstart template.