定位属性position的介绍

  1. 属性值有四个
  • position:static;
  • position:absolute;
  • position:fixed;
  • position:relative;
  • position:inherit

    上述属性值的讲解:

  • position:static表示默认行为,元素出现在正常流当中。
  • position:relative表示生成相对定位的元素,相对于正常位置进行定位。
  • position:absolute表示生成绝对定位,相对于其最近的第一个父元素进行定位,其父元素具有position:relative属性
  • position:fixed表示固定定位,始终相对于浏览器窗口进行定位。
  • position:inherit表示继承其父元素的position属性值。

    案例说明:文字的水平垂直居中

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>文字水平垂直居中</title>
    6. <style type="text/css">
    7. .box{
    8. width:20em;
    9. height: 30em;
    10. background-color: red;
    11. /*设置文字在盒子中水平居中显示*/
    12. text-align: center;
    13. /*设置文字在盒子的垂直居中显示*/
    14. line-height:30em;
    15. }
    16. </style>
    17. </head>
    18. <body>
    19. <div class="box">我是box</div>
    20. </body>
    21. </html>

    文字在盒子中垂直居中的小技巧:

  • 设置的line-height属性的高度要与盒子的高度一致

    案例说明:盒子的水平垂直居中

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>定位实现盒子的水平垂直居中</title>
    6. <style type="text/css">
    7. .parent{
    8. position: relative;
    9. background-color: yellow;
    10. border: 1px solid;
    11. width:25em;
    12. height: 25em;
    13. }
    14. .box{
    15. position: absolute;
    16. top:0;
    17. bottom: 0;
    18. left:0;
    19. right: 0;
    20. margin: auto;
    21. background-color: red;
    22. width:15em;
    23. height: 15em;
    24. text-align: center;
    25. line-height: 15em;
    26. }
    27. </style>
    28. </head>
    29. <body>
    30. <div class="parent">
    31. <div class="box">我是box</div>
    32. </div>
    33. </body>
    34. </html>
    总结:
    -在设置盒子水平垂直居中时,父元素设置相对定位,而子元素设置绝对定位,并且设置其子元素的四个方向为0;margin属性值设置为四个方向为auto

更多相关文章

  1. 演示box-sizing功能并实例/演示相对定位与绝对定位的区别与联系
  2. 学习CSS(二)
  3. box-sizing和定位
  4. box-sizing功能,相对定位/绝对定位
  5. 【CSS入门】学习CSS盒模型及常用样式属性详解(整理)
  6. iPhone如何修改手机定位?
  7. 无法定位程序输入点ucrtbase.terminate于动态链接库api-ms-win-c
  8. Selenium3自动化测试【15】元素定位之Class、Name
  9. Spring Boot利用Logback输出日志到指定位置的简单配置及使用方式

随机推荐

  1. php统计文件中的代码行数
  2. 如何用PHP迭代器来实现一个斐波纳契数列
  3. PHP中常用的加密解密方法总结
  4. php获取当前执行的php文件的文件名
  5. 如何解决php Function split() is deprec
  6. php调取摄像头实现拍照功能的方法
  7. PHP中将字符串全部转为小写或大写的方法
  8. PHP POSIX正则表达式
  9. 分享新浪图床上传接口源码
  10. php实现对文件夹目录中的文件进行排序的