html - 让 Bootstrap 4 导航栏侧边栏扩展到页面的全高

标签 html css asp.net-mvc bootstrap-4 navbar

我是第一次在 .NET MVC(非核心)入门应用程序中使用 Bootstrap 4。 Nuget 中的 Bootstrap 版本已更新至 4.1。

我需要在顶部有一个水平导航栏,在左侧有一个垂直导航栏(边栏)。

我一直在研究的例子: https://getbootstrap.com/docs/4.1/examples/dashboard/

我遇到的问题是侧边栏只能向下延伸到主页内容,我无法让它向下延伸到页面的 100%。我尝试在导航栏和侧边栏相关的 div 上手动设置高度,但没有效果。

_Layout.cshtml :

<body>
   <nav class="navbar navbar-dark fixed-top bg-dark flex-md-nowrap p-0 shadow">
    <a class="navbar-brand col-sm-3 col-md-2 mr-0" href="#">Company name</a>
    <ul class="navbar-nav px-3">
        <li class="nav-item text-nowrap">
            <a class="nav-link" href="#">Sign out</a>
        </li>
    </ul>
  </nav>
    <div class="container-fluid">
      <div class="row">
        <nav class="col-md-2 d-none d-md-block bg-light sidebar">
            <div class="sidebar-sticky">
                <ul class="nav flex-column">
                    <li class="nav-item">
                        <a class="nav-link active" href="#">
                            <span data-feather="home"></span>
                            Dashboard <span class="sr-only">(current)</span>
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#">
                            <span data-feather="file"></span>
                            Orders
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#">
                            <span data-feather="shopping-cart"></span>
                            Products
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#">
                            <span data-feather="users"></span>
                            Customers
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#">
                            <span data-feather="bar-chart-2"></span>
                            Reports
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#">
                            <span data-feather="layers"></span>
                            Integrations
                        </a>
                    </li>
                </ul>

                <h6 class="sidebar-heading d-flex justify-content-between align-items-center px-3 mt-4 mb-1 text-muted">
                    <span>Saved reports</span>
                    <a class="d-flex align-items-center text-muted" href="#">
                        <span data-feather="plus-circle"></span>
                    </a>
                </h6>                 
            </div>
        </nav>
        <main role="main" class="col-md-9 ml-sm-auto col-lg-10 px-4">      
                @RenderBody()               
        </main>
    </div>
  </div>
</body>

索引.cshtml:

<div class="jumbotron">
<h1>ASP.NET</h1>
<p class="lead">ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS and JavaScript.</p>
<p><a href="https://asp.net" class="btn btn-primary btn-lg">Learn more &raquo;</a></p>
</div>

<div class="row">
    <div class="col-md-4">
        <h2>Getting started</h2>
        <p>
            ASP.NET MVC gives you a powerful, patterns-based way to build dynamic websites that
            enables a clean separation of concerns and gives you full control over markup
            for enjoyable, agile development.
        </p>
        <p><a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkId=301865">Learn more &raquo;</a></p>
    </div>
    <div class="col-md-4">
        <h2>Get more libraries</h2>
        <p>NuGet is a free Visual Studio extension that makes it easy to add, remove, and update libraries and tools in Visual Studio projects.</p>
        <p><a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkId=301866">Learn more &raquo;</a></p>
    </div>
    <div class="col-md-4">
        <h2>Web Hosting</h2>
        <p>You can easily find a web hosting company that offers the right mix of features and price for your applications.</p>
        <p><a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkId=301867">Learn more &raquo;</a></p>
    </div>
</div>

外观(黄线代表我希望侧边栏向下延伸的大致位置):

enter image description here

最佳答案

这有点棘手... Bootstrap 使用额外的 CSS 来完成这个布局。它不适用于 Bootstrap。他们添加了一个类“.sidebar”:

.sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    z-index: 100;
    padding: 48px 0 0;
    box-shadow: inset -1px 0 0 rgba(0, 0, 0, .1);
}

这是一个代码笔示例: https://codepen.io/anon/pen/oRjmBN

我还添加了一个 'overflow-y: auto' 以防侧边栏中的内容变长。

希望对您有所帮助!

关于html - 让 Bootstrap 4 导航栏侧边栏扩展到页面的全高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56048910/

相关文章:

html - 在移动设备上使图像位于文本下方,而不是上方

html - 我有 2 个 UL,其中一个出现在悬停状态;但不能足够快地将鼠标移到它上面

css - 无法使用 CSS 限制图像大小

javascript - 如何添加事件监听器点击多个具有不同自定义数据键属性的div

css - 无论滚动位置如何,都突出显示虚拟滚动表中的行

asp.net-mvc - 如何在远程验证中使用多个 AdditionalFields - asp.net mvc

c# - MVC中访问ViewData的集中函数

asp.net-mvc - 服务器端使用 Edge.js 在 ASP.NET 中呈现 Angular 2

javascript - 为什么我的 HTML Canvas 不旋转?

html - 如何响应式地将文本放在图像的两侧?