CSS | Units
CSS has several different units for expressing the length and measurement. CSS units are needed to specify the measurement in stylesheet like padding:”5px”. Mainly there are two types of units in CSS.
- Absolute Length
- Relative Length
Absolute Length: It is not good for use on screen, cause screen size vary so much depending on the device used for that page it is recommended to use for print layout and where the output medium is known.
Absolute Length Units:
- cm: centimeter
Syntax:font-size: 0.5cm; line-height: 0.1cm;
Example:
<html><head><title>CSS units</title><style>.gfg {font-size: 1.2cm;font-weight:bold;}.geeks {font-size: 0.5cm;line-height: 0.1cm;}</style></head><body><divclass="gfg">GeeksforGeeks</gfg><divclass="geeks">A computer science portal for geeks</div></body></html>chevron_rightfilter_noneOutput:

-
mm: milimeter
Syntax:font-size: 5mm; line-height: 1mm;
Example:
<html><head><title>CSS units</title><style>.gfg {font-size: 12mm;font-weight:bold;}.geeks {font-size: 5mm;line-height: 1mm;}</style></head><body><divclass="gfg">GeeksforGeeks</gfg><divclass="geeks">A computer science portal for geeks</div></body></html>chevron_rightfilter_noneOutput:

-
in: inches
Note: inches (1in = 96px = 2.54cm)
Syntax:font-size: 0.2in; line-height: 0.1in;Example:
<html><head><title>CSS units</title><style>.gfg {font-size: .5in;font-weight:bold;}.geeks {font-size: .2in;line-height: .1in;}</style></head><body><divclass="gfg">GeeksforGeeks</gfg><divclass="geeks">A computer science portal for geeks</div></body></html>chevron_rightfilter_noneOutput:

-
px: pixels
Note: pixels (1px = 1/96th of 1in)
Syntax:font-size: 20px; line-height: 10px;
Example:
<html><head><title>CSS units</title><style>.gfg {font-size: 40px;font-weight:bold;}.geeks {font-size: 17px;line-height: 5px;}</style></head><body><divclass="gfg">GeeksforGeeks</gfg><divclass="geeks">A computer science portal for geeks</div></body></html>chevron_rightfilter_noneOutput:

-
pt:points
Note: points (1pt = 1/72 of 1in)
Syntax:font-size: 16pt; line-height: 8pt;Example:
<html><head><title>CSS units</title><style>.gfg {font-size: 35pt;font-weight:bold;}.geeks {font-size: 15pt;line-height: 5pt;}</style></head><body><divclass="gfg">GeeksforGeeks</gfg><divclass="geeks">A computer science portal for geeks</div></body></html>chevron_rightfilter_noneOutput:

-
pc: picas
Note: picas (1pc = 12 pt)
Syntax:font-size: 1pc; line-height: 0.5pc;Example:
<html><head><title>CSS units</title><style>.gfg {font-size: 3pc;font-weight:bold;}.geeks {font-size: 1.3pc;line-height: .2pc;}</style></head><body><divclass="gfg">GeeksforGeeks</gfg><divclass="geeks">A computer science portal for geeks</div></body></html>chevron_rightfilter_noneOutput:

Relative Length: It is good for use on screen, if screen size varies so much depending on the device then these relative length units are perfect because it changes with the different rendering mediums.
Relative Length Units:
-
em: Relative to the font size of that element.
Note: Here 2em meaning 2times the size of current font.
Syntax:font-size: 10px; line-height: 2em;Example:
<html><head><title>relative unit</title><style>p {font-size: 15px;line-height: 2em;}</style></head><body><p>GeeksforGeeks Line height: 2x10px = 20px</p><p>GeeksforGeeks Line height: 2x10px = 20px</p><p>GeeksforGeeks Line height: 2x10px = 20px</p></body></html>chevron_rightfilter_noneOutput:

-
ex: Relative to the X(axis)-height of the current font.
Syntax:font-size: 1ex;
Example:
<html><head><title>relative unit</title><style>p {font-size: 40px;}span {font-size: 1ex;}</style></head><body><p>GeeksforGeeks:<span>A computer scienceportal for geeks</span></p></body></html>chevron_rightfilter_noneOutput:
-
ch: Relative to width of the 0.
Syntax:font-size: 2ch;
Example:
<html><head><title>ch unit in CSS</title><style>body {font-size:50px;}div {font-size: 1ch;}</style></head><body><p>GeeksforGeeks</p><div>A computer science portal for geeks</div></body></html>chevron_rightfilter_noneOutput:

-
rem: Relative to the browser base font-size.
Syntax:font-size: 3rem;
Example:
<html><head><title>ch unit in CSS</title><style>body {font-size:4rem;}div {font-size: 1.6rem;}</style></head><body><p>GeeksforGeeks</p><div>A computer science portal for geeks</div></body></html>chevron_rightfilter_noneOutput:

-
vw: Relative to 1% of the width of the viewport.
Syntax:font-size: 10vw;
Example:
<html><head><title>vw unit</title><style>h1 {font-size: 4vw;}</style></head><body><h1>GeeksforGeeks</h1><p>A computer science portal for geeks</p></body></html>chevron_rightfilter_noneOutput:

-
vh: Relative to 1% of the height of the viewport.
Syntax:font-size: 10vh;
Example:
<html><head><style>h1 {font-size: 6vh;}</style></head><body><h1>GeeksforGeeks</h1><p>A computer science portal for geeks</p></body></html>chevron_rightfilter_noneOutput:

-
vmin: Relative to 1% of the viewport’s smaller dimension.
Syntax:font-size: 10vmin;
Example:
<html><head><title>vmin unit</title><style>h1 {font-size: 8vmin;}</style></head><body><h1>GeeksforGeeks</h1><p>A computer science portal for geeks</p><p>1vmin = 1vw or 1vh whichever is smaller.</p></body></html>chevron_rightfilter_noneOutput:

-
vmax: Relative to 1% of the viewport’s larger dimension.
Syntax:font-size: 20vmax;
Example:
<html><head><title>vmax unit</title><style>h1 {font-size: 5vmax;}</style></head><body><h1>GeeksforGeeks</h1><p>A computer science portal for geeks</p><p>1vmax = 1vw or 1vh whichever larger.</p></body></html>chevron_rightfilter_noneOutput:

-
%: % unit sets the font-size relative to the current font-size.
Syntax:font-size: 250%;
Example:
<html><head><title>CSS unit</title><style>body {font-size:250%;}div {font-size: 17px;}</style></head><body><p>GeeksforGeeks</p><div>A computer science portal for geeks</div></body></html>chevron_rightfilter_noneOutput:
Recommended Posts:
- How to detect operating system on the client machine using JavaScript ?
- What is the difference between children and childNodes in JavaScript?
- How to create hash from string in JavaScript ?
- Kotlin mutableSetOf() method
- Kotlin Map : mapOf()
- Kotlin Set : setOf()
- Kotlin list : listOf()
- CSS | perspective() Function
- PHP | Imagick levelImage() Function
- Kotlin | apply vs with
- Kotlin Ranges
- What are the main differences between the Java platform and other platforms?
- How to create a custom callback in JavaScript?
- How to place the image above the slider in mobile view in bootstrap?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



