Blog: CSS

Autoprefixer helps me write better code

Published

In a perfect world, everyone would play by the same rules. But the web isn't perfect. And different browsers need different helping hands to play the same game.

Enter vendor prefixes.

Admittedly modern browsers are a lot better at reading the same language, but still need a helping hand from time to time. Because they feel they can do better. Whatever. Have we not learned anything from those horried IE6-8 days? Anyway...

When I save my LESS or SCSS, CodeKit handles my compilation, and also runs Autoprefixer on the CSS file. What does it do?

It turns this:

.container {
    display: flex;
    flex-direction: column;
    width: 100%;
    transition: all 0.3s;
    box-shadow: 0 0 10px 0 rgba(0,0,0,0.2);
}

Into this:

.container {
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-orient: vertical;
    -webkit-box-direction: normal;
    -ms-flex-direction: column;
    flex-direction: column;
    width: 100%;
    -webkit-transition: all 0.3s;
    transition: all 0.3s;
    -webkit-box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.2);
    box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.2);
}

But why is it better?

I find there are two reasons:

1. My pre-complilation code is easier to read (and write)

If every browser played the same game with "display: flex;" we would be all set. But Microsoft and Webkit browsers still need a prefix, and having to write the prefixes every time I want to use "display: flex" (or any of the other properties that need prefixing) is such a task.

The shorthand of "display: flex;" is just so easy to read - it means my declaration for the .container is simple and I can quickly scan and read every line without having to skip duplicated prefixed lines.

As you can see, as soon as we add "flex-direction", we get 4 lines to define it for all browsers. I know I want my container to be a flex parent with a column of elements - so let me be clean and concise in saying just that, and not have to worry about remembering every prefix option.

With large projects, readability of code is so crucial.

2. My code is less error-prone

If I have to to remember to prefix every property every time, let's face it, I'll forget something somewhere, and then wonder why Safari on iOS decides to behave differently.

Using Autoprefixer takes the responsibility from me, and when using CodeKit, it never forgets I want it done, so I know my final compiled CSS will be ready for any browser. This means I can spend more time on writing more code, rather than remembering every nuance of every vendor prefix.

Blog

View all
JS

Visiting Tiny in Brisbane

Just as the weather started to get cooler in Melbourne, I got an invite up to Brisbane to hang out with the Tiny team for a few days of their Product Week 2019....

Continue reading...

Game

Days Gone

Note: this article is SPOILER FREE. Well, I wasn’t expecting this. When Days Gone was announced, and teased at E3, I thought it looked cool but horror-type...

Continue reading...

JS

TinyMCE 5: URL Dialog Component and Window Messaging

When TinyMCE 5 launched, there was a bit of an outcry that iframe support for dialogs had been removed – so much so that it stopped many developers from upgrading....

Continue reading...

JS

TinyMCE 5: Creating an Autocomplete Plugin

I had a great time last week hanging out with the Tiny team in Brisbane for their Product Week, seeing what the team are up to (and experimenting with during their...

Continue reading...

I am the Development Director (and co-owner) at Mity Digital, a Melbourne-based digital agency specialising in responsive web design, custom web development and graphic design.
Mity Digital