Blog

The Dangers of Logic in CSS

We can do a lot of logic in CSS. I’m thinking specifically of scenarios which take the form “if this, then that”.

There are media queries which will let us control the appearance of elements based on a variety of factors. Probably the most common is screen width, so we get things like “if the width is 1024px or higher, then show an extra element”.

There are pseudo selectors like :empty, :checked, :valid, :disabled which let us control appearance based on state, giving us, for example, “if the parent element has no children (:empty), then change its opacity”.

We can also do logic based on combinations of elements or selectors, like li + li, for example, “if the list item follows another list item, then give it a top border.”

I’m sure there are plenty more ways of applying logic but you get the idea. Each of these is equivalent to writing a conditional statement in a programming language – if this, then that.

The big question is, should we be doing this in CSS, and is there a point at which it becomes too difficult to manage in CSS and we need to defer to another technology, like JavaScript?

My Experience

I started thinking about this when I was designing a UI with a lot of logic going on. We had a busy table where we show some additional columns at wider screen resolutions. At smaller widths the user would be able to toggle between different groups of columns and save a preference. But then these additional columns are only offered if they contain data. So, we had logic around the screen width, user preference and presence of data, which, multiplied out, meant 8 permutations.

I initially tried to do it all with CSS. I set up media queries for the narrower and wider screen widths. Within each of these I then read a class for the user preference and then I used :empty to see if table cells contained any data. I managed it and it worked. Just.

However, I realised that it was really not scalable. Every time I added a new condition it doubled the amount of CSS I was having to write. If we wanted to add more logic in future it would create more and more work and be harder and harder to maintain.

By moving all the logic into JavaScript we can set simpler classes, one for each type of display we need rather than every permutation of logic. So now we just have 3 classes: 1) show the first set of columns, 2) show the second set or 3) show all columns.

We still have lots of branches in our JavaScript but the big difference is that it’s easy to read and follow. It’s also easy to edit or extend if we ever need to.

I think doing some logic with CSS is fine but as soon as you’re starting to layer it on and find yourself multiplying out the selectors you’re writing it’s probably time to move it over to JavaScript.

Clicking Off Things to Close Them

There’s a common design pattern these days where a bit of content is shown over the main screen. It might be a modal window or maybe a fly-in menu or sidebar. It’s also common, especially in native apps, that we can just click or tap in the space not used by these elements to close them. So, how can we do this?

I think there are 2 approaches, one which is pure JavaScript and another which involves an overlay element. Let’s look at both.

JavaScript Document Click Event Method

We can detect that a click is not on the content element using JavaScript. As all click events bubble up or propagate through the DOM tree they eventually reach the top document level. We can listen for a click on the document and then use the event target to check what was clicked. For this example let’s assume our content element has a class of ‘modal’, like <div class=”modal”></div>.

// JavaScript

function handleClick(event) {
  if (!event.target.closest('.modal')) {
    console.log('close');
  }
}

document.addEventListener('click', handleClick);

event.target gets the element that was clicked. This could be the modal element itself, another element inside it or another element outside of it. event.target.closest(‘.modal’) checks if the element clicked is the modal or is an antecedent of the modal element, an element within it. If the element clicked is not (!) in the modal we can close it.

It’s maybe worth noting that .closest() doesn’t work in IE11 if you need to support this but there is a polyfill available.

Using an Overlay Element

The other approach, and the one I tend to use, is to use an overlay element. This means adding an element that covers the whole screen area, slipped in between the modal and the main screen content. The idea is that this overlay will appear and disappear along with the modal and will pick up any click events which are not on the modal. It’s a bit like a safety net that will catch any stray clicks.

The CSS for the overlay would typically look something like this:

/* CSS */

.overlay {
  background-color:#000;
  bottom:0;
  left:0;
  opacity:.5;
  position:fixed;
  right:0;
  top:0;
  z-index:1;
}

.modal {
  /* styling */
  z-index:2;
}

The overlay has fixed display and goes to each edge so it fills the screen.

The overlay has z-index set to 1 so it’s over the main screen content, and the modal has it set to 2 so it’s over the overlay. These can increase as needed but the modal always needs to be higher.

I’ve given the overlay a background colour and 50% (.5) opacity so that it veils the main screen content underneath giving the modal a “lightbox” effect but these are not needed and our overlay can be effectively invisible.

You then just add a click event to the overlay which will close the modal and hide the overlay.

The reason I like this approach is that by covering the main screen we prevent any interactions with it. If the user tries to click on a button under the overlay their first click will just close the modal. We don’t need to worry about any other interactions – while the modal is showing it becomes the sole focus.

Extra tip. If you use a visible overlay it’s nicer not to show and hide it immediately but fade it in and out with a transition on the opacity, taking it between 0 and .5. This feels much smoother and less jerky.

Tips for Working from Home

I’ve been working from home 2 days a week for a few years so thought I’d share a few tips. These are little things that work for me – they may not be for everyone.

Get Showered and Dressed

It sounds obvious but don’t slouch around in your pyjamas. You need to make a work day different from the weekend and any little things you can do to create this clear separation will help your work-life balance and ensure you’re in the right mindset, both ways.

Set Start and End Times

Don’t try to be too flexible with your hours. Stick to a normal working day. Don’t slack off and think you can catch up later in your own time, and don’t do extra to overcompensate for being at home. You need clear times when work stops and home life starts. It’s very easy to get sucked in to “just finishing off this bit” and still be working hours later.

Find Multiple Locations to Work

If you’re working from home for several consecutive days it can help to vary where you work. I wouldn’t keep moving in one day but maybe start every third day in a new spot. For some people having a fixed work space within their home helps the separation so if this works for you, great.

Finding a Good Work Space

For me it’s important to be near a window. I actually work sitting in a bay window looking out onto my street. This naturally makes me look up from my screen from time to time as people pass by. My sleepy town isn’t interesting enough for this to become a distraction but I appreciate this may not work somewhere busier. I would avoid spaces with no natural light, like lofts or basements. They’re just not good for your state of mind.

Lunch

Set a start and end time for lunch as you would in an office environment. If you don’t, you risk not finding time at all, not taking a break or the day becomes one continuous snack-fest, which is not going to end well over a sustained period of time.

Managing Costs

If you’re home alone, do you really want to be heating your whole house? Or, if you’re lucky enough to be somewhere warmer, using air-con? I try to stick to being in one room, close the door and use a portable heater. This saves a small fortune on my heating bill.

Think about the money you’re saving on travel and put it aside for something which improves your home working lifestyle – a comfy office chair, a desk fan, a fancy microphone, a coffee machine.

Balancing Non-work Activity

Working from home does bring some advantages. You can have goods delivered. You can have the washing machine or dishwasher going. My tip would be to not worry about the small things like answering the door, putting a load of washing on – things that take a trivial amount of time. You should avoid the tasks that take longer and can be left until after work – online shopping, folding washing, cooking.

Distractions

You’re at work. You can ignore the door. You can ignore your phone. Don’t use social media, unless it’s work related.

Don’t Get Isolated

If you’re alone for a period of time it’s easy to start to feel isolated. When working from home you need to make extra effort to communicate with colleagues. Try to make the effort to actually talk to people rather than just using email or messaging services like Slack. You won’t run into colleagues in communal areas, like the office water cooler or kitchen, so, if a few of you are working remotely you may need to create your own virtual spaces where non work related chat can happen to stay connected on a human level.

What Works for You?

Everyone’s different so do what works for you and if you have any good tips of your own please share.

Switching APIs

It’s completely normal now for web apps to work with external third party JavaScript APIs. The good ones give you lots of documentation making them pretty straightforward to implement and use. You load a script from a URL or local resource, use their methods and properties in your code and away you go.

But what if you then want to switch to another API provider? Maybe the one you were using isn’t accessible, doesn’t support the older browsers you need? Maybe they put their prices up? Whatever the reason you may need to switch at some point.

This change means rewriting your app’s JavaScript to work with the new API. You’ll have to find every point it touches and update it. What if it works differently? What if this one takes an object parameter rather than an array? You’ll have to completely rewrite the data parts too.

There is a better way.

Rather than working directly with an API and using the properties and methods in your code, you create an intermediary, your own internal service, which handles this functionality. The internal service maps the properties and methods you need to those of the third party API. It handles any data transformation needed. Your app works with this service, your own API, so that when a change of service provider is needed the app itself does not need to be rewritten, just the internal service.

An internal service can also be set up to work with multiple APIs at once so it maps everything it needs for each. This allows you to push choice to your users. So, for example, if it’s a news, sport or weather feed built into a site, you can let your user choice their preferred information source. You could use different services for different regions, e.g. local news.

To use a slightly inappropriate analogy, it’s better to be dating these APIs than to move in with them. 😉