减小导航栏折叠菜单的宽度

时间:2018-02-04 09:24:21

标签: html css twitter-bootstrap bootstrap-4

当屏幕分辨率为< 992px,菜单覆盖整个屏幕宽度。

Like so

但我正在尝试减小宽度并将其设置为25%,

.collapse{
    max-width : 25%;
}

我们如何实现这一目标?

以下是代码的JSFiddle

2 个答案:

答案 0 :(得分:0)

我添加了以下css以使下拉内容位于中心位置(最低要求,因此您可以根据需要进行响应)。并且在折叠项目开始之前简单地移动</nav>标记。

  .navbar-collapse
    {
        background-color:#FA0;
        width:200px;
        margin-left:10rem;
    }

.navbar {
  background-color: black !important;
}

.nav-link,
.navbar-brand {
  color: white !important;
}

.custom-toggler .navbar-toggler-icon {
  background-image: url("data:image/svg+xml;charset=utf8,%3Csvg viewBox='0 0 30 30' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath stroke='rgb(255,255,255)' stroke-width='2' stroke-linecap='round' stroke-miterlimit='10' d='M4 8h24M4 16h24M4 24h24'/%3E%3C/svg%3E");
}


  .navbar-collapse {
    background-color: #FA0;
    width: 200px;
    margin-left: 10rem;
  }
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" 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/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

<nav class="navbar navbar-expand-lg bg-light navbar-light">
  <button class="navbar-toggler navbar-toggler-rigth custom-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon brand-icon"></span>
  </button>
  <a class="navbar-brand ml-auto" href="#">
    <img class="brand-icon" src="resources/img/brandIcon.svg" width="30" height="30" alt="">
  </a>
  <a class="mr-auto navbar-brand" href="#">Navbar</a>
</nav>

<div class="collapse navbar-collapse custom-navbar-items" id="navbarSupportedContent">
  <ul class="navbar-nav mx-auto navbar-products">
    <li class="nav-item active">
      <a class="nav-link" href="#">Product One <span class="sr-only">(current)</span></a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Product Two<span class="sr-only">(current)</span></a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Product Three<span class="sr-only">(current)</span></a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Product Four<span class="sr-only">(current)</span></a>
    </li>
  </ul>
  <ul class="navbar-nav navbar-products">
    <li class="nav-item">
      <a class="nav-link" href="#">about us<span class="sr-only">(current)</span></a>
    </li>
  </ul>
</div>

答案 1 :(得分:0)

我想如果你写

.navbar.navbar-expand-lg {
     width:25%;
}

它会起作用。