Bootstrap 4:带有徽标和2行的导航栏

nwo49xxi  于 2023-01-10  发布在  Bootstrap
关注(0)|答案(2)|浏览(238)

我尝试使用Bootstrap 4创建一个符合下面示意图的网站标题:

我用来实现这一点的代码如下(多余的代码省略):

<div class="navbar">
  <div class="container yellow">
    <div class="row">
      <div class="col-sm-4 green">Logo</div>
      <div class="col-sm-8 green">
        <div class="row">
          <div class="col text-right red">
            (00) 1234 5678
          </div>
        </div>
        <div class="row">
          <div class="col text-right red">
            <!-- nav links here -->
            <a href="#">link</a>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

唯一的自定义CSS是更改背景颜色以匹配原理图的颜色。
这将生成以下呈现的HTML页,其中的行不跨越容器:

但是,在Bootstrap 3中,这不是问题,只需将框架更改为Bootstrap 3即可给出正确的布局:

此外,如果从其父导航栏中删除容器(及其嵌套内容),则可以在Bootstrap 4中实现正确的布局:

我需要在Bootstrap 4中做些什么来实现这个想要的布局,同时仍然使用navbar类?

91zkwejq

91zkwejq1#

Bootstrap 4网格rowcol-*不是设计用于导航栏内部的。以下是支持的内容。现在Bootstrap 4使用了flexbox,不使用网格rowcol-*align Navbar content就容易多了。
如果您想要一个带有徽标和2行导航栏,请参阅此答案:
https://stackoverflow.com/questions/42635126/bootstrap-4-navbar-with-2-rows

<nav class="navbar navbar-expand navbar-dark fixed-top bg-dark">
    <div class="container">
        <h1 class="mb-0"><a href="#">Logo</a></h1>
        <div class="d-flex flex-column flex-wrap" id="navbarCollapse">
            <span class="navbar-text ml-auto py-0 px-lg-2">(00) 1234 5678</span>
            <ul class="navbar-nav mb-auto mt-0 ml-auto">
                <li class="nav-item active">
                    <a class="nav-link py-0" href="#">Home</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link py-0" href="#">Product</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link py-0" href="#">Company</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link py-0" href="#">Blog</a>
                </li>
            </ul>
        </div>
    </div>
</nav>
    • 示例:**

2行基本导航栏:https://codeply.com/go/ilJBKjJsEy
2行响应式导航栏:https://codeply.com/go/DsfePuoZy0
第二个例子在小屏幕上折叠成一个移动的堆叠菜单,可以使用汉堡包图标切换。使用flexbox and spacing utility classes来根据需要定位元素。有 * 许多 * 方法来实现你想要的:https://www.codeply.com/go/pGE8fTf9dM

yizd12fk

yizd12fk2#

您可以使用2个navbar-nav元素,然后使用flex-column类将它们堆叠起来,如下所示:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy" crossorigin="anonymous">

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/js/bootstrap.min.js" integrity="sha384-a5N7Y/aK3qNeh15eJKGWxsqtnX/wWdSZSKp+81YjTmS15nvnvxKHuzaWwXHDli+4" crossorigin="anonymous"></script>

<nav class="navbar navbar-expand-lg navbar-light bg-light">
    <div class="container">
        <a class="navbar-brand" href="#">
            <img src="https://picsum.photos/140/70" width="140" height="70" alt="Logo">
       </a>

        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
           <span class="navbar-toggler-icon"></span>
       </button>

        <div class="collapse navbar-collapse flex-column align-items-end" id="navbarCollapse">
            <!-- navbar1 -->
            <div class="navbar-nav mb-lg-0">
                <a class="nav-item nav-link" href="#">(00) 1234567</a>
            </div>

            <!-- navbar2 -->
            <ul class="navbar-nav mt-0">
                <li class="nav-item active">
                    <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">About</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">Features</a>
                </li>
                <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Products</a>
                    <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                        <a class="dropdown-item" href="#">Action</a>
                        <a class="dropdown-item" href="#">Another action</a>
                        <div class="dropdown-divider"></div>
                        <a class="dropdown-item" href="#">Something else here</a>
                    </div>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">Free Cookies</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">Contact</a>
                </li>
            </ul>
        </div>
    </div>
</nav>

相关问题