Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> CSS

CSS यूनिट्स को समझना

CSS यूनिट्स फॉन्ट-साइज, कैरेक्टर-साइज, व्यूपोर्ट डायमेंशन आदि जैसी विभिन्न श्रेणियों में आती हैं। मोटे तौर पर दो श्रेणियां हैं: एब्सोल्यूट और रिलेटिव यूनिट्स जिनमें उपरोक्त उप-श्रेणियां शामिल हैं।

निम्नलिखित CSS एब्सोल्यूट यूनिट्स हैं:

क्र.सं.यूनिट और नाम
1cm
सेंटीमीटर (1 cm = 100 mm)
2in
इंच (1 in = 2.54 cm)
3mm
मिलीमीटर
4pc
पिकास (1 pc = 12 pt)
5pt
पॉइंट्स (1 pt = 1/72 in)
6px
पिक्सेल (1 px = 0.75 pt)

आइए CSS एब्सोल्यूट यूनिट्स का एक उदाहरण देखते हैं:

उदाहरण

<!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; } #container {    display: flex; } .child{    margin: 5px 0;    height: 40px;    color: white;    border-width: 4px 0 4px 4px;    border-color: black;    border-style: solid; } .child:nth-of-type(1){    background-color: #FF8A00;    width:1cm; } .child:nth-of-type(2){    background-color: #F44336;    width:1in; } .child:nth-of-type(3){    background-color: #C303C3;    width:1mm; } .child:nth-of-type(4){ background-color: #4CAF50; width:1pc; } .child:nth-of-type(5){    background-color: #03A9F4;    width:1pt; } .child:nth-of-type(6){    background-color: #FEDC11;    width:1px;    border-right-width: 4px; } </style> </head> <body> <form> <fieldset> <legend>CSS-Absolute-Units</legend> <div id="container"> <div class="child"></div> <div class="child"></div> <div class="child"></div> <div class="child"></div> <div class="child"></div> <div class="child"></div> </div><br> <div><span class="child">cm</span><span class="child">in</span><span class="child">mm</span><span class="child">pc</span><span class="child">pt</span><span class="child">px</span></div><br> <input type="number" id="numSelect" value="1" min="1"> <input type="button" onclick="changeWidth()" value="Preview"> <script> var numSelect = document.getElementById("numSelect"); function changeWidth() {    var divChild = document.getElementsByClassName('child');    for(var i=0; i<6; i++){       divChild[i].style.width = numSelect.value+divChild[i+6].textContent; }} </script> </body> </html>

आउटपुट

किसी बटन पर क्लिक करने से पहले:

CSS यूनिट्स को समझना

'Preview' बटन पर क्लिक करने के बाद नंबर फील्ड सेट के साथ:

CSS यूनिट्स को समझना

निम्नलिखित CSS रिलेटिव यूनिट्स हैं:

क्र.सं.यूनिट और रिलेटिव टू
1%
पैरेंट एलिमेंट डायमेंशन
2em
एलिमेंट का फॉन्ट साइज
3ex
एलिमेंट के फॉन्ट की x-height
4rem
रूट एलिमेंट का फॉन्ट साइज
5vh
व्यूपोर्ट की ऊंचाई का 1%
6vmax
व्यूपोर्ट के बड़े आयाम का 1%
7vmin
व्यूपोर्ट के छोटे आयाम का 1%
8vw
व्यूपोर्ट की चौड़ाई का 1%

आइए CSS रिलेटिव यूनिट्स का एक उदाहरण देखते हैं:

उदाहरण

<!DOCTYPE html> <html> <head> <title>CSS Relative Units</title> <style> form {    width:70%;    margin: 0 auto;    text-align: center; } * {    padding: 2px;    margin:5px; } input[type="button"] {    border-radius: 10px; } #container {    display: flex; } .child{    margin: 5px 0;    height: 40px;    color: white;    border-width: 4px 0 4px 4px;    border-color: black;    border-style: solid; } .child:nth-of-type(1){    background-color: #FF8A00;    width:1%; } .child:nth-of-type(2){    background-color: #F44336;    width:1em; } .child:nth-of-type(3){    background-color: #C303C3;    width:1ex; } .child:nth-of-type(4){    background-color: #4CAF50;    width:1rem; } .child:nth-of-type(5){    background-color: #03A9F4;    width:1vh; } .child:nth-of-type(6){    background-color: #FEDC11;    width:1vw;    border-right-width: 4px; } </style> </head> <body> <form> <fieldset> <legend>CSS-Relative-Units</legend> <div id="container"> <div class="child"></div> <div class="child"></div> <div class="child"></div> <div class="child"></div> <div class="child"></div> <div class="child"></div> </div><br> <div><span class="child">%</span><span class="child">em</span><span class="child">ex</span><span class="child">rem</span><span class="child">vh</span><span class="child">vw</span></div><br> <input type="number" id="numSelect" value="1" min="1"> <input type="button" onclick="changeWidth()" value="Preview"> <script> var numSelect = document.getElementById("numSelect"); function changeWidth() {    var divChild = document.getElementsByClassName('child');    for(var i=0; i<6; i++){       divChild[i].style.width = numSelect.value+divChild[i+6].textContent; }} </script> </body> </html>

आउटपुट

किसी बटन पर क्लिक करने से पहले:

CSS यूनिट्स को समझना

'Preview' बटन पर क्लिक करने के बाद 'em' ऑप्शन चुने जाने और खाली टेक्स्ट फील्ड के साथ:

CSS यूनिट्स को समझना

  1. CSS एब्सोल्यूट और रिलेटिव यूनिट्स CSS एब्सोल्यूट और रिलेटिव यूनिट्स

    CSS एब्सोल्यूट यूनिट्स और रिलेटिव यूनिट्स दोनों दूरी की इकाइयों की श्रेणी में आते हैं।CSS रिलेटिव यूनिट्स किसी एलिमेंट की लंबाई को दूसरे एलिमेंट के संदर्भ में परिभाषित करते हैं।उदाहरण के लिए, vh रिलेटिव यूनिट व्यूपोर्ट हाइट के सापेक्ष होता है।निम्नलिखित CSS रिलेटिव यूनिट्स हैं:Sr.Noयूनिट और रिलेटिव

  1. CSS के साथ चाइल्ड एलिमेंट्स का चयन करना CSS के साथ चाइल्ड एलिमेंट्स का चयन करना

    परिचय CSS चाइल्ड कॉम्बिनेटर (>) का उपयोग पैरेंट एलिमेंट के **सभी डायरेक्ट चाइल्ड एलिमेंट्स** का चयन करने के लिए किया जाता है। CSS डिसेंडेंट कॉम्बिनेटर (स्पेस) का उपयोग पैरेंट के **सभी डिसेंडेंट्स** (नेस्टेड लेवल पर कोई भी गहराई) का चयन करने के लिए किया जाता है। सिंटैक्स चाइल्ड कॉम्बिनेटर (डायरे

  1. CSS में clientHeight, offsetHeight और scrollHeight प्रॉपर्टीज को समझना CSS में clientHeight, offsetHeight और scrollHeight प्रॉपर्टीज को समझना

    clientHeight clientHeight किसी एलिमेंट की ऊंचाई का माप देता है जिसमें **पैडिंग** शामिल होता है। ध्यान दें कि बॉर्डर, मार्जिन, और स्क्रॉलबार ऊंचाई (यदि रेंडर किया गया) इसमें शामिल नहीं होते। offsetHeight offsetHeight किसी एलिमेंट की ऊंचाई का माप देता है जिसमें **वर्टिकल पैडिंग, टॉप और बॉटम बॉर्डर्स