博主信息
博文 7
粉丝 0
评论 1
访问量 4641
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
1. 实例演示盒模型常用属性,注意box-sizing的用法 2. 实例演示媒体查询 3. 实例演示em,rem用法,并说出之间差别
迷途的耗子
原创
535人浏览过

1. 实例演示盒模型常用属性,注意box-sizing的用法

  • 宽度 width: 100px;
  • 高度 height: 100px;
  • 内边距 padding: 5px;
  • 背景色 background-color: cadetblue;
  • 边框 border: 1px solid #000;
  • 规定背景的绘制区域background-clip: content-box;
    1. /* border-box 背景被裁剪到边框盒。 */
    2. /* padding-box 背景被裁剪到内边距框。*/
    3. /* content-box 背景被裁剪到内容框。 */
  • 外边距 margin: 1px 2px 3px 4px 顺序为上右下左

box-sizing的用法,并举例

  • 默认情况下,元素的宽度与高度计算方式如下:
    width(宽) + padding(内边距) + border(边框) = 元素实际宽度
    height(高) + padding(内边距) + border(边框) = 元素实际高度

  • 在盒子内无内容时,这么计算是没有问题的,但是当盒子内的padding值改变时,盒子的大小也跟着改变了。

实例演示

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <style>
  6. .box {
  7. /* 元素的宽度 */
  8. width: 100px;
  9. /* 元素的高度 */
  10. height: 100px;
  11. }
  12. .box-1 {
  13. /* 内边距 */
  14. padding: 5px;
  15. /* 背景色 */
  16. background-color: #f29b76;
  17. /* 边框 */
  18. border: 1px solid #000;
  19. /* 规定背景的绘制区域: */
  20. /* border-box 背景被裁剪到边框盒。 */
  21. /* padding-box 背景被裁剪到内边距框。 */
  22. /* content-box 背景被裁剪到内容框。 */
  23. background-clip: content-box;
  24. /* 设置下外边距 */
  25. margin-bottom: 5px;
  26. }
  27. .box-2 {
  28. /* 内边距 */
  29. padding: 5px;
  30. /* 背景色 */
  31. background-color: #00a1e9;
  32. /* 边框 */
  33. border: 1px solid #000;
  34. /* 将边框包含在内容区的宽和高 */
  35. box-sizing: border-box;
  36. margin-bottom: 10px;
  37. }
  38. </style>
  39. <title>盒模型常用属性</title>
  40. </head>
  41. <body>
  42. <div class="box box-1"></div>
  43. <div class="box box-2"></div>
  44. </body>
  45. </html>

2. 实例演示媒体查询



  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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. <button class="btn samll">按钮1</button>
  11. <button class="btn middle">按钮2</button>
  12. <button class="btn large">按钮3</button>
  13. </body>
  14. <style>
  15. html {
  16. font-size: 10px;
  17. }
  18. .btn {
  19. background-color: seagreen;
  20. color: white;
  21. border: none;
  22. outline: none;
  23. }
  24. .btn:hover {
  25. cursor: pointer;
  26. opacity: 0.8;
  27. transition: 0.3s;
  28. padding: 0.4rem 0.8rem;
  29. }
  30. .btn.small {
  31. font-size: 1.2rem;
  32. }
  33. .btn.middle {
  34. font-size: 1.6rem;
  35. }
  36. .btn.large {
  37. font-size: 1.8rem;
  38. }
  39. @media (max-width: 374px) {
  40. html {
  41. font-size: 12px;
  42. background-color: #F20E11;
  43. }
  44. }
  45. @media (min-width: 375px) and (max-width: 413px) {
  46. html {
  47. font-size: 14px;
  48. background-color: #00a1e9;
  49. }
  50. }
  51. @media (min-width: 414px) {
  52. html {
  53. font-size: 16px;
  54. background-color: #21e900;
  55. }
  56. }
  57. </style>
  58. </html>

3. 实例演示em,rem用法,并说出之间差别

  • em的值并不是固定的,会继承父级元素的字体大小.
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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>
  11. <span>你好!</span>
  12. </div>
  13. <style>
  14. html {
  15. font-size: 6px;
  16. /* 在根元素中设置的字号,在其它地方引用是使用rem,并且这个值是不变的 */
  17. /* 因为一个页面,只有一个根元素, html */
  18. /* 1rem = 6px */
  19. }
  20. div {
  21. font-size: 3rem;
  22. }
  23. div span {
  24. /* 4rem = 4*6=24px */
  25. font-size: 4em;
  26. }
  27. </style>
  28. </body>
  29. </html>
  • rem为元素设定字体大小时,仍然是相对大小,但相对的只是HTML根元素。这个单位可谓集相对大小和绝对大小的优点于一身,通过它既可以做到只修改根元素就成比例地调整所有字体大小,又可以避免字体大小逐层复合的连锁反应。

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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>
  11. <span>你好!</span>
  12. </div>
  13. <style>
  14. html {
  15. font-size: 6px;
  16. /* 在根元素中设置的字号,在其它地方引用是使用rem,并且这个值是不变的 */
  17. /* 因为一个页面,只有一个根元素, html */
  18. /* 1rem = 6px */
  19. }
  20. div span {
  21. /* 4rem = 4*6=24px */
  22. font-size: 4rem;
  23. }
  24. </style>
  25. </body>
  26. </html>
批改老师:PHPzPHPz

批改状态:合格

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学