Skip to content

Creating freeCodeCamp's CSS Style guide #16445

Closed

Description

@tomasvn

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.

Browser Rounding

Activity

  1. tomasvn commented on Jan 8, 2018

    @tomasvn
    Author

    @raisedadead What do you think?

  2. reopened this on Jan 8, 2018
  3. changed the title [-]Creating FreeCodeCamp's CSS Styleguide[/-] [+]Creating freeCodeCamp's CSS Style guide[/+] on Jan 8, 2018
  4. raisedadead commented on Jan 8, 2018

    @raisedadead
    Member

    Thanks, are you aware of any way can lint these? Using any linters?

  5. vkWeb commented on Jan 8, 2018

    @vkWeb
    Member

    @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 Prettier and other similar extensions for their CSS Style formatting.

    Instead of creating a style guide. We should tell our contributors to Install Prettier In their IDE/Code editor and we'll be good to go.
    @raisedadead Thoughts on this?

  6. vkWeb commented on Jan 8, 2018

    @vkWeb
    Member

    Thanks, are you aware of any way can lint these? Using any linters?

    Prettier works well ;). It warns you when you violate any CSS style rule.

  7. raisedadead commented on Jan 8, 2018

    @raisedadead
    Member

    Instead of creating a style guide. We should tell our contributors to Install Prettier In their IDE/Code editor and we'll be good to go.

    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

  8. raisedadead commented on Jan 8, 2018

    @raisedadead
    Member

    According to me the process of creating a CSS style guide and sticking to it will in long-term not yield useful results.

    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.

  9. vkWeb commented on Jan 8, 2018

    @vkWeb
    Member

    @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?

  10. vkWeb commented on Jan 8, 2018

    @vkWeb
    Member

    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 every day.

    I strongly agree with you on this! Their JS style guides are really very helpful.

  11. raisedadead commented on Jan 8, 2018

    @raisedadead
    Member

    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?

    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.

  12. raisedadead commented on Jan 8, 2018

    @raisedadead
    Member

    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.

  13. vkWeb commented on Jan 8, 2018

    @vkWeb
    Member

    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.

    I'm with you on this too. Okay, let's go with a style guide and we can use stylelint as our linter.

  14. 30 remaining items

  15. ahmaxed commented on Oct 5, 2020

    @ahmaxed
    Member

    @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.

  16. ManasMahanand commented on Oct 6, 2020

    @ManasMahanand

    @ahmadabdolsaheb Will do!

  17. ManasMahanand commented on Oct 7, 2020

    @ManasMahanand

    @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?

  18. ManasMahanand commented on Oct 7, 2020

    @ManasMahanand

    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

  19. ManasMahanand commented on Oct 7, 2020

    @ManasMahanand

    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

  20. ManasMahanand commented on Oct 7, 2020

    @ManasMahanand

    image

    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?

  21. tomasvn commented on Oct 7, 2020

    @tomasvn
    Author

    No need to remove prettier, prettier has config for stylelint rules that you can use to extend prettier config file

  22. ManasMahanand commented on Oct 7, 2020

    @ManasMahanand

    Awesome. Will work on it tomorrow.

  23. ManasMahanand commented on Oct 8, 2020

    @ManasMahanand

    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

  24. ManasMahanand commented on Oct 8, 2020

    @ManasMahanand

    (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.

  25. ahmaxed commented on Oct 15, 2020

    @ahmaxed
    Member

    Thanks @ManasMahanand on your progress. I am tagging @ojeytonwilliams for a second opinion.

  26. ojeytonwilliams commented on Oct 15, 2020

    @ojeytonwilliams
    Contributor

    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.

  27. ahmaxed commented on Feb 2, 2021

    @ahmaxed
    Member

    @ManasMahanand I was wondering about your progress with your solution. let me know if I could be of any help.

  28. ahmaxed commented on Jul 23, 2021

    @ahmaxed
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

help wantedstatus: on the roadmap

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions