Design comparison
Solution retrospective
Hey guys.. new guy here, few questions wanna to ask
-
Not sure what is the correct name convention for css class names, is there any recommendation?
-
is there anything that I can refactor to keep it DRY
Thanks
Community feedback
- @satropPosted almost 2 years ago
@KyrieeWen - great question.
As a rule of thumb CSS classes are dash-separated, so I would take
CreditCardForm
and change that tocredit-card-form
. Camel case is usually reserved for JS.So if you couple this naming convention with BEM, Block, Emelent, and Modifier you can keep things clean and easy to "human read" in the HTML layer.
Let's take your
credit-card-form
for instance. This will sit on the parent<form>
and has children. Suppose your form has two buttons, submit and cancel following the BEM naming convention their class names might look like this...credit-card-form__button
- this would sit on both buttons and give general styling to them. The__button
is the "E" (element) in BEM. Now we want to style one of the buttons differently. It might look like this...credit-card-form__button--cancel
- this would sit on just one of the buttons and give different styles to that button, it has the modifier class attached to it denoted by the--cancel
double dash followed by the name.You want to keep your class name "human readable" to make it easy for future you or other devs to come in and quickly see what's what.
To add to this, BEM doesn't have logic for grandchildren baked in. And so this isn't a rule at all but something I might do (anyone please feel free to comment about this idea), using your button as an example, might use a single underscore, like this...
credit-card-form__button_button-icon
- this might get a little long so you could keep it simple (ish) and stick to just using the BEM method adding a class name to a grandchild...credit-card-form__button-icon
- this doesn't feel great to me.Hope this helps!
All the best, Steve
Marked as helpful0 - @AdrianoEscarabotePosted almost 2 years ago
Hi Kyrie, how are you? Welcome to the front-end mentor community! I really liked the result of your project, but I have some tips that I think you will enjoy:
To align some content in the center of the screen, always prefer to use
display: flex;
it will make the layout more responsive!body { margin: 0; padding: 0; display: flex; align-items: center; flex-direction: column; justify-content: center; min-height: 100vh; }
Avoid using
width
as this ends up making it difficult to make the project resonant, prefer usingmax-width
!The rest is great!
I hope it helps... 👍
0@KyrieeWenPosted almost 2 years ago@AdrianoEscarabote Hey Adriano. I am good, thank you.
For display:flex and media query breakpoints, whats your recommendation?
I see some people they use media query breakpoints to setup responsive individually.. but I know for this project.. it is easier and less code to use display: flex
0@AdrianoEscarabotePosted almost 2 years ago@KyrieeWen Yes, as this is a small project it helped to make it responsive without any
media query
, but in bigger projects themedia query
is very important!Marked as helpful0 - @SlothSanPosted almost 2 years ago
Hello,
Your CSS class names seem fine to me, they are descriptive and are in kebab case.
As the other person pointed out, look at SCSS and then you can re-use parts of your CSS so that you don't end up repeating yourself.
It will also allow you to nest selectors inside of other selectors to clean up the code.
Looks great, good work!
0@KyrieeWenPosted almost 2 years ago@SlothSan Hey Mike.. thanks for your reply
I used Sass for a while. it has those features you mentioned to use, is there any other tool you recommend to give a try? Thanks
1 - @satropPosted almost 2 years ago
Hi there!
A great place to start for CSS naming convention is the BEM method. BEM stands for Block, Element, Modifier. I wont go over the details here, there are a lot better wrights out there than myself. So a wee Google search will get you off a running in no time. I use it all the time.
As for keeping the CSS stack DRY, I suggest SCSS. If you’ve never used SCSS before I can be a little/a lot daunting but again, there is a plethora of tutorials out there to get you up and running but two really good sources I go to and HIGHLY recommend is a chap on YouTube, Kevin Powell. And a lady who’s channel is called “Coder-Coder”, I can’t recall her name. But these are great places of research.
Hope this helps and keep up the great work!
All the best, Steve.
Edit. Just realized this was a MD editor! So…
0@KyrieeWenPosted almost 2 years ago@satrop Thanks Steven.. really helpful.. save those video links to my bookmark haha but have you ever seen class name like CreditCardForm ? I am not sure this is a good way to write class names.. any thoughts?
1
Please log in to post a comment
Log in with GitHubJoin our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord