Blogger Information
Blog 8
fans 0
comment 0
visits 4837
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
浅谈em、rem、vh、vw
择善而从
Original
679 people have browsed it

浅谈em、rem、vh、vw

  • em、rem
  1. em是浏览器文本的默认字号的相对关键字,大多数浏览器em通常默认是16px;
  2. em是个相对长度单位,这个单位表示元素的 font-size 的计算值,继承父级元素的字号大小;
  3. rem这个单位代表根元素(通常为<html> 元素)的 font-size 大小,相对固定一些,根元素的字号大小不变时,它可以看成一个常量;
  4. 定义边框大小不能用em或者rem,要用px。
  1. html{
  2. /* 设置根元素字号大小 */
  3. font-size: 10px;
  4. }
  5. /* 直接使用"rem"来引用定义字号或其它属性 */
  6. div{
  7. /*此时 3rem : 3 * 10px = 30px; */
  8. font-size: 3rem;
  9. width: 20em;/*宽度为 20 *30px = 600px*/
  10. height: 20em;
  11. background-color: red;
  12. }
响应式按钮应用
  1. <head>
  2. <meta charset="UTF-8">
  3. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  4. <title>响应式按钮</title>
  5. <style>
  6. html{
  7. /* 设置根元素字号大小 */
  8. font-size: 10px;
  9. }
  10. .btn{
  11. /* 设置按钮背景颜色 */
  12. background-color: lightgreen;
  13. /* 按钮字体颜色设定 */
  14. color: coral;
  15. /* 去掉边框 */
  16. border:none;
  17. /* 去掉单击轮廓线 */
  18. outline: none;
  19. /* 内边距 */
  20. padding: 0.5rem 1rem;
  21. border-radius: 0.3rem;
  22. }
  23. /* 鼠标移上悬停效果 */
  24. .btn:hover{
  25. /* 透明度 */
  26. opacity: 0.8;
  27. /* 将鼠标设置为手型 */
  28. cursor: pointer;
  29. /* 阴影效果 */
  30. box-shadow: 0 0 3px #888;
  31. /* 阴影效果延时 */
  32. transition: 0.3s;
  33. }
  34. .small{
  35. font-size: 1rem;
  36. }
  37. .normal{
  38. font-size: 2rem;
  39. }
  40. .larger{
  41. font-size: 3rem;
  42. }
  43. </style>
  44. </head>
  45. <body>
  46. <button class="btn larger">Button</button>
  47. <button class="btn normal">Button</button>
  48. <button class="btn small">Button</button>
  49. </body>
  • vh、vw 视口百分比长度定义相对于文档的可见部分大小的值。
  1. vh是视口的初始包含块高度的 1%;
  2. vw是视口的初始包含块宽度的 1%;
  3. vh、vw在移动端布局使用最多;
  4. vmin和vmax可以用来指定视口高度 vw 和宽度 vh 两者之间的最小值和最大值。
  1. div{
  2. width: 80vw; /*宽度的80%*/
  3. height: 100vh;/*高度的100%*/
  4. background-color: cyan;
  5. }
  1. /*使用vmin或vmax,可得到正方形*/
  2. div{
  3. width: 80vmin;
  4. height: 80vmin;
  5. background-color: cyan;
  6. }
  • 总结:
    em,rem,vh,vw都是相对值,不是绝对值;
  1. em是相对于父元素的字体大小,
  2. rem是相对于根元素的字体大小,
  3. vh是相对于视口高度的百分比,
  4. vw是相对于视口的宽度百分比,
  5. vmax限定元素的视口宽度和高度的最大值,
  6. vmin限定元素的视口宽度和高度的最小值。
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!