盒模型

盒模型
CSS盒模型由内容区(content),内边距(padding),边框(border),外边距(margin)组成。width和height设置内容框(content box)的宽度和高度。
总宽度 = 左外边距 + 左边框 + 左内边距 + 内容区宽度 + 右内边距 + 右边框 + 右外边距
总高度 = 上外边距 + 上边框 + 上内边距 + 内容区高度 + 下内边距 + 下边框 + 下外边距
为方便排版,工作中常常使用box-sizing:border-box样式转化为html盒模型,width=content+padding+border(此时,当width、padding、border设定时,那么content会随着实际的宽度进行自动缩放;)
border-box

示例:

width和heigth都为200px,padding为5px,border为1px,在默认情况下,box占用了width+padding+border= 212

1

使用box-sizing:border-box后,width和heigth仍为200px,content压缩成了188.

]

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport"
  6. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  7. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  8. <title>Document</title>
  9. <style>
  10. * {
  11. margin: 0;
  12. padding: 0;
  13. font-size: 10px;
  14. box-sizing: border-box;
  15. }
  16. .box {
  17. border: 1px solid red;
  18. height: 20em;
  19. width: 20em;
  20. padding: 0.5em;
  21. margin: 1em;
  22. background-color: lightgreen;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <div class="box"></div>
  28. </body>
  29. </html>

position: relative;" class="reference-link">相对定位position: relative;

相对于元素框在文档流中的原始位置进行定位。元素框仍在文档流中。

相对定位

  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. .pos_left {
  10. position: relative;
  11. left: -30px;
  12. }
  13. .pos_right {
  14. position: relative;
  15. right: -30px;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <h3>这是正常位置</h3>
  21. <h3 class="pos_left">这个位置相对于正常位置向左</h3>
  22. <h3 class="pos_right">这个位置相对于正常位置向右</h3>
  23. </body>
  24. </html>

position: absolute;" class="reference-link">绝对定位position: absolute;

设置为绝对定位的元素框从文档流完全删除,并相对于其包含块定位,包含块可能是文档中的另一个元素或者是初始包含块。元素原先在正常文档流中所占的空间会关闭,就好像该元素原来不存在一样。

示例:

当没有相对定位元素时,从初始html定位。
初始定位
当有相对定位元素时,从相对定位元素定位。
相对元素

代码如下:其它代码相同,区别在是否开启position: relative;关闭效果为上图,开启效果如下图。

  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. margin: 0;
  11. padding: 0;
  12. box-sizing: border-box;
  13. }
  14. .gf {
  15. height: 40em;
  16. width: 40em;
  17. background-color: darkkhaki;
  18. }
  19. .parent {
  20. /* 父级元素相对定位 */
  21. position: relative;
  22. background-color: lightgreen;
  23. height: 20em;
  24. width: 20em;
  25. top: 5em;
  26. left: 5em;
  27. border: 1px solid #000000;
  28. }
  29. .box {
  30. /* 子级元素绝对定位 */
  31. position: absolute;
  32. height: 10em;
  33. width: 10em;
  34. left: 15em;
  35. top: 15em;
  36. background-color: lightpink;
  37. }
  38. </style>
  39. </head>
  40. <body>
  41. <div class="gf">祖级元素
  42. <div class="parent">
  43. 父级元素
  44. <div class="box">绝对定位</div>
  45. </div>
  46. </div>
  47. </body>
  48. </html>

块元素的垂直居中方法

position: absolute;
left: 0;
top: 0;
bottom: 0;
right: 0;
margin: auto;
垂直居中

  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. margin: 0;
  11. padding: 0;
  12. box-sizing: border-box;
  13. font-size: 10px;
  14. }
  15. .parent {
  16. /* 父级元素相对定位 */
  17. position: relative;
  18. background-color: lightgreen;
  19. height: 20em;
  20. width: 20em;
  21. top: 5em;
  22. left: 5em;
  23. }
  24. .box {
  25. /* 子级元素绝对定位 */
  26. position: absolute;
  27. height: 10em;
  28. width: 10em;
  29. left: 0;
  30. top: 0;
  31. bottom: 0;
  32. right: 0;
  33. margin: auto;
  34. background-color: lightpink;
  35. }
  36. </style>
  37. </head>
  38. <body>
  39. <div class="parent">
  40. 父级元素
  41. <div class="box">绝对定位</div>
  42. </div>
  43. </body>
  44. </html>

更多相关文章

  1. box-sizing功能及相对定位与绝对定位实例演示
  2. 演示box-sizing功能并实例/演示相对定位与绝对定位的区别与联系
  3. 学习CSS(二)
  4. box-sizing和定位原理
  5. css之盒模型与定位
  6. 定位属性position 的介绍
  7. box-sizing和定位
  8. 【CSS入门】CCS基本语法和常用选择器的使用方法
  9. box-sizing功能,相对定位/绝对定位

随机推荐

  1. Python动态图见得多了?Excel:亦可赛艇!我可
  2. 使用Eclipse实现自定义Jmeter函数助手
  3. Python自动化办公 | 同事要我帮忙补写178
  4. 数据分析师必知必会:AB测试项目复盘(附PPT
  5. 如何创建你的第一个Python项目
  6. 升级版,用Python来进行多条曲线动态演示全
  7. 实战 | Python爬取B站柯南弹幕+Gephi梳理
  8. 对不起,我把APP也给爬了
  9. Python实现数据写入 Excel 的三种模块!
  10. 绝了!Python定时爬取微博热搜+pyecharts动