首页 文章

在中心Bootstrap 3网格旁边创建固定侧边栏

提问于
浏览
67

我想创建一个固定的侧边栏 outside 我的中心Bootstrap网格 . 我在尝试这样做时面临的挑战是确定将哪些其他样式应用/覆盖到我的 .container ,以便在调整屏幕大小时它不会与我的侧边栏重叠 .

对于初学者,我只使用css框架的网格部分,所以 .container.row.col-md-12 是从 bootstrap.css 文件本身拉出的代码,不是自定义的 . 另外请记住我正在使用 Bootstrap 3 ,所以请不要为以前的线程中经常被问到的Bootstrap 2的流体网格解决方案提出建议 .

HTML

<div id="left-nav"></div>
<div class="container">
   <div class="row">
      <div class="col-md-12">
         <p>Lorem ipsum dolor sit amet, nunc dictum at.</p>
      </div>
   </div>
</div>

CSS

html, body {
  height: 100%;
  width: 100%;
}
#left-nav {
  background: #2b2b2b;
  position: absolute;
  top: 0px;
  left: 0px;
  height: 100%;
  width: 250px;
}

1 回答

  • 109

    As drew_w said,你可以找到good example here .

    HTML

    <div id="wrapper">
        <div id="sidebar-wrapper">
            <ul class="sidebar-nav">
                <li class="sidebar-brand"><a href="#">Home</a></li>
                <li><a href="#">Another link</a></li>
                <li><a href="#">Next link</a></li>
                <li><a href="#">Last link</a></li>
            </ul>
        </div>
        <div id="page-content-wrapper">
            <div class="page-content">
                <div class="container">
                    <div class="row">
                        <div class="col-md-12">
                            <!-- content of page -->
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    

    CSS

    #wrapper {
      padding-left: 250px;
      transition: all 0.4s ease 0s;
    }
    
    #sidebar-wrapper {
      margin-left: -250px;
      left: 250px;
      width: 250px;
      background: #CCC;
      position: fixed;
      height: 100%;
      overflow-y: auto;
      z-index: 1000;
      transition: all 0.4s ease 0s;
    }
    
    #page-content-wrapper {
      width: 100%;
    }
    
    .sidebar-nav {
      position: absolute;
      top: 0;
      width: 250px;
      list-style: none;
      margin: 0;
      padding: 0;
    }
    
    @media (max-width:767px) {
    
        #wrapper {
          padding-left: 0;
        }
    
        #sidebar-wrapper {
          left: 0;
        }
    
        #wrapper.active {
          position: relative;
          left: 250px;
        }
    
        #wrapper.active #sidebar-wrapper {
          left: 250px;
          width: 250px;
          transition: all 0.4s ease 0s;
        }
    
    }
    

    的jsfiddle

相关问题