美文网首页
css 之 position 属性浅谈

css 之 position 属性浅谈

作者: skylor | 来源:发表于2021-05-02 12:32 被阅读0次

    定义

    position 属性用来指定一个元素在网页上的位置,下午主要通过3个维度来讲述:基准点、是否脱标与占有位置、是否需要设置边距,一共有5种定位方式:staticrelativeabsolutefixedsticky。在讲定位之前先讲几个概念:正常模式与脱标:

    正常模式:

    所谓正常模式,也就是正常占有位置,不影响下一个元素的布局,也就是说没有漂浮起来。常见的有块级元素与内联元素:

    • 块级元素(典型的如div),在浏览器窗口(view point)中垂直布局——每个都将显示在上一个元素下面的新行上,并且它们的外边距将分隔开它们,常见的如多个div:
      div layout.png
    // html
    <body>
        <div>I am div1</div>
        <div>I am div2</div>
        <div>I am div3</div>
    </body>
    
    // css
    div {
        background-color: lightgrey;
        width: 100px;
        height: 50px;
    } 
    div:nth-child(2) {
        background-color: yellow;
        width: 100px;
        height: 50px;
    }
    div:last-child {
        background-color: red;
        width: 100px;
        height: 50px;
    }
    
    • 内联元素表现不一样——它们不会出现在新行上;相反,它们互相之间以及任何相邻(或被包裹)的文本内容位于同一行上,只要在父块级元素的宽度内有空间可以这样做。如果没有空间,那么溢流的文本或元素将向下移动到新行。


      span layout.png
    // html
    <body>
        <span>I am span1</span>
        <span>I am span2</span>
        <span>I am span3</span>
    </body>
    // css
    span {
        background-color: lightgrey;
        width: 100px;
        height: 50px;
    } 
    span:nth-child(2) {
        background-color: yellow;
        width: 100px;
        height: 50px;
    }
    span:last-child {
        background-color: red;
        width: 100px;
        height: 50px;
    }
    

    脱标

    所谓脱标,就是脱离了”标准流“(有的叫”正常流“,英文是”normal flow“),本来该占有的位置就不再占有了,下一个元素会占有它的位置,此时元素会出现重叠现象,通过设置z-index大小来显示元素重叠顺序。

    static 定位

    static 是浏览器的默认定位方式,如果没有给元素 style 添加 position,那么就是 static 定位。该定位的特征是:

    • 基准点:按代码的顺序来决定元素的布局,正常显示模式,即所谓的”标准流“。
    • 边偏移:通过设置 top right bottom left 无效。
    • 脱标:不脱标,正常占有该有的位置,不影响下一个元素布局。
    • 使用场景:清除定位,即一个设置了非static定位的box,不要定位了,就使用static清除,在浏览器调试过程中非常重要,比如,可通过static查看原有的位置应该在哪。
    static.png
    // html
    <body>
        <div>test static position</div>
    </body>
    
    // css
    div {
      background-color: pink;
      top: 100px;
    } 
    

    relative 定位

    relative 相对定位方式,该定位方式的特征是:

    • 基准点:自己在static定位模式下的位置作为基准点,俗称元素的默认位置。
    • 边偏移:必须通过设置 top / right / bottom / left 来准确定位。
    • 脱标:不脱标,正常占有该有的位置,不影响下一个元素布局,下一个元素仍然以”标准流“看待它。
    • 使用场景:一个很常用的口诀”子绝父相“,如果子元素需要设置absolute定位的时候,父元素可设置relative,当然还有其他场景了,这里不一一列举。


      relative.png
    <body>
        <div class="father">
            <div class="son"></div>
        </div>
    </body>
    
    // css
    .father {
        background-color: lightgrey;
        width: 300px;
        height: 200px;
    } 
    .son {
        background-color: yellow;
        position: relative;
        top: 20px;
        width: 200px;
        height: 100px;
    } 
    

    absolute 定位

    absolute 绝对定位方式,该定位方式的特征是:

    • 基准点:一般是父元素,但是前提是父元素设置了一个非非非static定位,如果父元素没有设置定位,那么就以浏览器窗口作为基点。
    • 边偏移:必须通过设置 top / right / bottom / left 来准确定位。
    • 脱标:完全脱标,不占有该有的位置,影响下一个元素布局,下一个元素就当该元素不存在。
    • 使用场景:如果一个元素需要以父元素的(0,0)坐标点对齐的时候,可以马上想到 absolute ,还有需要转化为inline-block模式也可以使用absolute。


      absolute.png
    //html
    <body>
        <div class="father">
            <div class="son"></div>
            <div class="son2"></div>
        </div>
    </body>
    // css
    .father {
        background-color: lightgrey;
        width: 300px;
        height: 200px;
    } 
    .son {
        background-color: yellow;
        position: absolute;
        top: 20px;
        width: 100px;
        height: 100px;
    } 
    .son2 {
        background-color: red;
        top: 20px;
        width: 200px;
        height: 150px;
    } 
    

    fixed 定位

    fixed 固定定位方式,该定位方式的特征是:

    • 基准点:浏览器窗口为基点,不管页面怎么布局与滚动,该位置就固定不动。
    • 边偏移:必须通过设置 top / right / bottom / left 来准确定位。
    • 脱标:完全脱标,不占有该有的位置,影响下一个元素布局,下一个元素就当该元素不存在。
    • 使用场景:比如页面可恶的广告,你怎么滑动就停在那里不动。
    fixed.png
    // html
    <body>
        <div class="father">
            <div class="son"></div>
            <div class="son2"></div>
        </div>
    </body>
    
    // css
    .father {
        background-color: lightgrey;
        width: 300px;
        height: 200px;
    } 
    .son {
        background-color: yellow;
        position: fixed;
        right: 10px;
        width: 100px;
        height: 100px;
    } 
    .son2 {
        background-color: red;
        top: 20px;
        width: 200px;
        height: 150px;
    } 
    

    sticky 定位

    sticky 粘性定位方式,该定位其实包含了 relative 与 fixed 这两种定位模式,但不是同时存在,需要一个触发条件,即边偏移 top / right / bottom / left 的值达到后就会切换 fixed 方式,不同定位方式,就分别显示该方式的定位特征。

    • 基准点:relative 方式以自身位置为基准点; fixed 方式以浏览器窗口为基点。
    • 边偏移:如果设置 top / right / bottom / left 就会同时具备relative 与 fixed 这两种定位模式,如果没设置就默认 relative,
    • 脱标:relative 不脱标,fixed 脱标
    • 使用场景:比如页面可恶的广告,你怎么滑动就停在那里不动。


      sticky.gif
    // html
    <body>
        <div class="father">
            <div class="son"></div>
            <div class="son2"></div>
        </div>
    </body>
    
    // css
    .father {
        background-color: lightgrey;
        position: relative;
        left: 200px;
        width: 300px;
        height: 1000px;
    } 
    .son {
        background-color: yellow;
        position: sticky;
        top: 30px;
        width: 90px;
        height: 60px;
    } 
    .son2 {
        background-color: red;
        top: 20px;
        width: 200px;
        height: 150px;
    } 
    
    

    结语

    以上就是对各种定位的解释,在实际工作中也许会很复杂,但基本都是这些定位的巧妙运用,如果讲述有什么错误,欢迎留言评论,码子码图不易,码gif图更不容易,转载请注明出处,谢谢。

    相关文章

      网友评论

          本文标题:css 之 position 属性浅谈

          本文链接:https://www.haomeiwen.com/subject/jqdtdltx.html