CSS 尺寸单位大全

作者: Ymuyi | 来源:发表于2016-04-05 19:33 被阅读855次

常用

%

百分比

px

像素 (计算机屏幕上的一个点)

em

相对于当前对象内文本的字体尺寸。例如:
1em 等于当前的字体尺寸;
2em 等于当前字体尺寸的两倍。
如果某元素以 12pt 显示,那么 2em 是24pt。
在 CSS 中,em 是非常有用的单位,因为它可以自动适应用户所使用的字体。

rem

使用rem同em一样皆为相对字体大小单位,不同的是rem相对的是HTML根元素。

rem中的“r”代表“root”,这意味着设置当前元素的字体大小的基准为根元素,大多数情况下,我们会设置在html元素上。

<style>    
html {font-size: 12px;}    
div  {font-size: 1.5rem;}
</style>
<body>    
<div>Test-01 (12px * 1.5 = 18px)       
     <div>Test-02 (12px * 1.5 = 18px)           
          <div> Test-03 (12px * 1.5 = 18px)  </div>        
     </div>    
</div>
</body>

当然,rem单位不仅应用在字体上,还可以实现到CSS 网格系统中。

不常见

vh 和 vw

在进行响应式布局时,我们常常会使用百分比来布局,然而CSS的百分比不总是解决每个问题的最佳方案,CSS的宽度相对于离它最近的父元素的宽度。 如果你想使用视口的宽度、高度而不是父元素的宽高,可以使用vh和vw单位。

1vh = viewportHeight * 1/100; 
1vw = viewportWidth * 1/100; 

使用vh、vw就可以保证元素的宽高适应不同设备。

vmin 和 vmax

vw和vh对应于viewport的width和height,而vmin和vmax分别对应于width、height中的最小值和最大值,例如如果浏览器的宽/高被设置为1000px/600px,那么

1vmin = 600 * 1/100;
1vmax = 1000 * 1/100;

ex 和 ch

ex、ch单位与em、rem相似之处在于都依赖于font-size,但是ex、ch还依赖于font-family,基于font-specific来计算。 引用w3C规范:

**ex unit
** Equal to the used x-height of the first available font. [CSS3-FONTS]The x-height is so called because it is often equal to the height of the lowercase "x". However, an ‘ex
’ is defined even for fonts that do not contain an "x". The x-height of a font can be found in different ways. Some fonts contain reliable metrics for the x-height. If reliable font metrics are not available, UAs may determine the x-height from the height of a lowercase glyph. One possible heuristic is to look at how far the glyph for the lowercase "o" extends below the baseline, and subtract that value from the top of its bounding box. In the cases where it is impossible or impractical to determine the x-height, a value of 0.5em must be assumed.
ch unit Equal to the used advance measure of the "0" (ZERO, U+0030) glyph found in the font used to render it.

用一副图来解释这两种单位的含义:

css-units
这两种单位,有许多用途,大部分是用于印刷时的微调整。例如,sup、sub元素分别显示上标和下标,但是我们可以使用position和bottom模拟:
<style type="text/css">
body {    margin: 0;    padding:0;}
.sup {    position: relative;    bottom: 1ex;}
.sub {    position: relative;    bottom: -1ex;}
</style>
<div>    
     AaB<span class="sup">b</span>
     CcXxD<span class="sub">d</span>EeFf
</div>

其他

in

英寸

cm

厘米

mm

毫米

pt

磅 (1 pt 等于 1/72 英寸)

pc

12 点活字 (1 pc 等于 12 点)

相关文章

  • CSS 尺寸单位大全

    常用 % 百分比 px 像素 (计算机屏幕上的一个点) em 相对于当前对象内文本的字体尺寸。例如:1em 等于当...

  • wxss和css的区别

    新增了rpx尺寸单位 css中需要手动进行像素单位换算,ps:rem wxss在底层支持新的尺寸单位rpx,在不同...

  • CSS中的尺寸单位

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

  • CSS中的尺寸单位

    绝对单位 相对单位 单位比例 运算

  • CSS常见的尺寸单位

    px(像素 ) 最常用的也是最基本的单位就是px,表示占多少像素值。页面默认是16像素。 em em也是经常用到的...

  • 小程序之WXSS

    WXSS和CSS区别有以下几个方面: 尺寸单位rpx 样式导入 内联样式 选择器 尺寸单位rpx rpx和rem相...

  • Web基础之CSS(二)

    CSS 单位尺寸: 颜色: 尺寸属性: width/height:宽高 overflow:当内容溢出元素框时如何处...

  • 微信小程序尺寸单位rpx以及样式相关介绍

    微信小程序尺寸单位 rpx单位是微信小程序中css的尺寸单位,rpx可以根据屏幕宽度进行自适应。规定屏幕宽为750...

  • CSS3 布局排版

    前提 在介绍布局排版之前,我们首先引入一个尺寸单位,这个尺寸单位是在 CSS3 中新引入的,它就是 rem ,所有...

  • 最全的CSS尺寸单位介绍

    前端开发过程中,尺寸单位是我们必须用到的,下面我们对css中常见的几种尺寸单位px,em,rem,rpx进行逐一介...

网友评论

  • 程序员1:Java,web前端,Python,人工智能,大数据 ,视频资料领取
    加微信:15003436931

    通过验证填写:A


    她就会负责百度云链接给你的

本文标题:CSS 尺寸单位大全

本文链接:https://www.haomeiwen.com/subject/gxbplttx.html