粘性页脚导航栏和居中的登录面板

时间:2016-02-19 11:55:16

标签: html css twitter-bootstrap

以下每个组件已经多次询问过这个问题:

  • 如何保持导航栏固定
  • 如何保持页脚固定
  • 如何将div置于页面中间

我想要实现的是同时拥有所有这些

我想将一个登录面板置于页面中间(水平和垂直),但我的页脚有一些问题

  • 我想在移动电话上正确呈现页面。登录区域绝不应位于页眉或页脚的下方。
  • 显示页面时,我希望显示页脚。目前我需要向下滚动才能看到页脚。登录表单和页脚之间的空间太大。页脚需要保持固定在底部。
  • 如果可能,登录表格应该更宽更高

感谢您的帮助

这是我需要弄清楚如何配置

的关键部分
/* Wrapper for page content to push down footer */
#wrap {
  min-height: 100%;
  height: 100%;
  /* Negative indent footer by its height */
  margin: 140px 0 -200px 0;
  /* Pad bottom by footer height */
  padding: 0;
}

请参阅jfiddle here以获取您的信息

目前

___________________________________________________     _ Visible Page 
|Navbar                                           |      |
|                                                 |      |
|_________________________________________________|      |
|                                                 |      |
|                                                 |      |
|                                                 |      |
|                      ^                          |      |
|                      |  Too much space          |      |
|                      |                          |      |
|                                                 |      |
|                _______________                  |      |    
|               |               |                 |      |
|               |  Login Area   |                 |      |  
|               |_______________|                 |      |
|                                                 |      |
|                                                 |      |
|                                                 |      |
|                                                 |      |
|                      ^                          |     _| 
|                      |  Too much space          |
|                      |                          |
|                                                 |
|                                                 |
|                                                 |
|                                                 |
|                                                 |
|_________________________________________________|
| Footer                                          |
|_________________________________________________|

我想要什么

___________________________________________________     _ Visible Page 
|Navbar                                           |      |
|                                                 |      |
|_________________________________________________|      |
|                                                 |      |
|                                                 |      |
|                                                 |      |
|                                                 |      |
|              _____________________              |      |    
|             |                     |             |      |
|             |                     |             |      |
|             |    Login Area       |             |      |  
|             |                     |             |      |
|             |_____________________|             |      |
|                                                 |      |
|                                                 |      |
|                                                 |      |
|                                                 |      |
|_________________________________________________|      |
| Footer                                          |      |
|_________________________________________________|     _|

2 个答案:

答案 0 :(得分:2)

这就是你如何做到的。查看有效的bootply示例here

/* Sticky footer styles
-------------------------------------------------- */

html,
body {
  height: 100%;
  /* The html and body elements cannot have any padding or margin. */
}

/* Wrapper for page content to push down footer */
#wrap {
  min-height: 100%;
  height: auto !important;
  height: 100%;
  /* 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 */
#footer {
  height: 60px;
  background-color: #f5f5f5;
}


/* Custom page CSS
-------------------------------------------------- */


#wrap > .container {
  padding: 60px 15px 0;
}
.container .credit {
  margin: 20px 0;
}

#footer > .container {
  padding-left: 15px;
  padding-right: 15px;
}

#footer {
	position: fixed;
    width: 100%;
    display: block;
    bottom: 0;
}

.center{
    position: absolute;
    height: 50px;
    width: 50px;
    background:red;
    top:calc(50% - 50px/2); /* height divided by 2*/
    left:calc(50% - 50px/2); /* width divided by 2*/
}
<!-- Wrap all page content here -->
<div id="wrap">
  
  <!-- Fixed navbar -->
  <div class="navbar navbar-default navbar-fixed-top">
    <div class="container">
      <div class="navbar-header">
        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
        </button>
        <a class="navbar-brand" href="#">Project name</a>
      </div>
      <div class="collapse navbar-collapse">
        <ul class="nav navbar-nav">
          <li class="active"><a href="#">Home</a></li>
          <li><a href="#about">About</a></li>
        </ul>
      </div><!--/.nav-collapse -->
    </div>
  </div>
  
  <!-- Begin page content -->
  <div class="container">
    <div class="center">
    	login goes here
    </div>
  </div>
</div>

<div id="footer">
  <div class="container">
    <p class="text-muted credit">Centered login footer.</p>
  </div>
</div>

答案 1 :(得分:1)

基于CSS3 calc和视口相关单元的有趣方法:vhvw

&#13;
&#13;
/* In the below @media queries I set body's background to red, in real application appropriate steps should be taken to fit the key elements of webpage and exclude all others */
/* Minimum width we need is 300px (width of login form) */
@media all and (max-width: 300px){
  body {
    background-color: red;
  }
}
/* Minimum height we need is 140px (height of login form plus heights of nav and footer) */
@media all and (max-height: 140px){
  body {
    background-color: red;
  }
}
html, body {
  height: 100%;
}
body {
  margin: 0;
}
nav {
  background-color: lightgreen;
  height: 20px;
}
main {
  background-color: lightblue;
  height: 100px;
  /*
  Margin from top and bottom:
    half of viewport's height minus
    half of login form's height minus
    half of sum of nav's and footer's heights
  Margin from left and right:
    half of viewport's width minus
    half of login form's width
  */
  margin: calc(50vh - 50px - 20px) calc(50vw - 150px);
  text-align: center;
  width: 300px;
}
footer {
  background-color: lightgrey;
  bottom: 0;
  height: 20px;
  position: absolute;
  width: 100%;
}
&#13;
<nav>
  <a href="#">Link 1</a>
  <a href="#">Link 2</a>
</nav>
<main>
  Login form<br/>
  Login form<br/>
  Login form<br/>
  Login form<br/>
  Login form
</main>
<footer>Footer</footer>
&#13;
&#13;
&#13;

<强> View in codepen.com

支持相当不错:

    IE9 +,Edge,Firefox 16+(4+带前缀),Chrome 26+(19+带前缀),Opera 15 +,Safari 6.1+(6+带前缀)支持
  • calc({ {3}},caniuse.com)。
  • IE9 +,Edge,Firefox 19 +,Chrome 20 +,Opera 15 +,Safari 6+(MDNcaniuse.com)支持
  • vhvw

calc s&#39;的图解说明值:

MDN

<小时/> 这种方法不是最优雅的方法,但它可能会让你满意。

相关问题