CSS Units Reference

CSS units are used to measure various properties such as length, width, and spacing in web development. Understanding and using different units is crucial for creating responsive and visually appealing designs. This guide will provide a detailed reference on CSS units, ranging from basic length units to more advanced ones.

Basic Length Units

Pixels (px)

The standard unit for screen display. One pixel corresponds to one dot on the screen.

				
					div {
  width: 200px;
}

				
			

Percentage (%)

Represents a percentage of the parent element’s size.

				
					img {
  width: 50%;
}

				
			

Viewport Width (vw) and Height (vh)

Relative to the viewport’s width and height, respectively.

				
					section {
  width: 50vw;
  height: 80vh;
}

				
			

Font-Related Units

EM and REM

Relative to the font-size of the element ('em') or the root element ('rem').

				
					p {
  font-size: 1.2em;
}

h1 {
  margin-bottom: 2rem;
}

				
			

Ch

Represents the width of the “0” (zero) character.

				
					input {
  width: 20ch;
}

				
			

Viewport-Percentage Lengths

Viewport Percentage Width (vw)

Represents a percentage of the viewport’s width.

				
					div {
  width: 50vw;
}

				
			

Viewport Percentage Height (vh)

Represents a percentage of the viewport’s height.

				
					div {
  height: 80vh;
}

				
			

Advanced Length Units

Viewpoint-Percentage Min (vmin) and Max (vmax)

Represent the smaller ('vmin') or larger ('vmax') of the viewport’s height and width.

				
					div {
  width: 40vmin;
  height: 60vmax;
}

				
			

Fractional Units (fr)

Represents a fraction of the available space in a grid container.

				
					.column {
  width: 1fr;
}

				
			

Calc() Function

Allows mathematical calculations to determine property values, combining different units.

				
					div {
  width: calc(50% - 20px);
}

				
			

Responsive Line Height (clamp())

Use the 'clamp()' function for responsive line heights.

				
					p {
  line-height: clamp(1.2, 5vw, 2);
}

				
			

Angle Units

Degrees (deg)

Represents an angle.

				
					transform: rotate(45deg);

				
			

Gradians (grad)

Divides the right angle into 100 grads.

				
					div {
  transform: rotate(90grad);
}

				
			

Radians (rad)

Represents an angle in radians.

				
					div {
  transform: rotate(1rad);
}

				
			

Turns (turn)

Represents a full rotation (360 degrees).

				
					div {
  transform: rotate(0.25turn);
}

				
			

Direction Values (to top, to right, etc.)

Simplifies angle values for gradients, providing a more intuitive syntax.

				
					div {
  background: linear-gradient(to right, red, blue);
}

				
			

Time Units

Seconds (s) and Milliseconds (ms)

Used in animations and transitions.

				
					div {
  transition: width 1s ease-in-out;
}

				
			

Advanced Viewport Units

Calculation with Viewport Units

Combine viewport units with 'calc()' for complex responsive layouts.

				
					section {
  width: calc(100vw - 20px);
}

				
			

Media Query Units

Device Pixel Ratio (dppx)

Use in media queries to target high-density displays.

				
					@media screen and (min-resolution: 2dppx) {
  /* styles for high-density displays */
}

				
			

Practical Considerations

Fluid Typography

Implement fluid typography to ensure text scales appropriately across different screen sizes.

				
					body {
  font-size: 16px;
  font-size: 1rem;
}

@media screen and (min-width: 600px) {
  body {
    font-size: 18px;
  }
}

				
			

Consider Responsive Images

Combine length units with responsive images for optimal display.

				
					img {
  max-width: 100%;
  height: auto;
}

				
			

Understanding and utilizing CSS units is fundamental to creating responsive and visually appealing designs. From basic length units to advanced ones, each serves a specific purpose in web development. By mastering these units, you can achieve precise control over your layouts and ensure a consistent user experience across devices. Happy coding! ❤️

Table of Contents