CSS魔法堂:Absolute Positioning就这个样

本文详细解析了CSS绝对定位的概念、定位参考系、containing block的作用以及top/right/bottom/left属性值的实际计算过程。同时,文章还讨论了fixed定位与absolutepositioning的关系,并提供了实例代码帮助理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

当我们以position:absolute之名让元素脱离Normal flow的控制后,以为通过lefttop属性值即可让元素得以无限的自由时,却发现还有各种神秘的力量左右着它的来去,于是我们意识到自己力量的微弱,开始迷茫不前。
后来有幸拾到各路前辈高人的秘笈,终于打通任督二脉,记录在案以便日后查阅。

以Normal flow为基础

Q:不是说好以左上角为原点(0,0)吗?怎么top:auto;right:auto;bottom:auto;left:auto;时的效果和Normal flow中的是一样的?

<style type="text/css">
  #parent{
    background: blue;
  }
  #sibling{
    text-align: center;
    line-height: 100px;
    margin: 0 20px;
    height: 100px;
    background: red;
  }
  #protagonist{
    text-align: center;
    line-height: 100px;
    width: 200px;
    height: 100px;
    background: yellow;
    position: absolute;
  }
</style>
<div id="parent">
  <div id="sibling">position:static</div>
  <div id="protagonist">position:absolute</div>
</div>

347002-20160406113833250-1378466207.jpg
A:那是因为Absolute positioning在初始化状态时(top:auto;right:auto;bottom:auto;left:auto;),浏览器会生成一个看不见的采用Normal flow定位的虚拟盒子(hypothetical box),若虚拟盒子对应的盒子没有设置top/right/bottom/left属性值,则该盒子将与虚拟盒子重叠。
 因此当我们仅仅设置position:absolute时,呈现出来的效果是跟position:static是无区别的。那这时我们会有两个疑问了,1. 既然top/right/bottom/left等默认值为auto,那实际计算值是多少呢?2. 假如显示设置top/right/bottom/left为特定数值后,那效果又是如何的呢?
 若要回答上述问题,则先要理解定位参考系。一说起定位我们必须找到对应的参考系,如相对定位那样望文生义就知道它对应着某个参考系,而绝对定位则隐晦得多,咋看之下会让我们忽视参考系的重要性,然后糊里糊涂地理解和解读它呈现的效果。
 绝对定位的参考系就是盒子所在的containing block,下面我们来深入一下吧!

定位参考系——containing block

 不管采用的是Normal flow、Floats还是Absolute positioning,总之定位的参考系就是一个名为containing block的四方盒子,但不同的position scheme会对应不同containing block。就Absolute positioning而言,首先会寻找最近的一个position:relative/fixed/absolute的父容器元素,若找到且父容器为block-level element则以父容器的的padding box作为containing block,若父容器为inline-level element则根据父容器的directionCSS属性值决定containing block;若一个都找不到则会以initial containing block作为其的containing block。
更多关于containing block的信息可参考《CSS魔法堂:不得不说的Containing Block》
 因此top/right/bottom/left的实际值则是相对于containing block而言,我们可以通过el.offsetLeft/Top来获取top和left的实际值。

绝对定位的奥义——top/right/bottom/left+box model

也许大家都见过以下这种水平垂直居中方式(IE7/6/5.5下均无效)

    <style type="text/css">
      #parent{
        background: blue;
        width: 200px;
        height: 200px;
        position: relative;
      }
      #protagonist{
        background: yellow;
        width: 100px;
        height: 100px;
    
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
    margin: auto;
      }
    </style>
    <div id="parent">
      <div id="protagonist"></div>
    </div>

347002-20160406113845000-840502464.jpg
为啥简单设置top/right/bottom/left:0;margin:auto就轻松搞定这么难搞的水平垂直居中呢?请看下图
347002-20160406114036312-1073102304.jpg
 当盒子采用绝对定位后,其top/right/bottom/left和box model以占满整个containing block为目的,除非每个属性均设置的特定数值导致整体宽度或高度均小于containing block的宽度或高度。也就是得到以下两个等式:

  1. 垂直方向:'top' + 'margin-top' + 'border-top-width' + 'padding-top' + 'height' + 'padding-bottom' + 'border-bottom-width' + 'margin-bottom' + 'bottom' = height of containing block
  2. 水平方向:'left' + 'margin-left' + 'border-left-width' + 'padding-left' + 'width' + 'padding-right' + 'border-right-width' + 'margin-right' + 'right' = width of containing block
     然后一切玄机则蕴藏在auto这个属性值上了。
    其中垂直方向上top/margin-top/height/margin-bottom/bottom可以设置为auto,而水平方向上则是left/margin-left/width/margin-right/right可以设置为auto。

对于non-replaced element的垂直方向等式

margin-top/bottom设置为auto时,实际值自动分配的情况

  1. top/height/bottom均不为auto时,那么margin-top/bottom两者的实际值相等,且足以满足等式1。

margin-top/bottom设置为auto时,实际值为0的情况

  1. top/height/bottom均为auto时,height的值由其子元素决定。top/bottom的值则根据虚拟盒子来决定,最终让定位效果如同采用position:static一般,反正要让等式1成立。
  2. top/bottom均不为auto,而height为auto时,height会自动计算以满足等式1。
  3. 其他情况height由子元素或自身属性值决定,top/bottom由自身属性值或以满足等式1来决定实际值。

注意:top/auto/bottom默认值为auto,而margin-top/bottom默认值为0。

对于non-replaced element的水平方向等式

margin-left/right设置为auto时,实际值自动分配的情况

  1. left/width/right均不为auto时,那么margin-left/right两者的实际值相等,且足以满足等式2。

margin-left/right设置为auto时,实际值为0的情况

  1. left/width/right均为auto时,width的值由其子元素决定。left/right的值则根据direction的值来决定,最终让定位效果如同采用position:static一般反,正要让等式2成立。
  2. left/right均不为auto,而width为auto时,width会自动计算以满足等式2。
  3. 其他情况width由子元素或自身属性值决定,left/right由自身属性值或以满足等式2来决定实际值。

注意:left/width/right默认值为auto,而margin-left/right默认值为0。

对于replaced element

 由于replaced element自身有固有的width/height,因此当设置width:auto;height:auto时,其实际值就是元素固有的width/height。也就是width/height不存在为满足等式2和1动态扩展/缩小实际值的情况。结果就是除"2. top/bottom均不为auto,而height为auto时,height会自动计算以满足等式1。"和"2. left/right均不为auto,而width为auto时,width会自动计算以满足等式2。"两条不满足外,其他情况均一致。

注意,IE5.5/6/7下会有以下例外:

  1. left/margin-left/margin-right/right均不为auto而width为auto时,IE5.5下width的实际值将由子元素决定;
  2. top/margin-top/margin-bottom/bottom均不为auto而height为auto时,IE5.5下height的实际值将由子元素决定;
  3. left/width/right均不为auto,而margin-left/right为auto时,IE5.5/6/7下margin-left/right的实际值为0;
  4. top/height/bottom均不为auto,而margin-top/bottom为auto时,IE5.5/6/7下margin-top/bottom的实际值为0.

Fixed positioning——Absolute positioning的子类

 对于position:fixed其实也属于Absolute positioning,但它参考系永远是由Viewport所产生的containing block而已,其他均与上述内容一致。
注意:由Viewport所产生的containing block与initail containing block是不同的详情请参考《CSS魔法堂:不得不说的Containing Block》

<style type="text/css">
  body{
    background: blue;
  }
  #protagonist{
    background: yellow;
    width: 100px;
    height: 100px;

    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    margin: auto;
   }
</style>
<div id="protagonist">fsjohnhuang</div>

347002-20160406113904265-1695324245.jpg
注意:IE6不支持position:fixed

总结

若有纰漏,望各位指正,谢谢!
尊重原创,转载请注明来自:http://www.cnblogs.com/fsjohnhuang/p/5358587.html^_^肥子John

感谢

深入理解CSS绝对定位
10 Visual formatting model details
KB012: 绝对定位( Absolute positioning )
https://www.w3.org/TR/CSS21/visuren.html#fixed-positioning

内容概要:本文针对国内加密货币市场预测研究较少的现状,采用BP神经网络构建了CCi30指数预测模型。研究选取2018年3月1日至2019年3月26日共391天的数据作为本,通过“试凑法”确定最优隐结点数目,建立三层BP神经网络模型对CCi30指数收盘价进行预测。论文详细介绍了数据预处理、模型构建、训练及评估过程,包括数据归一化、特征工程、模型架构设计(如输入层、隐藏层、输出层)、模型编译与训练、模型评估(如RMSE、MAE计算)以及结果可视化。研究表明,该模型在短期内能较准确地预测指数变化趋势。此外,文章还讨论了隐层节点数的优化方法及其对预测性能的影响,并提出了若干改进建议,如引入更多技术指标、优化模型架构、尝试其他时序模型等。 适合人群:对加密货币市场预测感兴趣的研究人员、投资者及具备一定编程基础的数据分析师。 使用场景及目标:①为加密货币市场投资者提供一种新的预测工具和方法;②帮助研究人员理解BP神经网络在时间序列预测中的应用;③为后续研究提供改进方向,如数据增强、模型优化、特征工程等。 其他说明:尽管该模型在短期内表现出良好的预测性能,但仍存在一定局限性,如本量较小、未考虑外部因素影响等。因此,在实际应用中需谨慎对待模型预测结果,并结合其他分析工具共同决策。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值