- 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>
广告