CSS 动态获取屏幕视口尺寸

[[css]] 来源: 【逆天!你能想到,只用CSS就可以动态检测屏幕视口尺寸吗?】 https://www.bilibili.com/video/BV1zNDrYEEcx/?share_source=copy_web&vd_source=496b0ec4fbea050bb36e4b35a7ead07e

 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
@property --vw {

syntax: '<length>';

inherits: true;

initial-value: 100vw;

  

}

  

@property --vh {

syntax: '<length>';

inherits: true;

initial-value: 100vh;

  

}

  

:root {

--w: tan(atan2(var(--vw), 1px));

--h: tan(atan2(var(--vh), 1px));

}

  

body::before {

content: counter(w) " X " counter(h);

counter-reset: w var(--w) h var(--h);

font-size: 150px;

font-weight: 900;

position: fixed;

inset: 0;

width: fit-content;

height: fit-content;

margin: auto;

}

这是一个脚注 1

Licensed under CC BY-NC-SA 4.0
最后更新于 2025-12-03 15:07
comments powered by Disqus
使用 Hugo 构建
主题 StackJimmy 设计