Wednesday 24 December 2014

Refactoring My Website - Part 2

In Part 1 of my website refactoring process, I looked at the overall state of the code in my website and considered some changes and improvements that I could implement without a huge amount of time and effort. I set up a Grunt file and started the process by changing my CSS files into SCSS files so that they can be generated by running the "grunt" task.

In this post, I will talk about how I continued the CSS/SCSS refactor and improved my Grunt file as I worked.

So, my to-do list was as follows:
  • Turn CSS into SCSS (done)
  • Split CSS into SCSS partials
  • Consolidate duplicate CSS properties
  • Change element selectors into classes
  • Nest selectors
  • Use variables
  • Use mixins for projectlistitem selectors? e.g. projectlistitem-UKC
  • Remove /*/mediaquery*/ hack from mediaqueries file (not sure what this is, perhaps it was to support IE7?)
  • Adjust breakpoints (905px and 1105px??)
  • Try to write code mobile-first i.e. no need for small-screen media queries
  • Move media queries into classes or within partials
Split CSS into SCSS partials
Before I even opened up my favourite text editor (for this I am using Coda but I do like Sublime Text 2 as well), I opened my website in a browser, viewed the source and printed the CSS files (tn.css and tn-mediaqueries.css). I then spent around an hour scribbling notes on the printouts, looking for duplicate properties, thinking about the use of element selectors and specificity and how they can be amended/removed, highlighting potential variables and mixins/placeholders. I also concentrated on grouping the CSS as per Jonathan Snook's SMACSS guidelines which I am a huge fan of. It is suitable for small sites as well as large ones so I wanted to give it a go and see how it works for me.

I really liked the method of printing out the code and looking at it away from the computer. I found it easy to flick through the pages and check the different CSS, and it helped to make everything clear in my mind as I hadn't really looked at the CSS for quite a few years. I also felt it was a good warm-up to the main event of re-writing my code.
From my notes and scribbles I could see my files and folders within the "scss" folder would look something like this:

> normalise.scss
> base.scss
> helpers.scss
> variables.scss
> mixins.scss (perhaps)
> placeholders.scss (perhaps)

> layout.scss

> header.scss
> navigation.scss
> breadcrumb.scss
> projectlistitem.scss
> footer.scss

> fontawesome.scss (might move this into modules)

I started to cut and paste the CSS from my tn.scss file into partials, and imported them using "@import" in "tn.scss". At this point I wasn't bothered about what would happen to my mediaqueries file as this would be dealt with in due course. I also took the opportunity to update my Gruntfile so that it watched for any changes in the "scss" folder, using the Grunt Watch plugin:

watch: {
        css: {
          files: ['scss/*/*.scss'],
          tasks: ['sass'],

and I set up my Grunt OS X notifications to save having to toggle to my Terminal window all the time:

notify_hooks: {
   options: {
     enabled: true,
     title: "Grunt tasks completed",
     success: true,
     duration: 5


At times, I noticed that the Grunt notifications weren't displaying and when I checked the Terminal, it hadn't picked up changes to my new partials. I'm not sure why this was happening, but to check that the new tn.css file was being generated correctly I added a comment at the top of every partial:

/**** filename.scss ****/

This allowed me to quickly check the new changes were in the CSS file. I also found that quitting out of Terminal every now and then helped too. Again, no idea can see the updated Gruntfile here. For now, I was happy with this; I could concentrate much more on the SCSS itself with the knowledge that some of my workflow was being dealt with separately.

Next, I moved on to changing the colours into variables. This was quite a good exercise as I had colours scattered throughout my partials, so I was able to briefly review the existing CSS code whilst swapping the hex's for variables. At this point it became apparent that I was using very similar colours throughout the site; #CCC, #DDD, #E2E2E2 - so I quickly compared them in Photoshop and where possible I tried to consolidate my colours.

Then came the fun part: improving the CSS proper, and making the changes to the markup. I made some placeholders for the common properties (e.g. used for h1, h2 headings), I changed all ID's to classes and I made sure I was no longer targeting element selectors i.e. I changed them into classes as well. I also changed any long CSS properties to short-hand to minimise the amount of lines in the CSS. Finally, I removed the now-deprecated <hgroup> element (in the Portfolio main page) and replaced it with <section class="...">.
Afterwards, I moved on to the media queries - I moved them inline within the partials files (therefore the link to the tn-mediaqueries.css file in every page would not be required) and I modified them so that the CSS is written from a mobile-first perspective. You can see a (mostly final) version of my SCSS files and Gruntfile here.

My next task will be to beef up my Gruntfile a bit more - but I'll leave that for another day (it is Christmas Eve after all!)

I hope you found my post useful and hope to see you for the next one!