- CSS 教程
- CSS - 首頁
- CSS - 路線圖
- CSS - 簡介
- CSS - 語法
- CSS - 選擇器
- CSS - 引入
- CSS - 測量單位
- CSS - 顏色
- CSS - 背景
- CSS - 字型
- CSS - 文字
- CSS - 圖片
- CSS - 連結
- CSS - 表格
- CSS - 邊框
- CSS - 塊級邊框
- CSS - 內聯邊框
- CSS - 外邊距
- CSS - 列表
- CSS - 內邊距
- CSS - 游標
- CSS - 輪廓
- CSS - 尺寸
- CSS - 捲軸
- CSS - 內聯塊
- CSS - 下拉選單
- CSS - 可見性
- CSS - 溢位
- CSS - 清除浮動
- CSS - 浮動
- CSS - 箭頭
- CSS - 調整大小
- CSS - 引號
- CSS - 順序
- CSS - 位置
- CSS - 連字元
- CSS - 懸停
- CSS - 顯示
- CSS - 焦點
- CSS - 縮放
- CSS - 轉換
- CSS - 高度
- CSS - 連字元字元
- CSS - 寬度
- CSS - 不透明度
- CSS - Z-Index
- CSS - 底部
- CSS - 導航欄
- CSS - 覆蓋層
- CSS - 表單
- CSS - 對齊
- CSS - 圖示
- CSS - 圖片庫
- CSS - 註釋
- CSS - 載入器
- CSS - 屬性選擇器
- CSS - 組合器
- CSS - 根元素
- CSS - 盒模型
- CSS - 計數器
- CSS - 剪裁
- CSS - 書寫模式
- CSS - Unicode-bidi
- CSS - min-content
- CSS - All
- CSS - Inset
- CSS - Isolation
- CSS - Overscroll
- CSS - Justify Items
- CSS - Justify Self
- CSS - Tab Size
- CSS - 指標事件
- CSS - Place Content
- CSS - Place Items
- CSS - Place Self
- CSS - Max Block Size
- CSS - Min Block Size
- CSS - Mix Blend Mode
- CSS - Max Inline Size
- CSS - Min Inline Size
- CSS - Offset
- CSS - Accent Color
- CSS - User Select
- CSS 高階
- CSS - 網格
- CSS - 網格佈局
- CSS - Flexbox
- CSS - 可見性
- CSS - 定位
- CSS - 層
- CSS - 偽類
- CSS - 偽元素
- CSS - @規則
- CSS - 文字效果
- CSS - 分頁媒體
- CSS - 列印
- CSS - 佈局
- CSS - 驗證
- CSS - 圖片精靈
- CSS - Important
- CSS - 資料型別
- CSS3 教程
- CSS3 - 教程
- CSS - 圓角
- CSS - 邊框圖片
- CSS - 多重背景
- CSS - 顏色
- CSS - 漸變
- CSS - 盒陰影
- CSS - 盒裝飾斷開
- CSS - 游標顏色
- CSS - 文字陰影
- CSS - 文字
- CSS - 2d 變換
- CSS - 3d 變換
- CSS - 過渡
- CSS - 動畫
- CSS - 多列
- CSS - 盒尺寸
- CSS - 工具提示
- CSS - 按鈕
- CSS - 分頁
- CSS - 變數
- CSS - 媒體查詢
- CSS - 函式
- CSS - 數學函式
- CSS - 遮罩
- CSS - 形狀
- CSS - 樣式圖片
- CSS - 特異性
- CSS - 自定義屬性
- CSS 響應式
- CSS RWD - 簡介
- CSS RWD - 視口
- CSS RWD - 網格檢視
- CSS RWD - 媒體查詢
- CSS RWD - 圖片
- CSS RWD - 影片
- CSS RWD - 框架
- CSS 工具
- CSS - PX 到 EM 轉換器
- CSS - 顏色選擇器和動畫
- CSS 資源
- CSS - 有用資源
- CSS - 討論
CSS 資料型別 - <length>
CSS 的 <length> 資料型別表示距離或測量值。它是可以以各種單位表示的值的通用術語,例如畫素 (px)、em (em)、rem (rem)、百分比 (%)、英寸 (in)、釐米 (cm)、毫米 (mm)、磅 (pt) 和 pica (pc) 等。
此資料型別可以應用於一系列 CSS 屬性,包括 font-size、text-shadow、border-width、height、margin、padding 和 width。
語法
<number><unit>
<number> 和一組單位在 <length> 資料型別中首先出現;單位之間沒有空格。
如果數字為 0,則不需要指定單位。
這些單位表示相對於其他長度(如字元大小、行高或視口大小)的度量,可以是絕對的或相對的。透過使用相對長度單位,可以更容易地在各種輸出上下文中縮放樣式表。
CSS <length> - 相對長度單位
此處列出的相對長度單位基於字型和視口。
基於字型的相對長度單位
透過比較元素或其父元素當前應用字型中某個字元或字型屬性的大小,字型長度確定 <length> 值。
<length> = cap <length> = ch <length> = em <length> = ex <length> = ic <length> = lh <length> = rem <length> = rlh
基於視口的相對長度單位
四個視口大小與視口百分比長度單位相關聯:小、大、動態和預設。它們適應不斷變化的瀏覽器介面,在介面調整大小或縮小時修改內容的可見性。
/* Small viewport */ <length> = sv*
/* Large viewport */ <length> = lv*
/* Dynamic viewport */ <length> = dv*
/* Viewport- percentage length values scaled according to height and width of containing block */ <length> = svh | lvh | dvh <length> = svw | lvw | dvw <length> = vmax <length> = vmin <length> = svb | lvb | dvb <length> = svi | lvb | dvb
容器查詢長度單位
當使用容器查詢來設定容器樣式時,允許的長度與查詢容器的尺寸成比例。
<length> = cqw <length> = cqh <length> = cqi <length> = cqb <length> = cqmin <length> = cqmax
絕對長度單位
當知道輸出介質的物理屬性時(例如列印佈局),物理測量由絕對長度單位表示。
<length> = px <length> = cm <length> = mm <length> = Q <length> = in <length> = pc <length> = pt
CSS <length> - 比較長度
在以下示例中,使用者可以輸入各種長度值,並且選定的長度將在結果欄中動態顯示。
在輸入框中輸入有效的長度值(例如,“300px”、“50%”、“30vw”)以在結果欄中動態檢查長度。
<html>
<head>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
margin: 50px;
padding: 50px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 50vh;
background-color: #ecf0f1;
}
#inputContainer {
display: flex;
align-items: center;
margin-bottom: 20px;
}
#inputField {
padding: 12px;
font-size: 16px;
margin-right: 10px;
border: 1px solid #3498db;
border-radius: 4px;
outline: none;
}
#inputField::placeholder {
color: #95a5a6;
}
#submitBtn {
padding: 12px 16px;
font-size: 16px;
background-color: #3498db;
color: #fff;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.3s ease;
}
#submitBtn:hover {
background-color: #2980b9;
}
#resultBar {
width: 0;
height: 20px;
background-color: #2ecc71;
transition: width 0.3s ease;
margin-top: 10px;
}
</style>
</head>
<body>
<div id="inputContainer">
<input type="text" id="inputField" placeholder="Enter length (e.g., 300px, 50%, 30vw)">
<button id="submitBtn" onclick="updateResult()">Enter</button>
</div>
<div id="resultBar"></div>
<script>
function updateResult() {
const inputField = document.getElementById('inputField');
const resultBar = document.getElementById('resultBar');
const inputValue = inputField.value.trim();
resultBar.style.width = inputValue;
}
// Trigger the updateResult function on pressing Enter key
document.getElementById('inputField').addEventListener('keyup', function(event) {
if (event.key === 'Enter') {
updateResult();
}
});
</script>
</body>
</html>
CSS <length> - 不同長度單位的演示
以下示例在一個螢幕上顯示了一些長度單位。
<html>
<head>
<style>
body {
font-family: 'Open Sans', sans-serif;
margin: 20px;
background-color: #f8f8f8;
}
h1, h2 {
color: #333;
text-align: center;
margin-bottom: 20px;
}
h3 {
color: #3498db;
text-align: center;
}
.length-examples {
display: flex;
justify-content: center;
align-items: flex-start;
flex-wrap: wrap;
}
.box {
width: 100px;
height: 100px;
border: 2px solid #3498db;
margin: 20px;
box-sizing: border-box;
background-color: #ecf0f1;
display: flex;
justify-content: center;
align-items: center;
font-weight: bold;
color: #333;
transition: transform 0.3s ease-in-out;
}
.box:hover {
transform: scale(1.1);
}
.px-example {
width: 100px;
height: 100px;
}
.em-example {
font-size: 1.5em;
}
.rem-example {
font-size: 1.5rem;
}
.percent-example {
width: 50%;
height: 50px;
background-color: #3498db;
}
</style>
</head>
<body>
<h1>Exploring Different Length Units in CSS</h1>
<div class="length-examples">
<div class="length-example">
<h3>Pixel (px)</h3>
<div class="box px-example">100px</div>
<div class="box px-example">100px</div>
</div>
<div class="length-example">
<h3>Em (em)</h3>
<div class="box em-example">1.5em</div>
<div class="box em-example">1.5em</div>
</div>
<div class="length-example">
<h3>Rem (rem)</h3>
<div class="box rem-example">1.5rem</div>
<div class="box rem-example">1.5rem</div>
</div>
<div class="length-example">
<h3>Percentage (%)</h3>
<div class="box percent-example">50%</div>
<div class="box percent-example">50%</div>
</div>
</div>
</body>
</html>
廣告