Published: Wed 19 Dec 2018

10 Simple Tips for Front-end Developers

Not exhaustive, not fully-fleshed out, but here's a braindump on what I believe helps sustainable front-end development. Oh, and there's 12 tips, so you can ignore the two controversial ones if you want!

1) Commit compiled CSS as one standalone commit

Commit compiled CSS as one commit separate to the rest of your commits. If not, it's almost impossible to read what has changed in a commit since all the sass maps will be bundled up in it. If the compiled code is committed in one commit (hopefully with the exact same message 'Adding compiled files' or something like that), it's easy to scan through a git log and ignore the complied files commits while focussing on what code a developer changed.

2) Write your code mobile first

This means we write code that is for small screens and/or common to all screen sizes first, then (min-width not max-width) media queries after it.

Avoid this:
  1. .card {
  2. background-color: #ff0000;
  3. font-size: 2rem;
  4. }
  5. @media (max-width: 40em) {
  6. .card {
  7. font-size: 1rem;
  8. }
  9. }
Write this:
  1. .card {
  2. background-color: #ff0000;
  3. font-size: 1rem;
  4. }
  5. @media (min-width: 40em) {
  6. .card {
  7. font-size: 2rem;
  8. }
  9. }

3) Group Media Queries with Selectors

Keep the media query with the selector you are writing about, not at the bottom of the file.

Avoid this:
  1. .class {
  2. // Code here
  3. }
  4. .class2 {
  5. // Code here
  6. }
  7. .class3 {
  8. // Code here
  9. }
  10. @media (min-width: 40em) {
  11. .class {
  12. // Code here
  13. }
  14. .class2 {
  15. // Code here
  16. }
  17. .class3 {
  18. // Code here
  19. }
  20. }
Write this:
  1. .class {
  2. // Code here
  3. }
  4. @media (min-width: 40em) {
  5. .class {
  6. // Code here
  7. }
  8. }
  9. .class2 {
  10. // Code here
  11. }
  12. @media (min-width: 40em) {
  13. .class2 {
  14. // Code here
  15. }
  16. }
  17. .class3 {
  18. // Code here
  19. }
  20. @media (min-width: 40em) {
  21. .class3 {
  22. // Code here
  23. }
  24. }
You'll probably be using sass, which makes this even easier.

4) Avoid Nesting in SASS/SCSS

Do not nest classes in SASS if possible. It’s very hard when you are 400 lines into a file to figure out exactly what is being affected when the nesting gets very deep.

Avoid this:

  1. .content {
  2. margin: auto;
  3. .search {
  4. background: green;
  5. .form {
  6. color: red;
  7. .input {
  8. padding: 1rem;
  9. .button {
  10. background-color: blue;
  11. .button::after {
  12. border: 2px solid black;
  13. .text {
  14. text-decoration: underline;
  15. }
  16. }
  17. }
  18. }
  19. }
  20. }
  21. }
Write this:
  1. .content {
  2. margin: auto;
  3. }
  4. .content .search {
  5. background: green;
  6. }
  7. .search .form {
  8. color: red;
  9. }
  10. .search .input {
  11. padding: 1rem;
  12. }
  13. .search .button {
  14. background-color: blue;
  15. }
  16. .search .button::after {
  17. border: 2px solid black;
  18. }
  19. .search .button-text {
  20. text-decoration: underline;
  21. }
Even better, use BEM classes to make your code much easier to understand, like so:
  1. .content {
  2. margin: auto;
  3. }
  4. .search {
  5. background: green;
  6. }
  7. .search__form {
  8. color: red;
  9. }
  10. .search__input {
  11. padding: 1rem;
  12. }
  13. .search__button {
  14. background-color: blue;
  15. }
  16. .search__button::after {
  17. border: 2px solid black;
  18. }
  19. .search__button-text {
  20. text-decoration: underline;
  21. }
  22. .search__button-text--large {
  23. font-size: 1.5rem;
  24. }

5) Automate the Coding Standards

Use CSScomb to auto format your SASS each time you save a file. This will ensure we are all following the exact same (Drupal, in my case) coding standards and makes it easier for us to compare our code. If only one person on your team uses CSSComb, it means when that person edits one of your files and clicks save, much of the commit changes may just be code reformatting. What I do in this case is open a file, save it, add a commit message "running CSSComb" (so I know I can ignore the contents of that commit), then make my changes and commit those. This means that my commit is the meaningful one where future-me can see exactly what I had changed. If everyone uses CSSComb (or  other auto linter type tool) it'll save this effort.

6) Use CSS Features that You Need: Part 1

Do not use CSS grid unless necessary - IE11 does not support it very well. I know, this is controversial, but most of the layouts we are working on these days (for better or worse) are perfectly achievable by flexbox. If you need to use grid, by all means use it - along with the IE11 autoprefixer. However, I did find on a project recently that the autoprefixer broke more than it fixed. Failing this, if you really want to use grid, campaign for IE11 to be shut down, locked up, and never seen again.

7) Use CSS Features that You Need: Part 2

Do not use custom CSS properties unless necessary. IE11 does not support it. Use $variables in sass instead. Yes, another controversial one and I'm sure I'll be told there's so much more you can do with custom properties. If they fit your use case, use them. They are great. But if it's just for declaring colours as variables, I find sass variables work perfectly and I don't need an IE11 fallback. Failing this, if you really want to use custom properties, campaign for IE11 to be shut down, locked up, and never seen again.

8) Test Your Code

Please test your code in (at least) Chrome, Firefox, Safari, and IE11, as well as mobile devices/browers. Just because your code works in one place does not mean it will work in another. Test early, test often; it'll save you a lot of work in the end - believe me, your client will be testing in more than one browser.

9) Start Finishing, Stop Starting

Complete your tasks first time around. Re-work costs time and money, and makes us go over budget and not have the projects completed on time. We have to get it right eventually, so let’s just get it right first time.

10) Componentise Your Code

Separate your sass partials. We should not have styling for .card in .teaser.

11) Use Visual Regression Testing Tools

I use BackstopJS for my visual regression. It means I can change some CSS in one file and easily check it doesn't break something else. I'll write up a longer post on this soon. But please, start using regression testing.

12) Don't Follow These Guidelines

If these guidelines do not suit you, don't follow them. If you can improve upon them, do so (and write a blog post about it so we can all learn from it).

Following the above 10 points, means we will work faster, smarter, and better.
(And hopefully soon I'll be writing up a similar post saying USE GRID, USE CUSTOM PROPERTIES. (I'm looking forward to that day.))

If you like what I write, you might like to follow me on Twitter @markconroy

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.