css - 在具有相对定位元素的页面上防止固定元素消失

标签 css css-position

我必须完全误解 position: fixed;

我希望页面底部的固定元素始终占据屏幕底部。它在 Chrome 48.0.2564.116 Mac 10.9.5 上按预期工作,即使调整窗口大小使得 window.document.body.clientWidth 小于相关元素的左侧位置,即 400px,小于 510px 。但是如果相对定位元素的左值大于屏幕宽度(或者如果它的宽度未设置为 0px(?!))。

<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <style>
    .positionedRelative {
      position:relative;
      top: 300px;
      width: 0px;
      left: 510px;
    }

    .justifiedBottom {
      padding: 5px;
      background-color: rgba(200,200,200,0.5);
      position: fixed;
      bottom: 0;
      left: 0;
      right: 0;
    }
  </style>

</head>
<body>
  <div class="positionedRelative">
    Hello
  </div>
  <div class="justifiedBottom">Next Page</div>
</body>
</html>

当 clientWidth 为 360 且 .positionedRelative 的左侧位置为 322px 时,将生成以下内容(注意固定元素被推离屏幕底部):

fixed element is being pushed off the bottom of the screen

任何正确方向的想法或指示将不胜感激。谢谢。

最佳答案

从外观上看,这与您的宽度有关。看看:https://jsfiddle.net/0Lqnfmad/3/

.positionedAbsolute { position:absolute; top: 300px; width: 100%; left: 0px; text-align: center; }
.justifiedBottom { padding: 15px; background-color: rgba(200,200,200,0.5); position: fixed; bottom: 0; left: 0; width: 100%; }

看看对你有没有帮助!

关于css - 在具有相对定位元素的页面上防止固定元素消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35760393/

相关文章:

html - 如何修复使用输入 :checked, 以防止单选按钮移动的 CSS/HTML?

jquery - JqG​​rid 在 jquery 对话框中显示为不可编辑的模式

html - 绝对定位元素和响应式设计

html - 如何让我的内容栏发挥作用?

html - 绝对位置不起作用

html - Electron :模态和自定义菜单栏的问题

html - CSS:无法让 :hover 工作,我错过了什么?

html - 父 div 上的 border-radius 打破了 child 的 perspective/translateZ

jquery - 使用 jquery css 在 textarea 关键字旁边定位搜索框

css - Bootstrap 4 Flexbox 侧边栏在使用位置 :fixed 时变薄