首页 > 开发 > CSS > 正文

浅谈CSS中的尺寸单位

2024-07-11 08:33:39
字体:
来源:转载
供稿:网友

浏览器的兼容性越来越好,移动端基本是清一色的webkit,经常会用到css的不同尺寸/长度单位,这里做个整理。

概览

绝对单位

px: Pixel 像素
pt: Points 磅
pc: Picas 派卡
in: Inches 英寸
mm: Millimeter 毫米
cm: Centimeter 厘米
q: Quarter millimeters 1/4毫米

相对单位

%: 百分比
em: Element meter 根据文档字体计算尺寸
rem: Root element meter 根据根文档( body/html )字体计算尺寸
ex: 文档字符“x”的高度
ch: 文档数字“0”的的宽度
vh: View height 可视范围高度
vw: View width 可视范围宽度
vmin: View min 可视范围的宽度或高度中较小的那个尺寸
vmax: View max 可视范围的宽度或高度中较大的那个尺寸

运算

calc: 四则运算

实例:


h1 {
width: calc(100% - 10px + 2rem);
}

单位比例

1in = 2.54cm = 25.4 mm = 101.6q = 72pt = 6pc = 96px

详细

绝对单位

px – Pixel 像素

像素 px 相对于设备显示器屏幕分辨率而言。


div { font-size: 12px }
p { text-indent: 24px }

pt Points 磅

1 pt = 1/72 英寸


div { font-size: 10pt }
p { height: 100pt }

pc Picas 派卡

十二点活字(印刷中使用的),相当于我国新四号铅字的尺寸。


div { font-size: 10pc }
p { height: 10pc }

in Inches 英寸


div { font-size: 10in }
p { height: 10in }

mm Millimeter 毫米


div { font-size: 10mm }
p { height: 10mm }

cm Centimeter 厘米


div { font-size: 10cm }
p { height: 10cm }

q Quarter millimeters 1/4毫米


div { font-size: 20q }
p { height: 100q }

相对单位

% 百分比

相对于父元素宽度


<body>
<div class="parent">
<div class="children"></div>
</div>
</body>

<style>
.parent { width: 100px }
.children { width: 66.6% }
/* children的宽度为 66.6px */
</style>

em Element meter 根据文档计算尺寸

相对于当前文档对象内文本的字体尺寸而言,若未指定字体大小则继承自上级元素,以此类推,直至 body,若 body 未指定则为浏览器默认大小。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表