CSS3 - 新单位vw、vh、vmin、vmax使用详解(附样例)
像 px、em 这样的长度单位大家肯定都很熟悉,前者为绝对单位,后者为相对单位。CSS3 又引入了新单位:vw、vh、vmin、vmax。下面对它们做个详细介绍。
一、基本说明
1,vw、vh、vmin、vmax 的含义
- vw:视窗宽度的百分比(1vw 代表视窗的宽度为 1%)
- vh:视窗高度的百分比
- vmin:当前 vw 和 vh 中较小的一个值
- vmax:当前 vw 和 vh 中较大的一个值
2,vw、vh 与 % 百分比的区别
3,vmin、vmax 用处
4,浏览器兼容性
- Chrome:自 26 版起就完美支持(2013年2月)
- Firefox:自 19 版起就完美支持(2013年1月)
- Safari:自 6.1 版起就完美支持(2013年10月)
- Opera:自 15 版起就完美支持(2013年7月)
- IE:自 IE10 起(包括 Edge)到现在还只是部分支持(不支持 vmax,同时 vm 代替 vmin)
- Android:自 4.4 版起就完美支持(2013年12月)
- iOS:自 iOS8 版起就完美支持(2014年9月)
二、一个简单的样例
1,页面代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
<!DOCTYPE html> < html > < head > < meta charset = "utf-8" > < title >hangge.com</ title > < style > html, body, div, span, h1, h2, h3 { margin: 0; padding: 0; border: 0; } .demo { width: 100vw; font-size: 5vw; margin: 0 auto;
color: #FFF; } .demo2 { width: 80vw; font-size: 5vw; margin: 0 auto;
} .demo3 { width: 50vw; height: 50vh; font-size: 1vw; margin: 0 auto;
color: #FFF; } </ style > </ head > < body > < div class = "demo" > < h1 >宽度100%, 字体5%</ h1 > </ div > < div class = "demo2" > < h2 >宽度80%, 字体5%</ h2 > </ div > < div class = "demo3" > < h3 >宽度50%, 高度50%, 字体1%</ h3 > </ div > </ body > </ html > |
2,效果图

三、实现完整覆盖的遮罩层
1,样例代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
<!DOCTYPE html> < html > < head > < meta charset = "utf-8" > < title >hangge.com</ title > < style > html, body, div, span, button { margin: 0; padding: 0; border: 0; } button { width: 120px; height: 30px; color: #FFFFFF; font-family: "微软雅黑"; font-size: 14px; background: #28B995; } #mask { width: 100vw; height: 100vh; position: fixed; top: 0; left: 0; background: #000000; opacity: 0.5; display: none; } </ style > </ head > < body > < button onclick = "document.getElementById('mask').style.display='inline'" >点击显示遮罩</ button > < div id = "mask" onclick = "document.getElementById('mask').style.display='none'" /></ div > </ body > </ html > |
2,效果图


四、实现居中显示的弹出框
1,弹出框大小随内容自适应
- 点击弹出按钮后,会显示一个在整个屏幕上居中显示的弹出框。
- 弹出框的大小根据内容的大小自适应(logo 图片),同时弹出框后面还有个覆盖整个屏幕的半透明遮罩层。
- 点击关闭按钮后,则隐藏弹出框。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
<!DOCTYPE html> < html > < head > < meta charset = "utf-8" > < title >hangge.com</ title > < script type = "text/javascript" src = "js/jquery.js" ></ script > < style > html, body, div, span, button { margin: 0; padding: 0; border: 0; } button { width: 120px; height: 30px; color: #FFFFFF; font-family: "微软雅黑"; font-size: 14px; background: #28B995; } .dialog-container { display: none; width: 100vw; height: 100vh;
text-align: center; position: fixed; top: 0; left: 0; z-index: 10; } .dialog-container:after { display: inline-block; content: ''; width: 0; height: 100%; vertical-align: middle; } .dialog-box { display: inline-block; border: 1px solid #ccc; text-align: left; vertical-align: middle; position: relative; } .dialog-title { line-height: 28px; padding-left: 5px; padding-right: 5px; border-bottom: 1px solid #ccc;
font-size: 12px; text-align: left; } .dialog-close { position: absolute; top: 5px; right: 5px; font-size: 12px; } .dialog-body {
} </ style > </ head > < body > < button onclick = "$('#dialogContainer').show();" >点击显示弹出框</ button > < div id = "dialogContainer" class = "dialog-container" > < div class = "dialog-box" > < div class = "dialog-title" >居中弹出框</ div > < a onclick = "$('#dialogContainer').hide();" class = "dialog-close" >关闭</ a > < div class = "dialog-body" > < img src = "logo.png" class = "demo-image" /> </ div > </ div > </ div > </ body > </ html > |
2,弹出框大小随视窗大小改变
- 点击弹出按钮后,会显示一个在整个屏幕上居中显示的弹出框。
- 弹出框的大小不再由内容的大小决定,而是随视窗大小改变(宽高均为屏幕可视区域的 80%)。
- 点击关闭按钮后,则隐藏弹出框。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
<!DOCTYPE html> < html > < head > < meta charset = "utf-8" > < title >hangge.com</ title > < script type = "text/javascript" src = "js/jquery.js" ></ script > < style > html, body, div, span, button { margin: 0; padding: 0; border: 0; } button { width: 120px; height: 30px; color: #FFFFFF; font-family: "微软雅黑"; font-size: 14px; background: #28B995; } .dialog-container { display: none; width: 100vw; height: 100vh;
text-align: center; position: fixed; top: 0; left: 0; z-index: 10; } .dialog-box { top:10vh; left:10vw; width: 80vw; height: 80vh; text-align: left; position: absolute; border: 1px solid #ccc; display: flex; flex-direction: column; } .dialog-title { line-height: 28px; padding-left: 5px; padding-right: 5px; border-bottom: 1px solid #ccc;
font-size: 12px; text-align: left; } .dialog-close { position: absolute; top: 5px; right: 5px; font-size: 12px; } .dialog-body {
flex:1; overflow: auto; } </ style > </ head > < body > < button onclick = "$('#dialogContainer').show();" >点击显示弹出框</ button > < div id = "dialogContainer" class = "dialog-container" > < div class = "dialog-box" > < div class = "dialog-title" >居中弹出框</ div > < a onclick = "$('#dialogContainer').hide();" class = "dialog-close" >关闭</ a > < div class = "dialog-body" > < img src = "logo.png" class = "demo-image" /> </ div > </ div > </ div > </ body > </ html > |
五、显示大图时限制其最大尺寸
1,效果图


2,样例代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
<!DOCTYPE html> < html > < head > < meta charset = "utf-8" > < title >hangge.com</ title > < script type = "text/javascript" src = "js/jquery.js" ></ script > < style > html, body, div, span, button { margin: 0; padding: 0; border: 0; } button { width: 120px; height: 30px; color: #FFFFFF; font-family: "微软雅黑"; font-size: 14px; background: #28B995; } .dialog-container { display: none; width: 100vw; height: 100vh;
text-align: center; position: fixed; top: 0; left: 0; z-index: 10; } .dialog-container:after { display: inline-block; content: ''; width: 0; height: 100%; vertical-align: middle; } .dialog-box { display: inline-block; text-align: left; vertical-align: middle; position: relative; } .demo-image { max-width: 90vw; max-height: 90vh; } </ style > </ head > < body > < button onclick = "$('#dialogContainer').show();" >点击显示大图</ button > < div id = "dialogContainer" class = "dialog-container" onclick = "$('#dialogContainer').hide();" > < div class = "dialog-box" > < img src = "image.jpg" class = "demo-image" /> </ div > </ div > </ body > </ html > |
六、实现 Word 文档页面效果
1,效果图


2,样例代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
<!DOCTYPE html> < html > < head > < meta charset = "utf-8" > < title >hangge.com</ title > < script type = "text/javascript" src = "js/jquery.js" ></ script > < style > html, body, div, span, button { margin: 0; padding: 0; border: 0; } body {
} page { display: block; height: 98vh; width: 69.3vh; margin: 1vh auto; padding: 12vh; border: 1px solid #646464; box-shadow: 0 0 15px rgba(0,0,0,.75); box-sizing: border-box;
position: relative; } page:after { content: attr(data-page); color: graytext; font-size: 12px; text-align: center; bottom: 4vh; position: absolute; left: 10vh; right: 10vh; } a { color: #34538b; font-size: 14px; } </ style > < script type = "text/javascript" > $(document).ready(function(){ var lenPage = $("page").length; //自动添加每页底部的页码 $("page").each(function(i){ $(this).attr("data-page", "第 "+ (i+1) +" 页,共 "+ lenPage +" 页"); }); }); </ script > </ head > < body > < page ></ page > < page ></ page > </ body > </ html > |
原文出自:www.hangge.com 转载请保留原文链接:http://www.hangge.com/blog/cache/detail_1715.html
CSS3 - 新单位vw、vh、vmin、vmax使用详解(附样例)的更多相关文章
- CSS3新单位vw、vh、vmin、vmax使用详解
像 px.em 这样的长度单位大家肯定都很熟悉,前者为绝对单位,后者为相对单位.CSS3 又引入了新单位:vw.vh.vmin.vmax.下面对它们做个详细介绍. 新单位也成为视窗单位,视窗(View ...
- CSS3新单位vw,vh,vmin,vmax详解
1,vw,vh,vmin,vmax是由视窗Viewport大小来决定的,单位1,代表1%,是一种相对单位,只要是为响应式适配视窗的一种解决方案: vw:view width(视窗宽度)的百分比,1vw ...
- css3新单位vw、vh、vmin、vmax的使用介绍
1,vw.vh.vmin.vmax 的含义 (1)vw.vh.vmin.vmax 是一种视窗单位,也是相对单位.它相对的不是父节点或者页面的根节点.而是由视窗(Viewport)大小来决定的,单位 1 ...
- css新单位vw,vh在响应式设计中的应用
考虑到未来响应式设计的开发,如果你需要,浏览器的高度也可以基于百分比值调整.但使用基于百分比值并不总是相对于浏览器窗口的大小定义的最佳方式,比如字体大小不会随着你窗口改变而改变,如今css3引入的新单 ...
- css新单位 vw , vh
考虑到未来响应式设计的开发,如果你需要,浏览器的高度也可以基于百分比值调整.但使用基于百分比值并不总是相对于浏览器窗口的大小定义的最佳方式,比如字体大小不会随着你窗口改变而改变,如今css3引入的新单 ...
- css3新单位vw、vh、vmin、vmax的使用详解(转载)
文章传送门: https://blog.csdn.net/ZNYSYS520/article/details/76053961
- CSS3 - 新单位vw、vh、vmin、vmax使用详解
参考文章出自:https://www.hangge.com/blog/cache/detail_1715.html
- css3新单位vw、vh的使用详解
响应式布局的单位我们第一时间会想到通过rem单位来实现适配,但是它还需要内嵌一段脚本去动态计算跟元素大小. 比如: (function (doc, win) { let docEl = doc.doc ...
- css3新单位学习
vw,vh,vmin,vmax vw 1vw = 视窗width*1% vh 1vh = 视窗heihgt*1% 如果视窗的宽度小于高度,1vmin = 1vw,如果视窗宽度大于高度,1vmin = ...
随机推荐
- groovy运行程序和类型推断
在 Java 中,如果要声明一个 String 变量,则必须输入: String value = "Hello World"; 等号右侧的字符已经表明 value 的类型是 Str ...
- Linux-debian系统 /etc/network/interface 文件解读
原文 http://wiki.slimdevices.com/index.php/SqueezeOS_networking 话说Debian系的网卡配置跟Redhat系很不一样,Redhat是放在/e ...
- tensorboard实现tensorflow可视化
1.工程目录 2.data.input_data.py的导入 在tensorflow更新之后可以进行直接的input_data的导入 # from tensorflow.examples.tutori ...
- BZOJ4598: [Sdoi2016]模式字符串(点分治 hash)
题意 题目链接 Sol 直接考虑点分治+hash匹配 设\(up[i]\)表示\(dep \% M = i\)的从下往上恰好与前\(i\)位匹配的个数 \(down\)表示\(dep \% M = i ...
- flask代码统计作业
用户表: create table userInfo( id int not null unique auto_increment, name )not null, password ) not nu ...
- linux系统PKWindows系统,从各方便分析linux和Windows的优劣
服务器系统linux系统和linux系统哪个好用,公说公有理婆说婆有理,今天鼎峰凡凡大概对Linux系统与Windows系统的优缺点PK!可以从以下几个方面来看 ①成本 赞成Linux的声音Linu ...
- ASP.NET错误处理的方式(二)
要创建页中的全局处理程序,请创建 Page_Error 事件的处理程序.要创建应用程序范围的错误处理程序,请在 Global.asax 文件中将代码添加到 Application_Error 方法.只 ...
- POP动画[1]
POP动画[1] pop动画是facebook扩展CoreAnimation的,使用及其方便:) 1:Spring系列的弹簧效果(两个动画kPOPLayerBounds与kPOPLayerCorner ...
- js的一道经典题目
今天碰到一道题,里面既包含了匿名函数的知识,也包含了预编译,函数的传参(形参),感觉迷迷糊糊的,所以想着做个总结. var foo={n:1}; (function(foo){ console.log ...
- nlog 2.0 强制转换使用 4.0 版本
今天下午研发代码,发现调用其他小组研发的代码,发现其中有使用nlog功能,但nlog版本是2.0 ,而我的项目使用4.0 版本 导致部分功能不能使用,故在web配置文件中加入以下代码即可 <de ...