博客列表 >第四课 box-sizing功能示例、相对定位与绝对定位的区别与联系及示例

第四课 box-sizing功能示例、相对定位与绝对定位的区别与联系及示例

phpcn_u40613
phpcn_u40613原创
2021年03月28日 22:53:25550浏览

1、box-sizing功能并实例演示;

  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>Document</title>
  8. <style>
  9. /* 代码的初始化 */
  10. *{
  11. margin: 0;
  12. padding: 0;
  13. box-sizing: border-box;
  14. }
  15. /* :root===html */
  16. :root{
  17. /* 浏览器默认:font-size: 16px ; */
  18. /* 为了便于计算和使用方便 */
  19. font-size: 10px;
  20. }
  21. /* em,rem */
  22. /* em:根据元素的上下文来确定它的值; */
  23. /* rem:根据根元素的字号来设置 */
  24. /* 将W3C的标准盒子转为IE的盒子 */
  25. /* 就是将盒子的padding和border计算在它的width和height内; */
  26. /* 就用box-sizing */
  27. /* box-sizing: border-box; */
  28. /* 当前盒子的边际在那里,当前盒子不在内容区,而是到了边框,这时候内容区就会被压缩。
  29. 内容区始终包含了内边距和边框的*/
  30. .box{
  31. width: 20em;
  32. height: 20em;
  33. padding: 10px;
  34. margin: 10px;
  35. border: 2px solid red;
  36. }
  37. </style>
  38. </head>
  39. <body>
  40. <p>1. 理解 box-sizing功能并实例演示; </p>
  41. <p> 2. 理解相对定位与绝对定位,并实例演示他们的区别与联系</p>
  42. <div class="box">
  43. </div>
  44. </body>
  45. </html>

没设置box-sizing: border-box; 时:

设置了box-sizing: border-box; 时:

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. <style>
  9. body{
  10. border: 2px solid red;
  11. }
  12. /* relative相对定位(相对于自身定位) */
  13. .box{
  14. width: 10rem;
  15. height: 10rem;
  16. background-color: aquamarine;
  17. /* position: static;静态定位,没有定位,默认 */
  18. /* position: static; */
  19. /* 相对定位,自动的转为定位元素了 */
  20. /* 定位元素:只要这个元素上有非static的定位属性,就是定位元素 */
  21. position: relative;
  22. /* 只要有定位元素,定位偏移就有效了 */
  23. top:5rem;
  24. left: 10rem;
  25. font-size: 5rem;
  26. }
  27. /* absolute:绝对定位,根据父元素定位,脱离了文档流 */
  28. /* 文档流:显示顺序与书写顺序一致 */
  29. /* 相对于它在原始文档流中的位置进行定位 */
  30. .box1{
  31. height: 10rem;
  32. border: 2px solid aquamarine;
  33. position: relative;
  34. }
  35. .box2{
  36. width: 10rem;
  37. height: 10rem;
  38. background-color: rgb(253, 72, 238);
  39. position: absolute ;
  40. left: 10rem;
  41. top:12rem;
  42. font-size: 2rem;
  43. }
  44. </style>
  45. </head>
  46. <body>
  47. <div class="box">box</div>
  48. <h1>PHP中文网</h1>
  49. <div class="box1">
  50. <div class="box2">box1</div>
  51. <h2>PHP学习</h2>
  52. </div>
  53. </body>
  54. </html>

定位的展示

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