【问题标题】:How can I get a vertical navbar to not take up space on smartphone when collapsed?如何让垂直导航栏在折叠时不占用智能手机空间?
【发布时间】:2019-11-23 16:00:49
【问题描述】:

当我有一个水平导航栏时,它在我的智能手机上显示为折叠,只占用切换图标的垂直空间。但是,当我尝试创建一个垂直导航栏时,我能得到的最好的结果就是让切换图标占据整个列。

我有一个现有网站,我正在将其转换为 bootstrap 4.3.1,以便它在智能手机上运行得更好。你可以在http://www.lionsclub.ca/ 看到我的实验,我在那里发布了最新版本的主页,以便我可以通过我的智能手机进行测试。我只是在复制新版本之前确保它没有完全损坏。

我希望导航栏位于左侧,主要内容位于右侧,因此导航栏和主要内容位于 .如果我不这样做,我无法让主要内容浮动到导航栏的右侧。

为了让导航栏在计算机屏幕上展开,我必须为导航元素指定类 navbar-expand-md,这似乎需要我为 ul 导航栏元素指定类 flex-column。当我这样做时,导航栏正确显示,折叠切换在大屏幕上不存在,但在智能手机上显示。如果我想看的话,我可以切换菜单。

但是,要让第二个 div(用于主要内容)显示在右侧,我必须指定 col-9 之类的内容作为其类(如果我变大,它不会浮动在右侧) . Flex-column 不起作用。

当我这样做时,事情的表现几乎是合理的。然而折叠的导航栏占据了整列,占用了大量的屏幕空间。有没有办法让它折叠到封闭左上角的浮动元素?

<div class="container">
  <div class="row">
    <nav class="navbar navbar-expand-md navbar-light bg-light">
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse" id="navbarSupportedContent">
        <ul class="navbar-nav flex-column">
<li class="nav-item">
      <a class="nav-link" href="index.html" title="West Hill - Highland Creek Lions Club home page">&nbsp;home&nbsp;</a>
    </li>
        </ul>
      </div>
    </nav>
    <div class="col-9">
    some content here
    </div>
  </div>
</div>

正如我所说,代码可以正常工作,但不符合我的要求。我真的希望折叠的导航栏占用的空间不超过切换图标。水平导航栏也存在同样的问题,但不那么令人反感,因为水平内容在滚动时会消失。垂直内容总是占用空间。

有没有人知道如何让这项工作比我想出的更好?

【问题讨论】:

    标签: bootstrap-4


    【解决方案1】:

    如何在&lt;div class='row'&gt; 中使用带有col-[xx]-[99] 的2 个div,它可以为您做到这一点...

    • 对于左侧导航,我们使用:&lt;div class='col-12 col-sm-12 col-md-3'&gt; ... &lt;/div&gt;
    • 对于正确的内容区域,我们使用:&lt;div class='col-12 col-sm-12 col-md-9'&gt; ... &lt;/div&gt;
    • 您不会在下面的 sn-p 中加载图像,因为您没有使用 HTTPS(您应该这样做)

    工作 sn-p 下面

    @media screen and (max-width:768px) {
      .bg-light {
        background-color: transparent !important;
      }
    }
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
    
    <script src="http://www.lionsclub.ca/scripts/email.js"></script>
    
    <link rel="stylesheet" href="http://www.lionsclub.ca/css/style.css" type="text/css">
    </link>
    
    <div class="banner">
      <img src="http://www.lionsclub.ca/images/LionBanner.png" />
    </div>
    <div class="container">
      <div class="row">
        <div class='col-12 col-sm-12 col-md-3'>
          <nav class="navbar navbar-expand-md navbar-light bg-light">
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                        <span class="navbar-toggler-icon"></span>
                    </button>
            <div class="collapse navbar-collapse" id="navbarSupportedContent">
              <ul class="navbar-nav flex-column">
                <li class="nav-item">
                  <a class="nav-link" href="index.html" title="West Hill - Highland Creek Lions Club home page">&nbsp;home&nbsp;</a>
                </li>
                <li class="nav-item">
                  <a class="nav-link" href="about.html" title="a history of the West Hill - Highland Creek Lions Club">&nbsp;about&nbsp;</a>
                </li>
                <li class="nav-item">
                  <a class="nav-link" href="activities.html" title="ongoing activities of the West Hill - Highland Creek Lions club">&nbsp;activities&nbsp;</a>
                </li>
                <li class="nav-item">
                  <a class="nav-link" href="contact.html" title="how to contact the West Hill - Highland Creek Lions Club">&nbsp;contact&nbsp;</a>
                </li>
                <li class="nav-item">
                  <a class="nav-link" href="events.html" title="upcoming events of the West Hill - Highland Creek Lions Club">&nbsp;events&nbsp;</a>
                </li>
                <li class="nav-item">
                  <a class="nav-link" href="join.html" title="how to join the West Hill - Highland Creek Lions Club">&nbsp;join&nbsp;</a>
                </li>
                <li class="nav-item">
                  <a class="nav-link" href="links.html" title="other community organizations">&nbsp;links&nbsp;</a>
                </li>
                <li class="nav-item">
                  <a class="nav-link" href="members.html" title="current members of the West Hill - Highland Creek Lions Club">&nbsp;members&nbsp;</a>
                </li>
                <li class="nav-item">
                  <a class="nav-link" href="news.html" title="latest news from the West Hill - Highland Creek Lions Club">&nbsp;news&nbsp;</a>
                </li>
                <li class="nav-item">
                  <a class="nav-link" href="partners.html" title="our community partners">&nbsp;Community
                                    Partners&nbsp;</a>
                </li>
                <li>&nbsp;</li>
                <li class="nav-item">
                  <a class="nav-link" href="http://www.a711lions.org/" title="Lions Club district web site">&nbsp;Lions&nbsp;District&nbsp;A-711&nbsp;</a><br />&nbsp;
                </li>
                <li class="nav-item">
                  <a class="nav-link" href="http://www.lionsclubs.org/" title="The Lions Clubs International"><img src="http://a711lions.ca/images/LCI_primary_sig_2color_web.jpg" /></a>
                </li>
                <li class="nav-item">
                  <a class="nav-link" href="http://www.dogguides.com/" title="The Lions Foundation of Canada"><img src="images/dogguides.gif" alt="Lions Foundation of Canada logo" /></a><br />&nbsp;
                </li>
                <li class="nav-item">
                  <a class="nav-link" href="http://www.lcif.org/" title="Lions Clubs International Foundation">
                    <img src="http://www.a711lions.ca/images/lcif_h3c.gif" alt="LCIF logo" />
                  </a>
                  <br />&nbsp;
                </li>
                <li class="nav-item">
                  <a class="nav-link" href="https://www.facebook.com/WestHillHighlandCreekLionsClub" title="follow us on Facebook">
                    <img id="followus" src="http://www.lionsclub.ca/images/facebook-icon.png" alt="follow us on Facebook" />
                  </a>
                </li>
              </ul>
            </div>
          </nav>
        </div>
        <div class="col-12 col-sm-12 col-md-9">
          <!-- start of inserted content -->
          <div class="hot">
    
            <a href="http://www.lionsclub.ca/rainbarrel.html" title="rain barrel sale">
              <img src="http://www.lionsclub.ca/images/rainbarrel.jpg" alt="rain barrel image" style="float: left;" />
              <h6>Lions rainbarrel sale</h6>
              <h6>Order from Stock!</h6>
            </a>
            <h4>New Stock!</h4>
            <p>&nbsp;</p>
    
          </div>
          Welcome to the West Hill - Highland Creek Lions Club's web site. It's fun to be a Lion! We hope you'll join us.</p>
          <p>The Club was chartered on November&nbsp;3,&nbsp;1944 and has been active in the community ever since. The club started the community's first newspaper and built the original <a href="https://www.toronto.ca/data/parks/prd/facilities/complex/2025/index.html">Heron Park
                            Community Centre</a>, which it later donated to the City.</p>
          <p>We normally meet on the 2<sup>nd</sup> and 4<sup>th</sup> Tuesdays of each month (except for July &amp; August when we don't meet, and June &amp; December when we skip the last meeting) at the <a href="https://www.toronto.ca/data/parks/prd/facilities/complex/2025/index.html">Heron Park
                            Community Centre</a> (<a href="http://maps.google.com/maps?f=q&hl=en&geocode=&q=292+Manse+Road,Toronto,+ON&sll=37.0625,-95.677068&sspn=114.541383,98.261719&ie=UTF8&z=16&iwloc=addr&om=1">292
                            Manse Road</a> South of Lawrence) at 7:00&nbsp;PM. If you're not a member and would like to be a guest, contact us at the e-mail address below.</p>
          <p><button onclick="initMailtoButton('secretary','');" style="border: 0px; margin:0px; padding: 0px;">Contact the club via e-mail</button>.<br />&nbsp;
          </p>
          <!-- end of inserted content -->
        </div>
      </div>
    </div>
    <div class="footer">Serving the community since 1944</div>
    <div class="full">
      <div class="copyright">copyright &copy; West Hill - Highland Creek Lions Club&nbsp;</div>
    </div>

    【讨论】:

      猜你喜欢
      • 2012-06-25
      • 1970-01-01
      • 2017-07-17
      • 2020-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多