2_rem与em.html 963 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  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>Document</title>
  7. <style>
  8. *{
  9. margin: 0;
  10. padding: 0;
  11. }
  12. html{
  13. font-size: 20px;
  14. }
  15. /* rem 相对于根元素(html)的字体大小 */
  16. .box{
  17. width: 10rem;
  18. height: 10rem;
  19. background-color: red;
  20. }
  21. .div1{
  22. font-size: 30px;
  23. }
  24. /* em 相对于本身 及 祖先元素的font-size */
  25. /* em 先找自己有没有font-size 如果没有就找祖先元素的font-size */
  26. .div2{
  27. font-size: 20px;
  28. width: 10em;
  29. height: 10em;
  30. background-color: blue;
  31. }
  32. </style>
  33. </head>
  34. <body>
  35. <div class="box"></div>
  36. <div class="div1">
  37. <div class="div2"></div>
  38. </div>
  39. </body>
  40. </html>