Press enter to see results or esc to cancel.

CSS optimizations everyone should be using

CSS is a standard technology in web design, which most web developers use on a day to day basis. As with all vast technologies, you can never know everything about CSS; we have compiled some of our favourite and most valuable CSS tricks that could save you hours of frustration.

Box sizing: border-box

This simple CSS property changes the way that boxes are sized within the browser. Put simply, it calculates the width of the box from the border, as opposed to before the padding. This means that percentage widths can be used more predictably, and is especially useful for responsive designs. There have been many projects I have picked up throughout my career, and a common flaw is odd widths such as 98%, which have only been applied to take into account margin and padding (usually mixing dreaded fixed margin and padding with percentage width boxes). Simply changing the box sizing will save a lot of frustration, and make your code more understandable and maintainable.

 Chaining IDs and classes

It is possible to select an element based on 2 or more classes/ID that are applied to an element. For example, the CSS selector:

Will match all tags with a class of both container and header, such as:

This is a useful way of selecting elements, but not very widely used.

Horizontal center of absolute and fixed elements

A common solution to this problem is using the below:

The main issue here is that you have to know the width of the element, and hard code this into the CSS. This method restricts the element to only ever be fixed width, and is harder to maintain. Another solution is:

This method works in all major browsers (besides IE8), and doesn’t require a fixed width. This is a cleaner solution, and easier to maintain.

Responsive images with max-width

An easy fix for images that need to be responsive is to add:

This will cause the image to scale with the container, so if the container width reduces (such as on a mobile or tablet device), the image will shrink to fill the container in proportion.

You don’t need to use !important…

Ok, so it’s not a trick, more an extremely common misuse of an attribute. The !important attribute is used commonly when styles are being overwritten on an element; it is a quick and dirty fix that when used often results in an overwhelming mess of CSS, and the vast majority of styles also requiring !important to override your other !important styles.

Except in a few situations, if your styles are not being applied it means that your CSS is not cascading properly. You should fix the underlying issue instead of using !important; the time you take to cascade styles properly will be saved many times over during the course of a project.

For example, the second h1 selector in the below CSS will not work:

The problem with this is that it will not select .box-title on:

This is because the first selector cascades deeper, so is given priority. Instead of adding !important to the second selector, you could do the below to cascade deeper than the first selector:

This is more readable, and logically makes more sense as styles should become more specific the further cascaded they are. If other developers want to add to your styles, they can just cascade their own styles correctly to override them, as is intended with CSS!

Final thoughts

I have covered a few useful ways to use CSS in this blog posts, if you have your own useful CSS tip that you use frequently to solve common problems, leave a comment below, and this may be included in a second part of this post.