简体   繁体   English

将固定位置放在相对位置时如何工作

[英]how does fixed position work when put it inside a relative position

According to this statement : 根据此声明

When position is set to absolute or fixed, the left property specifies the distance between the element's left edge and the left edge of its containing block. 当position设置为绝对或固定时,left属性指定元素的左边缘与其包含块的左边缘之间的距离。 (The containing block is the ancestor to which the element is relatively positioned.) (包含块是元素相对定位到的祖先。)

I put a fixed element inside a relative element, and set its 'left' property to some value.This value should relative to its parent div. 我在相对元素中放置了一个固定元素,并将其'left'属性设置为某个值,该值应相对于其父div。 But it doesn't work as expected. 但是它没有按预期工作。 see my codepen: https://codepen.io/iamnotstone/pen/PgdPrJ?&editable=true 看到我的代码笔: https ://codepen.io/iamnotstone/pen/PgdPrJ ?& editable = true

The text should inside the red box. 文本应在红色框中。

 body { width: 500px; height: 1400px; margin: 0 auto; } .test { width: 500px; height: 132px; background-color: red; position: relative; left: 200px } h1 { position: fixed; top: 0px; width: 500px; margin: 0 auto; padding: 10px; left: 0px } 
 <div class='test'> <h1>Fixed positioning</h1> </div> 

According to the doc: Fixed positioning 根据文档: Fixed positioning

Fixed positioning is similar to absolute positioning. 固定定位类似于绝对定位。 The only difference is that for a fixed positioned box, the containing block is established by the viewport. 唯一的区别是,对于固定位置的框,包含块是由视口建立的。

Instead of left you can use margin-left property: 而不是离开你可以用利润率左属性:

 body { width: 500px; height: 1400px; margin: 0 auto; } .test{ width: 500px; height: 132px; background-color: red; position: relative; left: 200px } h1 { position: fixed; top: 0px; width: 500px; margin: 0 auto; padding: 10px; margin-left: 0px } 
 <div class='test'> <h1>Fixed positioning</h1> </div> 

It's very strange. 真奇怪 When I set a transform property inside its parent, everything just works as expected! 当我在其父级中设置转换属性时,一切都按预期工作!

 body { width: 500px; height: 1400px; margin: 0 auto; } .test{ width: 500px; height: 132px; background-color: red; position: relative; left: 200px; transform: translate(0) /* added this, and works as expected */ } h1 { position: fixed; top: 0px; width: 500px; margin: 0 auto; padding: 10px; left: 0px } 
 <div class='test'> <h1>Fixed positioning</h1> </div> 

finally , I found the answer. 终于,我找到了答案。

There is also a statement here : 还有一种说法在这里

  1. If the position property is fixed, the containing block is established by the viewport (in the case of continuous media) or the page area (in the case of paged media). 如果position属性是固定的,则包含块是由视口(对于连续媒体而言)或页面区域(对于分页媒体而言)建立的。

  2. If the position property is absolute or fixed, the containing block may also be formed by the edge of the padding box of the nearest ancestor element that has the following: 如果position属性是绝对的或固定的,则包含块也可以由最接近祖先元素的填充框的边缘形成,该边缘具有以下内容:

    A transform or perspective value other than none 转换或透视值,无

    A will-change value of transform or perspective 转换或透视的意愿变化值

    A filter value other than none or a will-change value of filter (only works on Firefox). 除none或filter-will-change值以外的过滤器值(仅适用于Firefox)。

    A contain value of paint (eg contain: paint;) 涂料的包含值(例如,包含:涂料;)

If you use position: fixed; 如果您使用position: fixed; you are defining the position of an element in the viewport. 您正在定义元素在视口中的位置。 If you use position: absolute; 如果使用position: absolute; then you are defining the position of an element in its parent's box. 那么您正在定义元素在其父对象框中的位置。 Also CSS prioritises code lower down, so for example in your code h1 is the most prioritised element (does not apply for classes and ID's, Classes take priority over elements and ID's take priority over classes). 同样,CSS优先级代码也降低了,因此,例如,在代码h1中,优先级最高的元素(不适用于类和ID,类优先于元素,ID优先于类)。

You need to use position: fixed for the document's body and use position: absolute for the elements with the parent which has position: relative , so change h1's position to absolute : 您需要将position: fixed用于文档的正文,并将position: absolute用于父元素具有position: relative的元素,因此请将h1的位置更改为absolute:

body {
  width: 500px;
  height: 1400px;
  margin: 0 auto;
}
.test{
  width: 500px;
  height: 132px;
  background-color: red;
  position: relative;
  left: 200px;
  transform: translate(0) /* added this, and works as expected */
}


h1 {
  position: absolute;   // 
  top: 0px;
  width: 500px;
  margin: 0 auto;
  padding: 10px;
  left: 0px
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM