博客列表 >演示盒模型常用属性、演示媒体查询、演示em,rem用法

演示盒模型常用属性、演示媒体查询、演示em,rem用法

Blackeye
Blackeye原创
2022年03月27日 02:38:03600浏览

part_1

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>盒模型常用属性</title>
  8. </head>
  9. <body>
  10. <div class="box"></div>
  11. <div class="box"></div>
  12. <style>
  13. /* 设置盒模型为长*宽 200*200,颜色为浅蓝色 */
  14. .box{
  15. width: 200px;
  16. height: 200px;
  17. background-color: aqua;
  18. }
  19. /* 设置上下左右内边距为5px,边框为5px 黑色实线 */
  20. .box{
  21. padding: 10px;
  22. border: 5px solid black;
  23. }
  24. /* 设置填充区,避免padding透明被颜色覆盖 */
  25. .box{
  26. background-clip: content-box;
  27. }
  28. /* 让浏览器自动计算盒子大小确保盒子宽高为200px,而不是内容区为200px */
  29. .box{
  30. box-sizing: border-box;
  31. }
  32. /* 设置外边距为20px */
  33. .box{
  34. margin: 20px;
  35. }
  36. /* 当出现外边距折叠时,外边距参照最大值设置 */
  37. .box:last-of-type{
  38. margin-top: 50px;
  39. }
  40. </style>
  41. </body>
  42. </html>

part1

part_2

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>媒体查询</title>
  8. </head>
  9. <body>
  10. <div class="box"></div>
  11. <style>
  12. /* 设置根元素字号大小,为rem准备 */
  13. html{
  14. font-size: 20px;
  15. }
  16. .box{
  17. width: 20rem;
  18. height: 20rem;
  19. background-color: green;
  20. box-sizing: border-box;
  21. border: 10px solid red;
  22. }
  23. @media (min-width: 1000px){
  24. html{
  25. font-size: 18px;
  26. }
  27. }
  28. @media (min-width: 500px) and (max-width: 1000px){
  29. html{
  30. font-size: 15px;
  31. }
  32. }
  33. @media (max-width: 500px){
  34. html{
  35. font-size: 10px;
  36. }
  37. }
  38. </style>
  39. </body>
  40. </html>

part2_1
part2_2

part_3

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>em,rem用法</title>
  8. </head>
  9. <body>
  10. <div class="show-em">
  11. <span>em</span>
  12. </div>
  13. <div class="show-rem">
  14. <span>rem</span>
  15. </div>
  16. <style>
  17. html{
  18. font-size: 10px;
  19. }
  20. .show-em{
  21. font-size: 3em;
  22. }
  23. .show-em span{
  24. font-size: 2em;
  25. }
  26. .show-rem{
  27. font-size: 20px;
  28. }
  29. .show-rem span{
  30. font-size: 2rem;
  31. }
  32. </style>
  33. </body>
  34. </html>

em基于父级元素参照,容易发生参照混乱,不宜与页面布局。
rem基于根元素参照,便于统一管理,即使其父元素方式变更也不会影响其相对的值。
part3_1
part3_2

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议