[Feature Proposal] Add negative/positive as variants option for margin
I really like using the variants setting in config to control what CSS is generated. I am working on a smaller project where I am really trying to cut down a lot of the styles Tailwindcss generates. There is one area that stood out to me, the negative margins. This small project definitely would not need them.
My thoughts were both positive and negative styles are generated by default, but you could use something like this to only output the positive values if you wanted.
variants: {
margin: ["positive"]
},
I think there are approx. 126 styles generated for .-m* with the default config. A small shaving I admit :)
I am obviously not sure that this meets the ethos of variants so if it doesn't, that is fair.
I really like using the
variantssetting in config to control what CSS is generated. I am working on a smaller project where I am really trying to cut down a lot of the styles Tailwindcss generates.
If you really want to cut down the file size, I think you should consider PurgeCSS to remove all unused classes. Alternatively you could specify the margins in your tailwind.config.js file.
Hi @royvanv, thanks for the feedback.
I agree that PurgeCSS will inevitably help. I know there will be a bunch of classes I am not going to use, but this instance caught my attention as more of "prevention" versus a "treatment".
I have added custom margins to the theme -> spacing property. Tailwindcss seems to generate the negative margins automatically. So that may not be the answer.
spacing: {
px: "1px",
"0": "0",
"1": "0.25rem",
"2": "0.5rem",
"3": "0.75rem",
"4": "1rem",
"5": "1.25rem",
"6": "1.5rem",
"8": "2rem",
"10": "2.5rem",
"12": "3rem",
"16": "4rem",
"20": "5rem"
}
Excerpt of the css it produces below:
.m-0 {
margin: 0;
}
.m-1 {
margin: 0.25rem;
}
.m-2 {
margin: 0.5rem;
}
.m-3 {
margin: 0.75rem;
}
.m-4 {
margin: 1rem;
}
.m-5 {
margin: 1.25rem;
}
.m-6 {
margin: 1.5rem;
}
.m-8 {
margin: 2rem;
}
.m-10 {
margin: 2.5rem;
}
.m-12 {
margin: 3rem;
}
.m-16 {
margin: 4rem;
}
.m-20 {
margin: 5rem;
}
.m-auto {
margin: auto;
}
.m-px {
margin: 1px;
}
.-m-1 {
margin: -0.25rem;
}
.-m-2 {
margin: -0.5rem;
}
.-m-3 {
margin: -0.75rem;
}
.-m-4 {
margin: -1rem;
}
.-m-5 {
margin: -1.25rem;
}
.-m-6 {
margin: -1.5rem;
}
.-m-8 {
margin: -2rem;
}
.-m-10 {
margin: -2.5rem;
}
.-m-12 {
margin: -3rem;
}
.-m-16 {
margin: -4rem;
}
.-m-20 {
margin: -5rem;
}
.-m-px {
margin: -1px;
}
Hi @royvanv, thanks for the feedback.
I agree that PurgeCSS will inevitably help. I know there will be a bunch of classes I am not going to use, but this instance caught my attention as more of "prevention" versus a "treatment".
I have added custom margins to the theme -> spacing property. Tailwindcss seems to generate the negative margins automatically. So that may not be the answer.
spacing: { px: "1px", "0": "0", "1": "0.25rem", "2": "0.5rem", "3": "0.75rem", "4": "1rem", "5": "1.25rem", "6": "1.5rem", "8": "2rem", "10": "2.5rem", "12": "3rem", "16": "4rem", "20": "5rem" }Excerpt of the css it produces below:
.m-0 { margin: 0; } .m-1 { margin: 0.25rem; } .m-2 { margin: 0.5rem; } .m-3 { margin: 0.75rem; } .m-4 { margin: 1rem; } .m-5 { margin: 1.25rem; } .m-6 { margin: 1.5rem; } .m-8 { margin: 2rem; } .m-10 { margin: 2.5rem; } .m-12 { margin: 3rem; } .m-16 { margin: 4rem; } .m-20 { margin: 5rem; } .m-auto { margin: auto; } .m-px { margin: 1px; } .-m-1 { margin: -0.25rem; } .-m-2 { margin: -0.5rem; } .-m-3 { margin: -0.75rem; } .-m-4 { margin: -1rem; } .-m-5 { margin: -1.25rem; } .-m-6 { margin: -1.5rem; } .-m-8 { margin: -2rem; } .-m-10 { margin: -2.5rem; } .-m-12 { margin: -3rem; } .-m-16 { margin: -4rem; } .-m-20 { margin: -5rem; } .-m-px { margin: -1px; }
Having a separated config for negative margins would help. That's what I do in my PetricorCSS library (It's scss, but you get the point):
// Spacing
// Margins
$margin: () !default;
$margin: map.merge(
(
'0' : 0,
'1' : 0.25rem, // 4px
'2' : 0.5rem, // 8px
'3' : 1rem, // 16px
'4' : 1.5rem, // 24px
'5' : 2rem, // 32px
'6' : 2.5rem, // 40px
'7' : 3rem, // 48px
'8' : 3.5rem, // 56px
'9' : 4rem, // 64px
'10' : 4.5rem, // 72px
'px' : 0.0625rem, // 1px
'auto': auto,
),
$margin
);
// Negative margins
$margin-negative: () !default;
$margin-negative: map.merge(
(
'1' : -0.25rem, // 4px
'2' : -0.5rem, // 8px
'3' : -1rem, // 16px
'4' : -1.5rem, // 24px
'5' : -2rem, // 32px
'6' : -2.5rem, // 40px
'7' : -3rem, // 48px
'8' : -3.5rem, // 56px
'9' : -4rem, // 64px
'10' : -4.5rem, // 72px
'px' : -0.0625rem, // 1px
),
$margin-negative
);
Update: Being able to enable/disable negative margins independently is also a good thing. We've never use as much negative margins as default margins. It does make sense, at least for me.
// ENABLE/DISABLE VARIANTS
$variants: () !default;
$variants: map.merge(
(
'margin-negative': false,
'margin-stack': 'responsive',
'margin': [responsive],
),
$variants
);
I think this is not a bad idea. And I do not agree with the notion, that purgeCss is the ultimate solution to resolve any bloated css created by tailwind. There are situations, where purgeCss is simply not going work. My own solution to remove unwanted css is completely different and custom and I would invite any more refined way to be able to control any aspect of tailwind config.
That said, I assume there is a way to control - to basically remove the negative margins via the config file. I did not test this, but the stub file contains this portion of code, which is responsible for margins, including negative margins:
margin: (theme, { negative }) => ({
auto: 'auto',
...theme('spacing'),
...negative(theme('spacing')),
}),
Given we are not extending the default generated values, can't you simply remove the spread of negative values? As I said I did not test it, but it looks to me this should not add any negative margins into your final build.
So I would try :
margin: (theme, { negative }) => ({
auto: 'auto',
...theme('spacing'),
})
Please let me know if this worked.
Thanks.
Hi @cizza thank you for the reply.
I can confirm that using:
margin: (theme, { negative }) => ({ auto: 'auto', ...theme('spacing'), })
within the theme section worked, and the output css does not have the negative margins anymore.
I feel this gives me the control I wanted, so I am going to close the issue.