Blogger Information
Blog 40
fans 0
comment 1
visits 34284
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
CSS基础学习(box-sizing属性、内容溢出设置、最小高度、常见元素的的居中方式)
景云
Original
679 people have browsed it

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>CSS基础学习(box-sizing属性、内容溢出设置、最小高度、常见元素的的居中方式)</title>
  7. <style>
  8. /* 将通用属性设置一下适用于绝大部门页面制作 */
  9. *{
  10. margin: 0;
  11. padding: 0;
  12. /* 全局使用IE盒子模型 */
  13. box-sizing: border-box;
  14. }
  15. /* <!-- 1.使盒子大小不受宽高之外属性的影响 --> */
  16. .box{
  17. width: 200px;
  18. height: 100px;
  19. /* 方案一(笨方法),把影响大小的元素值减掉,比如padding值或者border:calc(); */
  20. /* width: calc(200px - 22px); */
  21. /* height: calc(100px - 22px); */
  22. /* 方案二(新方法),让用户自己决定要不要将padding/border等影响盒子大小的属性加入到计算方式中:box-sizing() */
  23. /* box-sizing: content-box;默认值,w3c标准盒子模型,不含border/padding */
  24. box-sizing: border-box;/* 固定大小,不额外计算padding/border;这种盒子模型,最早由微软的IE浏览器实现的,称之为IE盒子模型 */
  25. border: 1px solid greenyellow;
  26. padding: 10px;
  27. background-color: hotpink;
  28. background-clip: content-box;
  29. }
  30. /* 2.当内容超出元素的高度设置方式(文档流是元素默认布局方式): */
  31. .content{
  32. width: 20em;
  33. height: 10em;
  34. background-color: greenyellow;
  35. /* overflow: visible;设置宽高后,溢出部分不处理,此为默认值; */
  36. /* overflow: scroll;设置宽高后,溢出部分以滚动条方式下拉显示,默认为Y轴下拉 */
  37. overflow: auto;/* 设置宽高后,当内容溢出时默认按照Y轴下拉显示溢出部分,无溢出则隐藏滚动条 */
  38. }
  39. /* 3.最小高度:只限制最小高度未限制最大高度
  40. 当内容超过最小高度时会自动伸展
  41. 当内容不足时,使用最小高度,保证页面保持规则不会缩小 */
  42. .mh div:nth-of-type(2){
  43. min-height: 5em;
  44. background-color: blueviolet;
  45. overflow: auto;
  46. }
  47. /* 4.常见元素的居中方式 */
  48. .inline{
  49. width: 20em;
  50. height: 15em;
  51. border:1px solid cadetblue;
  52. box-sizing: border-box;
  53. }
  54. /* a.行内/行内块元素 */
  55. .inline{
  56. /* text-align: center;水平居中 */
  57. }
  58. .inline a{
  59. /* line-height: 15em;垂直居中,父元素需要设置宽高,行高与父元素的高度一致即可实现。其中图片无效,因为它是行内块元素,有自己的宽高。 */
  60. }
  61. /* b.块元素居中 */
  62. .inline>div{
  63. width: 10em;
  64. height: 5em;
  65. background-color: chartreuse;
  66. }
  67. /* 使用margin来实现块元素的水平居中 */
  68. .inline>div{
  69. /* margin:0 auto;水平居中 */
  70. }
  71. /* 使用padding来实现块元素的垂直居中 */
  72. .inline{
  73. /* padding: 3em 0;垂直居中,不能设置高度 */
  74. }
  75. /* c.行内元素的水平+垂直居中 */
  76. /* .inline{
  77. text-align: center;
  78. line-height: 15em;
  79. } */
  80. /* c.块元素的水平+垂直居中 */
  81. /* 方案一 */
  82. .inline{
  83. /* padding: 5em; */
  84. }
  85. /* 方案二 */
  86. .inline{
  87. position: relative;
  88. }
  89. .inline div{
  90. position: absolute;
  91. top: 0;right: 0;bottom: 0;left: 0;
  92. margin: auto;
  93. }
  94. </style>
  95. </head>
  96. <body>
  97. <div class="box"></div>
  98. <div class="content">
  99. 新华社北京1月3日电(记者胡喆)记者从国家航天局获悉,截至1月3日6时,“天问一号”探测器已经在轨飞行163天,飞行里程突破4亿公里,距离地球约1.3亿公里,距离火星约830万公里。探测器姿态稳定,按计划将在一个多月后实施近火制动,进入环火轨道,准备着陆火星。
  100. “天问一号”任务是我国独立开展行星际探测的第一步,将通过一次发射实现对火星的“绕、着、巡”,即火星环绕、火星着陆、火面巡视。“天问一号”探测器总重约5吨,由环绕器和着陆巡视器组成,着陆巡视器主要包括进入舱和火星车。目前,环绕器已完成第三次在轨自检,各系统工作正常。(完)
  101. </div>
  102. <div class="mh">
  103. <div>
  104. aaa
  105. </div>
  106. <div>
  107. bbb<br>
  108. bbb<br>
  109. </div>
  110. </div>
  111. <div class="inline">
  112. <!-- <a href="https://www.php.cn" target="_blank">PHP中文网</a> -->
  113. <div></div>
  114. </div>
  115. </body>
  116. </html>
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!