Repository navigation
Description
I would like to propose freeCodeCamp's CSS Style guide to improve overall maintainability of the codebase. As the FreeCodeCamp has a lot of contributors, and everyone writes the CSS in a different way. And it is hard to keep of track of different ways of writing CSS.
Initial Recommendations:
Comments
-
Add comments, when you are starting a new LESS file, so the developers know what are they working with, also add comments when you start new section which is related the code block, it helps with the readability, see EXAMPLES below (I have encountered these style of comments across the code base, therefore I would like to continue writing it, instead of proposing a new style)
-
Example 1 - when starting a new LESS file:
//
// Navigation
// --------------------
- Example 2 - when adding a section, related to navigation
//
// Navigation - Mobile Styles
// ------------------------------
Spacings
-
Add spaces after a property, it will be much easier to read such a codebase, and maintain. See EXAMPLES below.
-
Example 1 - Correct Way
p {
color: #fff;
}
- Example 2 - Incorrect Way
- What is wrong?
No space after a colon
p {
color:#fff;
}
Colors
-
Prefer lowercase, instead of uppercase. Now across the CSS, contributors are mixing both. Lower case is faster to write, easier to read. Again improves overall maintainability, if the CSS has consistent convention. Also if possible shorten the hex value. See EXAMPLES below.
-
Example 1 - Correct Way
p {
color: #fff;
}
- Example 2 - Incorrect Way
- What is wrong?
upper-cased hex value, not shortened
p {
color: #FFFFFF;
}
RGBA or RGB colors
-
Add spaces after values, improves readability. See EXAMPLES below:
-
EXAMPLE 1 - Correct Way
p {
color: rgba(123, 123, 0, 0.1);
}
- EXAMPLE 2 - Incorrect Way
- What is wrong?
No spaces after values
p {
color: rgba(123,123,0,0.1);
}
##No unit when declaring 0
-
In property such as box-shadow, if you set a 0 value, then you should avoid adding the unit. See EXAMPLE below.
-
EXAMPLE 1 - Correct Way
p {
box-shadow: 1px 1px 0 rgb(0, 0, 0);
}
- EXAMPLE 2 - Incorrect Way
- What is wrong?
Units after 0
p {
box-shadow: 1px 1px 0px rgb(0, 0, 0);
}
Avoid floating numbers
- If possible you should avoid floating numbers in CSS for different sizing, as it could render a bit differently. See link attached below.
Activity
tomasvn commentedon Jan 8, 2018
@raisedadead What do you think?
[-]Creating FreeCodeCamp's CSS Styleguide[/-][+]Creating freeCodeCamp's CSS Style guide[/+]raisedadead commentedon Jan 8, 2018
Thanks, are you aware of any way can lint these? Using any linters?
vkWeb commentedon Jan 8, 2018
@raisedadead @tomasvn According to me the process of creating a CSS style guide and sticking to it will in long-term not yield useful results. React, Yarn, Babel and lots of other open source projects use
Prettierand other similar extensions for their CSS Style formatting.Instead of creating a style guide. We should tell our contributors to Install
PrettierIn their IDE/Code editor and we'll be good to go.@raisedadead Thoughts on this?
vkWeb commentedon Jan 8, 2018
Prettier works well ;). It warns you when you violate any CSS style rule.
raisedadead commentedon Jan 8, 2018
Hiding from problems never helped anyone... 😅 !
I am strongly in favor of avoiding formatters and using linters instead. Automating things sound cool and easy, but they take away the key point of writing standard code.
IMHO, we being the community around learning should not shy away with fixing issues in code style pointed by linters, rather.
Also please note Prettier is an Opinionated Code Formatter
raisedadead commentedon Jan 8, 2018
I think, this is is an incorrect argument (feel free to prove me wrong if you have stats).
If you check the Airbnb JS style guide, JS Standard etc. they, have helper write standard code and are being used by thousands of companies, and devs everyday.
vkWeb commentedon Jan 8, 2018
@raisedadead We're not hiding away from problems...! We can use linters but the same task can be done with a single key binding with Prettier so why we're trying to take a long run on it?
vkWeb commentedon Jan 8, 2018
I strongly agree with you on this! Their JS style guides are really very helpful.
raisedadead commentedon Jan 8, 2018
Because, we want to write standard code? Contributors are free to use any tools they like, but that does not dictate we do not need of a guide.
raisedadead commentedon Jan 8, 2018
Anyways, lets keep this thread for constructive feedback on the guide, we can always have discussions in the chat instead.
@tomasvn RFC looks interesting, and since you are interested in the feedback, do you think it will be possible for you to look up some, established style guides and tooling that we can take inspiration from.
vkWeb commentedon Jan 8, 2018
I'm with you on this too. Okay, let's go with a style guide and we can use stylelint as our linter.
30 remaining items
ahmaxed commentedon Oct 5, 2020
@ManasMahanand, thank you for your interest.
It would be great if you could look into the available css linters, compare them and see if there is anything better than stylelint.
Then, add the linter to our repo and change the CSS files to make sure the repo is linting error free.
ManasMahanand commentedon Oct 6, 2020
@ahmadabdolsaheb Will do!
ManasMahanand commentedon Oct 7, 2020
@ahmadabdolsaheb Wait what do you mean add to repo?
Do you want me to install the linter as a dependency, like eslint is already?
Or is there a way of installing tests on github itself, so it won't allow merging unless the style guide is followed?
ManasMahanand commentedon Oct 7, 2020
Ok, looks like stylelint is a good option itself. I am installing that.
For experimenting, I will set rules as suggested by OP. I will then remove all rules and open a pr. We can then discuss and set rules
ManasMahanand commentedon Oct 7, 2020
https://stylelint.io/user-guide/rules/about
You can have a look at this link to see the types of rules that can be set
ManasMahanand commentedon Oct 7, 2020
currently there is already a lint:css command that uses prettier. We can change it to use stylelint.
Any how, I have installed it, and it successfully detects errors based on rules set. Shall I open a PR?
tomasvn commentedon Oct 7, 2020
No need to remove prettier, prettier has config for stylelint rules that you can use to extend prettier config file
ManasMahanand commentedon Oct 7, 2020
Awesome. Will work on it tomorrow.
ManasMahanand commentedon Oct 8, 2020
I didn't find prettier config for stylelint rules (or how to do that), but currently I have installed another package stylelint-config-prettier that will check for conflicts and will turn off any rules that does conflict.
What I am thinking is in lint:css command we can also run the stylelint command as well along with prettier
ManasMahanand commentedon Oct 8, 2020
(Opinion) I feel that we can write all custom stylelint rules based on what makes sense for fcc, and even completely remove prettier. This is just an opinion, it is left to you guys to decide.
ahmaxed commentedon Oct 15, 2020
Thanks @ManasMahanand on your progress. I am tagging @ojeytonwilliams for a second opinion.
ojeytonwilliams commentedon Oct 15, 2020
I think prettier does a good job with formatting, so I'd rather keep that. If we use https://github.com/prettier/stylelint-prettier then prettier is used as a plugin of stylelint and we get the best of both worlds.
This mirrors the approach we already use for JavaScript linting.
ahmaxed commentedon Feb 2, 2021
@ManasMahanand I was wondering about your progress with your solution. let me know if I could be of any help.
ahmaxed commentedon Jul 23, 2021
Since we are moving away from custom css and leaning towards our component libraries's tailwind for most of our styling, adding CSS style guide would not be necessary; However, I added a note to add a CSS linter to our component library which should address this issue.
I will go ahead and close this issue accordingly. Let me know if this issue has been closed by mistake or if it needs further discussion. Thank you.