css 导航菜单不工作

c7rzv4ha  于 2023-11-19  发布在  其他
关注(0)|答案(2)|浏览(117)

如何解决 Bootstrap 导航栏面临的这3个问题:

  • 调整大小时菜单无法正确折叠(窗口变小)
  • 将图标放在菜单链接旁边,而不是它们下面
  • 当调整窗口大小时,让注册/登录按钮链接到另一个页面

下面是一个demo:
https://jsfiddle.net/whywymam/zpdpsuLn/
HTML代码:

<div class="container">

      <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">

          <navbar>

              <div class="upperRow">

               <div class="navbar-header">
                    <a href="#" class="brand navbar-brand"> <img src="image/logo.png" class="img-responsive"></a>
               </div>

               <div id="btnTopInline">

                   <ul class="nav navbar-nav navbar-right imgicon hidden-xs">

                       <li>
                         <div class="indivColl">
                            <a href="signupLogin.php">
                               <img src="image/jobseekerlogo.png" class="indi">
                               <p class="indit">Job Seeker</p>
                            </a>
                         </div>
                       </li>

                       <li>
                         <div id="empColl">
                            <a href="signupLoginEmp.php">
                               <img src="image/employerlogo.png" class="emp">
                               <p class="empt">Employer</p>
                            </a>
                         </div>
                       </li>

                   </ul>

               </div>

               <!--                for tablet and mobile view-->
                <button type="button" class="btn btn-login pull-right visible-xs">
                    <a href="signupLoginEmp.php">
                        Employer<br>Log In
                    </a>
                </button>

                <button type="button" class="btn btn-login pull-right visible-xs">
                    <a href="signupLogin.php">
                        Job Seeker<br>Log In
                    </a>
                </button>

              </div>

            <div class="middleRow">

                <div class="navbar-inner navbar-default navbar-static-top navcolor">

                          <div class="navbar-header ">

                              <!--button to appear when display is on mobile device-->
                              <button type="button" class="navbar-toggle pull-right" data-toggle="collapse" data-target=".nav-collapse">
                                  <span class="sr-only">Toggle navigation</span>
                                  <span class="icon-bar"></span>
                                  <span class="icon-bar"></span>
                                  <span class="icon-bar"></span>
                              </button>

                          </div>

                          <div class="nav-collapse collapse-in" id="nav-collapse">

                              <ul class="nav  navbar-nav center-block">
                                <li><a href="home.php">Home<img src="image/home.png" width="75" height="65" alt="" title="" /></a></li>
                                <li><a href="about.php">About<img src="image/about.png" width="75" height="65" alt="" title="" /></a></li>
                                <li><a href="pyw.php">Prove Job<img src="image/PYW.png" width="75" height="65" alt="" title="" /></a></li>
                                <li><a href="#">Job<img src="image/jobs.png" width="75" height="65" alt="" title="" /></a></li>
                                <li><a href="rescources.php">Resources<img src="image/resource.png" width="75" height="65" alt="" title="" /></a></li>
                             </ul>

                          </div>

                    </div> <!-- end inner navbar -->

            </div> <!-- end middle row -->

          </navbar>

      </div> <!-- end col-lg-12 col-md-12 col-sm-12 col-xs-12 -->

  </div><!-- end container -->

字符串
CSS:

.img-responsive {
max-width: 60%;
margin-bottom: 15%;
padding-top: -3%;
}
.imgicon {
padding-left: 50%;
margin-top: -2%;
}

.middleRow{
padding-top: 10%;
margin-top: 5%;
}

.navcolor{
background-color: transparent;
}

.navbar-default {
margin-top: 3%;
}

.navbar-nav {
float:none;
margin: 0 auto;
display: table;
table-layout: fixed;
text-align: center;
}

#btnTopInline{
display: inline-block;
float: right;
clear: none;
}

@media(min-width:768px) {
  .nav-collapse{
        display: inline;
  }
  }


谢谢你提前:)

qkf9rpyu

qkf9rpyu1#

这里是代码我做了一些修改,在html和css
为切换按钮工作使用data-target=".nav-collapse"代替data-target=".navbar-collapse"
对于图标旁边的菜单链接添加img标签内的ancor标签.
以下是变化

<body>
         <div class="navbar">
            <div class="navbar-inner">
               <div class="container">
                  <div class="navbar-header">
                     <a href="#" class="brand navbar-brand"> <img src="image/logo.png" class="img-responsive"></a>
                  </div>
                  <ul class="nav navbar-nav navbar-right imgicon hidden-xs">
                     <div id="indivColl">
                        <a href="signupLogin.php">
                           <img src="image/jobseekerlogo.png" class="indi">
                           <p class="indit">Job Seeker</p>
                        </a>
                     </div>
                     <div id="empColl">
                        <a href="signupLoginEmp.php">
                           <img src="image/employerlogo.png" class="emp">
                           <p class="empt">Employer</p>
                        </a>
                     </div>
                  </ul>
                  <!--                for tablet and mobile view-->
                  <button type="submit" id="btn-login-header" class="btn btn-login pull-right visible-xs mobile-login" 
                     id="header-mobile-login-button" data-target="#indivColl">
                  Employer<br>Log In
                  </button>
                  <button type="submit" id="btn-login-header" class="btn btn-login pull-right visible-xs mobile-login" 
                     id="header-mobile-login-button" data-target="#empColl">
                  Job Seeker<br>Log In
                  </button>

                  <button type="button" class="navbar-toggle btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
                  <span class="icon-bar">Toggle navigation</span>
                  <span class="icon-bar"></span>
                  <span class="icon-bar"></span>
                  </button>

                  <div class="nav-collapse collapse in" id="navbar-collapse">
                     <ul class="nav navbar-nav">
                        <li><a href="home.php">Home<img src="image/home.png" width="75" height="65" alt="" title="" /></a></li>
                        <li><a href="about.php">About<img src="image/about.png" width="75" height="65" alt="" title="" /></a></li>
                        <li><a href="pyw.php">Prove Your Worth<img src="image/PYW.png" width="75" height="65" alt="" title="" /></a></li>
                        <li><a href="#">Job<img src="image/jobs.png" width="75" height="65" alt="" title="" /></a></li>
                        <li><a href="rescources.php">Resources<img src="image/resource.png" width="75" height="65" alt="" title="" /></a></li>
                     </ul>
                  </div>
               </div>
            </div>
         </div>
   </body>

字符串
在CSS中添加

@media(min-width:768px) {
      .nav-collapse{
            display: block;
      }
 }

qgelzfjb

qgelzfjb2#

首先:对于响应式布局工作, Bootstrap 需要处理行和列。您需要将导航栏放在里面。

<div class="row">
    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
         <navbar>
         </navbar>
    </div>
</div>

字符串
第二:我不测试,但我认为在导航栏的li中,你需要创建一个div,类包含text-align:center;或者把这个类放在li中。在div中,把文本和图标,每个都有一个类,包含float:left和clear:both。

<ul class="nav navbar-nav navbar-right imgicon hidden-xs">
            <div id="indivColl">
            <a href="signupLogin.php"><img src="image/jobseekerlogo.png" class="indi"><p class="indit">Job Seeker</p></a>
            </div>

            <div id="empColl">
            <a href="signupLoginEmp.php"><img src="image/employerlogo.png" class="emp"><p class="empt">Employer</p></a>
            </div>
        </ul>


上面你需要把每个div放在一个li中。在img中插入类,并将p标记转换为span标记,之后,将相同的类添加到span中。这个例子是一个假设,我没有测试,但你可以在www.w3cschools.com中搜索更多关于float和clear的信息。
你的代码中navbar的逻辑是完全错误的。你有一个navbar在一个navbar中,有两个容器。你需要回顾一下bootstrap,html和css的概念。比较一下你的:

<div class="navbar navbar-default navbar-static-top navcolor">

<div class="navbar-header">
  <a href="#" class="navbar-brand"> <img src="image/logo.png" class="img-responsive"></a>
</div>

<!-- for tablet and mobile view-->
<button type="submit" id="btn-login-header" class="btn btn-login pull-right navbar-btn visible-xs mobile-login" id="header-mobile-login-button" data-target="#indivColl">  Job Seeker Log In </button>
<button type="submit" id="btn-login-header" class="btn btn-login pull-right navbar-btn visible-xs mobile-login" id="header-mobile-login-button" data-target="#empColl"> Employer Log In </button>

<div class="navbar-header">
  <!--button to appear when display is on mobile device-->
  <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
    <span class="sr-only">Toggle navigation</span>
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
  </button>
</div>

<div class="collapse navbar-collapse" id="navbar-collapse">
  <ul class="nav navbar-nav">
    <li><a href="home.php">Home</a><img src="image/home.png" width="75" height="65" alt="" title="" /></li>
    <li><a href="about.php">About</a><img src="image/about.png" width="75" height="65" alt="" title="" /></li>
    <li><a href="pyw.php">Prove Your Worth</a><img src="image/PYW.png" width="75" height="65" alt="" title="" /></li>
    <li><a href="#">Job</a><img src="image/jobs.png" width="75" height="65" alt="" title="" /></li>
    <li><a href="rescources.php">Resources</a><img src="image/resource.png" width="75" height="65" alt="" title="" /></li>
    <li class="imgicon hidden-xs">
      <div id="indivColl">
        <a href="signupLogin.php"><img src="image/jobseekerlogo.png" class="indi"><p class="indit">Job Seeker</p></a>
      </div>
    </li>
    <li class="imgicon hidden-xs">
      <div id="empColl">
        <a href="signupLoginEmp.php"><img src="image/employerlogo.png" class="emp"><p class="empt">Employer</p></a>
      </div>
    </li>
  </ul>
</div>


我没有详细介绍css或完全引导规则,但我认为这段代码对你很有用。
这将是一种方法。再见,喜欢我的回答,如果送达!

相关问题