Blogger Information
Blog 18
fans 0
comment 0
visits 15944
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
em、rem、vh和vw单位的区别与使用场景
沉寂的博客
Original
2788 people have browsed it

em、rem、vh和vw单位的区别与使用场景

em与rem之间的区别

em与rem是代码中字体大小,长度大小的单位,
em设置根元素字体大小,如果需要根据根元素改变的元素,大小长度单位需要用rem
如下代码所示:
html { font-size: 0.75em; }
.panel { padding: 1rem;}
一下为用em和rem的实例:

  1. <style type="text/css">
  2. html {
  3. font-size: 1.25em;
  4. }
  5. .btn {
  6. /* display: grid; */
  7. width: 10em;
  8. height: 2em;
  9. line-height: 2em;
  10. background-color: lightpink;
  11. text-align: center;
  12. /* border: solid 1px #000; */
  13. /* margin-top: 1em; */
  14. outline: none;
  15. border-radius: 0.3em;
  16. box-shadow: 0 0 3px lightpink;
  17. color: white;
  18. }
  19. /* 根据响应式布局来改变按钮大小 */
  20. .small {
  21. font-size: 12px;
  22. }
  23. .normal {
  24. font-size: 16px;
  25. }
  26. .largar {
  27. font-size: 20px;
  28. }
  29. .btn:hover {
  30. cursor: pointer;
  31. opacity: 0.8;
  32. /* 延迟变色 */
  33. transition: 0.3s;
  34. }
  35. </style>
  1. <button class="btn small">btn1</button>
  2. <button class="btn normal">btn2</button>
  3. <button class="btn largar">btn3</button>

执行结果如下:
em与rem

vh与vw

  vw:可视宽度的百分之一(1/100)vh:可视高度的百分之一(1/100)vh与vw也是用在可响应式布局当中,只作为单位 ;
  也有vmax和vmin,这两个单位的做用固定形状时用列如固定正方形,不管可视宽度和高度怎么改变 ,固定形状不变;
  最小宽度和最小高度 都是按照百分比给的,会随可视窗体大小改变而改变,最大宽度和最大高度都是按照百分比给的,可视窗体变大会变大,窗体缩小到最大宽度是不会再缩小 。
实例代码如下:

  1. <style>
  2. html {
  3. font-size: 1.25em;
  4. }
  5. /*.squire {
  6. width: 20vw;
  7. height: 20vh;
  8. background-color: lightpink;
  9. }*/
  10. .squire {
  11. width: 20vmax;
  12. height: 20vmax;
  13. }

<div class="squire"></div>
执行结果如下所示:
vh与vw

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!