
- CSS Tutorial
- CSS - Home
- CSS - Introduction
- CSS - Syntax
- CSS - Inclusion
- CSS - Measurement Units
- CSS - Colors
- CSS - Backgrounds
- CSS - Fonts
- CSS - Text
- CSS - Images
- CSS - Links
- CSS - Tables
- CSS - Borders
- CSS - Margins
- CSS - Lists
- CSS - Padding
- CSS - Cursors
- CSS - Outlines
- CSS - Dimension
- CSS - Scrollbars
- CSS Advanced
- CSS - Visibility
- CSS - Positioning
- CSS - Layers
- CSS - Pseudo Classes
- CSS - Pseudo Elements
- CSS - @ Rules
- CSS - Text Effects
- CSS - Media Types
- CSS - Paged Media
- CSS - Aural Media
- CSS - Printing
- CSS - Layouts
- CSS - Validations
- CSS3 Tutorial
- CSS3 - Tutorial
- CSS3 - Rounded Corner
- CSS3 - Border Images
- CSS3 - Multi Background
- CSS3 - Color
- CSS3 - Gradients
- CSS3 - Shadow
- CSS3 - Text
- CSS3 - Web font
- CSS3 - 2d transform
- CSS3 - 3d transform
- CSS3 - Animation
- CSS3 - Multi columns
- CSS3 - User Interface
- CSS3 - Box Sizing
- CSS Responsive
- CSS - Responsive Web Design
- CSS References
- CSS - Questions and Answers
- CSS - Quick Guide
- CSS - References
- CSS - Color References
- CSS - Web browser References
- CSS - Web safe fonts
- CSS - Units
- CSS - Animation
- CSS Resources
- CSS - Useful Resources
- CSS - Discussion
CSS Absolute and Relative Units
Both CSS Absolute Units and Relative Units fall under the category Distance units.
CSS Relative Units define a length of an element in reference to another element.
For example, vh relative unit is relative to viewport height.
Following are the CSS relative units −
Sr.No | Unit & Relative to |
---|---|
1 | % Parent element dimensions |
2 | em Font size of the element |
3 | ex x-height of the element's font |
4 | lh Line height of the element |
5 | rem Font size of the root element |
6 | rlh Line height of the root element |
7 | vb 1% of viewport's size in the root element's block axis |
8 | vh 1% of viewport's height |
9 | vmax 1% of viewport's larger dimension |
10 | vmin 1% of viewport's smaller dimension |
11 | vw 1% of viewport's width |
Example
Let us see an example for CSS relative units −
<!DOCTYPE html> <html> <head> <title>CSS Relative Units</title> <style> html{ font-size: 14px; line-height: normal; } form { width:70%; margin: 0 auto; text-align: center; } * { padding: 2px; margin:5px; } input[type="button"] { border-radius: 10px; } #textContain { font-size: 20px; line-height: 2; } </style> </head> <body> <form> <fieldset> <legend>CSS-Relative-Units</legend> <input type="text" id="textSelect" placeholder="Type Text Here..."> <input type="number" id="numSelect" value="1" min="1"> <div id="radioBut"> <input class="radio" type="radio" name="heading" value="em" checked><label>em</label> <input class="radio" type="radio" name="heading" value="rem"><label>rem</label> <input class="radio" type="radio" name="heading" value="vw"><label>vw</label> <input class="radio" type="radio" name="heading" value="vh"><label>lh</label> <input class="radio" type="radio" name="heading" value="ex"><label>ex</label> </div> <div id="textContain">Text Preview: <span id="textPreview">Output will show up here</span></div> <input type="button" onclick="changeText()" value="Preview"> </fieldset> </form> <script> var textSelect = document.getElementById("textSelect"); var numSelect = document.getElementById("numSelect"); var textPreview = document.getElementById("textPreview"); function changeText() { if(textSelect.value === '') textPreview.textContent = 'Type some text first'; else{ for(var i=0; i<5; i++){ var radInp = document.getElementsByClassName('radio')[i]; if(radInp.checked === true){ textPreview.textContent = textSelect.value; textPreview.style.fontSize = numSelect.value+radInp.value } } } } </script> </body> </html>
Output
Following is the output for above code −
Before clicking any button −
After clicking ‘Preview’ button with ‘em’ option selected and empty text field −
After clicking ‘Preview’ button with ‘rem’ option selected and non-empty text field −
After clicking ‘Preview’ button with ‘em’ option selected and non-empty text field −
Following are the CSS absolute units −
Sr.No | Unit & Name |
---|---|
1 | cm Centimeters (1 cm = 100 mm) |
2 | in Inches (1 in = 2.54 cm) |
3 | mm Millimeters |
4 | pc Picas (1 pc = 12 pt) |
5 | pt Points (1 pt = 1/72 in) |
6 | px Pixels (1 px = 0.75 pt) |
Example
Let us see an example for CSS absolute units −
<!DOCTYPE html> <html> <head> <title>CSS Absolute Units</title> <style> form { width:70%; margin: 0 auto; text-align: center; } * { padding: 2px; margin:5px; } input[type="button"] { border-radius: 10px; } </style> </head> <body> <form> <fieldset> <legend>CSS-Absolute-Units</legend> <input type="text" id="textSelect" placeholder="Type Text Here..."> <input type="number" id="numSelect" value="10" min="1"> <div id="radioBut"> <input class="radio" type="radio" name="heading" value="pc"><label>pc</label> <input class="radio" type="radio" name="heading" value="pt"><label>pt</label> <input class="radio" type="radio" name="heading" value="px" checked><label>px</label> <input class="radio" type="radio" name="heading" value="in"><label>in</label> <input class="radio" type="radio" name="heading" value="cm"><label>cm</label> </div> <div id="textContain">Text Preview: <span id="textPreview">Output will show up here</span></div> <input type="button" onclick="changeText()" value="Preview"> </fieldset> </form> <script> var textSelect = document.getElementById("textSelect"); var numSelect = document.getElementById("numSelect"); var textPreview = document.getElementById("textPreview"); function changeText() { if(textSelect.value === '') textPreview.textContent = 'Type some text first'; else{ for(var i=0; i<5; i++){ var radInp = document.getElementsByClassName('radio')[i]; if(radInp.checked === true){ textPreview.textContent = textSelect.value; textPreview.style.fontSize = numSelect.value+radInp.value } } } } </script> </body> </html>
Output
Following is the output for above code −
Before clicking any button −
After clicking ‘Preview’ button with ‘px’ option selected and non-empty text field −
After clicking ‘Preview’ button with ‘pt’ option selected and non-empty text field −
After clicking ‘Preview’ button with ‘pc’ option selected and non-empty text field −
- Related Articles
- Understanding CSS Absolute and Relative Units
- CSS Relative units
- CSS Absolute Units
- Relative Length Units in CSS
- Absolute Length Units in CSS
- Absolute and Relative Imports in Python
- Absolute and Relative frequency in Pandas
- Absolute and Relative Magnetic Permeability (µ)
- Difference between an Absolute URL and a Relative URL
- CSS position: relative;
- What is the difference between relative and absolute XPath in Selenium?
- CSS Measurement Units
- Understanding CSS Units
- CSS position: absolute;
- Relative Positioning with CSS
