【问题标题】:Container vertical align center considering header and footer考虑页眉和页脚的容器垂直对齐中心
【发布时间】:2018-02-13 11:58:25
【问题描述】:

我有代码:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
  <style>
    html, body {
      height: 100%;
 
    }
    
    .container {
        min-height: 200px;
        max-height: 500px;
        display: -ms-flexbox;
          display: flex;
          -ms-flex-pack: justify;
          justify-content: space-between;
          position: absolute;
          top: 0;
          left: 0;
          right: 0;
          bottom: 0;
          box-sizing: border-box;
          max-width: 1440px;
          margin: auto;
        
          height: 100%;
    }
    header, footer {
      height: 150px;
      background: #ccc;
    }
    
    footer {
      position: absolute;
      width: 100%;
      height: 50px;
      bottom: 0;
    }
  </style>
</head>
<body>
  <header></header>
  <div class="container">
    content blah blah blah
  </div>
  <footer>
    fsdfsdfsdfsdfsdffdsadsfasd
  </footer>
</body>
</html>

考虑到页眉高度和页脚(位置:绝对和高度),我如何垂直对齐该容器的中心。我想,我们可以用 display: flex 做什么?

我使用 Bootstrap 3 网格

示例jsbin:http://jsbin.com/timatozuco/edit?html,output

【问题讨论】:

    标签: css alignment


    【解决方案1】:

    这里试试..根据你的调整配置更新的代码..

    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width">
      <title>JS Bin</title>
      <style>
        html, body {
          height: 100%;
    
        }
    
        .container {
    
              display: -ms-flexbox;
              display: flex;
    
              top: 10%;
              left: 50%;
              position: absolute;
    
              box-sizing: border-box;
    
    
    
    
        }
        header, footer {
          height: 150px;
          background: #ccc;
        }
    
        footer {
          position: absolute;
          width: 100%;
          height: 50px;
          bottom: 0;
        }
      </style>
    </head>
    <body>
      <header></header>
      <div class="container">
        content blah blah blah
      </div>
      <footer>
        fsdfsdfsdfsdfsdffdsadsfasd
      </footer>
    </body>
    </html>
    

    【讨论】:

      【解决方案2】:

      如果我正确理解你的问题,那么我猜你想根据页眉和页脚大小将整个容器放在页面中心。

      所以下面的代码是解决方案

      <!DOCTYPE html>
      <html>
          <head>
              <meta charset="utf-8">
              <meta name="viewport" content="width=device-width">
              <title>JS Bin</title>
              <style>
                  html, body {
                      width: 100%;
                      height: 100%;
                      margin: 0;
                      padding: 0;
                  }
          
                  body {
                      font-family: arial, sans-serif;
                      font-size: 14px;
                      color: #000;
                      line-height: 22px;
                      text-decoration: none;
                  }
          
                  .wrapper {
                      width: 100%;
                      background: #fff;
                      margin: -128px auto 0;
                      height: 100%;
                      text-align: left;
                      clear: both;
                      display: table;
                      position: relative;
                      z-index: 1;
                  }
                  
                  header {
                      width: 100%;
                      margin: auto;
                      background: #ccc;
                      height: 66px;
                      position: relative;
                      z-index: 2;
                      border-bottom: 20px solid #fff;
                  }
                  
                  footer {
                      background: #ccc;
                      width: 100%;
                      margin: auto;
                      height: 22px;
                      clear: both;
                      border-top: 20px solid #fff;
                      position: relative;
                      z-index: 2;
                  }
                  
                  .container {
                      vertical-align: middle;
                      display: table-cell;
                      padding: 128px 0 0;
                  }
                  
                  .container .content {
                      text-align: center;
                      background: yellow;
                      padding: 0 10px;
                      width: 300px;
                      height: 200px;
                      margin: 0 auto;
                  }
              </style>
          </head>
          
          <body>
              <header>
                  Header content
              </header>
              <div class="wrapper">
                  <div class="container">
                      <div class="content">
                          Container content
                      </div>
                  </div>
              </div>
              <footer>
                  Footer content
              </footer>
          </body>
      </html>

      或者您也可以访问此链接:Codepen - centered container based on header & footer

      【讨论】:

      • 但是如果我使用 bootstrap 3,我该如何实现这段代码?
      • 好吧,现在这是一个完全不同的问题,您之前没有提到过,但是如果您使用 Bootstrap,那么这可能会对您有所帮助。 stackoverflow.com/questions/15641142/…
      • @DronaxD 现在您在问题中添加了 Bootstrap 3 :)
      猜你喜欢
      • 2016-01-17
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-12
      相关资源
      最近更新 更多