Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What are Units in CSS?

CSS, or Cascading Style Sheets, is a fundamental part of web development that allows you to design the look and feel of your website. CSS helps you style elements on a webpage such as text, images, and buttons. One crucial aspect of CSS is the ability to define units of measurement. In this blog post, we'll take a deep dive into the world of CSS units and learn how they help us create beautiful and responsive web designs.

Why Do We Need Units in CSS?

Before we get into the nitty-gritty of units in CSS, let's understand why we need them in the first place. When you create a webpage, you are essentially designing a layout for different elements on the page. These elements can be text, images, buttons, or forms. To position and size these elements, we need to define their dimensions, and for that, we need units of measurement.

CSS units help us define the size, spacing, and positioning of elements on a webpage, allowing us to create responsive designs that look good on different screen sizes and resolutions.

Absolute Units

Absolute units are fixed and do not change based on the size of the viewport (the area of a browser window where the content is displayed) or the font size of the parent element. The most common absolute units are:

  • px (pixels)
  • pt (points)
  • pc (picas)
  • in (inches)
  • cm (centimeters)
  • mm (millimeters)

Pixels (px)

Pixels are the most commonly used absolute unit in CSS. A pixel is the smallest unit of display on a screen. When you define the size of an element in pixels, it will be displayed using the specified number of pixels, regardless of the screen size or resolution.

For example, if you want to set the font size of a paragraph to 16 pixels, you can do so using the following CSS rule:

p {
  font-size: 16px;
}

Points (pt), Picas (pc), Inches (in), Centimeters (cm), and Millimeters (mm)

These units are less commonly used than pixels, but they can still be useful in certain situations. Points, picas, inches, centimeters, and millimeters are all units that are used in print media, and their size is fixed, regardless of the screen size or resolution.

Here's an example of how to set the font size of a paragraph to 12 points:

p {
  font-size: 12pt;
}

Relative Units

Relative units, unlike absolute units, are not fixed and can change based on the size of the viewport or the font size of the parent element. The most common relative units are:

  • em
  • rem
  • % (percentage)
  • vw (viewport width)
  • vh (viewport height)
  • vmin (viewport minimum)
  • vmax (viewport maximum)

em

The em unit is relative to the font size of the parent element. For example, if the parent element has a font size of 16 pixels, then 1em is equal to 16 pixels.

Here's an example of how to set the font size of a paragraph to 1.5 times the font size of its parent element:

p {
  font-size: 1.5em;
}

rem

The rem unit is similar to the em unit, but it is relative to the font size of the root element (usually the <html> element). This means that if the root element has a font size of 16 pixels, then 1rem is equal to 16 pixels.

Here's an example of how to set the font size of a paragraph to 1.5 times the font size of the root element:

p {
  font-size: 1.5rem;
}

Percentage (%)

The percentage unit is relative to the size of the parent element, which can be useful for creating responsive designs. For example, if you want an element to take up 50% of the width of its parent, you can do so using the following CSS rule:

.element {
  width: 50%;
}

Viewport Width (vw), Viewport Height (vh), Viewport Minimum (vmin), and Viewport Maximum (vmax)

Viewport units are relative to the size of the viewport, which makes them great for creating responsive designs that adapt to different screen sizes. There are four viewport units:

  • vw: 1 unit is equal to 1% of the viewport width.
  • vh: 1 unit is equal to 1% of the viewport height.
  • vmin: 1 unit is equal to the smaller of vw and vh.
  • vmax: 1 unit is equal to the larger of vw and vh.

Here's an example of how to set the font size of a paragraph to 5% of the viewport width:

p {
  font-size: 5vw;
}

Putting It All Together

Now that you know the different types of units in CSS, you might be wondering when to use each one. Here are some general guidelines:

  • Use px for small, fixed-size elements and when you need precise control over the element's size.
  • Use em and rem for font sizes and spacing, as they allow for better scaling and accessibility.
  • Use % for layouts and element sizing when you want the element to be relative to its parent.
  • Use viewport units (vw, vh, vmin, vmax) for responsive designs that need to adapt to different screen sizes.

By understanding and using CSS units correctly, you can create beautiful and responsive web designs that look great on any device. So go ahead and start experimenting with different units in your projects and see the magic unfold!