屏幕尺寸
CSS 屏幕基础与尺寸单元详解
对页面布局原理有更深的了解,从而准确地控制好网页元素,保证按照预定的方式显示。
屏幕基础
Pixel | 像素
像素(Pixel)是数字图像的最小组成单元,它不是一个物理尺寸,但和物理尺寸存在一个可变的换算关系(物理尺寸之间的换算是固定的
PPI(Pixel Per Inch)是指每英寸包含的像素数,同时也是针对这个换算关系的一个描述性指标。其中的英寸(Inch)和厘米(cm
与
设备像素(device pixels)是指与硬件设备直接相关的像素,是真实的屏幕设备中的像素点。比如说,一个电脑显示器的参数中,最佳分辨率是
另一个概念是
对前端开发来说,设备像素没有意义,我只会关心
viewport | 视口
正常情况下,
if (typeof window.innerWidth != "undefined") {
(viewportwidth = window.innerWidth), (viewportheight = window.innerHeight);
}
width 控制viewport 的宽度,可以是固定值,也可以是device-width ,即设备的宽度height 高度initial-scale 控制初始化缩放比例,1.0 表示不可以缩放maximum-scale 最大缩放比例minimum-scale 最小缩放比例user-scalable: 是否允许用户缩放
可见如果不定义
比如,手机浏览器的窗口宽度可能是

.full-width {
width: 100vw;
position: relative;
left: 50%;
right: 50%;
margin-left: -50vw;
margin-right: -50vw;
}
然后,手机浏览器的供应商是这么考虑的:由于手机屏幕的宽度对于
和可见视口不同,布局视口用于元素布局和尺寸计算(比如百分比的宽度值
手机中的布局视口是可以更改的。你一定在很多移动版网页中见到过下边这个标签元素。
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
这是最早由
做一下总结
- 当 视觉视口
= 布局视口, 不会出现横向滚动条, 而且1css 像素= 1 设备像素 - 当 视觉视口
< 布局视口, 会出现横向滚动条, 而且1css 像素= 1 设备像素 - 当 视觉视口
> 布局视口, 不会出现横向滚动条, 而且1css 像素> 1 设备像素
Viewport
-
vw:是
Viewport ’s width 的简写,1vw 等于window.innerWidth 的1% -
vh:和
vw 类似,是Viewport ’s height 的简写,1vh 等于window.innerHeihgt 的1% -
vmin:
vmin 的值是当前vw 和vh 中较小的值 -
vmax:
vmax 的值是当前vw 和vh 中较大的值

Lengths Units | 尺寸单元
Relative Lengths Units | 相对尺寸单元
Relative length units specify a length relative to another length property. Relative length units scales better between different rendering mediums.
Unit | Description |
---|---|
em | Relative to the font-size of the element (2em means 2 times the size of the current font)Try it |
ex | Relative to the x-height of the current font (rarely used)Try it |
ch | Relative to width of the “0” (zero) |
rem | Relative to font-size of the root element |
vw | Relative to 1% of the width of the viewport*Try it |
vh | Relative to 1% of the height of the viewport*Try it |
vmin | Relative to 1% of viewport’s* smaller dimensionTry it |
vmax | Relative to 1% of viewport’s* larger dimensionTry it |
% |
Absolute Lengths
The absolute length units are fixed and a length expressed in any of these will appear as exactly that size.
Absolute length units are not recommended for use on screen, because screen sizes vary so much. However, they can be used if the output medium is known, such as for print layout.
Unit | Description |
---|---|
cm | centimetersTry it |
mm | millimetersTry it |
in | inches (1in = 96px = 2.54cm)Try it |
px | pixels (1px = 1/96th of 1in)Try it |
pt | points (1pt = 1/72 of 1in)Try it |
pc | picas (1pc = 12 pt) |