CSS 資料型別 - <dimension>



CSS <dimension> 資料型別表示沿單個軸的測量值。它用於接受長度、寬度、高度、邊距、內邊距等單位值的屬性。此資料型別允許您使用各種單位指定測量值,例如距離(<length>)、持續時間(<time>)、頻率(<frequency>)、解析度(<resolution>)以及其他各種數量。

語法

<value><unit>

這裡,<value> 是一個數值,<unit> 是測量單位。常用單位包括:

  • px:畫素

  • em:相對於元素的字型大小

  • rem:相對於根元素的字型大小

  • vw:相對於視口寬度的 1%

  • vh:相對於視口高度的 1%

  • cm, mm, in, pt, pc:絕對長度單位

有效的尺寸

以下示例顯示了使用不同單位來指定長度、時間和頻率:

20px     20 pixels
2rem     2 rem
1.5pt    1.5 points
1400ms   1400 milliseconds
10s       10 seconds
100hz    100 Hertz
100Hz    100 Hertz (values are case insensitive)

無效的尺寸

使用<dimension>資料型別時,需遵循以下規則:

12 px     The unit must immediately follow the number.
12"px"    Units are identifiers and should not be surrounded by quotations.
3sec      The seconds unit is denoted by "s" rather than "sec".

CSS <dimension> - 高度

以下示例演示了透過將寬度和高度指定為 100px 來使用<dimension>:

<html>
<head>
<style>
   .box {
      width: 100px; 
      height: 100px; 
      background-color: pink; 
   }
</style>
</head>
<body>
   <div class="box">
      <dimension>
   </div>
</body>
</html>
廣告
© . All rights reserved.