Am I breaking up my css too much?
I have container that looks like this:
As you can see, I have a lot of classes. I'm writing these classes with reusability, readability, and maintainability in mind. My css is like follows:
etc. you get the point. I'm also following BEM so I'm trying to be super strict about my class names and what they do but I think I might be being too strict but I've never truly worked on professional projects where I could see how they divide their css. Any advice and guidance would be appreciated!
18 Replies
Now that I look at it I could probably delete
.flex
and make 2 classes: .flex-column
and .flex-row
(but now that I look at it again I'm not sure if that would be in the spirit of BEM)?
This is harder than writing actual css 😅I think you might enjoy tailwind css 👀
https://tailwindcss.com/
Why's that 👀
Because is all about small utility classes that you write on the html, hardly ever need to write any css
But I love css! my favorite part about web development 😂
It's just the design patterns that I don't have a clue about haha
Well in that case back on topic
I personally think its fine to make this much separation, like classes like
.flex
and .flex-row
, etc. but only if I use them once or twice for specific cases
Whatever else you plan to use them for, you can and probably should use something else like BEM or whatever other naming convention you come up with.
That's just me though. If I wanted something that flexible I would be using some existing CSS framework/library instead of making my own.Is the way that I wrote/designed classes and BEM mutually exclusive though? For example:
.flex-center
flex is the block and center is the modifier therefore .flex-center
. Or should the block names be more descriptive rather than a general flex
container?
That's a good point but my own css is fun :)Yes, they should be more descriptive and also unique. If you plan on using this only once then it's fine, but of course doesn't really make sense to have two blocks with the same name
If I see
flex-center
on css class I immediately assume it's a utility that serves one very specific purpose though. But you can still use it with BEM as that, a utility, instead of a modifier. A modifier is supposed to be a variation of a block or element.
So you could have card card--transparent flex-center
It means, this particular card is of the transparent variant, and also centered (for whatever reason). Since being centered is not really a quality of card, just a position, I don't think is worth making its own modified for that.
Just a quick off the top example so not entirely sure this applies alwaysNo that makes a lot of sense, thank you! I ended up rewriting it to this:
not quite sure how I like that still but I think it's more inline with BEM
Yeah the thing with BEM is that it's quite a mouthful sometimes. But it allows to see what it's supposed to represent and/or how the code is organized, rather than how it's styled (which is css responsibility)
Right! From what I read it seems class names should be readable/verbose enough to not have to rely on nested selectors and the like
Yeah, sometimes is okay to not write any class for example:
Let's say is for a recipe or something. In this case I really don't think there's much need for
menu__ingredient
. The text itself makes it clear, and if you need to select it with css you can do it easily and without creating conflicts with other rules
That's a valid point! I'm thinking about that with my
phone__wrapper
class right now but I don't think its purpose would be clear if I just did an element selector with it so I'm probably going to keep itMaybe, you can always come back to it later. If you see that you need to repeat the pattern in other places, you can make it its own block. Then instead of
phone__wrapper
you would have container
or something. It's also fine to mix blocks within blocks, though you don't see it often.Awesome, thank you! I can't believe this has taken me almost an hour and a half just to come up with a proper naming scheme 😂
Better believe it 😄
There are only two hard things in computer science: cache invalidation and naming things. — Phil Karlton
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Thank you!! I appreciate your insight 😊😊