温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

css中position属性为absolute时其百分值怎么计算

发布时间:2021-08-03 10:31:55 来源:亿速云 阅读:112 作者:小新 栏目:web开发

这篇文章主要为大家展示了“css中position属性为absolute时其百分值怎么计算”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“css中position属性为absolute时其百分值怎么计算”这篇文章吧。

当position为absolue时候,其相关属性的百分比是相对它参考的元素(包含块)来进行计算并进行位置渲染的 。

首先我们必须知道:

1、[百分比的参照][1]:

根据包含块计算百分值(1)元素的margin/padding/left/right/width参照包含块的width来计算;(2)要计算 height /top 及 bottom 中的百分值,是通过包含块的 height 的值。如果包含块的 height 值会根据它的内容变化,而且包含块的 position 属性的值被赋予 relative 或 static ,那么,这些值的计算值为 0。

2、[确定包含块][2]:

确定一个元素的包含块的过程完全依赖于这个元素的 position 属性:

(1)如果 position 属性为 static 或 relative ,包含块就是由它的最近的“祖先块元素”(比如说inline-block, block 或 list-item元素)或格式化上下文(比如说 a table container, flex container, grid container, or the block container itself)的内容区的边缘(content)组成的。

(2)如果 position 属性为 absolute ,包含块就是由它的最近的 position 的值不是 static (也就是值为fixed, absolute, relative 或 sticky)祖先元素的内边距区的边缘(padding-left + content + padding-right)组成。

<!DOCTYPE html>
<html lang="en">
<head>
    <style>
     body {
  color: orange;
}
div {

  position: absolute; 
  /*box-sizing: border-box; /*加box-sizing: border-box;时的content=(width-border-padding);未加时的width=content*/*/
  width: 400px;
  border: 5px solid orange;
  padding: 50px;
  height: 160px;
  background: lightgray;
}

p {
  position: absolute; /* 包含块为最近的祖先元素(可能是块也可能不是块元素)的内边距边缘(padding-left + content + padding-right)组成;
  width: 50%;   /* == (50+400+50)px * 50% = 250px */
  height: 25%;  /* == (50+160+50)px * 25% = 65px */
  margin: 5%;   /* == (50+400+50)px * 5% = 25px */
  border: 5px solid orange;
  padding: 5%;  /* == (50+400+50)px * 5% = 25px */
  background: pink;
  color: green;
}
/*p {
   /* 包含块为最近的祖先块元素(只能是块元素)或格式化上下文的内容区的边缘(content)组成;
  width: 50%;   /* == 400px * 50% = 200px */
  height: 25%;  /* == 160px * 25% = 40px */
  margin: 5%;   /* == 400px * 5% = 20px */
  border: 5px solid orange;
  padding: 5%;  /* == 400px * 5% = 20px */
  background: pink;
  color: green;
}*/
    
    </style>
</head>
<body>
    <div>
        <p>This is a paragraph!</p>
    </div>
</body>
</html>

css中position属性为absolute时其百分值怎么计算css中position属性为absolute时其百分值怎么计算

以上是“css中position属性为absolute时其百分值怎么计算”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI