Set min-width using tailwind spacing units without global config? - tailwind-css

Is there a way to apply spacified spacing units from tailwind.config.js to e.g. min-width other than by global config? That's as far I can see the only way but would mean to duplicate all (necessary) spacing units which is from my pov a potential error source as each relevant space must get duplicated then.
// Dummy code explaining what I want to achieve
.myButton{
#apply bg-red-100 w-auto;
min-width: #apply w-11 // Not working just for theoretical explanation
}

I'll provide every known option for me
1. Provide value inside config
module.exports = {
theme: {
extend: {
minWidth: {
11: '2.75rem'
}
}
}
}
This will generate min-w-11 class with min-width: 2.75rem. But what if for some reason Tailwind change 11 value to let say 197px, how can we sync it? Well every option has access to default theme options like
module.exports = {
theme: {
extend: {
minWidth: theme => ({
11: theme('spacing[11]')
})
}
}
}
This time min-w-11 will set min-width: 197px;. It referenced here
2. Use theme() directive
It is works inside CSS files like - no need to set config unless you need new value which is not present in default theme
.myButton{
#apply bg-red-100 w-auto;
min-width: theme('spacing[11]'); // 2.75rem
}
More information about theme() directive here
3. With a JIT mode enabled - requires Tailwind version 2.1 or higher
module.exports = {
mode: 'jit'
}
This on its own will generate CSS properties on the fly
<button class="myButton min-w-[2.75rem]">My Button</button>
<button class="myButton min-w-[197px]">My Button</button>
generated properties are
.min-w-\[2\.75rem\] {
min-width: 2.75rem;
}
.min-w-\[197px\] {
min-width: 197px;
}
You can read about JIT here
Please let me know if my answer is not what are you looking for as I may misunderstood question

min-w-[theme('spacing[11]')] also works, tested in Tailwind v3.

Related

How to get a Tailwind theme color's actual value from resolveConfig?

I'm using Next.js and Tailwind. I have a theme defined as a set of CSS vars. How do I access the actual value that results from Tailwind theme colors defined with alphavalue?
When I try to access it via resolveConfig I just get the string rgb(var(--color-one) / <alpha-value>), but the result I would like to get is rgb(255, 0, 0) or rgba(255, 0, 0, 1).
I need to access it for use inside a TSX component file which has an SVG for two different stopColors in the same gradient. Eventually there will be multiple themes, so its best if I can fetch it since the gradient should change with the theme.
global.css
#layer base {
:root {
--color-one: 255 0 0;
}
}
tailwind.config.js
module.exports = {
content: ['./pages/**/*.tsx', './components/**/*.tsx'],
theme: {
extend: {
textColor: {
skin: {
one: 'rgb(var(--color-one) / <alpha-value>)',
}
},
}
}
My TSX component
const { one } = (twFullConfig?.theme?.textColor as any)?.skin || {}
console.log(one)
This logs rgb(var(--color-one) / <alpha-value>) to the console.
How can I get the actual value of the color, rgb(255,0,0), instead of the Tailwind string?
I came up with what feels like an extremely hacky way of doing this: putting classes on a div that is hidden/has no height/width, and then putting a ref on that div, and using window.getComputedStyles to access the styles on the div. If there is a better way to do this I'd like to know because this seems like an ugly solution.

Tailwind: extend theme with a custom _responsive_ spacing

I'm new to Tailwind. I'm currently extending the list of components/utilities with a lot of similary purposed classes:
/* components.css / utilities.css (doesn't matter in this context) */
.pt-section {
#apply pt-16 lg:pt-20;
}
.mt-section {
#apply mt-16 lg:mt-20;
}
.-mt-section {
#apply -mt-16 lg:-mt-20;
}
.top-section {
#apply top-16 lg:top-20;
}
.-top-section {
#apply -top-16 lg:-top-20;
}
/*
Also: pb-section, mb-section, -mb-section bottom-section, -bottom-section, gap-section...
*/
The purpose of this utils is to be able to quickly make e.g. grid gap "same width as the section margin-bottom height" ā€“ pretty usable stuff.
I see that it would be much nicer to define a single responsive value in the config. I already do that for non-responsive constants describing body and header heights:
// tailwind.config.js
theme: {
extend: {
spacing: {
"13": "3.25rem",
"18": "4.5rem",
"header": "5rem",
"header-1": "calc(5rem - 1px)",
"body": "calc(100vh - 5rem)",
"sectionT": ??? should be responsive !!!
"sectionB": ??? should be responsive !!!
}
}
},
In this way TW would auto-derive all that I need. I would get mt-sectionT, gap-sectionB etc. classes automatically.
Is there a way to declare custom responsive (breakpoint-depending) values in the tailwind.config.js? Without writing a custom plugin, I mean.
It sounds like a common case, yet I can't find the answer in the docs (checked https://tailwindcss.com/docs/theme#customizing-the-default-theme and other pages) or on StackOverflow.

How to write many values of property css in tailwind?

I want to convert my css to tailwind but when I find many sources, there is no solution.
Here is My css
.selector {
background-size: contain, cover;
}
and If anyone have clue or solution for another property please feel free to comment the solution šŸ™šŸ»
You cannot use contain and cover at the same time in CSS that is why it is also not possible in tailwind. Choose one, either bg-contain or bg-cover. Here are examples of what it does: https://tailwindcss.com/docs/background-size
You need to extend default background-sizes with your own. First argument is class name and second one is desired CSS property
/** #type {import('tailwindcss').Config} */
module.exports = {
theme: {
extend: {
backgroundSize: {
'contain-cover': 'contain, cover', // here
}
},
},
plugins: [],
}
and use it like bg-contain-cover (you may name it as you wish and use like bg-your-custom-bg-size-name)
DEMO
from the docs https://tailwindcss.com/docs/background-size
you can use multiple using bg-[] syntax
like this
<div class="bg-[length:contain,cover]"></div>
also, make sure you are in the NPM package tailwind.
and there isn't any space between the brackets.
length: is important for making the tailwind you are referring to size, because bg can be color, image, or any other thing.

How to add custom padding-right to tailwind?

I want a larger value for padding right than tailwind currently offers (pr-96 which is around 24rem) Iā€™d like something around 40rem for my project.
Following documentation, I tried, but I cannot run npm run build without an error.
// tailwind.config.js
module.exports = {
theme: {
padding: {
xxl: 'padding-right: 40rem',
}
}
}
ā€™ā€™ā€™
Error:the xx class does not exist, but xx does.
If I remove the code I added above, error goes away.
You don't need to specify the property padding-right in your creation of the xxl utility only the value and unit, this line:
xxl: 'padding-right: 40rem'
should be:
xxl: '40rem'
This will give you an xxl size of 40rem for all padding classes p-xxl, pr-xxl, pl-xxl, etc...
However, you should only use padding as a direct child of theme when you want to replace all padding utilities. If you wanted to just add this size to the existing padding sizes you should use extend inside theme like this:
// tailwind.config.js
module.exports = {
theme: {
extend: {
padding: {
xxl: '40rem'
}
}
}
}

React-Admin - adding material ui theme overrides specific css selector as a global

In React-Admin, I'm trying to apply certain css code inside my Material UI theme as a global attribute. Right when I'm creating my theme, I've been added those lines inside my overrides:
overrides: {
"#global": {
"[class*='RaLayout-content']": {
overflow: "auto !important",
maxWidth: "100vw !important",
},
},
In the entire admin I have many classes like: RaLayout-content-4, RaLayout-content-221, RaLayout-content-31, which are generated by the React-Admin, and I want to apply those css lines in every element that contains the RaLayout-content class.
Because of class names minimization of Heroku deploy, I cannot write those css lines in my index.css cause they'll not apply after the minimization.
Here's how I implemented them before, inside my index.css file (which is working only in development mode):
[class*="RaLayout-content"] {
overflow: auto !important;
max-width: 100vw !important;
}
Notice: I've been also trying to add the MuiCssBaseLine with no success.
Thanks in advance!
I think that you can override the theme with
overrides: {
RaLayout: {
content: {
// your overrides
},
},
...
}

Resources