custom property naming

Hey, i'm kinda stumped on naming conventions for my spacing properties. I use these for things such as padding blocks, flow gaps and tie them in with the utility classes for those. Currently i use the 100 - 900 naming method e.g. --space-400: XYZ; with the values going up in certain increments depending on the project, for example-
--space-100: .5em;
--space-200: 1em;
--space-300: 1.5em;
--space-400: 2em;
--space-500: 2.5em;
--space-600: 3em;
--space-700: 3.5em;
--space-800: 4em;
--space-900: 4.5em;
--space-100: .5em;
--space-200: 1em;
--space-300: 1.5em;
--space-400: 2em;
--space-500: 2.5em;
--space-600: 3em;
--space-700: 3.5em;
--space-800: 4em;
--space-900: 4.5em;
This has been doing the job along with changing the properties values in media queries if they get bigger on different viewports etc. But for a couple of projects i need to extend beyond the 900. So i was wondering whether i should continue to 1000 and so on or go in 25s or 50s such as --space-125: XYZ;. I'd appreciate anyones help with this, thank you in advance!
60 Replies
Chooβ™šπ•‚π•šπ•Ÿπ•˜
Have you considered following the number system used in Tailwind? It seems to work for a lot of projects.
snxxwyy
snxxwyyOPβ€’16mo ago
like naming the properties --space-2.5: 2.5em; and --space-4: 4em; for example?
RΓ€gnar O'ock
RΓ€gnar O'ockβ€’16mo ago
doing that is as good as not having a variable at all don't put your values in your variable names... ever but to answer the post's question. What is stoping you from using --space-1000 ? and do you actually use the whole range of values you are declaring it's not because the increment in the variables name is linear that the values should be too
Chooβ™šπ•‚π•šπ•Ÿπ•˜
No. Look at how Tailwind handles the number in the class names for margins. The actual measurement isn't in that number. https://tailwindcss.com/docs/margin
Margin - Tailwind CSS
Utilities for controlling an element's margin.
snxxwyy
snxxwyyOPβ€’16mo ago
nothing is stopping me at all, i was just asking if that's an okay practice for naming properties for the most part, for the projects i don't that's when i use the 100-900 method as i can leave out some of the numbers. For the projects where i use all of them and above, i go to 1000 and beyond but i was just asking if that was something you should be doing as i was questioning it. ah okay my bad, not familiar with tw, i thought it was that. So from what i've seen, the values increment in 2s and are named in .5s, e.g.
m-0.5 = margin: 0.125rem /*2px*/
m-1 = margin: 0.25rem /*4px*/
m-1.5 = margin: 0.375rem; /*6px*/
m-0.5 = margin: 0.125rem /*2px*/
m-1 = margin: 0.25rem /*4px*/
m-1.5 = margin: 0.375rem; /*6px*/
you'd recommend this sort of thing?
Chooβ™šπ•‚π•šπ•Ÿπ•˜
Yes. Tailwind is used in millions of projects and it seems to work for them.
snxxwyy
snxxwyyOPβ€’16mo ago
alrighty i'll give that a shot. Thank you! so i've set it up to look something like this, you would say this is a more suitable soltution?
--space-0-5: 1rem;
--space-1: 1.2rem;
--space-1-5: 1.4rem;
--space-2: 1.6rem;
--space-2-5: 1.8rem;
--space-3: 2rem;
etc...
--space-0-5: 1rem;
--space-1: 1.2rem;
--space-1-5: 1.4rem;
--space-2: 1.6rem;
--space-2-5: 1.8rem;
--space-3: 2rem;
etc...
Chooβ™šπ•‚π•šπ•Ÿπ•˜
I actually don't like Tailwind, but it works for a lot of projects.
snxxwyy
snxxwyyOPβ€’16mo ago
ah fair enough, what would you usually do in this scenario?
Chooβ™šπ•‚π•šπ•Ÿπ•˜
I like the principle of utility classes. I just don't like how Tailwind names things. I actually would not make all these variables or class names. I make a minimal set of what is needed for the current project instead of a general purpose solution like this.
snxxwyy
snxxwyyOPβ€’16mo ago
ah so you'd experiment with what values are needed and once you have them create your utility class names around them?
Chooβ™šπ•‚π•šπ•Ÿπ•˜
Not exactly experiment. As I am coding, I plan ahead and make what is needed, then I reuse it in other parts.
snxxwyy
snxxwyyOPβ€’16mo ago
ah i didn't mean experiment sorry, that was the wrong word, for example if you had a figma file, you'd get all the spacing values and structure your utility classes around them instead of having general classes like you said?
snxxwyy
snxxwyyOPβ€’16mo ago
ah gotcha, much appreciate your help!
RΓ€gnar O'ock
RΓ€gnar O'ockβ€’16mo ago
it being willdly used doesn't mean it's good...
Chooβ™šπ•‚π•šπ•Ÿπ•˜
I know. I didn't say it's good. I mentioned that because the OP wanted something that works. It has been tested on millions of projects, so we know it works.
b1mind
b1mindβ€’16mo ago
vars + token also if you are going to do .5 numbers you better off keep the 3 digits imo --spacer-150 --spacer-50 I can't say why but I see --spacer-1-5 as a multi-value or something Also best to set all those up and then have a var(--token, fallback) in your code so like --spacer-lg: var(--space-700, 4.5rem); πŸ˜‰ or what ever too much to scroll to make it accurate to what you had haha you get the idea.
snxxwyy
snxxwyyOPβ€’16mo ago
oh i never thought about doing it like that, i have a couple questions if that's okay? if i have this for example what would be the benefit of the fallback for the spacer property?
:root {
--space-200: 1.5em;
--spacer-small: var(--space-200, xyz)
}

.padding-block-small {
padding-block: var(--spacer-small);
}
:root {
--space-200: 1.5em;
--spacer-small: var(--space-200, xyz)
}

.padding-block-small {
padding-block: var(--spacer-small);
}
would i need all the different space properties e.g. --space-200: 1.5em; or could i just use their values as the fallback? for example
:root {
~~--space-200: 1.5em;~~
--spacer-small: var(xyz, 1.5em);
}
:root {
~~--space-200: 1.5em;~~
--spacer-small: var(xyz, 1.5em);
}
would i need to organize the spacer property into small, medium and large values, or can i integrate it into a utility class? for example
<div class="test | padding-block"></div>
<div class="test | padding-block"></div>
.padding-block {
padding-block: var(--padding-block-spacer);
}

.test {
--padding-block-spacer: 1.5em;
}
.padding-block {
padding-block: var(--padding-block-spacer);
}

.test {
--padding-block-spacer: 1.5em;
}
though i guess the last one is actually more than just simply adding a padding-block to test πŸ˜…
b1mind
b1mindβ€’16mo ago
the fallback is just incase you are loading vars from another file and or they are not there for some silly reason. Its kinda over the top honestly. I do it by habit cause I might be setting my tokens afterward so no value yet. so like instead of just coding the hard value I put in the token name and a fallback so its got some styles while I'm coding the component out or what not. Not sure if thats a bad habit or not but its def better than going back to add the var() I got into it from doing fill="var(--fill, currentColor/#defautColor)" on svg's I think lol Like if I had a brand icon I wanted to keep their brand color on if I needed it πŸ€·β€β™‚οΈ i'm weird ik I think fallbacks are cool too b1mindCool I might be wrong in this logic but its mine lol I want a "contextual token" or a more human-readable token but also have the variation of variables to design with. It might be a bit redundant (not as much as it and a fallback but lol) but I find it helps me keep consistency better. like I don't have to remember was my main gutter 500 or 600? Also its allows for like
/* vars */
--white-500: white;
--black-400: slate;

/* tokens */
--text-clr: --white-500;
--primary-bg: --black-400;

@media (prefers-color-scheme: light) {
--text-clr: --black-400;
--primary-bg: --white-500;
}
/* vars */
--white-500: white;
--black-400: slate;

/* tokens */
--text-clr: --white-500;
--primary-bg: --black-400;

@media (prefers-color-scheme: light) {
--text-clr: --black-400;
--primary-bg: --white-500;
}
--text-clr .... might not be the best token but you get the idea πŸ˜„ --primary-txt is what I used yea thats better πŸ€”
snxxwyy
snxxwyyOPβ€’16mo ago
Ohh that actually makes a lot of sense
b1mind
b1mindβ€’16mo ago
oh no I don't have one yet... sorry I'm still coming up with my own naming/system still too (i've been all over with naming things... cause naming shits hard) yea --text-clr might actually fit for mine πŸ€” cause I do --text-lg for sizes gotta stew on that one
snxxwyy
snxxwyyOPβ€’16mo ago
Ah so you prefer to have your gutter values and then set a value called let’s say space-large to the gutter value with a fallback as you showed me?
b1mind
b1mindβ€’16mo ago
not quite so it would be more context than that
snxxwyy
snxxwyyOPβ€’16mo ago
Would you mind explaining so I can understand?
b1mind
b1mindβ€’16mo ago
so like I'm trying to come up with a better way still as I mentioned but I like to have some fluidity between my gutters/padding/spacers but like you would have --gutter: var(--gap-xl) --card-pad: var(--gap-md); not quite... but its close lol you name things how you want but the idea is the same. creat a token that has context to what it does then have a set of vars you can assign to multiple tokens. that make sense for fluidity So you are not changing things in 100 spots or having all these different-sized margins/paddings on everything (something that scares me about TW) Also keep in mind you don't need to go crazy if the project does not need that much "context" sometimes just the var value does the trick dont get all BEM thinking on it πŸ˜„ (find a middle ground or "happy path")
snxxwyy
snxxwyyOPβ€’16mo ago
ah so for example you'd have a setup like this?
:root {
--gap: value (what setup would you use for this? increments etc);
--gap-xl: var(--gap, fallback);
--gutter: var(--gap-xl, fallback)
}

.example-div {
margin-inline: var(--gutter);
}
:root {
--gap: value (what setup would you use for this? increments etc);
--gap-xl: var(--gap, fallback);
--gutter: var(--gap-xl, fallback)
}

.example-div {
margin-inline: var(--gutter);
}
and the fluidity comes because you can do something like this?
.example-div {
--gutter: new value;
margin-inline: var(--gutter);
}
.example-div {
--gutter: new value;
margin-inline: var(--gutter);
}
sorry, meant to reply to the one under that πŸ˜…
b1mind
b1mindβ€’16mo ago
so --gap would take the "main" gap --gap: gap-xl; so then if you wanted gap and gutter to flow together you can easy or if they needed to be a bit different they can sorry I know its hard to understand me sometimes and I'm doing to many things at once atm lol really its less about the naming and more about the logic/function of them You are trying to create a "design system" that allows you to control aspects of the site together or apart depending on your needs. That way when designers go... look that is 32px and thats is 24px you can be like oh right mb ... --gap: --gap-lg; xD I'm still learning too mind you there are lots of design system experts out there that share different opinions and some of the same.
b1mind
b1mindβ€’16mo ago
Two awsome folks and a good watch. Mike is a full time Design systems dev (meaning he pretty much only does CSS all day) His favorite saying is "I write CSS so your devs don't have too" - Aparicio and I love that 🀣 You might pick up on some differences too and have ideas from watching that so do. And share back if you do ❀️ 🌱 we grow together.
snxxwyy
snxxwyyOPβ€’16mo ago
ohh so it'd look more like this? sorry if i keep getting this wrong, i'm trying to understand the best i can
:root {
--value-100: 1em;
--value-900: 9em;

--gap-xl: var(--value-900);

--gutter: var(--gap, fallback)
}

.example-div {
--gap: var(--gap-xl);
margin-inline: var(--gutter);
}
:root {
--value-100: 1em;
--value-900: 9em;

--gap-xl: var(--value-900);

--gutter: var(--gap, fallback)
}

.example-div {
--gap: var(--gap-xl);
margin-inline: var(--gutter);
}
no worries at all, you're a big help oh awesome i'll check that out! will defo share back if i find anything!
b1mind
b1mindβ€’16mo ago
I'm rewatching it now cause I'm in the middle of this myself hehe Trying to build out a sensible system I might reuse for once πŸ˜„ normally I just toss up a few dozen props and call it a day
snxxwyy
snxxwyyOPβ€’16mo ago
Ohhh haha we’re in it together then πŸ˜… Would it be okay if you share your naming conventions once you find them? And if I manage to do anything with mine I can too πŸ˜… Awesome much appreciate that πŸ‘πŸ»
b1mind
b1mindβ€’16mo ago
Naming things is hard I thought you replied in this thread lol to carry on from https://discord.com/channels/436251713830125568/1149025998554271864 I might also start looking at other Design system naming today when I need a break from designing see what others are doing and find something that makes sense to my brain too.
snxxwyy
snxxwyyOPβ€’16mo ago
i was just about to ping you here haha, yeah my fault i'm still stumped on it too tbh can't seem to find an organized flexible design for it that ties in with my flows and padding and other properties @b1mind hope you don't mind the ping, i've been playing around with the one i've got, i know it may quite possibly be redundant or something haha but i feel like i'm getting at something even if it's a little off, anything you'd tweak? it's just those default value sorta things that bugs me, like --spacing-100, they don't feel right to use
:root {
--spacing-100: .5em;
--spacing-200: 1.5em;
--spacing-300: 2.5em;

--gutter-small: var(--spacing-100, 2em);
}


.padding-block-small {
padding-block: var(--gutter-small);
}

.flow > *:not(:last-child) {
margin-bottom: var(--flow-gap, 1rem);
}

.flow-gap-small {
--flow-gap: var(--gutter-small);
}
:root {
--spacing-100: .5em;
--spacing-200: 1.5em;
--spacing-300: 2.5em;

--gutter-small: var(--spacing-100, 2em);
}


.padding-block-small {
padding-block: var(--gutter-small);
}

.flow > *:not(:last-child) {
margin-bottom: var(--flow-gap, 1rem);
}

.flow-gap-small {
--flow-gap: var(--gutter-small);
}
b1mind
b1mindβ€’16mo ago
I'm still all over the place Who would have thought .... naming things is so hard <,<;;
snxxwyy
snxxwyyOPβ€’16mo ago
yeah its quite the curve ball haha
b1mind
b1mindβ€’16mo ago
No description
b1mind
b1mindβ€’16mo ago
I'm over here like... why do I have primary/highlight and font? also I thought I had a mental model around how I wanted to do the "modifier" name first.... but now I'm second guessing lol
Adedoyin Adeyemo Muhammed
This looks great @b1mind
snxxwyy
snxxwyyOPβ€’16mo ago
That seems like a pretty good layout though, definitely gives me some ideas, I have a question though as I see it being done differently sometimes- what’s the difference between doing β€”primary-clr: hsl(var(β€”purple-5)); compared to β€”primary-clr: 0, 0% 0%; for example?
b1mind
b1mindβ€’16mo ago
by not putting the hsl in the first tokens (vars) I can add opacity on them in the other.
snxxwyy
snxxwyyOPβ€’16mo ago
Ohhh I see, and if you didn’t have opacity you’d use the other?
b1mind
b1mindβ€’16mo ago
well no kinda so like --primary-clr-op: hsl(var(--purple), 0.5); I might double back on doing it that way cause of two reasons though. First is I've heard its almost always better to have the solid colors than a opacity for contracts. (so you would find the color value of the opacity on x back ground and get the color) Second is you don't get to treat those as colors anymore in the editor as they are just values. if you notice you don't see the color next to them
b1mind
b1mindβ€’16mo ago
No description
b1mind
b1mindβ€’16mo ago
It's something I hadn't really played with before so I wanted to. it does come in handy when doing overlays though xD like darkening a picture background or dimming the screen for a modal bg.
snxxwyy
snxxwyyOPβ€’16mo ago
Ah yeah I see, I notice some people rather than having the straight values like XYZ will set variables for the hue and lightness etc and have them there, then change those properties in a different section rather than the whole thing Not sure if that’s redundant or not though haha
b1mind
b1mindβ€’16mo ago
yea πŸ€·β€β™‚οΈ
snxxwyy
snxxwyyOPβ€’16mo ago
Appreciate the help πŸ‘πŸ»
snxxwyy
snxxwyyOPβ€’16mo ago
what do you think of this sorta thing? i've kinda come to the conclusion that i'm going to have to set those space properties πŸ˜…, any ideas on the em and rem thing i've got going on too? i feel there's an easier way to do that sort of thing haha
No description
b1mind
b1mindβ€’16mo ago
only thing is make sure you want those gutters to grow with font size em for next set and rem for root like text flow sure probably but like I have to sets of vars for mine so I can keep font flow spacing and gaps gutters seperate. Cause I use rem for one and px for other πŸ˜„ padding* on a button in em/rem? sure maybe if that is your thing. padding* on other elements ? #depends I'm fighting with when I want padding for a spacer (to keep the bg color of the section) or margin flows xD #devLife
snxxwyy
snxxwyyOPβ€’16mo ago
Oh right yeah makes sense haha, should probably change the normal flow to ems So you do a similar thing to me? Hm yeah even i’d have to think about your one πŸ˜…
snxxwyy
snxxwyyOPβ€’16mo ago
would you recommend a utility class for something like a padding block with this layout or simply just defining the padding block in the section?
No description
Joao
Joaoβ€’16mo ago
Well, it's just a utility class so it won't really hurt but this how I like to think about it: padding is part of the element's content, just like font-size or line-height, it actually affects how the content within it is displayed. Therefore, padding should be part of the element's styles. This is in contrast to margin which is not part of the element's content. You can use margin to push other elements and create space for the element but that's a layout issue, not content. An element should look good on its own and then you can apply margin from external sources: flex/grid gap, column system, utility classes, etc to position it according to the layout and other elements around it. When I have an element that may need different amounts of padding when reused in different places, I like to use variants. For example .hero-section, .hero-section-small, .hero-section-vertical, etc. I personally like BEM convention so this would be more like: .hero-section, .hero-section--small,.hero-section--vertical (just quick examples). When I see this in my code I can already tell that the element not only can but is meant to be used in different places. Of course, using a simple utility class may just solve the problem right then and there so... and in fact I actually do this quite a bit πŸ˜… but always with a mental note to come back and "fix it later".
snxxwyy
snxxwyyOPβ€’16mo ago
That actually really helped a lot, thank you for the explanation and your time! so for example, if i have a padding-block that uses two different properties, is it more recommended to define the straight properties within the declaration or have two local properties for the top and bottom?
snxxwyy
snxxwyyOPβ€’16mo ago
No description
Joao
Joaoβ€’16mo ago
I don't think it's necessary, you can use the custom variables that you already have. If you need to use different values for them, you can always overwrite them inside that particular scope without affecting the rest of elements.
snxxwyy
snxxwyyOPβ€’16mo ago
there’s one different for the top and bottom, the padding is using two globally scopes variables defined in root, I’m not changing the values of them, I was asking since I was using two if I should make two local ones for the top and bottom
Joao
Joaoβ€’16mo ago
Oh I see Well, I guess if you're only going to make them available inside that scope... why not just write the values directly? But nothing wrong with that approach, perhaps the only downside is that its a bit more code to read and keep track of
snxxwyy
snxxwyyOPβ€’16mo ago
yeah very true, i think i'll go with writing the values directly then, thank you
Want results from more Discord servers?
Add your server