【图解CSS#Position】

关于CSS position,来自MDN的描述:

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

先看一个图片:


child没设置position的样式

代码如下:

<body>
  <div class="parent1">
    <div class="child1-1">child1-1</div>
    <div class="child1-2">child1-2</div>
    <div class="child1-3">child1-3</div>
    <div class="child1-4">child1-4</div>
  </div>
</body>
<style type="text/css">
    .parent1{
      background-color: chocolate;
      width: 300px;
      height: 300px;
      margin: 25px 50px 75px;
    }
    .child1-1{
      background-color: cornflowerblue;
      width: 60px;
      height: 60px;
    }
    .child1-2{
      background-color:crimson;
      width: 60px;
      height: 60px;
    }
    .child1-3{
      background-color:darkcyan;
      width: 60px;
      height: 60px;
    }
    .child1-4{
      background-color: darkgreen;
      width: 60px;
      height: 60px;
    }
</style>

这里展示的就是一个正常“文档流”,

Normal flow
Boxes in the normal flow belong to a formatting context, which may be block or inline, but not both simultaneously. Block-level boxes participate in a block formatting context. Inline-level boxes participate in an inline formatting context.

个人理解:顾名思义就是像写文档一样,一行一行的排列,如图,每个child 都是一个60*60 的小方块,即使在他们右边有多余的位置,但是它们还是占一行。position就是每个元素的定位属性,而默认就是 position = static,即上图中parent 和 child 都是展示的文档流样式。

而position属性一共有以下取值:

  1. static
    默认值。没有定位,元素出现在正常的流中(忽略 top, bottom, left, right 或者 z-index 声明)

  2. relative
    生成相对定位的元素,相对于其正常位置进行定位。 如我们将上面的代码改成

    // ...
    .child1-2{
        background-color:crimson;
        width: 60px;
        height: 60px;
        position: relative;
        left: 20px;
        top: 30px;
    }
    
    relative

    可以看到 child1-2 在原来的位置上,左边多出了 20px ,距离顶部多出了 30px

  3. absolute
    生成绝对定位的元素,相对于 static 定位以外的第一个父元素进行定位。
    我们将上例的child1-2恢复,然后操作child1-3

    .child1-3{
          background-color:darkcyan;
          width: 60px;
          height: 60px;
          position: absolute;
          left: 20px;
          top: 30px;
    }
    
    absolute

    可以看到,child1-3 的位置已经和 parent 无关了(因为 parent 的 position = "static"),很多时候,都会将 parent 设置成relative 来和 absolute child 配合使用。
    我们再改一下 child1-3 的代码

    .child1-3{
        background-color:darkcyan;
        width: 60px;
        height: 60px;
        position: absolute;
        left: 20px;
        bottom: 30px;   // 注意这个
      }
    
    bottom 30px

    bottom 30px 增加浏览器的高度

    可以看到,child1-3 其实是一直跟着浏览器的高度在变化的。

  4. fixed

    .child1-4{
          background-color: darkgreen;
          width: 60px;
          height: 60px;
          position: fixed;
          bottom: 30px;
          left: 70px;
    }
    
    fixed

    而且,当浏览器高度变化时,他们两个也会跟着变化。
    但是,两者的区别就在于,当 parent 设置成 position = relative 时,

    .parent1{
          background-color: chocolate;
          width: 300px;
          height: 300px;
          margin: 25px 50px 75px;
          position: relative;
    }
    
    parent position = relative

    即此时,child1-3 的 left,right 等以 parent 为基准。

  5. inherit
    规定应该从父元素继承 position 属性的值。
    这个就不需要图解了。。。。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML标准。 注意:讲述HT...
    kismetajun阅读 27,958评论 1 45
  • 1 css3知识解读 css3解读链接:css3知识解读 自定义字体利用@font-face{}引入自定义字体;创...
    果木山阅读 377评论 0 1
  • 本文主要讲述页面布局样式方面涉及的知识点,更全面的对CSS相应的技术进行归类、整理、说明,没有特别详细的技术要点说...
    Joel_zh阅读 1,013评论 0 1
  • 各种纯css图标 CSS3可以实现很多漂亮的图形,我收集了32种图形,在下面列出。直接用CSS3画出这些图形,要比...
    剑残阅读 9,786评论 0 8
  • 推荐指数: 6.0 书籍主旨关键词:特权、焦点、注意力、语言联想、情景联想 观点: 1.统计学现在叫数据分析,社会...
    Jenaral阅读 5,773评论 0 5