×

CSS Units

CSS Units

In CSS, there are so many units available to show the length’s measurement. A single unit of CSS is applied to examine the size of the property, which the user set for content or element. The CSS unit is required to depict any measurement like margin: 30px; where the pixel or px is a unit of CSS. These units are applied to set the lengths, padding, margin, and many others.

Let’s understand some key points.

  • We can’t apply the whitespace among the unit and the number.
  • A unit may be neglected for 0.
  • A few CSS properties permit the length’s negative values.

The CSS units can be of two types:

  • Relative length
  • Absolute length

Relative lengths

This type of length is suitable to design any responsive site for the reason it scales relative to a parent or any size of the window. It describes the length, i.e., relative to other length properties.

If the screen size varies a lot, then the units of relative length are better because these units scale among the distinct rendering mediums (depending over the device). We can apply any relative units similarly, like a default for a responsive unit. The relative length provides help to ignore update designs for distinct screen sizes.

A few relative lengths are discussed and explained in the following table:

UnitName
emThis unit is relative to an element’s font-size.
exThese units are relative to the component’s font’s x-height. It is used rarely. Any x-height can be examined by the lowercase letter ‘x’ height.
chThis unit is quite similar to the ex unit, but rather than using the ‘x’ letter’s height, it measures an integer “0” (zero) width.
remIt is considered as the font size of the root element.
vhThese units corresponds to the viewport’s height. 1vh= 1% or 1/100 of the height of viewport.
vwThese units are relative to the viewport’s width. 1vh= 1% or 1/100 of the width of viewport.
vminThese units are relative to the viewport’s smaller dimension. 1vh= 1% or 1/100 of the lower dimension of a viewport.
vmaxThese units are relative to the viewport’s smaller dimension. 1vh= 1% or 1/100 of the lower dimension of a viewport’s.
%This unit is applied to represent a measurement like a percentage, i.e., relative to other values.

Consider the below examples:

Example:

<!DOCTYPE html> 
<html> 
<head> 
<style> 
body
{ 
text-align: center; 
} 
p
{ 
line-height: 0.1cm; 
color: navy; 
} 
</style> 
</head> 
<body> 
<h1> Relative units </h1> 
<p style = "font-size: 2em;" > It has the font-size: 2em; </p> 
<p style = "font-size: 8ex;" > It has the font-size: 8ex; </p> 
<p style = "font-size: 6ch;" > It has the font-size: 6ch; </p> 
<p style = "font-size: 4rem;" > It has the font-size: 4rem; </p> 
<p style = "font-size: 4vw;" > It has the font-size: 4vw; </p> 
<p style = "font-size: 10vh;" > It has the font-size: 10vh; </p> 
<p style = "font-size: 10vmin;" > It has the font-size: 10vmin; </p> 
<p style = "font-size: 8vmax;" > It has the font-size: 8vmax; </p> 
<p style = "font-size: 400%;" > It has the font-size: 400%; </p> 
</body> 
</html> 

Output:

CSS Units

Example: Time

A few animations attribute need values to show in time.

UnitDescription
sThis value is the time duration in seconds.
msThis value Is the time duration in milliseconds. 1ms= 1/100 of any second
<!DOCTYPE html> 
<html> 
<head> 
<style>  
div 
{ 
width: 200px; 
height: 200px; 
background: lime; 
border-radius: 50px; 
transition-property: background, width, height; 
transition-duration: 1s, 2s, 3s; 
} 
div:hover 
{  
width:300px; 
background: orange; 
height:300px; 
border-radius: 80px; 
} 
</style> 
</head> 
<body> 
<center> 
<h2> Hover to examine the effects. </h2> 
<div></div> 
</center> 
</body> 
</html> 

Output:

CSS Units

Example: Angles

The transformation attributes of CSS need values to show in angles.

UnitsDescription
degIt shows the angles inside the degrees.
gradIt shows the angles inside the gradians, which is 1/400 of any turn.
turnIt shows the angles inside the turns, which is 360 degrees.
<!DOCTYPE html> 
<html> 
<head> 
<style> 
img 
{ 
border: 9px ridge gray; 
border-radius: 30px; 
margin: 10px;  
transition-duration: 2s; 
} 
#img1:hover
{  
transform: rotate(30deg); 
transform-origin: bottom left 50px; 
} 
</style> 
</head> 
<body> 
<center> 
<img src="Lion1.png" id="img1"/> 
</center> 
</body> 
</html> 

Output:

CSS Units

Absolute lengths

The absolute length is the units of fixed-length type. The length, i.e., is displayed with the use of the absolute units can be represented absolutely of that size. As the screen size varies a lot, thus it is not suggested to use the on-screen. So, any absolute unit must be applied if the output’s medium is known, like the print layout.

These units will be helpful if the responsiveness will not be considered inside a project. It is not more favorable for some responsive sites due to it does not scale if any screen changes.

The absolute length will be considered to be of the same size all the time, generally. The units of absolute length are discussed below:

UnitsNameDescription
cmCentimetersThis unit applied to depict the measurements within the centimeters.
mmMillimetersThis unit asked to illustrate the measurements within the millimeters.
inInchesThis unit referred to represent the measurements within the inches. 1in= 96px= 2.54cm
ptPointsThis unit applied to depict the measurements within the points. 1pt = 1/72 of 1 inch
pcPicasThis unit referred to represent the measurements within the picas. 1pc = 12pt so, there 6 picas will equal to 1 inch.
pxPixelsThis unit applied to depict the measurements within the pixels. 1px = 1/96th of inch

Example:

In the following illustration, we will apply the CSS property font-size for some paragraphs to represent the values with the use of the units as above-mentioned of absolute length.

<!DOCTYPE html> 
<html> 
<head> 
<style> 
body
{ 
text-align: center; 
} 
</style> 
</head> 
<body> 
<h1> Absolute units </h1> 
<p style = "font-size: 20px;" > It has the font-size: 20px; </p> 
<p style = "font-size: 1.2cm;" > It has the font-size: 1.2cm; </p> 
<p style = "font-size: .7in;" > It has the font-size: .7in; </p> 
<p style = "font-size: 18pt;" > It has the font-size: 18pt; </p> 
<p style = "font-size: 2pc;" > It has the font-size: 2pc; </p> 
<p style = "font-size: 10mm;" > It has the font-size: 10mm; </p> 
</body> 
</html> 

Output:

CSS Units

Related Topics

CSS Sticky

CSS Sticky The position property in CSS is applied to set an element’s position. This property is also applied to position an element behind the other element. It is useful for...

1 minute read.

CSS Radio Button

Radio Button A radio button has been defined as an element of HTML. It can support us to take any input from the user. But it is challenging to design a...

4 minutes read.

CSS Lists

CSS Lists: Several properties are available in CSS that are used to manage the lists. Lists are distributed as ordered and unordered kind of lists. The ordered lists provide the...

7 minutes read.

CSS Text-Overflow

Introduction: The text-overflow property of CSS is used to format the text which is overflowing the boundaries of the container. This property helps user to clip the text, to display the...

5 minutes read.

CSS Font-Size

A Cascading Style Sheet (CSS) is a simple design scripting language intended to simplify the process of making web pages more visually appealing with the kind of high-level formatting. Cascading...

3 minutes read.

CSS Margin

CSS Margin: The CSS margin property is employed to describe the space throughout the elements. It clears the range throughout the element. These properties do not have a background color...

2 minutes read.

CSS Functions

CSS functions are used for different CSS properties. Every front-end developer should have knowledge of all CSS functions. It makes the coding easier. FunctionsExplanationattr().It returns the value of an attribute of...

5 minutes read.

CSS Selector

CSS Selector: There are some selectors used in CSS, which allows us to choose the content we wish to style. Various selectors are considered as the separation of the set...

5 minutes read.

CSS Background-clip

CSS Background-clip: The background-clip property describes the background’s painting area. It limits an area where the image or color of the background appears by using any clipping box. Everything will...

2 minutes read.

Difference between CSS Grid and CSS Flexbox

The way that information is arranged on a website reveals a lot about you or your company. Organizational abilities are just as important as having the right materials. The design...

3 minutes read.

CSS Links

CSS Link is used to create styled link. There are various ways to implement it. Styling Links Link can be styled with CSS properties like color, font-family, background etc. Example: <!DOCTYPE html>   <html>   <head>   <style>   a {       color: red;   }   </style>   </head>   <body>   <p><b><a href="https://www.tutorialandexample.com"    target="_blank">This is a link</a></b></p>   </body>   </html> Try Now There are four...

2 minutes read.

CSS Background-blend-mode

Background-blend-mode The background-blend-mode property sets a blend mode of the component’s every background layer (color/image). It illustrates how the element background image blends well with the element background color. We can...

9 minutes read.

CSS Filter

CSS Filter CSS filter is used to apply visual effects on the images, text, and other factors of the webpage. This filter property in CSS allows us to admit the effects like...

7 minutes read.

How To Use CSS

You can use CSS(Cascading Style Sheet) in four ways. These are given below: The given above CSS type, in which various technique will be used. These are different from each other and...

2 minutes read.

Grid Vs Flexbox in CSS

Grid: - CSS Grid Layout is a 2D grid-based matrix-based layout method with columns and rows that simplifies web pages created by eliminating the need for floats and placement. A grid layout, like...

4 minutes read.

CSS Background Color

It is one of the CSS properties to apply styling to html elements.This property is used to apply the background color to any HTML element.The background of an element includes...

2 minutes read.

CSS Superscript and Subscript

Superscript and Subscript To specify the superscript and the subscript text, HTML provides us two tags that are <sup> and <sub>. The superscript content looks in the shorter font and half...

2 minutes read.

CSS @keyframes rule

CSS @keyframes rule It describes the rule of animation that depicts the properties of CSS for the components at every state along with the timeline. We may make the properties of complex...

4 minutes read.

CSS Content Property

Content Property The content attribute produces dynamic content. It can be applied with any pseudo-element ::after and ::before. These CSS properties are entirely supported inside every browser and applied to include...

5 minutes read.

How to change background color in CSS

How to change background color in CSS? The background-color property in CSS would be used to specify the color of an element's background. As the element's background, it uses solid colors. The...

3 minutes read.