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
PHP

Extending the ItemHelper for Joomla

There are times in Joomla where you want a bit of control over your content at the template override level – such as showing the first X characters of a string. PHP...

Continue reading...

JS

How Tiny helps me deliver the best content authoring experience

At Joomla Day Australia 2019 in Brisbane, I spoke about how TinyMCE helps deliver the best content authoring experience. And for those who couldn’t make it on...

Continue reading...

Photo

How to show real-time highlights and shadows clipping in Photoshop

I’ve used Photoshop for years. Decades even. Yikes, showing age there. But for my photography, I tended to use Photoshop for specific things – such as cleaning...

Continue reading...

CSS

Safari: quirky or rule follower?

CSS

Internet Explorer still exists. Just. And while its last-supported version, Internet Explorer 11, generally behaves nicely, it’s the little things that can make...

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