An Introduction to CSS Units

This tutorial explores what CSS units are and how to use them when developing a website

This post may contain affiliate links. If you use these links to buy something we may earn a commission. Thanks.

In this tutorial we’re going to cover CSS units, what they are, why they’re necessary and give a few examples of how to use them for those just getting started with learning CSS.

In basic terms ‘units’ are the unit of measurement that follows a number within CSS.

For example if you’re setting the size of some text to 24px the unit here is px.

So firstly what units are there:

Relative Lengths:

Vh – This is relative to 1% the height of the browser window.

Vw – This is relative to 1% the width of the browser window.

Vmin – This is relative to 1% of the width of the browser windows’ smaller dimension.

Vmax – This is relative to 1% of the width of the browser windows’ larger dimension.

Rem – This is relative to the font size of the root element.

Em – This is relative to the font size of the element.

% – Relative to the parent element.

Common / Fixed / Absolute Lengths:

cm – Centimeters

pt – Points (1pt = 1/72 of 1in)

px – Pixels (1px = 1/96th of 1in)

in – Inches (1in = 96px)

pc – Picas  (1pc = 12pt)

mm – Millimeters

Implementing CSS units:

When developing websites you will come across multiple different reasons to use different units for example you may want to scale an item relative to the view port (width of the browser window) this is where you’ll use Vh or VW.

However as many screens are based on pixel widths you may set elements to a specific pixel size. This will all depend on the elements that you’re creating and how they’re expected to scale across mobile and desktop devices.

It’s important to ensure that when creating a website you consider all device widths and scales or at least as many as is reasonably possible and aim to keep your elements appropriately sized across devices.

For example a section with 4 Images may look great on desktop but it’s likely that the images will be much smaller on a mobile device and will look better perhaps in a row of 2 or 1 to match their size.

It’s also important to consider the overall layout of your website when implementing the units that your site is going to use. For example when using units such as Rem, Em and any others that take their size from other components within the website.

Further reading:

Understanding CSS font weights

We hope this provides you with an idea of how to utilise CSS units and what they are. We’d appreciate a share or any comments if you found this useful, thanks!

Tech Typed
Tech Typed
Articles: 186

Leave a Reply

Your email address will not be published. Required fields are marked *