0.4.0 • Published 3 years ago

tailwindcss-fluid-spacing v0.4.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

tailwindcss-fluid-spacing

A Tailwind CSS plugin that provides fluid-responsive spacings across viewport widths.

Installation

Install the plugin from npm:

npm install -D tailwindcss-fluid-spacing

or

yarn add -D tailwindcss-fluid-spacing

Then add the plugin to your tailwind.config.js file:

// tailwind.config.js
module.exports = {
  theme: {
    // ...
  },
  plugins: [
    require('tailwindcss-fluid-spacing'),
    // ...
  ],
}

Usage

Basic

Fluid-spacing is inherited by the padding, margin, width, height, maxHeight, gap, inset, space and translate core plugins. Insert vw- before {size} to each utilities.

Examples:

<div class="p-vw-8">...</div> <!-- padding: 2.5vw; -->
<div class="mx-vw-16">...</div> <!-- margin-left: 5vw; margin-right: 5vw; -->
<div class="-mt-vw-16">...</div> <!-- margin-top: -5vw; -->
<div class="w-vw-64">...</div> <!-- width: 20vw; -->
<div class="h-vw-32">...</div> <!-- height: 10vw; -->
<div class="gap-vw-10">...</div> <!-- gap: 3.125vw; -->
<div class="top-vw-24">...</div> <!-- top: 7.5vw; -->
<div class="inset-vw-4">...</div> <!-- top: 1.25vw; right: 1.25vw; bottom: 1.25vw; left: 1.25vw; -->
<div class="translate-y-vw-12">...</div> <!-- --tw-translate-y: 3.75vw; transform: var(--tw-transform); -->

Responsive

To control the fluid-spacing at a specific breakpoint, add a {screen}: prefix to any existing utility. For example, adding the class md:p-vw-8 to an element would apply the p-vw-8 utility at medium screen sizes and above.

<div class="md:p-vw-8">...</div>

To provide a minimum or a maximum value, add a -{min|max}@{screen} suffix to any existing utility; to privide both minmun and maximum values, add a -min@{screen}-max@{screen} suffix. For example, the class mt-vw-16-min@sm would provide the 5vw value with the minmum value 2rem (32px) which equals 5vw at the small screen.

<div class="mt-vw-16-min@sm">...</div> <!-- margin-top: max(5vw, 2rem); -->
<div class="mt-vw-16-max@xl">...</div> <!-- margin-top: min(5vw, 4rem); -->
<div class="mt-vw-16-min@sm-max@xl">...</div> <!-- margin-top: clamp( 2rem, 5vw, 4rem ); -->

The -{min|max}@{screen} and -min@{screnn}-max@{screen} suffixes are also available for any breakpoint.

<div class="my-10 sm:my-vw-20-max@xl">...</div>

Default fluid-spacing

SizeValue320pxsm: 640pxmd: 768pxlg: 1024pxxl: 1280px2xl: 1536px
10.3125vw1px2px2.4px3.2px4px4.8px
1.50.46875vw1.5px3px3.6px4.8px6px7.2px
20.625vw2px4px4.8px6.4px8px9.6px
2.50.78125vw2.5px5px6px8px10px12px
30.9375vw3px6px7.2px9.6px12px14.4px
3.51.09375vw3.5px7px8.4px11.2px14px16.8px
41.25vw4px8px9.6px12.8px16px19.2px
51.5625vw5px10px12px16px20px24px
61.875vw6px12px14.4px19.2px24px28.8px
72.1875vw7px14px16.8px22.4px28px33.6px
82.5vw8px16px19.2px25.6px32px38.4px
92.8125vw9px18px21.6px28.8px36px43.2px
103.125vw10px20px24px32px40px48px
113.4375vw11px22px26.4px35.2px44px52.8px
123.75vw12px24px28.8px38.4px48px57.6px
144.375vw14px28px33.6px44.8px56px67.2px
165vw16px32px38.4px51.2px64px76.8px
206.25vw20px40px48px64px80px96px
247.5vw24px48px57.6px76.8px96px115.2px
288.75vw28px56px67.2px89.6px112px134.4px
3210vw32px64px76.8px102.4px128px153.6px
3611.25vw36px72px86.4px115.2px144px172.8px
4012.5vw40px80px96px128px160px192px
4413.75vw44px88px105.6px140.8px176px211.2px
4815vw48px96px115.2px153.6px192px230.4px
5216.25vw52px104px124.8px166.4px208px249.6px
5617.5vw56px112px134.4px179.2px224px268.8px
6018.75vw60px120px144px192px240px288px
6420vw64px128px153.6px204.8px256px307.2px
7222.5vw72px144px172.8px230.4px288px345.6px
8025vw80px160px192px256px320px384px
9630vw96px192px230.4px307.2px384px460.8px

Configuration options

You can invoke the plugin passing along options when registering it in plugins configuration.

Customize the default setting

Use the sizes key to customize the default setting.

// tailwind.config.js
module.exports = {
  theme: {
    // ...
  },
  plugins: [
    require('tailwindcss-fluid-spacing')({
      sizes: [16, 20, 24],
    }),
    // ...
  ],
}

This will only use 16, 20 and 24 as the {size} to generate classes like p-vw-16, m-vw-20, w-vw-24, etc.

Extend the default setting

To extend the default setting, put the sizes key in extend section.

// tailwind.config.js
module.exports = {
  theme: {
    // ...
  },
  plugins: [
    require('tailwindcss-fluid-spacing')({
      extend: {
        sizes: [68, 76],
      },
    }),
    // ...
  ],
}

This will generate classes like p-vw-68 and m-vw-76 in addition to all of the default fluid-spacing utilities.

License

MIT