Giter Club home page Giter Club logo

tailwindcss-fluid-type's Introduction

πŸ‘‰πŸ» tailwindcss-fluid-type

Tailwincss Fluid Type

A plugin that makes the use of Fluid Type a breeze.

πŸ‘‰πŸ» Installation

Install the plugin from npm:

# Using npm
npm install tailwindcss-fluid-type

# Using Yarn
yarn add tailwindcss-fluid-type

Then add the plugin to your tailwind.config.js file and do your settings if you're not happy with the defaults:

// tailwind.config.js
module.exports = {
  // You can disable the fontSize core plugin if you don't need non fluid font sizes.
  // If you don't disable it, the fluid-type plugin simply overrule the default font-sizes if the keys are the same.
  // Or you can use both alongside when you set an prefix in the settings
  corePlugins: {
    fontSize: false,
    // ...
  },
  plugins: [
    require("tailwindcss-fluid-type"),
    // ...
  ],
};

πŸ‘‰πŸ» Usage

Nothing changed here to the default tailwindcss configuration:

<article>
  <h1 class="text-xl">Fluid type</h1>
</article>

πŸ‘‰πŸ» Configuration

The plugin comes with a default configuration (see below) but it's possible to customize this config for your project. As default, we use rem for better accessibility, but you can also use px.


Important Note:
If you set values you have to set all values that you need for your font-sizes. There is no value merging here.


Default configuration

// tailwind.config.js
module.exports = {
  plugins: [
    require("tailwindcss-fluid-type")({
      // your fluid type settings
      // works only with unitless numbers
      // This numbers are the defaults settings
      settings: {
        fontSizeMin: 1.125, // 1.125rem === 18px
        fontSizeMax: 1.25, // 1.25rem === 20px
        ratioMin: 1.125, // Multiplicator Min
        ratioMax: 1.2, // Multiplicator Max
        screenMin: 20, // 20rem === 320px
        screenMax: 96, // 96rem === 1536px
        unit: "rem", // default is rem but it's also possible to use 'px'
        prefix: "", // set a prefix to use it alongside the default font sizes
        extendValues: true, // When you set extendValues to true it will extend the default values. Set it to false to overwrite the values.
      },
      // Creates the text-xx classes
      // This are the default settings and analog to the tailwindcss defaults
      // Each `lineHeight` is set unitless and we think that's the way to go especially in context with fluid type.
      values: {
        xs: [-2, 1.6],
        sm: [-1, 1.6],
        base: [0, 1.6],
        lg: [1, 1.6],
        xl: [2, 1.2],
        "2xl": [3, 1.2],
        "3xl": [4, 1.2],
        "4xl": [5, 1.1],
        "5xl": [6, 1.1],
        "6xl": [7, 1.1],
        "7xl": [8, 1],
        "8xl": [9, 1],
        "9xl": [10, 1],
      },
    }),
  ],
  variants: {
    fluidType: ["responsive"],
  },
};

Fluid type configuration without lineHeight

It is also possible to set just the fontSize without set the lineHeight

// tailwind.config.js
module.exports = {
  plugins: [
    require("tailwindcss-fluid-type")({
      values: {
        // ...
        base: 0,
        // ...
      },
    }),
  ],
};

Fluid type configuration with lineHeight & letterSpacing

And yes, you can also set the letterSpacing & lineHeight as you know from the tailwind documentation. letterSpacing can be all values that you like.

// tailwind.config.js
module.exports = {
  plugins: [
    require("tailwindcss-fluid-type")({
      values: {
        // ...
        base: [
          0,
          {
            lineHeight: 1.6,
            letterSpacing: "-0.1rem",
          },
        ],
        // ...
      },
    }),
  ],
};

πŸ‘‰πŸ» Samples

Just set the fontSize property

// tailwind.config.js
module.exports = {
  plugins: [
    require("tailwindcss-fluid-type")({
      settings: {
        fontSizeMin: 1.125,
        fontSizeMax: 1.25,
        ratioMin: 1.125,
        ratioMax: 1.2,
        screenMin: 20,
        screenMax: 96,
        unit: "rem",
        prefix: "",
      },
      values: {
        // ...
        base: 0,
        // ...
      },
    }),
  ],
};
<p class="text-base">The quick brown fox jumps over the lazy dogs</p>
.text-base {
  font-size: clamp(
    1.125rem,
    calc(1.125rem + (1.25 - 1.125) * ((100vw - 20rem) / (96 - 20))),
    1.25rem
  );
}

Set the fontSize & lineHeight property

// tailwind.config.js
module.exports = {
  plugins: [
    require("tailwindcss-fluid-type")({
      settings: {
        fontSizeMin: 1.125,
        fontSizeMax: 1.25,
        ratioMin: 1.125,
        ratioMax: 1.2,
        screenMin: 20,
        screenMax: 96,
        unit: "rem",
        prefix: "",
      },
      values: {
        // ...
        base: [0, 1.6],
        // ...
      },
    }),
  ],
};
<p class="text-base">The quick brown fox jumps over the lazy dogs</p>
.text-base {
  font-size: clamp(
    1.125rem,
    calc(1.125rem + (1.25 - 1.125) * ((100vw - 20rem) / (96 - 20))),
    1.25rem
  );
  line-height: 1.6;
}

Set the fontSize, lineHeight & letterSpacing property

// tailwind.config.js
module.exports = {
  plugins: [
    require("tailwindcss-fluid-type")({
      settings: {
        fontSizeMin: 1.125,
        fontSizeMax: 1.25,
        ratioMin: 1.125,
        ratioMax: 1.2,
        screenMin: 20,
        screenMax: 96,
        unit: "rem",
        prefix: "",
      },
      values: {
        // ...
        base: [
          0,
          {
            lineHeight: 1.6,
            letterSpacing: "-0.1rem",
          },
        ],
        // ...
      },
    }),
  ],
};
<p class="text-base">The quick brown fox jumps over the lazy dogs</p>
.text-base {
  font-size: clamp(
    1.125rem,
    calc(1.125rem + (1.25 - 1.125) * ((100vw - 20rem) / (96 - 20))),
    1.25rem
  );
  line-height: 1.6;
  letter-spacing: -0.1rem;
}

Set a value as string

// tailwind.config.js
module.exports = {
  plugins: [
    require("tailwindcss-fluid-type")({
      values: {
        // ...
        "2xs": "11px",
        // ...
      },
    }),
  ],
};
<p class="text-2xs">The quick brown fox jumps over the lazy dogs</p>
.text-2xs {
  font-size: 11px;
}

Set a prefix

// tailwind.config.js
module.exports = {
  plugins: [
    require("tailwindcss-fluid-type")({
      settings: {
        // ...
        prefix: "fluid-",
      },
    }),
  ],
};
<p class="fluid-text-base">The quick brown fox jumps over the lazy dogs</p>
.fluid-text-base {
  font-size: clamp(
    1.125rem,
    calc(1.125rem + (1.25 - 1.125) * ((100vw - 20rem) / (96 - 20))),
    1.25rem
  );
  line-height: 1.6;
  letter-spacing: -0.1rem;
}

πŸ‘‰πŸ½ Compability with Tailwind Merge

To ensure compabibility with tailwind-merge, extends it's configuration to recognize fluid-text sizes. This enables seamless overriding and filtering of tailwind classes with tailwind-merge utility.

import { type ClassValue, clsx } from "clsx";
import { extendTailwindMerge } from "tailwind-merge";

const twMerge = extendTailwindMerge({
  extend: {
    classGroups: {
      "font-size": [
        {
          "fluid-text": ["sm", "base", "lg", "xl", "2xl", "3xl", "4xl", "5xl", "6xl", "7xl", "8xl", "9xl",
          ],
        },
      ],
    },
  },
});


export function cn(...inputs: ClassValue[]) {
  return twMerge(clsx(inputs));
}

Overwrite default line-height

Sometimes it may be useful to override the default line height that you set in your config. Therefore, this plugin supports the Tailwind font size line-height shorthand. This works as follows.

<h1 class="text-9xl/snug">The quick brown fox jumps over the lazy dogs</h1>

See the official Tailwind documentation for more information.

πŸ‘‰πŸ» Live Demo

Fluid Type Live Demo

tailwindcss-fluid-type's People

Contributors

davidhellmann avatar dependabot[bot] avatar salmans-teikametrics avatar spiderdan98 avatar thebiltheory avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

tailwindcss-fluid-type's Issues

dependency issues with [email protected]

Hi there,

just wanted to drop a note that I wanted to try your pluging and run it with the latest alpha and it would not install properly:

npm resolution error report

2021-12-08T10:20:58.372Z

While resolving: [email protected]
Found: [email protected]
node_modules/tailwindcss
dev tailwindcss@"^3.0.0-alpha.2" from the root project

Could not resolve dependency:
peer tailwindcss@">=2.0.0" from [email protected]
node_modules/tailwindcss-fluid-type
tailwindcss-fluid-type@"*" from the root project

Fix the upstream dependency conflict, or retry
this command with --force, or --legacy-peer-deps
to accept an incorrect (and potentially broken) dependency resolution.

Thanks

Best Regards

Marc

How to configure in Typescript (tailwind.config.ts)?

I set text-sm, but the font-size value is clamp(1rem, calc(1rem + ((1.0416666666666667 - 1) * ((100vw - 20rem) / (96 - 20)))), 1.0416666666666667rem). Is this value normal? The font is larger than I expected.
I'm using Nuxt3 with TypeScript.I can't use require way to configrue the plugin.

Question about the use of "leading-" with plugin

Hey David,

First of all thank you so much for this plugin!

It seems like I cannot do the following:

className="leading-snug text-base"

The leading does not seems to change anything about the fluid text. My question is, is this intended? if so, is there a work-around to this issue?

Why require scales to be integers?

I see you are doing checks to see whether the values are integers and if they are - returning the value instead of calculating the modular scale. Why is this? Would be nice to have x.5 scales or any arbitrary scale. If I submitted a PR would you accept it?

Default settings analog to tailwindcss defaults?

Much thanks for your work on this. It seems very powerful.

In the readme for the default configuration it mentions:

This are the default settings and analog to the tailwindcss defaults

When I apply these settings I get different font sizes at the max screen size than I do from the tailwindcss defaults.

Are these default values aligned at a midpoint? Is it possible to adjust the settings so they align at the max screen size?

After reading through the readme, I'm not quite sure what some of the settings do. Are there any resources you can point me to that would explain the relationships between fontSizeMin, fontSizeMax, ratioMin, and ratioMax?

Much thanks for any help you can provide!

text-xs produces invalid clamp

Hi,

First of thanks for the package it is really useful.

I have noticed that the default 'xs' property produces the following:

font-size: clamp(0.8888888888888888rem, calc(0.8888888888888888rem + ((0.8680555555555557 - 0.8888888888888888) * ((100vw - 20rem) / (96 - 20)))), 0.8680555555555557rem);

The min value is higher then the max value.

Tailwind CSS Vesion 3 Breaking Change

Upon upgrade to Tailwind CSS v.3.0, the following import is now breaking within tailwind.config.js: require('tailwindcss-fluid-type');. Package.json has been updated to Tailwind v3.0.

Demo also breaks when chaning the Tailwind version to 3.0: https://play.tailwindcss.com/3LDf3gzbhb

Side Note: Thanks for your awesome work on this plugin. We absolutely love it.

Best practices to add the fluid-type settings to β€œprose” sections of a page (official Tailwind CSS Typography plugin)?

This is a question from a Tailwind CSS noob and fluid type noob. So, sorry if it’s stupid.

I love the tailwindcss-fluid-type plugin since it makes using a fluid scales for font sizes a no-brainer in Tailwind CSS. But for certain parts of pages I need the Typography plugin, whenever I have to include β€œunstyled” content from a typical CMS WYSIWYG editor field. A container element with the prose class usually works fine.

I have to adapt the default styling of the Typography plugin to the rest of the site anyway. So this is not the point.

But to include fluid type settings, I would copy the final font-size definitions from the rendered CSS file and paste them into the settings of the Typography plugin. For example, I would copy the clamp function of a .text-base selector and include it as font-size for prose-base in my configuration, like so:

module.exports = {
  theme: {
    extend: {
      typography: {
        DEFAULT: {
          css: {
            'font-size':
              'clamp(1.125rem, calc(1.125rem + ((1.25 - 1.125) * ((100vw - 20rem) / (96 - 20)))), 1.25rem)',
          },
        },
      },
    },
  },
  plugins: [
    require('@tailwindcss/typography'),
    require('tailwindcss-fluid-type'),
  ],
}

And then the same for the prose-sm, prose-lg, prose-xl and prose-2xl.

It works. And since the Typography plugin uses relative font-sizes, it also works nicely out of the box for all elements like headlines, etc.

But it is neither an elegant nor a flexible solution – especially not, if I change the settings of the fluid-type plugin later on, and then will have to manually adapt all the typography plugin settings to the new clamp outputs.

Is there a better way? Or any best practices?

Fluid paddings, margins, etc. possible?

Hello there,

First of all β€” thank you for this plugin! <3

I'm asking myself how to deal with text that acts as button label, etc. For most of my projekts that would look like:

<a class="inline-flex justify-center border border-black p-4 rounded-md text-lg">Button</a>

In this case I'd would like to have the padding as a fluid as well to keep proportions. I found a workaround by setting the padding to an arbitrary like p-[0.25em] but I'm sure theres better ways...

All the best from Berlin,
Hans

Clamp structure invalid property value

After installing the package and setting it up through the tailwind config it was creating the font size as following :
font-size: clamp(20px) calc(20px + (30 - 20) * ((100vw - 320px) / (1230 - 320))), 30px);
image

It works perfectly fine whenever I change the value within the inspector tool :
font-size: clamp(20px, calc(20px + (30 - 20) * ((100vw - 320px) / (1230 - 320))), 30px);
image

My tailwind setup :
image

After digging around in the index.js I changed line 56 - 58 into 1 complete line :
Before :
image

After :
image

This generated the right value for me.

Did I do something wrong or has this occurred before? Thanks in advance

Can't target exact font sizes on each end of scale?

Hi, I just can seem to get this setup correctly.

I have a requirement to have a font 80/88px on <1500px, and 32/36 on <320px. Scaling is working correctly, but it's unclear from your settings how to correctly setup font size to be exactly 100px on 1500px and exactly 32px on 320px. I have to guess around with decimals and ratio/font-size max doesn't make sense to me? Is there some documentation or calculation that I'm missing? Thanks!

How to determine the array 'values'?

Hi David,
First of all, thank you for this plugin!

In my default Tailwind 'text-' classes I like to name them after the font-size they represent in px (eg: 'text-20' = 20px = 1.25rem)
So I renamed your example values array to:
values: { '20' : [2], // 1.25rem }
But I would like to do this for other font-sizes too that aren't in your example nor the tailwind defaults (like '32px' for example). So how do you calculate which value needs to be between brackets for '32' like [2] was for '20'?

Not so great at maths so probably not seeing something obvious here πŸ˜… thank you!

Only default settings work

I'm trying to customize the configuration, but it doesn't matter what values ​​I put, the default configuration always appears, is it possible to solve this? For example, I would like the text-base to be equal to 1rem in addition to customizing the scaling ratios. Thank you

Support for Padding, Margin?

Hi, thank you very much for creating this plugin. I know that this plugin was developed mainly for the font size. Would there still be the possibility to support padding and margin as well? That would be pretty handy, since the configuration would only need to be set once.

`leading` utility class gets override

Hey, thanks for the plugin

I was coding and noticed it's overriding leading utility class.
image

I replaced addUtilities with addComponents, but I don't think it is fundamentally correct.
Because we have to disable fontSize core plugin - or fluid font size will not work!
image

and sorry for my bad EN :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.