Measurement Units in CSS Language

Measurement Units in CSS Language

CSS (Cascading Style Sheets) is an essential technology for web developers, allowing them to control the visual presentation of web pag

es. When working with CSS, one of the key aspects to understand is the use of measurement units. These units determine the size and positioning of elements on a web page. In this post, we’ll explore the various measurement units in CSS and provide examples of their usage.

Pixels (px)

Pixels are the most commonly used measurement unit in CSS. A pixel is a single dot on a computer screen. When you specify an element’s size or spacing in pixels, it will appear the same size on all screens, regardless of their resolution.

Example:

div {
  width: 200px;
  height: 100px;
  margin: 10px;
}

Em (em)

The “em” unit is a relative measurement based on the font size of the parent element. For example, if the parent element has a font size of 16px, 1em is equal to 16px. Em units are useful for creating responsive and scalable designs.

Example:

p {
  font-size: 1.2em; /* 1.2 times the font size of the parent element */
  margin: 0.5em;
}

Percentage (%)

Percentage units are another relative measurement. They are often used to define sizes in relation to the parent element’s size or the viewport’s dimensions.

Example:

.container {
  width: 80%;
}

.item {
  width: 50%; /* 50% of the container's width */
}

Viewport Width (vw) and Viewport Height (vh)

Viewport units are relative to the size of the viewport, which is the visible area of the web page. “1vw” is equal to 1% of the viewport’s width, and “1vh” is equal to 1% of the viewport’s height. These units are great for creating responsive layouts.

Example:

header {
  width: 80vw; /* 80% of the viewport's width */
  height: 10vh; /* 10% of the viewport's height */
}

REM (root em)

The “rem” unit is similar to “em,” but it’s relative to the font size of the root element (usually the element). This makes it easier to maintain consistent sizing across the entire document.

Example:

html {
  font-size: 16px; /* Set the root font size */
}

p {
  font-size: 1.2rem; /* 1.2 times the root font size */
  margin: 1rem; /* 1 times the root font size */
}

Discover more from PiEmbSysTech

Subscribe to get the latest posts sent to your email.

Leave a Reply

Scroll to Top

Discover more from PiEmbSysTech

Subscribe now to keep reading and get access to the full archive.

Continue reading