调整浏览器窗口大小后,为什么导航菜单会消失?

时间:2018-10-05 12:41:38

标签: html css twitter-bootstrap toggle navbar

HTML

  html {
    	height: 100%;
    }
    body {
    	    background-image: url("https://cdn-images-1.medium.com/max/2000/1*M6iNwOtplNTw1EOTcUklkQ.png");
    	    background-size: cover; 
    	    background-position: center;
    
    	     color: white !important;
      		font-family: 'Raleway', sans-serif;
      		font-weight: 700;
    }
    
    
    nav .navbar-nav li a {
      color: white !important;
      font-family: 'Raleway', sans-serif;
      font-weight: 700;
      }
    
     .navbar-brand {
      color: white !important;
      }
    
      #content {
    	text-align: center;
    	padding-top: 27%; 
    	text-shadow: 0px 4px 3px rgba(0,0,0,0.5);
    }
<!DOCTYPE html>
    <html>
    <head>
    	<title>James McGill</title>
    	<script
      src="https://code.jquery.com/jquery-3.3.1.min.js"
      integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
      crossorigin="anonymous"></script>
      <link rel="stylesheet" type="text/css" href="style.css">
      <link href="https://fonts.googleapis.com/css?family=Raleway:400,700" rel="stylesheet">
    </head>
    
    <body>
    
    <nav class="navbar navbar-expand-md sticky-top">
      <div class="container-fluid">
        <a class="navbar-brand" href="#">J.M</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarResponsive">
          <ul class="navbar-nav ml-auto">
            <li class="nav-item">
              <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#">About me</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#">Portfolio</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#">Contact me</a>
            </li>
          </ul>
        </div>
      </div>
    </nav>
    
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
    
    </body>
    </html>


  

我似乎无法弄清楚为什么发生这种情况。 请看一下我的代码,并给我一些有关如何使其工作的建议。

我已经添加了用于切换的按钮和图标,正如您在HTML代码中看到的那样,但是它并未显示在浏览器中。

我在这里做错了什么?让我知道。

谢谢。

2 个答案:

答案 0 :(得分:0)

这是因为您的div中使用了引导类navbarnavbar-collapse:在这里: <div class="collapse navbar-collapse" id="navbarResponsive">

这些是引导程序类,因此当您按原样使用它们时:当屏幕低于768px时它们会折叠:

@media (min-width: 768px)

导航栏css的说明位于Bootstrap site

答案 1 :(得分:0)

一个简单的解决方法是将 navbar-light bg-light navbar-dark bg-dark 添加到 nav 中标签。
为什么?
当您查看navbar.scss文件并向下滚动到 .navbar-light 时,您会发现嵌套在其中的是一个 .navbar-toggler-icon 导航栏折叠时的汉堡状图像。
当然,您可以使用自己的CSS对此进行自定义。我将引导您参阅引导文档以完成此操作。
navbar color-schemes
theming

  html {
    	height: 100%;
    }
    body {
    	    background-image: url("https://cdn-images-1.medium.com/max/2000/1*M6iNwOtplNTw1EOTcUklkQ.png");
    	    background-size: cover; 
    	    background-position: center;
    
    	     color: white !important;
      		font-family: 'Raleway', sans-serif;
      		font-weight: 700;
    }
    
    
    nav .navbar-nav li a {
      color: white !important;
      font-family: 'Raleway', sans-serif;
      font-weight: 700;
      }
    
     .navbar-brand {
      color: white !important;
      }
    
      #content {
    	text-align: center;
    	padding-top: 27%; 
    	text-shadow: 0px 4px 3px rgba(0,0,0,0.5);
    }
<!DOCTYPE html>
<html>
<head>
	<title>James McGill</title>
	<script
  src="https://code.jquery.com/jquery-3.3.1.min.js"
  integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
  crossorigin="anonymous"></script>
  <link rel="stylesheet" type="text/css" href="style.css">
  <link href="https://fonts.googleapis.com/css?family=Raleway:400,700" rel="stylesheet">
</head>

<body>

<nav class="navbar navbar-dark bg-dark navbar-expand-md sticky-top">
  <div class="container-fluid">
    <a class="navbar-brand" href="#">J.M</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarResponsive">
      <ul class="navbar-nav ml-auto">
        <li class="nav-item">
          <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">About me</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Portfolio</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Contact me</a>
        </li>
      </ul>
    </div>
  </div>
</nav>

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

</body>
</html>