响应式菜单在我的网站上无法正常工作

时间:2016-04-28 05:24:43

标签: html css

这是我的代码:

<meta name="viewport" content="width=device-width, initial-scale=1">

<link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
<link href="css/style_inventive.css" rel="stylesheet" media="screen">

其中style_inventive.css是我用于菜单的样式表

这是CSS文件内容

@media (min-width: 768px){
.navbar-right .dropdown-menu {

   right: auto; 

 }}

 .nav>li>a:hover
  {
    background-color: #336699;
    color:#fff;
   }
  .dropdown-menu
  {
    margin: 0px;
    padding-top :0px;
    padding-bottom: 0px;
    color: #fff;
  }
 .dropdown-menu>li>a:hover
  {
      background-color: #336699;
      color:#fff;
  }

  .dropdown-menu li
   {
     height: 40;
     color: #fff;
     float: none;
     display: block;
     margin: 0;
     text-align: left;
     border: 0;
     border-bottom: 1px solid #d5d5d5;
  }
  .dropdown-menu a
  {     
     margin-top: 10;    
  }

这是菜单的HTML代码

<div class="col-sm-7 menu">
        <div class="collapse navbar-collapse">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-responsive-collapse">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <ul class="nav navbar-nav navbar-right">        
                <li><a href="home_inventive.html">HOME</a></li>
                <li class="dropdown" id="licompany">
                    <a href="#" id="acompany" class="dropdown-toggle" data-toggle="dropdown">COMPANY </a>
                    <ul class="dropdown-menu" role="menu">
                        <li><a href="about_us.html">About Us</a></li>
                        <li><a href="team.html">Meet The Team</a></li>
                        <li><a href="join_us.html">Join Us</a></li>
                    </ul>
                </li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">SERVICE </a>
                    <ul class="dropdown-menu" role="menu">
                        <li><a href="mobile_app_development.html">Mobile App Development</a></li>
                        <li><a href="software_development.html">Software Development</a></li>
                        <li><a href="web_development.html"> Web development</a></li>

                    </ul>
                </li>                   
                <li><a href="product.html">PORTFOLIO</a></li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">TECHNOLOGY </a>
                    <ul class="dropdown-menu" role="menu">
                        <li><a href="php.html">PHP</a></li>
                        <li><a href="java.html">JAVA</a></li>
                        <li><a href="android.html">Android</a></li>
                        <li><a href="dotnet.html">.Net</a></li>
                        <li><a href="ios.html">iOS</a></li>
                    </ul>
                </li>
                <li><a href="sitemap.html">SITEMAP</a></li>
            </ul>
        </div>                                       
    </div>  

除了菜单,我调整浏览器大小时会显示所有内容。 调整大小时不显示菜单。

4 个答案:

答案 0 :(得分:1)

我添加了一些可能不在您的代码中的代码 工作模式

@media (min-width: 768px) {
  .navbar-right .dropdown-menu {
    right: auto;
  }
}

.nav>li>a:hover {
  background-color: #336699;
  color: #fff;
}

.dropdown-menu {
  margin: 0px;
  padding-top: 0px;
  padding-bottom: 0px;
  color: #fff;
}

.dropdown-menu>li>a:hover {
  background-color: #336699;
  color: #fff;
}

.dropdown-menu li {
  height: 40;
  color: #fff;
  float: none;
  display: block;
  margin: 0;
  text-align: left;
  border: 0;
  border-bottom: 1px solid #d5d5d5;
}

.dropdown-menu a {
  margin-top: 10;
}
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="navbar" class="navbar navbar-default navbar-fixed-top">
  <div class="navbar-container" id="navbar-container">
    <button type="button" class="navbar-toggle menu-toggler pull-left fixed" id="menu-toggler" data-target="#sidebar">
      <span class="sr-only">Toggle sidebar</span> <span class="icon-bar"></span><span class="icon-bar">
                </span><span class="icon-bar"></span>
    </button>
    <div class="navbar-header pull-left">


      <div class="col-sm-7 menu">
        <div class="collapse navbar-collapse">
          <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-responsive-collapse">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <ul class="nav navbar-nav navbar-right">
            <li><a href="home_inventive.html">HOME</a></li>
            <li class="dropdown" id="licompany">
              <a href="#" id="acompany" class="dropdown-toggle" data-toggle="dropdown">COMPANY </a>
              <ul class="dropdown-menu" role="menu">
                <li><a href="about_us.html">About Us</a></li>
                <li><a href="team.html">Meet The Team</a></li>
                <li><a href="join_us.html">Join Us</a></li>
              </ul>
            </li>
            <li class="dropdown">
              <a href="#" class="dropdown-toggle" data-toggle="dropdown">SERVICE </a>
              <ul class="dropdown-menu" role="menu">
                <li><a href="mobile_app_development.html">Mobile App Development</a></li>
                <li><a href="software_development.html">Software Development</a></li>
                <li><a href="web_development.html"> Web development</a></li>

              </ul>
            </li>
            <li><a href="product.html">PORTFOLIO</a></li>
            <li class="dropdown">
              <a href="#" class="dropdown-toggle" data-toggle="dropdown">TECHNOLOGY </a>
              <ul class="dropdown-menu" role="menu">
                <li><a href="php.html">PHP</a></li>
                <li><a href="java.html">JAVA</a></li>
                <li><a href="android.html">Android</a></li>
                <li><a href="dotnet.html">.Net</a></li>
                <li><a href="ios.html">iOS</a></li>
              </ul>
            </li>
            <li><a href="sitemap.html">SITEMAP</a></li>
          </ul>
        </div>
      </div>
    </div>
  </div>
</div>

demo

答案 1 :(得分:1)

我刚刚根据bootstrap example工作更新了html结构。

  1. 如果您看到bootstrap示例,则在{my}之外使用navbar-header collapse navbar-collapse但在你的情况下你错过了它。
  2. 第二个你的目标是navbar-responsive-collapse data-target=".navbar-responsive-collapse"但您还没有定义 它与navbar-collapse一起,所以我只是添加示例目标ID data-target="#bs-example-navbar-collapse-1"并在<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">中定义。
  3. 我只是在navbar navbar-default中选择了另一个元素col-sm-7 menu这是您在没有navbar的情况下错过的重要事情 上课它显然不起作用。
  4. 请参阅fiddle

    <div class="col-sm-7 menu">
      <nav class="navbar navbar-default">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">Brand</a>
        </div>
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
          <ul class="nav navbar-nav navbar-right">
            <li><a href="home_inventive.html">HOME</a></li>
            <li class="dropdown" id="licompany">
              <a href="#" id="acompany" class="dropdown-toggle" data-toggle="dropdown">COMPANY </a>
              <ul class="dropdown-menu" role="menu">
                <li><a href="about_us.html">About Us</a></li>
                <li><a href="team.html">Meet The Team</a></li>
                <li><a href="join_us.html">Join Us</a></li>
              </ul>
            </li>
            <li class="dropdown">
              <a href="#" class="dropdown-toggle" data-toggle="dropdown">SERVICE </a>
              <ul class="dropdown-menu" role="menu">
                <li><a href="mobile_app_development.html">Mobile App Development</a></li>
                <li><a href="software_development.html">Software Development</a></li>
                <li><a href="web_development.html"> Web development</a></li>
    
              </ul>
            </li>
            <li><a href="product.html">PORTFOLIO</a></li>
            <li class="dropdown">
              <a href="#" class="dropdown-toggle" data-toggle="dropdown">TECHNOLOGY </a>
              <ul class="dropdown-menu" role="menu">
                <li><a href="php.html">PHP</a></li>
                <li><a href="java.html">JAVA</a></li>
                <li><a href="android.html">Android</a></li>
                <li><a href="dotnet.html">.Net</a></li>
                <li><a href="ios.html">iOS</a></li>
              </ul>
            </li>
            <li><a href="sitemap.html">SITEMAP</a></li>
          </ul>
        </div>
      </nav>
    
    </div>
    

答案 2 :(得分:1)

使用以下代码检查我在代码中所做的更改后看看

第1个你错过了<nav class="navbar navbar-default">导航栏和第2个你错过了<div class="navbar-header">移动屏幕导航栏如果你错过了父类,它会应用课程,而且菜单也没有响应。

@media (min-width: 768px){
.navbar-right .dropdown-menu {

   right: auto; 

 }}

 .nav>li>a:hover
  {
    background-color: #336699 !important;
    color:#fff !important;
   }
  .dropdown-menu
  {
    margin: 0px;
    padding-top :0px;
    padding-bottom: 0px;
    color: #fff;
  }
  
 .dropdown-menu>li>a:hover
  {
      background-color: #336699 !important;
      color:#fff !important;
  }

  .dropdown-menu li
   {
     height: 40;
     color: #fff;
     float: none;
     display: block;
     margin: 0;
     text-align: left;
     border: 0;
     border-bottom: 1px solid #d5d5d5;
  }
  .dropdown-menu a
  {     
     margin-top: 10;    
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="col-sm-7 menu">
  <nav class="navbar navbar-default">
    <!-- Brand and toggle get grouped for better mobile display -->
      <div class="navbar-header">
        <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
          <span class="sr-only">Toggle navigation</span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
        </button>
        <a class="navbar-brand visible-xs" href="#">Brand</a>
      </div>
      <!-- Collect the nav links, forms, and other content for toggling -->
      <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
        <ul class="nav navbar-nav">
          <li><a href="home_inventive.html">HOME</a></li>
          <li class="dropdown" id="licompany">
            <a href="#" id="acompany" class="dropdown-toggle" data-toggle="dropdown">COMPANY </a>
            <ul class="dropdown-menu" role="menu">
              <li><a href="about_us.html">About Us</a></li>
              <li><a href="team.html">Meet The Team</a></li>
              <li><a href="join_us.html">Join Us</a></li>
            </ul>
          </li>
          <li class="dropdown">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown">SERVICE </a>
            <ul class="dropdown-menu" role="menu">
              <li><a href="mobile_app_development.html">Mobile App Development</a></li>
              <li><a href="software_development.html">Software Development</a></li>
              <li><a href="web_development.html"> Web development</a></li>
            </ul>
          </li>                   
          <li><a href="product.html">PORTFOLIO</a></li>
          <li class="dropdown">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown">TECHNOLOGY </a>
            <ul class="dropdown-menu" role="menu">
              <li><a href="php.html">PHP</a></li>
              <li><a href="java.html">JAVA</a></li>
              <li><a href="android.html">Android</a></li>
              <li><a href="dotnet.html">.Net</a></li>
              <li><a href="ios.html">iOS</a></li>
            </ul>
          </li>
          <li><a href="sitemap.html">SITEMAP</a></li>
        </ul>
      </div><!-- /.navbar-collapse -->
  </nav>
</div>

答案 3 :(得分:0)

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

在页面顶部/底部包含此Js