【问题标题】:How to position child element fixed in Vue?如何在Vue中定位固定的子元素?
【发布时间】:2021-02-16 15:19:51
【问题描述】:

我无法弄清楚为什么一个元素不会相对于它在 Vue 中的父元素保持固定。它仅与#app 相关。如何让子元素成为 Vue 中其父元素的 100% 且位置固定。

HTML:

 <div id="app">
   <div class="parent_relative">
     <div class="child_fixed"></div>
   </div>
 </div>

CSS:

.parent_relative{
    position: relative;
    width: 500px;
    height: 20px;
    border: 1px solid green;  
}
.child_fixed{
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 20px;
    background-color: blue; 
}

下图是它的样子:

搜索栏周围有一个固定位置的 div。它不应该在浏览器的顶部。它应该在您在其下方看到的深色 div 内。

【问题讨论】:

  • child_fixed的css中删除topbottom
  • position: absolute 有什么问题?
  • 去掉顶部和底部对您有帮助吗?

标签: css vue.js position fixed


【解决方案1】:

带有position: fixed; 的元素相对于视口,而不是父元素。也许,您正在搜索的是 position: absolute;,它与它的父级相关。

【讨论】:

  • 好吧,我正在尝试将搜索栏固定在该父 div 中。但是,绝对的位置不会允许这种情况发生。您还有其他想法如何实现吗?
  • 一种解决方案是使用position: fixed;.child_fixed 添加一个子元素,并将.child_fixed 更改为position: absolute;。看到这个fiddle,让我知道它有任何帮助。
猜你喜欢
  • 2015-05-27
  • 1970-01-01
  • 2014-09-07
  • 1970-01-01
  • 1970-01-01
  • 2019-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多