如何在Bootstrap的col-lg-8容器内固定定位button按钮?
我想要实现下方截图展示的效果但未能成功设置,现将我编写的代码分享如下,请问该如何修复这个问题?

我的代码:
#mybtn{ position: fixed; bottom: 10px; right: 10px; }
<div class="row"> <div class="col-lg-8" style="position: relative;"> <p>this is my content</p> <button id="mybtn">My Fixed Button</button> </div> <div class="col-lg-4"> <p>content area</p> </div> </div>
问题原因
position: fixed 定位的参考系是浏览器视口,父元素设置的position: relative对其不生效,因此按钮会固定在整个窗口的右下角,无法局限在左侧.col-lg-8容器范围内。
修复方案
方案1:按钮随页面滚动,位于左侧容器右下角
如果不需要按钮钉死在视口,仅需要它始终在左侧内容区域的右下角,直接修改定位属性为absolute即可:
#mybtn{ position: absolute; bottom: 10px; right: 10px; }
方案2:按钮钉死在视口右下角,不超出左侧容器范围
如果需要按钮滚动时固定在视口右下角,但始终不进入右侧.col-lg-4区域,使用position: sticky实现,修改CSS如下即可:
#mybtn{ position: sticky; float: right; bottom: 10px; right: 10px; margin: 0; }
内容的提问来源于stack exchange,提问作者Berke Kurnaz
相关产品推荐
相关产品推荐

