溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

CSS中的長度單位是什么

發(fā)布時間:2021-08-18 10:45:57 來源:億速云 閱讀:127 作者:chen 欄目:web開發(fā)

本篇內(nèi)容主要講解“CSS中的長度單位是什么”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“CSS中的長度單位是什么”吧!

前面的話

本文分為絕對長度單位和相對長度單位來介紹CSS中的長度單位的主要知識

絕對長度單位

絕對長度單位代表一個物理測量

像素px(pixels)
 
在web上,像素px是典型的度量單位,很多其他長度單位直接映射成像素。最終,他們被按照像素處理

英寸in(inches)
 
1in = 2.54cm = 96px

厘米cm(centimeters)
 
1cm = 10mm = 96px/2.54 = 37.8px

毫米mm(millimeters)
 
1mm = 0.1cm = 3.78px

1/4毫米q(quarter-millimeters)
 
1q = 1/4mm = 0.945px

點pt(points)
 
1pt = 1/72in = =0.0139in = 1/72*2.54cm = 1/72*96px = 1.33px

派卡pc(picas)
 
1pc = 12pt = 1/6in = 1/6*96px = 16px

字體相關相對長度單位

em、ex、ch、rem是字體相關的相對長度單位

em
 
em表示元素的font-size屬性的計算值,如果用于font-size屬性本身,相對于父元素的font-size;若用于其他屬性,相對于本身元素的font-size

CSS Code復制內(nèi)容到剪貼板

  1. <style>  

  2. .box{font-size20px;}  

  3. .in{  

  4.     /* 相對于父元素,所以2*2px=40px */  

  5.     font-size: 2em;  

  6.     /* 相對于本身元素,所以5*40px=200px */  

  7.     height: 5em;  

  8.     /* 10*40px=400px */  

  9.     width: 10em;  

  10.     background-color: lightblue;  

  11. }  

  12. </style>  

XML/HTML Code復制內(nèi)容到剪貼板

  1. <div class="box">  

  2.     <div class="in">測試文字</div>      

  3. </div>  

CSS中的長度單位是什么

rem
 
rem是相對于根元素html的font-size屬性的計算值

兼容性: IE8-不支持

CSS Code復制內(nèi)容到剪貼板

  1. <style>  

  2. /* 瀏覽器默認字體大小為16px,則2*16=32px,所以根元素字體大小為32px */  

  3. html{font-size: 2rem;}  

  4. /* 2*32=64px */  

  5. .box{font-size: 2rem;}  

  6. .in{  

  7.     /* 1*32=32px */  

  8.     font-size: 1rem;  

  9.     /* 1*32=32px */  

  10.     border-left: 1rem solid black;  

  11.     /* 4*32=128px */  

  12.     height: 4rem;  

  13.     /* 6*32=192px */  

  14.     width: 6rem;  

  15.     background-color: lightblue;  

  16. }  

  17. </style>  

XML/HTML Code復制內(nèi)容到剪貼板

  1. <div class="box">  

  2.     <div class="in" id="test">測試文字</div>      

  3. </div>  

CSS中的長度單位是什么

ex
 
ex是指所用字體中小寫x的高度。但不同字體x的高度可能不同。實際上,很多瀏覽器取em值一半作為ex值

[注意]ex在實際中常用于微調(diào)

CSS Code復制內(nèi)容到剪貼板

  1. <style>  

  2. .box{font-size20px;}  

  3. .in{  

  4.     font-size: 1ex;  

  5.     border-left: 1ex solid black;  

  6.     height: 10ex;  

  7.     width: 20ex;  

  8.     background-color: lightblue;  

  9. }  

  10. </style>  

XML/HTML Code復制內(nèi)容到剪貼板

  1. <button>宋體</button><button>微軟雅黑</button><button>arial</button><button>serif</button>  

  2. <div class="box">  

  3.     <div class="in" id="test">測試文字</div>      

  4. </div>  

JavaScript Code復制內(nèi)容到剪貼板

  1. <script>  

  2. var aBtns = document.getElementsByTagName('button');  

  3. for(var i = 0; i < aBtns.length; i++ ){  

  4.     aBtns[i].onclick = function(){  

  5.         test.style.fontFamily = this.innerHTML;  

  6.     }  

  7. }      

  8. </script>  

ch
 
ch與ex類似,被定義為數(shù)字0的寬度。當無法確定數(shù)字0寬度時,取em值的一半作為ch值

兼容性: IE8-不支持

[注意]ch在實際中主要用于盲文排版
CSS中的長度單位是什么

CSS Code復制內(nèi)容到剪貼板

  1. <style>  

  2. .box{font-size20px;}  

  3. .in{  

  4.     font-size: 1ch;  

  5.     border-left: 1ch solid black;  

  6.     height: 10ch;  

  7.     width: 20ch;  

  8.     background-color: lightblue;  

  9. }  

  10. </style>  

XML/HTML Code復制內(nèi)容到剪貼板

  1. <button>宋體</button><button>微軟雅黑</button><button>arial</button><button>serif</button>  

  2. <div class="box">  

  3.     <div class="in" id="test">測試文字</div>      

  4. </div>  

JavaScript Code復制內(nèi)容到剪貼板

  1. <script>  

  2. var aBtns = document.getElementsByTagName('button');  

  3. for(var i = 0; i < aBtns.length; i++ ){  

  4.     aBtns[i].onclick = function(){  

  5.         test.style.fontFamily = this.innerHTML;  

  6.     }  

  7. }      

  8. </script>  

CSS中的長度單位是什么

視口相關相對長度單位

視口相關的長度值相對于初始包含塊的大小。當初始包含塊的寬高變化時,他們都會相應地縮放。然而,當根元素的overflow值為auto時,任何滾動條會假定不存在。

關于視口相關的單位有vh、vw、vmin、vmax4個單位

兼容性:IE8-不支持,IOS7.1-不支持,android4.3-不支持(對于vmax,所有IE瀏覽器都不支持)

[注意]黑莓錯誤的將其相對于視覺視口來計算;而safari奇怪地相對于html元素來計算,如果html中增加了內(nèi)容,這兩個單位也會發(fā)生變化

vh
 
布局視口高度的 1/100

vw
 
布局視口寬度的 1/100

CSS Code復制內(nèi)容到剪貼板

  1. <style>  

  2. body{margin: 0;}  

  3. .box{  

  4.     /* 實現(xiàn)與屏幕等高的效果 */  

  5.     height: 100vh;  

  6.     background-color: lightblue;  

  7. }      

  8. </style>  

XML/HTML Code復制內(nèi)容到剪貼板

  1. <div class="box"></div>  

CSS中的長度單位是什么

vmin
 
布局視口高度和寬度之間的最小值的 1/100

CSS Code復制內(nèi)容到剪貼板

  1. /*類似于contain效果*/  

  2. .box{  

  3.     height: 100vmin;  

  4.     width: 100vmin;  

  5. }  

CSS中的長度單位是什么

vmax

布局視口高度和寬度之間的最大值的 1/100

CSS Code復制內(nèi)容到剪貼板

  1. /*類似于cover效果*/  

  2. .box{  

  3.     height: 100vmax;  

  4.     width: 100vmax;  

  5. }      

CSS中的長度單位是什么

到此,相信大家對“CSS中的長度單位是什么”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關內(nèi)容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

css
AI