美文网首页
css的relative、absolute和float

css的relative、absolute和float

作者: 狐尼克朱迪 | 来源:发表于2016-05-31 11:29 被阅读0次

前言总结

  1. 绝对定位的元素脱离了文档流,而浮动元素依旧在文档流中
  2. 浮动就是个带有方位的display:inline-block属性,absolute也是个inline-block化属性。

z-index

position:relative、position:absolute和position:fixed参与的情况下才有作用,表示层级

relative的特性

没有脱离正常的文档流,被设置元素相对于其原始位置而进行定位,其原始占位信息仍存在。
它可以提升元素的z-index层级,如下面的两列布局。如果左边不设置position:relative,会造成左边区域不可点击。

  <div class="g-bd1 f-cb">
    <div class="g-sd1">
        <p>左侧定宽</p>
    </div>
    <div class="g-mn1">
        <div class="g-mn1c">
            <p>右侧自适应</p>
        </div>
    </div>
</div>

/* 两列右侧自适应布局 */
.g-bd1{margin:0 0 10px;}
.g-sd1{position:relative;float:left;width:190px;margin-right:-190px;}
.g-mn1{float:right;width:100%;}
.g-mn1c{margin-left:200px;}

absolute的特性

包裹性

简单点就是元素inline-block化,如一个div默认宽度为100%,一旦被设置absolute属性,那么100%默认宽度会变成内部自适应的宽度:

  // html
  <div class="div">
    ![](https://img.haomeiwen.com/i1975863/f1c832ca67a391af.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
  </div>
  
  <div class="div absdiv">
    ![](https://img.haomeiwen.com/i1975863/f1c832ca67a391af.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
  </div>

// css
.div{
  background-color: #0f9345;
  padding: 20px;
  margin: 10px 0;
}
.absdiv{
  position: absolute;
}

得到的结果图:

normal和absolute

float也是典型的inline-block化,正常情况下我们要给行内元素设置宽高需要设置display:block属性,但是设置float或者absolute属性后,display:block就可以省了:

  // html
  <span class="normal">normal</span>
  <span class="fl">float left</span>
  <span class="abs">position absolute</span>

  //css
  .normal{
  display: block;
  width: 100px;
  height: 100px;
  background-color: red
}
.fl{
  float: left;
  width: 100px;
  height: 100px;
  background-color: yellow
}
.abs{
  position:absolute;
  width: 100px;
  height: 100px;
  background-color: green
}
Paste_Image.png

破坏性

浮动的破坏性在于切断line box链,致使高度塌陷,但其占据的实体位置还是在的。而absolute绝对定位不占据文档流的实体位置,因此会造成高度和宽度的塌陷。

  // html
  <div class="divdes">
    ![](https://img.haomeiwen.com/i1975863/f1c832ca67a391af.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
  </div>
  
  <div class="divdes">
    ![](https://img.haomeiwen.com/i1975863/f1c832ca67a391af.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
  </div>

// css
.divdes{
  padding: 20px;
  margin: 10px;
  float:left;
  background-color: #0f9345;
}
.abs{
  position: absolute;
}
Paste_Image.png

相关文章

  • css的relative、absolute和float

    前言总结 绝对定位的元素脱离了文档流,而浮动元素依旧在文档流中 浮动就是个带有方位的display:inline-...

  • 2017-12-30

    position:absolute and position:relative 的理解 Absolute,CSS中...

  • CSS

    absolute position和relative position:https://css-tricks.co...

  • float,position,relative,absolute

    在解释之前,首先说几本CSS的经典之作(要搞前台的人应该必读的): In CSS 2.1, a box may b...

  • CSS 定位 absolute和relative

    1、块级框从上到下一个接一个地排列,框之间的垂直距离是由框的垂直外边距计算出来。 2、display : none...

  • CSS 布局

    布局 1.position:absolute 、relative2.float:left、right3.margi...

  • 页面布局:absolute/relative/float

    块元素前后都要独占一行而內联元素不需要,这是基础。 position:fixed 当元素设置该属性之后,这个元素...

  • CSS布局

    一.绝对定位与相对定位 position:absolute/relative 二.浮动 float:left/ri...

  • html之relative,absolute和float理解

    relative: 相对定位,在其默认显示的位置的基础上,通过上下左右四个参数设置偏移一定的距离,但是仅仅是显示出...

  • CSS-position-relative/absolute/f

    absolute、relative与浮动float三者的区别 relative相对定位 将定位元素不偏离正常文档流...

网友评论

      本文标题:css的relative、absolute和float

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