HOME C C++ PYTHON JAVA HTML CSS JAVASCRIPT BOOTSTRAP JQUERY REACT PHP SQL AJAX JSON DATA SCIENCE AI

CSS Units

CSS units refer to the measurement units used in Cascading Style Sheets (CSS) to define various properties such as length, width, margin, padding, font size, etc. They allow web developers to specify sizes and distances in a consistent and flexible manner. CSS units can be categorized into two main types: absolute units and relative units.

Absolute Units:

  • Pixels (px): A pixel is a single dot on a computer screen, and it is a common unit for defining exact sizes.
  • Points (pt): A point is a physical unit of measurement commonly used in print media. One point is equal to 1/72nd of an inch.
  • Inches (in): Represents length in inches.
  • Centimeters (cm): Represents length in centimeters
  • Millimeters (mm): Represents length in millimeters.

Relative Units:

  • Percentage (%): Represents a percentage of the parent element's size. For example, setting width: 50% means the element's width will be half of its parent's width.
  • em: Equal to the computed font-size of the element itself. For example, if the font-size of a parent element is 16px, 1em would be equal to 16px.
  • rem: Similar to em, but relative to the root element (html). This makes it easier to scale styles across an entire document, as it's not affected by ancestor elements' font sizes.
  • vw (viewport width) and vh (viewport height): These units represent a percentage of the viewport's width and height, respectively. For example, 1vw is equal to 1% of the viewport's width.

There are also some less commonly used units like ex (height of the lowercase "x" letter), ch (width of the "0" character in the current font), etc.

Using appropriate units is crucial for creating responsive and accessible web designs that can adapt well to various screen sizes and devices.