首页 \ 问答 \ 如何使用相对父位置将页脚绑定到页面底部?(How to bound a footer to the page bottom with relative parent position?)

如何使用相对父位置将页脚绑定到页面底部?(How to bound a footer to the page bottom with relative parent position?)

我正在使用bootstrap来创建页面布局。 我的页脚绑定到页面底部,如此处所述。 这很好用。

但是当我将一个页脚放在容器内部相对位置时,我无法将页脚绑定到页面底部。 这是一个例子

html,
body{
  height:100%;
  margin:0;
  padding:0;
}

.container{
  position:relative;
  min-height:100%;
  height:auto !important;
  height:100%;
}

header{
  height:120px;
  background-color:#eee; 
}

.middle{
  background-color:#aaa;  
}

footer{
  height:120px;
  background-color:#888;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
</head>
<body>
  <div class="container">
    <header>
      Header
    </header>
    <div class="middle">
      Middle
    </div>
    <footer>
      Footer
    </footer>
  </div>
</body>
</html>

是否可以只用CSS做到这一点?


I'am using bootstrap to create a page layout. My footer bound to the page bottom as described here. This works fine.

But when I'am place a footer inside of container with relative position, I can't to bound footer to the page bottom. Here is example

html,
body{
  height:100%;
  margin:0;
  padding:0;
}

.container{
  position:relative;
  min-height:100%;
  height:auto !important;
  height:100%;
}

header{
  height:120px;
  background-color:#eee; 
}

.middle{
  background-color:#aaa;  
}

footer{
  height:120px;
  background-color:#888;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
</head>
<body>
  <div class="container">
    <header>
      Header
    </header>
    <div class="middle">
      Middle
    </div>
    <footer>
      Footer
    </footer>
  </div>
</body>
</html>

Is it possible to do it with only CSS?


原文:https://stackoverflow.com/questions/40897085
更新时间:2023-04-30 19:04

最满意答案

好的,我知道你要做什么。 我以前做过这个,我发现了一个简单的方法。

您希望仍然在控制器中调用远程方法的按钮,该控制器在浏览器中执行一些javascript以刷新该DIV。 我知道,我知道,你不想拥有它,但我在这里。 我的页面上有一个,但我把它隐藏到一边,以便没有人能找到它,链接这个:

:id => "refresh_button", :style => "position: absolute; left: -9999px" // Rails ERB notation

然后,我将此添加到同一个文件中,我认为该文件是.html.erb文件。

<script type="text/javascript">
  function doSomething() {
    $('#refresh-button').click();
  }

  setInterval('doSomething()',2000);
</script>

这将每2秒按一次按钮,调用您的远程功能,就像有人每两秒点击一次该按钮一样。 我知道,这不是最干净的方式,但是它会很紧凑,直到你能为你找到更强大的东西。 虽然你可以在技术上使用它。

希望有所帮助。


I ended up putting code in application.js that would:

  • Check if the div exists
  • Make a timer using setInterval
  • Request a js script from Rails at the current URL which would lead to the evaluating of a js.erb view file. When this file was run it would update my div.

Behold...

assets/javascripts/application.js

$(function() {

  if ($('#scuttlebutt').length) {
    setInterval(function(){
      $.getScript(window.location.pathname, function(data, textStatus, jqxhr) {
        console.log('Load was performed.');
      });
    },2000);
  }

});

views/tickets/show.js.erb

$("#scuttlebutt").html(" <%= escape_javascript(render 'scuttlebutt') %>" );

相关问答

更多
  • 正如你所说的406错误表明你的后端无法用请求的格式(这里是HTML)进行响应。 一个可能的解决方案是在您的请求中指定json格式,如: 注意:id之后的.json app.factory('Post', function($resource) { return $resource("/posts/:id.json", { id: '@id' }, { 'destroy': { method: 'DELETE', params: {id: '@id', responseType: 'j ...
  • 简单的解决方案是在您的应用程序中创建一个rake任务,以查找您需要检查并更新它们的传真任务。 坚持它在一个cron工作和你的好事。 每次为您配置cron作业时都请检查一下。 我想建议一个很好的工作安排系统,但我一直在寻找使用Rails几年。 The easy solution is to create a rake task in your application that finds the fax tasks you need to check and then updates them. Stick ...
  • 尝试这个