【问题标题】:How to fix nav bar to bottom of the screen for mobile view>如何将导航栏固定到屏幕底部以进行移动视图>
【发布时间】:2016-03-26 16:15:24
【问题描述】:

当我在桌面上查看时,我目前有一个侧边栏作为导航栏。在桌面视图中,图标和文本与左侧的图标和右侧的文本并排显示。

我试图让它在折叠到移动视图时,侧边栏折叠到固定在屏幕底部的导航栏。图标在顶部并在文本下方居中。

这是我目前拥有的:http://codepen.io/anon/pen/bpRgEJ

这是我试图到达底部位置的代码:

@media (max-width:35em) {

    .main-nav {
        background: #ECC264;
        border-right: 1px solid #e5e5e5;
        position: fixed;
        margin: 0;
        left: 0;
        bottom: 0;
        height: 5em;
        width: 100%;
    }
}

它目前折叠成一个顶部导航栏,并且图标和文本是并排的,而不是顶部和底部。

任何帮助将不胜感激,谢谢!

【问题讨论】:

    标签: html css


    【解决方案1】:

    首先,我建议你按如下方式组织你的 CSS:

    1.- 具有影响一切的规则的通用选择器和类

    2.- 仅影响特定宽度的行为的媒体查询。

    因此,如果您需要一个始终固定的导航栏,但桌面位于屏幕左侧,而移动设备位于页面底部,您应该编写如下内容:

    .sidebar{ /* Every rule that will be always visible, something like colors, fonts, etc. */
      background-color: yellow; 
      position: fixed;
    }
    
    @media(min-width:35.1em){ /* Larger than mobile devices */
      .sidebar{
        left: 0;
        top: 0;
        bottom: 0;
        width: 40px;
      }
    }
    
    @media(max-width:35em){ /* Breakpoint only for mobile */
      .sidebar{
        bottom: 0;
        width: 100%;
      }
    }
    

    这样你就可以避免处理不匹配的规则。

    您的完整 CSS 应如下所示:

    .fa-2x {
        font-size: 2rem;
    }
    .fa {
        position: relative;
        text-align: center;
        vertical-align: middle;
        font-size: 2rem;
    }
    
    .main-nav {
        background: #ECC264;
        border-right: 1px solid #e5e5e5;
        position: fixed;
        z-index: 1000;
    }
    .main-nav>ul {
        margin: 0rem 0rem;
    }
    
    nav ul,
    nav li {
        outline: 0;
        margin: 0;
        padding: 0;
    }
    .main-nav li:hover>a,
    nav.main-nav li.active>a{
        color: #fff;
        background-color: #919191;
    }
    
    .main-nav .nav-text, .main-nav li>a{
      font-family: 'Roboto', sans-serif;
    }
    
    .main-nav li>a {
          border-collapse: collapse;
          border-spacing: 0;
          color: white;
          font-size: 1.03rem;
          text-decoration: none;
    }
    
    @media (min-width:35.1em) {
      .fa{
        display: table-cell;
        width: 5rem;
        height: 5rem;
      }
    
      .main-nav:hover,
    nav.main-nav.expanded {
        width: 15em;
        overflow: visible;
    }
    
      .main-nav{
        top: 0;
        bottom: 0;
        height: 100%;
        left: 0;
        width: 5rem;
        overflow: hidden;
        -webkit-transition: width .05s linear;
        transition: width .05s linear;
        -webkit-transform: translateZ(0) scale(1, 1);
      }
    
      .main-nav li {
        position: relative;
        display: block;
        width: 18rem;
      }
      .main-nav li>a {
          position: relative;
          display: table;
          -webkit-transform: translateZ(0) scale(1, 1);
          -webkit-transition: all .1s linear;
          transition: all .1s linear;
      }
    
      .main-nav .nav-text {
          position: relative;
          display: table-cell;
          vertical-align: middle;
          width: 10rem;
      }
    }
    
    
    @media (max-width:35em) {
    
        .main-nav {
            background: #ECC264;
            border-right: 1px solid #e5e5e5;
            position: fixed;
            margin: 0;
            left: 0;
            bottom: 0;
            /*height: 5em;*/
            width: 100%;
        }
    
      .main-nav ul{
        display: table;
        width: 100%; 
      }
    
        .main-nav li{
          float: left;
          width: 25%;
          list-style-type: none;
          text-align: center;
        }
    
      .main-nav a{
        display: block;
        padding: 5px;
        height: 74px;
      }
    
      .main-nav i, .main-nav span{
        display: block;
      }
    
      .main-nav span{
        font-size: 14px;
      }
    }
    

    您在 Codepen 中的示例:http://codepen.io/xWaZzo/pen/BKZpQE

    【讨论】:

      【解决方案2】:

      这应该可以解决问题。

       #myNavbar {
              position: relative;
          }
      
          #myNavbar .nav {
              position: absolute; 
              bottom: 0; 
              right: 0;
              margin-bottom: -10px;
          }
      

      http://jsfiddle.net/DTcHh/1819/

      【讨论】:

      • 嗨,该代码看起来是用于固定顶部导航的,这与我正在寻找的完全相反。
      • tutorialrepublic.com/…点击此链接
      【解决方案3】:

      你应该从

      中删除 top:0
      .main-nav {
      background: #ECC264;
      border-right: 1px solid #e5e5e5;
      position: fixed;
      / *top: 0;*/
      bottom: 0;
      height: 100%;
      left: 0;
      width: 5rem;
      overflow: hidden;
      -webkit-transition: width .05s linear;
      transition: width .05s linear;
      -webkit-transform: translateZ(0) scale(1, 1);
      }
      

      在媒体查询类中也增加高度

      @media (max-width:35em) {
      
      .main-nav {
      background: #ECC264;
      border-right: 1px solid #e5e5e5;
      position: relative;
      margin: 0;
      left: 0;
      bottom: 0;
      height: 20em;
      width: 100%;
      }
      }
      

      希望这是你想要的吗?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-10-15
        • 2020-03-06
        • 2017-09-13
        • 1970-01-01
        • 1970-01-01
        • 2019-01-25
        • 2013-01-29
        相关资源
        最近更新 更多