简体   繁体   English

在引导网格中创建一个粘性div

[英]Make a sticky div in a bootstrap grid

I'm looking for a way to make a div sticky to the bottom, like a footer, but inside a bootstrap grid, so not over the whole width. 我正在寻找一种方法来使div像页脚一样粘在底部,但是要放在自举网格内,因此不能覆盖整个宽度。

Bootstrap has an example of a sticky footer , I tried to tweak it to make it work in my case but couldn't. Bootstrap有一个粘性页脚的示例 ,我尝试对其进行调整以使其在我的情况下有效,但不能。

This is another approach, but also assumes your sticky div is not nested inside another div. 是另一种方法,但是还假设您的粘性div不嵌套在另一个div中。

Below is my code, I need to make .app-footer sticky to the bottom over the width of it's parent div. 下面是我的代码,我需要在父div的整个宽度.app-footer粘贴到底部。

<div class="row main">
  <div class="col-xs-2 col-md-4 col-lg-5">...</div>

  <div class="col-xs-8 col-md-4 col-lg-2">
    <div class="app"></div>
    <div class="app-footer"></div>
  </div>

  <div class="col-xs-2 col-md-4 col-lg-5">...</div>
</div>

Edit: the CSS I've tried but doesn't work: 编辑:我尝试过但不起作用的CSS:

html,
body {
  height: 100%;
}

/* Wrapper for page content to push down footer */
.app {
  min-height: 100%;
  height: auto;
  /* Negative indent footer by its height */
  margin: 0 auto -60px;
  /* Pad bottom by footer height */
  padding: 0 0 60px;
}

/* Set the fixed height of the footer here */
.app-footer {
  height: 60px;
  background-color: #f5f5f5;
}

Edit: Ok, got it. 编辑:好的,知道了。 The solution seems to be to add {position: relative; 解决方法似乎是添加{position:relative; height: 100%} to ALL parent divs. 高度:100%}分配给所有父级div。 In my case I had the grid wrapped in <div class="row"> , when I added it there too it worked. 以我为例,将网格包裹在<div class="row"> ,当我将其添加到那里时,它也可以工作。

if you want to make the nested child div positioned to the bottom of the parent div: 如果要将嵌套的子div放置在父div的底部,请执行以下操作:

add {position:relative} to the css of the the parent div; 将{position:relative}添加到父div的CSS; add {position:absolute;bottom:0} to the css of the the child div; 将{position:absolute; bottom:0}添加到子div的CSS中;

the nested div should be the same width as the parent (taknig into account the padding and margins), but you could force it to be by adding width:100% to the child div's css 嵌套div的宽度应与父div的宽度相同(将填充和边距考虑在内),但是您可以通过在子div的css中添加width:100%来强制使其为

You could try this in your CSS: 您可以在CSS中尝试:

.app-footer{
  position: absoute;
  bottom: 0px
  width: 100%;
  height: 80px;
  background-color: red;
}

Use this css to make your app-footer div sticky: 使用此CSS使您的应用程序页脚div保持粘性:

.app-footer {
  height: 60px;
  background-color: #f5f5f5;
  position: fixed;
  bottom:0px;
  left:0px;
}

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

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