【问题标题】:Fixed position on top顶部固定位置
【发布时间】:2014-06-09 22:08:20
【问题描述】:

我尝试在页面内容之上创建一个半透明的 div。这个 div 应该是窗口宽度和高度的 100%。

如果我将透明 div 放在它不会出现的内容之后。我不理解为什么?此 div 定位固定,根据定义固定位置“元素相对于浏览器窗口定位”

这可以通过将 .trans 放在 html 中的首位来解决,但这是一个非常复杂页面的简化案例。另一方面,我想了解这个问题。

那么为什么要先看到固定的 div?

来玩吧:http://jsfiddle.net/26dPg/

CSS:

body { margin:0; }

.trans{
    position:fixed;
    width:100%; height:100%;
    margin:0;
    opacity:0.7; filter: alpha(opacity = 70);
    background-color:red;
    cursor:pointer;
    z-index:5;
}

#content {
    position:relative;
    margin:30px auto;
    width:95%; max-width:890px; height:1200px;
    z-index:1;
}

HTML:

<div id="content"></div>
<div class="trans"></div> /*if this div goes after #content, this cannot be seen*/

【问题讨论】:

  • 您可能会看到类似这样的内容,因为您没有指定 top 属性:stackoverflow.com/questions/8712047/…
  • 是的,设置top: 0; 可以解决这个问题。 DEMO HERE
  • 是的,top:0 解决了这个问题。谢谢你。但我不明白为什么?你能解释一下吗?如果固定是尊重窗口,我把 100%,为什么需要顶部?

标签: css css-position


【解决方案1】:

在你的 .trans 类中,只需添加:

top:0;

【讨论】:

  • 是的,top:0 解决了这个问题。谢谢你。但我不明白为什么?你能解释一下吗?没有那个顶部:0; .trans 在哪里?
  • 看来您必须为固定位置(顶部、底部、左侧或右侧)提供一个属性。
  • 现在我明白了,但我不明白为什么。如果我说固定的 div 应该是 100% 并且根据定义固定位置是相对于窗口的,为什么还要说什么?
【解决方案2】:

只需将#content 的位置更改为absolute

  body { margin:0; }

    .trans{
        position:fixed;
        width:100%; height:100%;
        margin:0;
        opacity:0.7; filter: alpha(opacity = 70);
        background-color:red;
        cursor:pointer;
        z-index:5;
    }

    #content {
        position:absolute;
        margin:30px auto;
        width:95%; max-width:890px; height:1200px;
        z-index:1;
        background-color:blue;
        cursor:pointer;
    }

【讨论】:

  • absolute 仅在页面小于视口且用户不滚动时才有效。
  • 这不是解决方案...是的,添加top: 0; 将解决此问题。正如您现在所看到的,因为第一条评论链接到一个类似的问题,而第二条评论显示了一个工作示例。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 2011-11-24
  • 2013-12-04
  • 2019-08-28
相关资源
最近更新 更多