Blogger Information
Blog 16
fans 2
comment 0
visits 19889
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
盒模型的常用属性和元素的水平和垂直对齐
肖傲的博客
Original
1183 people have browsed it

1.盒模型的常用属性

盒子模型有4个部分组成:宽高、内间距、边框、外间距

1.1内边距padding

padding:设置内边距,即边框与内容之间的距离
示例如下:

  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>Document</title>
  7. <style>
  8. .box {
  9. width: 200px;
  10. height: 200px;
  11. }
  12. .box.one {
  13. /* 设置内边距*/
  14. padding: 10px;
  15. /* 给盒子设置2px的边框并设置颜色为黑 */
  16. border: 2px solid #000;
  17. background-color: pink;
  18. background-clip: content-box;
  19. margin: 20px;
  20. }
  21. .box.two {
  22. /* 设置内边距*/
  23. padding: 10px;
  24. /* 给盒子设置2px的边框并设置颜色为黑 */
  25. border: 2px solid #000;
  26. background-color: pink;
  27. background-clip: content-box;
  28. margin: 20px;
  29. }
  30. </style>
  31. </head>
  32. <body>
  33. <div class="box one"></div>
  34. <div class="box two"></div>
  35. </body>
  36. </html>

1.2外边距margin

margin:设置外边距,即控制盒子与盒子之间的距离
示例如下:

  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>Document</title>
  7. <style>
  8. .box {
  9. width: 200px;
  10. height: 200px;
  11. }
  12. .box.one {
  13. padding: 10px;
  14. /* 给盒子设置2px的边框并设置颜色为黑 */
  15. border: 2px solid #000;
  16. background-color: pink;
  17. background-clip: content-box;
  18. /* 设置外边距 */
  19. margin: 20px;
  20. }
  21. .box.two {
  22. padding: 10px;
  23. /* 给盒子设置2px的边框并设置颜色为黑 */
  24. border: 2px solid #000;
  25. background-color: pink;
  26. background-clip: content-box;
  27. /* 设置外边距 */
  28. margin: 20px;
  29. }
  30. </style>
  31. </head>
  32. <body>
  33. <div class="box one"></div>
  34. <div class="box two"></div>
  35. </body>
  36. </html>

1.3边框属性

边框有3要素:边框类型、边框颜色、边框厚度
示例如下:

  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>Document</title>
  7. <style>
  8. .box {
  9. width: 200px;
  10. height: 200px;
  11. }
  12. .box.one {
  13. padding: 10px;
  14. /* 给盒子设置2px厚度的实线且颜色为黑色的边框 */
  15. border: 2px solid #000;
  16. background-color: pink;
  17. background-clip: content-box;
  18. /* 设置外边距 */
  19. margin: 20px;
  20. }
  21. .box.two {
  22. padding: 10px;
  23. /* 给盒子设置2px厚度的虚线且颜色为红色的边框*/
  24. border: 2px dashed red;
  25. background-color: pink;
  26. background-clip: content-box;
  27. /* 设置外边距 */
  28. margin: 20px;
  29. }
  30. </style>
  31. </head>
  32. <body>
  33. <div class="box one"></div>
  34. <div class="box two"></div>
  35. </body>
  36. </html>

2.元素的大小的计算方法

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

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

示例如下:

  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>用户自定义元素大小的计算方式</title>
  7. <style>
  8. .box {
  9. width: 200px;
  10. height: 200px;
  11. border: 3px solid #000;
  12. padding: 10px;
  13. background-color: pink;
  14. background-clip: content-box;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div class="box"></div>
  20. </body>
  21. </html>

  • 这意味着当你调整一个元素的宽度和高度时需要时刻注意到这个元素的边框和内边距。

box-sizing 属性可以被用来调整这些表现:

content-box 是默认值。如果你设置一个元素的宽为100px,那么这个元素的内容区会有100px 宽,并且任何边框和内边距的宽度都会被增加到最后绘制出来的元素宽度中。

border-box 告诉浏览器:你想要设置的边框和内边距的值是包含在width内的。

示例如下:

  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>用户自定义元素大小的计算方式</title>
  7. <style>
  8. .box {
  9. width: 200px;
  10. height: 200px;
  11. border: 3px solid #000;
  12. padding: 10px;
  13. background-color: pink;
  14. background-clip: content-box;
  15. /* 重新计算盒大小,宽和高只用在内容区部分 */
  16. box-sizing: content-box;
  17. /* 将边框包含在内容区的宽和高 */
  18. box-sizing: border-box;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div class="box"></div>
  24. </body>
  25. </html>

3.元素的水平居中和垂直居中

3.1文本居中对齐

  • 文本的水平居中可以使用 text-align: center;
  • 文本的垂直居中可以使用line-height 来设置

示例如下:

  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>文本居中</title>
  7. <style>
  8. .box {
  9. width: 400px;
  10. height: 400px;
  11. border: 2px solid red;
  12. /* 水平居中 */
  13. text-align: center;
  14. /* 设置与高度相同值来实现垂直居中 */
  15. line-height: 400px;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <div class="box">文本居中对齐</div>
  21. </body>
  22. </html>

3.2块级元素的居中对齐

  • 文本中可以通过使用 text-align: center;来实现水平居中,但是在块级元素中就无法实现。这时我们可以通过使用 margin: auto; 让浏览器自动计算左右外边距来实现水平居中。

示例如下:

  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>水平居中</title>
  7. <style>
  8. .box {
  9. width: 200px;
  10. height: 200px;
  11. background-color: skyblue;
  12. }
  13. .box .box1 {
  14. width: 100px;
  15. height: 100px;
  16. background-color: coral;
  17. /* 让浏览器自动计算左右外边距实现水平居中 */
  18. margin: auto;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div class="box">
  24. <div class="box1"></div>
  25. </div>
  26. </body>
  27. </html>

  • 如果要实现垂直居中,我们可以通过定位来实现。通过设置父级容器的top、bottom、left、right值为0,让当前元素绝对定位的上下文充满整个父级容器。

示例如下:

  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>水平居中</title>
  7. <style>
  8. .box {
  9. width: 200px;
  10. height: 200px;
  11. background-color: skyblue;
  12. /* 设置相对定位属性 */
  13. position: relative;
  14. }
  15. .box .box1 {
  16. width: 100px;
  17. height: 100px;
  18. background-color: coral;
  19. /* 水平居中 */
  20. margin: auto;
  21. /* 设置绝对定位属性 */
  22. position: absolute;
  23. /* 左上上角开始 */
  24. top: 0;
  25. left: 0;
  26. /* 右下角结束 */
  27. right: 0;
  28. bottom: 0;
  29. }
  30. </style>
  31. </head>
  32. <body>
  33. <div class="box">
  34. <div class="box1"></div>
  35. </div>
  36. </body>
  37. </html>

总结:通过学习和了解盒子模型常用属性以及块级元素的定位对齐水平居中,为后面学习布局打下基础。

Correcting teacher:WJWJ

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!