How I’d Learn Front End Web Development Now

When I started out with doing web stuff in 1999, things were very different, much simpler in some ways but much harder in others. So, the way I gradually learnt was quite different from how somebody would learn now.

Back in my day…

Don’t yawn, there is a point to this. In 1999 when I started learning this stuff, there was only HTML, CSS and JavaScript. No TypeScript, no Sass, no real frameworks to speak of, no packages, no build tools. You get the picture.

So, there was a lot less to learn but learning was harder. There wasn’t great documentation online, or, at least, not easy to find, so I used books. Cross browser compatibility was hard.

In short, it’s a completely different landscape now. So, I thought it would be interesting to think about how I would learn or advise others to learn if starting out now.

The basics

The basics are still the basics. HTML, CSS and JavaScript are the technologies used by web browsers. That hasn’t changed in all this time and it’s not likely to change any time soon. While trends will come and go a good understanding of these three areas will always be time well spent.

Accessibility

This is a critical area that is often overlooked. The web is a universal platform that should be for everyone, not just for those with good eyesight, expensive devices and fast networks. We need to consider how we deliver content that works for all.

It’s definitely worth learning this from the beginning so things are done in the right way as it’s harder to correct bad habits later. Being able to demonstrate an understanding of accessibility will put you ahead of other candidates who have skipped over it when it comes to job hunting.

Go deep

Going further, I’d say it’s worth exploring these HTML, CSS and JavaScript in some depth before moving on. I think it’s worth skimming through everything on them at pace without worrying about retaining too much. This could be either a course or just working through documentation. Just becoming familiar with what features there are and some terminology will help you understand more conversations and documentation, as well as know what to search for when the time comes to use them. Nobody can remember it all and even experienced developers have to look up syntax. That’s not generally what an employer would expect, it’s more about the broad understanding of how you can use features to solve problems.

Responsive design

It’s rare these days that we can create a web page that will be for a single screen size, so we need to know how to make a user interface that looks good on a big desktop screen and a phone. This is a whole area that is again worth tackling fairly early on. It’s a bit of an art, a collection of techniques and tricks that are not easy to discover. This is an area that would probably benefit from a course.

Source control and package management

Once you’ve got enough of the basics to make something amazing, you’ll naturally want to share it with the world, and then you’ll want to make use of your amazing thing or other people’s in your future projects.

Source control is about publishing code but also collaboration – reviewing each other’s code, branching out and merging it back together. The most commonly used system for this is probably git. Worth reading up on.

Package management is about bundling up your project into a package that can be easily exported and then imported into other projects. The most common platform for front end code is npm.

These two areas usually involve using the command line, a terminal where you type commands to make things happen. It’s not as scary as it looks and makes you look like a proper hacker to those not in the know.

Frameworks

Probably later than some would expect, but I genuinely feel the other things are more important to get good longer term. If you learn a framework too early their way of doing things becomes your normal and then if you change framework you have to start again. If you understand the core technologies then you can see what the framework is giving you and can adjust more easily.

CSS frameworks can be very useful for providing ready-made styling, bringing consistency and the CSS is well structured. It’s a good way of learning how to structure your own CSS for larger projects.

JavaScript frameworks like React, Vue or Angular take some investment to learn but it is paid off in terms of faster development down the line.

There are also UI frameworks and design systems which provide complete components ready to use.

With all of these frameworks, there’s lots of time saving and knowledge to be gained but it’s important to remember that these are just someone’s opinion and one way of doing things. There is no single right way or best way.

Extending the basics with pre-compilation

I’d say that’s probably everything you need but there are a couple of other bits that are cheap to learn so worth throwing in there – more skills to boast about can’t hurt.

Sass is a superset of CSS – it’s CSS with added bells and whistles. You write in .scss files, making use of the fancy additional features and then it gets compiled to normal CSS. The great part is that it’s additional features, not a different language so CSS is valid Sass. You can start out with a .scss file, just write normal CSS and it gets compiled to CSS. It’s a bit pointless but means you can start off with nothing and then gradually learn and use Sass one feature at a time. It’s a nice easy learning curve.

It’s the same idea with TypeScript. This is enhanced JavaScript so you can write in .ts files and get JavaScript, learning the new features as you go.

With both of these, once you get into the new features you’ll never go back.

Then, anything goes

Once you’ve got these skills, it’s really an open playing field. You can learn whatever takes your interest, or maybe whatever your job needs.

Let me know if you’d do things differently.

Progress Indicators

I’ve been working on a questionnaire with a requirement to show progress through the questions. It has thrown up some interesting challenges which I felt were worth sharing.

When I first saw the requirement I thought it would be simple – progress is just number of questions divided by number of answers, easy maths, right? Well, it could be in a very simple questionnaire but ours threw up a few more curved balls. Here are some additional considerations:

Some questions are mandatory, others are optional, so are we counting all questions or only the mandatory ones?

There is some conditional logic so a question may be shown or hidden depending on how you answer another question. How do we handle that?

Some questions can have multiple answers, like a checkbox list, so how many do we count?

Some answers may automatically answer other questions. I know this sounds odd but it’s a big old questionnaire and sometimes it was useful to repeat some questions and answers in a different context in case it changed the answer.

Our architecture has questions and answers as 2 separate API calls, which each bring back object arrays. Questions and answers are linked by a common data point, which is just an identifier so that we can match them up. So, time to work through our extra considerations…

We only want to count mandatory questions. These are denoted by an isRequired property on each question so we can filter to that like this:

const filteredQuestions = questions.filter(q => q.isRequired);

We also only want to count questions that are shown. We can’t reasonably expect a user to answer a question they haven’t seen. This is similar to the isRequired property but using isVisible, so to filter on both of these (instead of just the one property above) we can do this:

const filteredQuestions = questions.filter(q => q.isRequired && q.isVisible);

If questions have multiple answers, it doesn’t matter, we’re just interested in whether or not they have an answer.

Next we need to deal with duplicated questions and/or answers. We can convert our filtered questions and our answers into arrays of unique data points. First we simplify the array to only hold the data point string, then we dedupe it by converting the array to a set and back again.

const questionDataPoints = questions.map(q => q.dataPoint);
const uniqueQuestionDataPoints = [...new Set(questionDataPoints)];

const answerDataPoints = answers.map(a => a.dataPoint);
const uniqueAnswerDataPoints = [...new Set(answerDataPoints)];

Now that we’ve got a list of unique question data points and a list of unique answer data points we only need to see which of our questions have answers, which we can do like this:

const numberOfQuestions = uniqueQuestionDataPoints.length;
const questionsAnswered = uniqueQuestionDataPoints.filter(q => uniqueAnswerDataPoints.includes(q));
const numberOfAnswers = questionsAnswered.length;

These figures can now be used as our value and max attributes or numerator and denominator in a fraction. To give a percentage we can do this:

const percentage = Math.round((numberOfAnswers/numberOfQuestions) * 100);

If we want a normalised figure, a decimal in the range between 0 and 1, we can stick with the simpler fraction calculation like this:

const normalized = numberOfAnswers/numberOfQuestions;

The <progress> HTML element can take max and value attributes or if we don’t specify a max we can simply pass a normalised value.

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.

Why Are We Making Web Development So Hard?

I don’t think I’m the first person to think this by any stretch. There are lots of infographics around showing the difference between web tooling now and 10 years ago and it’s clearly ballooned at an extraordinary rate.

In the last year or so I’ve started working with Angular (as opposed to Angular.js a.k.a. Angular 1) and have also dabbled in Vue and React. All of these frameworks seem to share the same basic ideas – build everything as a component and push and pull them from the DOM as needed. Each component is made up of a HTML template, embedded CSS styles and a JavaScript controller.

This feels like an efficient way of working, very DRY. But is it? You create a component, let’s say a time picker. One it’s developed you can put it into multiple views or uses relatively quickly. However, if you later modify it to work slightly differently, using adding complexity, you have to test it in each use case and make sure it still works with no detrimental effects.

All UIs are basically built up of HTML, CSS and JavaScript. Traditionally HTML is for content, CSS for styling and JavaScript for reacting to user behaviour. With components we’re adding a whole load of JavaScript just to make the basic elements appear before we even get into any behavioural usage. This adds to page weight (size of files being transferred to the browser) and render times. There’s the library itself, often split into many smaller modules, plus a controller for the UI to assemble it and then the components. It could just be one HTML file, one CSS file and one JavaScript file.

Increasingly, what the developer writes in their editor is becoming further and further from what actually appears in the DOM. The frameworks insert a whole host of IDs and classes to elements, bloating the HTML and making it harder to read. Styles are inserted as inline <style> blocks rather than being cached in an external stylesheet. Opening the browser’s Dev Tools to track down and fix a problem is becoming harder, not easier.

Also, the more tools you use, the more you’ve got to maintain, keep up to date, upgrade and replace. It’s creating work just to keep up and inviting technical debt.

I think that what we’re gaining in terms of being able to develop new UI more quickly is all rooted in developer efficiency and production cost saving. It’s not thinking about the user. We’re making things slower for users. The mobile experience on most sites is still awful and the overhead of these frameworks is partly to blame.

The latest frameworks also rely on good browsers with all the JavaScript features. They do not use progressive enhancement but just grind to a halt on an older browser. This goes against the universal nature of the web – it should be easy to read and easy to write. The bells and whistles on the top is just that and should be optional, not prevent the basics from working.

I think we need to get back to making things simple. Write HTML, CSS and as little JavaScript as we can get away with. Go static wherever possible. Make it fast and simple for users. User experience should trump developer experience.