CSS之position定位

太过爱你忘了你带给我的痛 2022-12-14 15:54 333阅读 0赞

一、前言

CSS position属性用于指定一个元素在文档中的定位方式。top、right、bottom、left 属性则决定了该元素的最终位置。position方式包括以下几种:

  • static 默认值。没有定位,元素出现在正常的流中(忽略 top, bottom, left, right 或者 z-index 声明)。
  • absolute
    生成绝对定位的元素,相对于 static 定位以外的第一个父元素进行定位。元素的位置通过 “left”, “top”, “right” 以及 “bottom” 属性进行规定。
  • relative 生成相对定位的元素,相对于其正常位置进行定位。因此,“left:20” 会向元素的 LEFT 位置添加 20 像素。
  • fixed
    生成绝对定位的元素,相对于浏览器窗口进行定位。元素的位置通过 “left”, “top”, “right” 以及 “bottom” 属性进行规定。
  • inherit
    规定应该从父元素继承 position 属性的值。

二、实例

1、static

static是position的默认值。该关键字指定元素使用正常的布局行为,即元素在文档常规流中当前的布局位置。此时 top、right、bottom、left 属性无效。

2、absolute(绝对定位)

定位方法:如果它的父元素设置了除static之外的position定位方式,那么它就会相对于其最接近的一个最有定位设置的父级元素进行绝对定位,如果元素的父级没有设置定位属性,则依据 body 元素左上角作为参考进行定位。绝对定位元素可层叠,层叠顺序可通过 z-index 属性控制,z-index值为无单位的整数,大的在上面,可以有负值。

示例代码:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>absolute定位</title>
  6. </head>
  7. <style>
  8. #position{
  9. width: 80%; margin: 0 auto;
  10. background: cornsilk;
  11. height: 500px;
  12. border: 2px solid seagreen ;
  13. position: absolute;
  14. }
  15. #main{
  16. width: 80%; margin: 0 auto;
  17. margin-top: 50px;
  18. height: 400px;
  19. background: darkseagreen;
  20. border: 2px solid aquamarine ;
  21. position: absolute;
  22. left: 20px;
  23. }
  24. #box{
  25. width: 50px;
  26. height: 50px;
  27. background-color: cornflowerblue;
  28. position: absolute;
  29. left: 50px;
  30. top: 50px;
  31. }
  32. </style>
  33. <body>
  34. <div id="position">
  35. <div id="main">
  36. <div id="box"></div>
  37. </div>
  38. </div>
  39. </body>
  40. </html>

div的三个容器都采用了除static的position定位方式,可看出id为main的div容器的left为20的间距则相对于父类id为positoin的div容器定位,box的left为50与top为50则相对于main父容器来定位。

效果图如下:
在这里插入图片描述

3、relative(相对定位)

相对定位元素不可层叠,依据left、right、top、bottom等属性在正常文档流中偏移自身位置。同时也可以用z-index分层设计。

示例:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>relative定位</title>
  6. </head>
  7. <style>
  8. #position {
  9. width: 80%; margin: 0 auto;
  10. background: cornsilk;
  11. height: 500px;
  12. border: 2px solid seagreen;
  13. }
  14. #box1 {
  15. width: 50px;
  16. height: 50px;
  17. color: white;
  18. background-color: cornflowerblue;
  19. margin-left: 50px;
  20. position: relative;
  21. left: 50px;
  22. top: 50px;
  23. }
  24. #box2 {
  25. width: 50px;
  26. height: 50px;
  27. color: white;
  28. background-color: darkviolet;
  29. position: relative;
  30. left: 50px;
  31. }
  32. </style>
  33. <body>
  34. <div id="position">
  35. <div id="box1">box1</div>
  36. <div id="box2">box2</div>
  37. </div>
  38. </body>
  39. </html>

box1采用相对定位方式relative,设置left边距50px即在相对自身位置的基础上与左边进行偏移50px,加上本身marginleft的50px,此时即距离左边100px,设置top边距50px即在当前位置上与上方偏移50px。最终的位置为距离左边100px,距离上方50px。box同理。

效果图:
在这里插入图片描述

4、fixed

固定定位与绝对定位类似,但它是相对于浏览器窗口定位,不会随着滚动条进行滚动。

常见的用途一般在页面固定头部、固定脚部或者固定侧边栏。

示例:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>relative定位</title>
  6. </head>
  7. <style>
  8. body{
  9. margin: 0px;
  10. padding: 0px;
  11. }
  12. #position {
  13. width: 80%; margin: 0 auto;
  14. background: cornsilk;
  15. height: 500px;
  16. border: 2px solid seagreen;
  17. }
  18. #box {
  19. width: 50px;
  20. height: 50px;
  21. color: white;
  22. background-color: cornflowerblue;
  23. position: fixed;
  24. left: 50px;
  25. top: 50px;
  26. }
  27. </style>
  28. <body>
  29. <div id="position">
  30. <div id="box">box</div>
  31. </div>
  32. </body>
  33. </html>

box控件定位方式采用fixed,位置则相对于整个浏览器窗口进行定位。

效果图:
在这里插入图片描述

5、inherit

定位方式:从父元素继承 position 属性的值

示例

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>inherit定位</title>
  6. </head>
  7. <style>
  8. body{
  9. margin: 0px;
  10. padding: 0px;
  11. }
  12. #position {
  13. width: 80%; margin: 0 auto;
  14. background: cornsilk;
  15. height: 500px;
  16. border: 2px solid seagreen;
  17. position: relative;
  18. }
  19. #box {
  20. width: 50px;
  21. height: 50px;
  22. color: white;
  23. margin-left: 50px;
  24. background-color: cornflowerblue;
  25. position: inherit;
  26. top: 50px;
  27. }
  28. </style>
  29. <body>
  30. <div id="position">
  31. <div id="box">box</div>
  32. </div>
  33. </body>
  34. </html>

box采用了inherit的定位方式,因为父类采用relative的相对定位的方式,则box继承了该定位方式,所以讲按照relative的定位规则来进行定位。

效果图:
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,333人围观)

还没有评论,来说两句吧...

相关阅读

    相关 CSS 定位属性position

    一、position 属性:规定元素的定位类型。即元素脱离文档流的布局,在页面的任意位置显示。 有4种不同类型的定位: ①absolute :绝对定位;脱离文档流的布局,遗

    相关 CSS position定位

    文档流 文档流,指的是元素排版布局过程中,元素会自动从左往右,从上往下的流式排列。并最终窗体自上而下分成一行行,并在每行中按从左到右的顺序排放元素。让元素脱离文档流的方法

    相关 HTML定位position

    一、CSS定位(Posotioning)属性允许你对元素进行定位, position属性值:      static(默认值):元素框正常生成。块状元素生成一个人矩形框,作为