Blogger Information
Blog 41
fans 0
comment 0
visits 24862
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
css 基础知识
自由之上
Original
518 people have browsed it

css 基础知识

一、盒子模型 大小、 位置、 计算方式

  1. <div class="box">
  2. 盒子宽度(约250px)=盒子内容宽度(200px) +左内边距(20px)+右内边距(20px)
  3. +左边框宽度(5px)+右边框宽度(5px)
  4. </div>
  5. <div class="box">
  6. 盒子内容宽度(约150px) = 盒子宽度(200px)-(左内边距(20px)+右内边距(20px)
  7. +左边框宽度(5px)+右边框宽度(5px))
  8. </div>
  1. * {
  2. margin: 0;
  3. padding: 0;
  4. outline: 1px dashed lightgray;
  5. }
  6. /* 定义所有盒子的大小、内边距与边框大小 */
  7. .box {
  8. width: 200px;
  9. height: 200px;
  10. background-color: lightpink;
  11. padding: 20px;
  12. border: 5px dashed red;
  13. /* 绝对定位 */
  14. position: absolute;
  15. }
  16. /* 选择第一个盒子并定义盒子背景裁剪为外边框与盒子计算方式为内容为主 */
  17. .box:first-child {
  18. background-clip: border-box;
  19. box-sizing: content-box;
  20. /* 从左算起偏移10像素 从顶算起偏移20像素 */
  21. left: 10px;
  22. top: 20px;
  23. }
  24. /* 选择第二个盒子并定义盒子背景裁剪为内容与盒子计算方式为边框为主 */
  25. .box:nth-child(2) {
  26. background-clip: content-box;
  27. box-sizing: border-box;
  28. /* 从右偏移50像素 从底部算起偏移30像素 */
  29. right: 50px;
  30. bottom: 30px;
  31. }

说明

  • outline: 1px dashed lightgray; 可以给标签加上一个轮廓线,起辅助作用
  • div 默认为包裹内容大小,通过 width 与 height 属性可以给 div 设置一个大小
  • box-sizing: content-box;(默认属性)以盒子内容为中心计算盒子大小,
    盒子大小=内容大小(标签定义的宽、高)+内边距*2+边框宽度*2

  • box-sizing: border-box; 以盒子边框为中心计算盒子大小,
    盒子大小=标签定义的宽、高
    内容大小=盒子大小-(内边距*2+边框宽度*2)

二、绝对定位与相对定位的区别与应用

  1. <h3>绝对定位</h3>
  2. <hr />
  3. <div><span>默认位置</span></div>
  4. <div>
  5. 绝对定位(body)</br>距左180px&nbsp;&nbsp;&nbsp;&nbsp;距顶80px
  6. <span>绝对定位(父标签)</br>距左20px&nbsp;&nbsp;距顶50px</span>
  7. </div>
  8. <h3>相对定位</h3>
  9. <hr />
  10. <div class="relative"><span>默认位置</span></div>
  11. <div class="relative"><span>相对定位</span></div>
  1. /* 初始化标签 */
  2. * {
  3. margin: 0;
  4. padding: 0;
  5. box-sizing: border-box;
  6. }
  7. div {
  8. width: 150px;
  9. height: 150px;
  10. border: 1px dashed lightgray;
  11. }
  12. div > span {
  13. background-color: lightgray;
  14. }
  15. div:nth-of-type(1) {
  16. margin-bottom: 100px;
  17. }
  18. /* 以div 父类标签为准选择父类标签下所有子标签集合
  19. 选择器所指定的标签与所在的索引有可能不匹配,导致选择失败 */
  20. div:nth-child(4) {
  21. background-color: lightseagreen;
  22. color: yellow;
  23. position: absolute;
  24. left: 180px;
  25. top: 80px;
  26. }
  27. /* 分组选择器,选择父标签下所有的div组, */
  28. div:nth-of-type(2) > span {
  29. background-color: red;
  30. /* 以父标签定位以父标签为标准,否则以body标签为标准定位 */
  31. position: absolute;
  32. left: 20px;
  33. top: 50px;
  34. }
  35. .relative:nth-of-type(4) {
  36. background-color: yellowgreen;
  37. background-clip: content-box;
  38. border: 1px dashed red;
  39. position: relative;
  40. left: 180px;
  41. top: 80px;
  42. }
  43. .relative:nth-of-type(4) > span {
  44. background-color: red;
  45. color: yellow;
  46. position: relative;
  47. left: 20px;
  48. top: 50px;
  49. }

 说明

CSS 之绝对定位与相对定位的区别

一:绝对定位
position: absolute

绝对定位:绝对定位是相对于元素最近的已定位的祖先元素,如果元素没有已定位的祖先元素,那么它的位置则是相对于最初的包含块(也就是 body)。

绝对定位本身与文档流无关,因此不占空间,普通文档流中的元素的布局就当绝对定位的元素不存在时一样,所以它们可以覆盖页面上其他的元素,且可以通过 z-index 属性来控制这些层的相应顺序。

二:相对定位

position: relative

相对定位:相对位置的坐标参考系是以自己上一次的位置(x,y)作为原点(0,0)。

注意:在使用相对定位时,无论是否进行移动,元素仍然占据原来的空间。因此,移动元素会导致它覆盖其它框。

三、固定定位与绝对定位

固定定位与绝对定位

  1. <div class="father">
  2. 父标签
  3. <div id="fixed1">固定定位</div>
  4. <div id="absolute1">绝对定位</div>
  5. </div>
  1. * {
  2. margin: 0;
  3. padding: 0;
  4. box-sizing: border-box;
  5. }
  6. .father {
  7. width: 200px;
  8. height: 200px;
  9. border: 1px dashed red;
  10. position: relative;
  11. left: 30px;
  12. top: 180px;
  13. }
  14. #fixed1,
  15. #absolute1 {
  16. width: 100px;
  17. height: 100px;
  18. background-color: lightblue;
  19. border: 1px dashed red;
  20. left: 80px;
  21. top: 60px;
  22. }
  23. #fixed1 {
  24. position: fixed;
  25. }
  26. #absolute1 {
  27. position: absolute;
  28. }

说明

css 固定定位和绝对定位的区别是什么?

绝对定位

绝对定位即脱离文档流的定位。定位参照物为自己的父级,但是自己的父级必须拥有 position 属性(父级 position 属性为 static 也不行,必须为 absolute,relative,fixed 中的一个)。如果自己的父级没有设置 position 属性,会一直向上寻找有 position 属性且不为 static 的的祖先元素,直到 body 元素。

固定定位

固定定位是相对于浏览器窗口进行定位的,无论怎样移动你的滑动条,它都会固定在相对于浏览器窗口的固定位置,另外要注意,它的兄弟元素将会在位置排布上忽视它的存在。这个时候用的 top,bottom,left,right 也是相对于浏览器窗口而言的。

固定定位和绝对定位的区别

固定定位于绝对定位最根本的区别还是偏移基准的不同,固定定位是相对于浏览器窗口,而绝对定位是相对于父级元素,而且最好还要注意 ie6 不兼容固定定位而兼容绝对定位。

四、使用绝对定位实现垂直居中

  1. <div id="box1">
  2. 外部盒子
  3. <div id="box2">内部盒子</div>
  4. </div>
  1. * {
  2. margin: 0;
  3. padding: 0;
  4. box-sizing: border-box;
  5. }
  6. #box1 {
  7. width: 200px;
  8. height: 200px;
  9. border: 1px dashed red;
  10. position: relative;
  11. }
  12. #box2 {
  13. width: 80px;
  14. height: 60px;
  15. background-color: lightskyblue;
  16. position: absolute;
  17. top: 0;
  18. bottom: 0;
  19. margin: auto;
  20. }

说明

  • 正文区左侧菜单使用默认定位
  • 内容区使用绝对定位,因为是根据正文区定位,需要求其父容器采用定位方式

五、使用绝对定位实现二列布局

  1. <div class="header">
  2. <div class="content">
  3. <ul class="nav">
  4. <li><a href="">导航1</a></li>
  5. <li><a href="">导航2</a></li>
  6. <li><a href="">导航3</a></li>
  7. <li><a href="">导航4</a></li>
  8. <li><a href="">导航5</a></li>
  9. </ul>
  10. </div>
  11. </div>
  12. <div class="main">
  13. <div class="left">左侧菜单</div>
  14. <div class="container">内容区</div>
  15. </div>
  16. <div class="footer">
  17. <div class="content">
  18. <p>欢迎光临|@ &copy;版权号132465</p>
  19. </div>
  20. </div>
  1. * {
  2. margin: 0;
  3. padding: 0;
  4. box-sizing: border-box;
  5. outline: 1px dashed lightgrey;
  6. }
  7. body {
  8. width: 800px;
  9. margin: 0 auto;
  10. }
  11. .header > .content,
  12. .footer > .content {
  13. width: 800px;
  14. height: 40px;
  15. background-color: lightcyan;
  16. line-height: 40px;
  17. margin: 0 auto;
  18. }
  19. .header > .content > .nav {
  20. width: 500px;
  21. margin: 0 auto;
  22. }
  23. .header > .content > .nav > li {
  24. list-style: none;
  25. float: left;
  26. text-align: center;
  27. }
  28. .header a {
  29. text-decoration: none;
  30. padding: 0 20px;
  31. }
  32. a:hover {
  33. color: red;
  34. }
  35. .footer > .content > p {
  36. text-align: center;
  37. }
  38. .main {
  39. position: relative;
  40. border: 5px dashed red;
  41. margin: 10px 0;
  42. }
  43. .left {
  44. height: 500px;
  45. width: 200px;
  46. background-color: turquoise;
  47. }
  48. .container {
  49. height: 500px;
  50. width: 580px;
  51. position: absolute;
  52. top: 0;
  53. left: 200px;
  54. background-color: violet;
  55. margin-left: 10px;
  56. }

六、使用浮动实现三列布局

  1. <div class="header">
  2. <div class="content">
  3. <ul class="nav">
  4. <li class="item"><a href="">导航1</a></li>
  5. <li class="item"><a href="">导航2</a></li>
  6. <li class="item"><a href="">导航3</a></li>
  7. <li class="item"><a href="">导航4</a></li>
  8. <li class="item"><a href="">导航5</a></li>
  9. <li class="item"><a href="">导航6</a></li>
  10. </ul>
  11. </div>
  12. </div>
  13. <div class="main">
  14. <div class="left">左侧</div>
  15. <div class="container">内容区</div>
  16. <div class="right">右侧</div>
  17. </div>
  18. <div class="footer">
  19. <div class="content">
  20. <p>欢迎您的光临 |&copy;版权所有</p>
  21. </div>
  22. </div>
  1. * {
  2. margin: 0;
  3. padding: 0;
  4. box-sizing: border-box;
  5. outline: 1px dashed lightgray;
  6. }
  7. a {
  8. text-decoration: none;
  9. padding: 0 20px;
  10. }
  11. a:hover {
  12. color: red;
  13. }
  14. .nav {
  15. width: 500px;
  16. margin: 0 auto;
  17. }
  18. .nav li {
  19. float: left;
  20. list-style: none;
  21. }
  22. .content {
  23. height: 35px;
  24. background-color: lightskyblue;
  25. line-height: 35px;
  26. text-align: center;
  27. }
  28. .main {
  29. width: 1000px;
  30. border: 5px dashed red;
  31. overflow: hidden;
  32. margin: 10px auto;
  33. }
  34. .left {
  35. width: 200px;
  36. height: 500px;
  37. background-color: lightcyan;
  38. float: left;
  39. }
  40. .right {
  41. width: 200px;
  42. height: 500px;
  43. background-color: lightgray;
  44. float: left;
  45. }
  46. .container {
  47. width: 590px;
  48. height: 500px;
  49. background-color: yellowgreen;
  50. float: left;
  51. }
  52. @media screen and (max-width: 800px) {
  53. .main {
  54. width: 800px;
  55. }
  56. .right {
  57. display: none;
  58. }
  59. }

说明

  • 正文区内三个 div 全部浮动
  • 使用 overflow: hidden;解决浮动导致的高度塌陷
  • 边框有一定宽度会计算在整体宽度中,需要从子元素中减去相应宽度

七、圣杯布局实现三列布局

  1. <div class="header">
  2. <ul class="nav">
  3. <li><a href="">导航1</a></li>
  4. <li><a href="">导航2</a></li>
  5. <li><a href="">导航3</a></li>
  6. <li><a href="">导航4</a></li>
  7. <li><a href="">导航5</a></li>
  8. <li><a href="">导航6</a></li>
  9. </ul>
  10. </div>
  11. <div class="main">
  12. <div class="container">主要内容</div>
  13. <div class="left">左侧</div>
  14. <div class="right">右侧</div>
  15. </div>
  16. <div class="footer">
  17. <p>欢迎光临|&copy;版权号123465</p>
  18. </div>
  1. * {
  2. margin: 0;
  3. padding: 0;
  4. box-sizing: border-box;
  5. }
  6. body {
  7. min-width: 500px;
  8. }
  9. a {
  10. text-decoration: none;
  11. padding: 0 20px;
  12. }
  13. a:hover {
  14. color: red;
  15. }
  16. li {
  17. list-style: none;
  18. float: left;
  19. }
  20. .header,
  21. .footer {
  22. height: 40px;
  23. background-color: lightskyblue;
  24. line-height: 40px;
  25. text-align: center;
  26. }
  27. .header ul {
  28. width: 500px;
  29. margin: 0 auto;
  30. }
  31. .main {
  32. min-width: 700px;
  33. border: 5px dashed red;
  34. overflow: hidden;
  35. padding: 0 200px;
  36. }
  37. /* 主要内容 */
  38. .container {
  39. width: 100%;
  40. min-height: 300px;
  41. background-color: yellow;
  42. float: left;
  43. }
  44. .left {
  45. width: 200px;
  46. height: 300px;
  47. background-color: lightgray;
  48. float: left;
  49. margin-left: -100%;
  50. left: 200px;
  51. position: relative;
  52. left: -200px;
  53. }
  54. .right {
  55. width: 200px;
  56. height: 300px;
  57. background-color: lightsalmon;
  58. float: left;
  59. margin-left: -200px;
  60. right: 200px;
  61. position: relative;
  62. left: 200px;
  63. }

说明

  1. 圣杯布局中所有直接子元素都需要浮动

  2. 优先显示主要内容,并通过内边距挤出左右菜单栏的宽度

  3. 左右菜单通过左边距覆盖到主要内容图层上

  4. 左右菜单通过相对布局调整位置到主内容被挤出的位置上增.

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:布局重点是思路与步骤
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!