Blogger Information
Blog 23
fans 0
comment 3
visits 23408
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
相对单位em/rem/vh/vw 的区别和联系
a.
Original
1309 people have browsed it

em和rem

  • em
    • em具有继承性
    • em不是固定的,继承于父元素的font-size字号大小
  • rem
    • rem的字号大小相对于根元素(html)
    • 如果根元素没有设置字号,则默认为16px(绝大多数浏览器)
  • em和rem的区别
    • em的字号大小承继于父元素的font-size大小
    • rem的字号大小等于根元素或当前元素设置的字号大小
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>菜鸟教程(runoob.com)</title>
  6. <style>
  7. html{
  8. font-size:1.8px;
  9. }
  10. body{
  11. font-size:2px;
  12. }
  13. .p1{font-size:10em;}
  14. .p2{font-size:10rem;}
  15. </style>
  16. </head>
  17. <body>
  18. <p class="p1">这是一个段落。</p>
  19. <p class="p2">这又是一个段落。</p>
  20. </body>
  21. </html>

使用em和rem动态响应页面案例

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>em,rem</title>
  7. <style>
  8. html {
  9. /*1.25*16=20px*/
  10. font-size: 1.25em;
  11. }
  12. .box {
  13. /*1.1*20=22px*/
  14. font-size: 1.1rem;
  15. /*22*0.9=19.8px*/
  16. padding: 0.9em;
  17. margin: 0.3em;
  18. border: solid 1px;
  19. border-radius: 0.75em;
  20. background-color: rgba(15, 9, 9, 0.13);
  21. }
  22. .title {
  23. margin: 0.3em;
  24. }
  25. .content {
  26. font-size: 0.8rem;
  27. text-indent: 2.1em;
  28. line-height: 2;
  29. }
  30. @media screen and (min-width: 600px) {
  31. html {
  32. font-size: 1.3rem;
  33. }
  34. .box {
  35. background-color: rgb(94, 235, 199);
  36. }
  37. @media screen and (min-width: 800px) {
  38. html {
  39. font-size: 1.4rem;
  40. }
  41. .box {
  42. background-color: skyblue;
  43. }
  44. @media screen and (min-width: 1000px) {
  45. html {
  46. font-size: 1.6rem;
  47. }
  48. .box {
  49. background-color: rgb(230, 204, 89);
  50. }
  51. }
  52. @media screen and (min-width: 1200px) {
  53. html {
  54. font-size: 1.8rem;
  55. }
  56. .box {
  57. background-color: rgb(243, 164, 61);
  58. }
  59. @media screen and (min-width: 1400px) {
  60. html {
  61. font-size: 2rem;
  62. }
  63. .box {
  64. background-color: rgb(77, 206, 238);
  65. }
  66. /*
  67. @media screen and (min-width: 800px) {
  68. /* 14/16 = 0.875em */
  69. /*
  70. html {
  71. font-size: 0.875em;
  72. }
  73. .box {
  74. background-color: wheat;
  75. }
  76. }
  77. */
  78. </style>
  79. </head>
  80. <body>
  81. <div class="box">
  82. <h2 class="title">我国5个新冠病毒疫苗进行Ⅲ期临床试验</h2>
  83. <p class="content">
  84. 新华社北京12月16日电(记者陈芳、董瑞丰)“我国目前已有5个新冠病毒疫苗进行Ⅲ期临床试验,数量位于全球前列。”国务院联防联控机制科研攻关组疫苗研发专班工作组组长、国家卫健委医药卫生科技发展研究中心主任郑忠伟日前接受新华社记者专访时说,“疫苗研发已经进入冲刺阶段,我们处于全球第一方阵,但不为第一而抢跑。”
  85. </p>
  86. </div>
  87. </body>
  88. </html>

vh和vw

  • vw是相对于浏览器显示html文档的可视窗口宽度比例单位,如视口宽度为1900px,那么1vw=19px
  • vh是相对于浏览器显示html文档的可视窗口高度比例单位,如视口宽度为900px,那么1vh=9px
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>vh和vw</title>
  6. <style>
  7. .box{
  8. height:50vh;
  9. width:50vw;
  10. background-color:red;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <div class="box">我是一个盒子</div>
  16. </body>
  17. </html>

vmin和vmax

  • vmin:取当前Vw和Vh中较小的那一个值
  • vmax:取当前Vw和Vh中较大的那一个值

    此时上面的盒子都显示为正方形

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!