Adding utility classes for Clamp(), Max) & Min() in tailwind CSS. #10647
Replies: 5 comments 13 replies
-
That is a very good idea! |
Beta Was this translation helpful? Give feedback.
-
Hey! Any ideas on APIs for this that would be better than what you can currently do with arbitrary values?
|
Beta Was this translation helpful? Give feedback.
-
The syntax should look something like this: fluid.tw |
Beta Was this translation helpful? Give feedback.
-
Adding some good use-cases examples: https://www.smashingmagazine.com/2024/10/css-min-all-the-things/?ref=dailydev |
Beta Was this translation helpful? Give feedback.
-
Revisiting this discussion for v4. Has anyone tried fluid-tailwind plugin with latest version? I'm not sure I like the syntax since it abstracts the existing utility class values. As pointed out above, Seems like this feature would make the most sense being a custom variant that can be used on top of existing utilities (i.e. fs:p-4, fs:text-md, fs:border-2, etc). Anyone tried to solve this with css or is this use case too complicated for a pure css solution? For example, here's a basic fluid scale function mapped to a variable: @layer properties {
:root {
--fs-min-w: 30rem; /* 480px minimum viewport width */
--fs-max-w: 160rem; /* 2560px maximum viewport width */
--fs-min-fs: 1.375rem; /* 22px minimum font size */
--fs-max-fs: 2.75rem; /* 44px maximum font size */
--fs-min-ts: 1.2; /* 1.2 minimum type scale */
--fs-max-ts: 1.25; /* 1.25 maximum type scale */
}
}
@theme inline {
--spacing-fs: clamp(
var(--fs-min-fs),
calc(
/* Base rem value */
var(--fs-min-fs) +
/* Viewport width percentage */
((var(--fs-max-fs) - var(--fs-min-fs)) / (var(--fs-max-w) - var(--fs-min-w))) * 100vw
),
var(--fs-max-fs)
);
} I think the viewport width formula needs to be developed further to account for edge cases (i.e negative numbers, etc). Adding some variety to exclude font sizes and type scales would help with that. Making this work with existing utilities is what I'm not sure about because the docs don't really provide much details about creating complex utilities and variants. From what I understand so far, I would need to override the --spacing variable with a clamp calc using existing modifiers and values for spacing, borders, gap, text, etc (anything that uses px or rem values). I just don't know if --value and --modifier is available inside For example, take this variant: @custom-variant fs {
--spacing: var(--spacing-fs);
@slot;
} Can I nest other functions inside to override some of the utils? (i.e. reduce the scale for borders, using type scale and font sizes for text, etc). Something like this for example (pretty sure this is wrong): @custom-variant fs {
@utility border-* {
--fs-min-fs: calc(--modifier(number) - var(--spacing, 0.25rem));
--fs-max-fs: calc(--modifier(number) + var(--spacing, 0.25rem));
--spacing-fs: clamp(
var(--fs-min-fs),
calc(
/* Base rem value */
var(--fs-min-fs) +
/* Viewport width percentage */
((var(--fs-max-fs) - var(--fs-min-fs)) / (var(--fs-max-w) - var(--fs-min-w))) * 100vw
),
var(--fs-max-fs)
);
border-width: --value(--spacing-fs, --spacing-*, [*]);
}
--spacing: var(--spacing-fs);
@slot;
} Any thoughts on the best path forward for a css solution or does it make more sense to stick to js for something like this? I based the clamp function on this fluid type scale calculator: https://utopia.fyi/type/calculator/?c=480,22,1.2,2560,44,1.25,5,2,&s=0.75|0.5|0.25,1.5|2|3|4|6,s-l&g=s,l,xl,12 |
Beta Was this translation helpful? Give feedback.
-
I believe that Tailwind CSS can benefit from the addition of new classes that use clamp(), min(), and max() functions. These functions can provide more flexibility and efficiency in implementing responsive designs for typography, padding, and containers.
By using clamp(), min(), and max(), you can achieve responsive designs more efficiently and with fewer lines of code. For example, consider the following code without these functions:
By using clamp(), you can achieve the same result with a single line of code:
In conclusion, the addition of clamp(), min(), and max() functions to Tailwind CSS can provide a more streamlined and effective approach to implementing responsive designs for typography, padding, and containers. By including these new classes, Tailwind CSS can stay at the forefront of CSS development and continue to meet the needs of developers in an ever-evolving web development landscape.
Beta Was this translation helpful? Give feedback.
All reactions