简体   繁体   English

CSS位置:固定不起作用?

[英]CSS position:fixed is not working?

I used below code to position this element <div class="col-scroll-fixed"></div> on the right side of the screen. 我使用下面的代码将此元素<div class="col-scroll-fixed"></div>放置在屏幕的右侧。 but it is not getting fixed to the screen. 但它并没有固定在屏幕上。 I even tried adding !important to each CSS property but couldn't get the result what I wanted. 我什至尝试将!important添加到每个CSS属性,但无法获得所需的结果。

.col-scroll-fixed{
    width: 310px;
    position: fixed;
    top:0;
    right: 30px;
    bottom: 0;
   z-index: 9999;
}

So, I want to know relative to what `position:fixed` will work. 因此,我想知道相对于什么“ position:fixed”将起作用。 Is there anyone to help me to resolve this issue. 有没有人可以帮助我解决此问题。

Note: Checked in chrome and firefox, Device: OSX 注意:已在Chrome和Firefox中签入,设备:OSX

 #outer { transform: scale(1.5); width: 100px; height: 100px; border: 1px solid blue; } #inner { position: fixed; top:0; right:0; width: 50px; height: 50px; border: 1px solid red; } 
 <div id="outer"> <div id="inner">Inner</div> </div> 

Maybe it's because you set both the bottom and top properties to 0. That's not possible for the browser to follow. 可能是因为您将bottom和top属性都设置为0。浏览器无法跟随。 Either top or bottom must be set, but not both. 必须设置顶部或底部,但不能同时设置。

I've also noticed that position: fixed; 我也注意到这个position: fixed; does not always work when the element is inside another position: fixed; 当元素在另一个position: fixed;时并不总是起作用position: fixed; or position: absolute; position: absolute; element, at least in Chrome. 元素,至少在Chrome中。 So you should check the element's parents and make sure they are all relatively positioned. 因此,您应该检查元素的父级,并确保它们都处于相对位置。

Unfortunately, although I can reproduce this in a project I'm working on, I couldn't reproduce it in a snippet. 不幸的是,尽管我可以在我正在研究的项目中重现它,但是我无法在摘要中重现它。

You'll also want to make sure that it doesn't have float applied to it or any flexbox parents, as these both also seem to override the fixed positioning behavior. 您还需要确保没有对其应用float或任何flexbox父级,因为它们似乎都覆盖了固定的定位行为。

Also, remove any transform rules on it, or it's parents, as this seems to cause an issue in Chrome, Firefox, Opera, and Edge. 另外,删除它或其父对象上的所有transform规则,因为这似乎在Chrome,Firefox,Opera和Edge中引起问题。 The attached snippet demonstrates this issue. 随附的片段演示了此问题。

Edit: Turns out this is intended behavior per the specs, but definitely will throw you off if you don't know about it. 编辑:事实证明,这是规范的预期行为,但是如果您不了解,肯定会把您甩开。 Really the important thing here is that it's likely that one of the element's parents forms a new stacking context, which causes it align to its parent instead of the window. 真正重要的是,元素的父元素之一可能会形成新的堆叠上下文,这会导致该元素与其父元素(而不是窗口)对齐。

I logged this as an issue for Chrome , Firefox , Opera, and Edge (it works fine in IE). 我将此问题记录为ChromeFirefox ,Opera和Edge的问题 (在IE中工作正常)。

An element with position: fixed; position: fixed;的元素position: fixed; is positioned relative to the screen's viewport , which means it always stays in the same place even if the page is scrolled. 相对于屏幕的视口定位 ,这意味着即使滚动页面,它也始终位于同一位置。 The top, right, bottom, and left properties are used to position the element. top,right,bottom和left属性用于定位元素。 When printing, the element will appear on every page. 打印时,该元素将出现在每一页上。

Here's your code, I added background-color and height so we can see it. 这是您的代码,我添加了背景颜色和高度,以便我们可以看到它。 it is where it was expected to be. 这是预期的地方。

 .col-scroll-fixed { width: 310px; height: 30px; background-color: purple; position: fixed; top:0; right: 30px; bottom: 0; z-index: 9999; } 
 <div class="col-scroll-fixed"></div> 

On using 使用时

position: fixed

The element is positioned relative to the browser window. 元素相对于浏览器窗口定位。 For an example: 例如:

.className{
    positon: fixed;
    top: 0;
    left: 0;
}

The above CSS will position the element to the top left most corner of the screen. 上面的CSS会将元素定位到屏幕的最左上角。

Here is a fiddle with your CSS example: 这是您的CSS示例的小玩意儿:

http://jsfiddle.net/244ju0ar/ http://jsfiddle.net/244ju0ar/

Works totally fine. 完全正常。 If you want "div" to be at the right most of the screen. 如果您希望“ div”出现在屏幕的最右边。 You'll need to use 您需要使用

right: 0;

Else, if there is some other issue, let me know. 否则,如果还有其他问题,请告诉我。

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

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