如何将Bootstrap导航栏移到右侧?

时间:2017-09-19 11:04:09

标签: html css twitter-bootstrap

我在twitter Bootstrap中使用绝对定位的导航栏,但是当我使用导航栏项目在我的标签内部使用.pull-right类时,导航栏项目保持在左侧。我也尝试过使用float:right;和text-align:对;那些没用。导航栏的绝对定位是否会影响此行为?如何让导航栏项目转到横幅的右侧?

.wrapper {
  position: relative;
}

#banner {
  height: 50vh;
  width: 100vw;
  position: relative;
}

#responsive-nav {
  background-color: transparent;
  position: absolute;
  z-index: 2;
}

#logo {
  position: absolute;
  top: 15px;
  left: 60px;
  z-index: 1;
}
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Mock Up</title>
    <!-- Custom CSS -->
    <link rel="stylesheet" type="text/css" href="css/style.css">
    <!-- Bootstrap Link -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>

<body>
    <nav class="navbar navbar-static-top">
        <div class="container-fluid">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <!-- Collapsing Hamburger Buttons for mobile -->
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <div class="collapse navbar-collapse" id="responsive-nav">
                    <ul class="nav navbar-nav pull-right">
                        <li class="nav-item">
                            <a href="#about">Home</a>
                        </li>
                        <li class="nav-item">
                            <a href="#work">Projects</a>
                        </li>
                        <li class="nav-item">
                            <a href="#contact">Contact</a>
                        </li>
                    </ul>
                </div>
                <div class="wrapper">
                <img class="img-responsive" id="logo" src="https://lh3.google.com/u/0/d/0B7B-ke12S7B2ZS1qSS1wOGJCVzg=w1046-h653-iv1">
                <img class="img-responsive" id="banner" src="https://lh3.google.com/u/0/d/0B7B-ke12S7B2SDRjaVpyY3FITFk=w1366-h653-iv1">
            </div>
            </div>
        </div>
        
    </nav>
    <script src="https://use.fontawesome.com/bd8b80bd9d.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</body>

</html>

3 个答案:

答案 0 :(得分:2)

您可以使用解决方案https://jsfiddle.net/j6sqyru8/

.wrapper {
  position: relative;
}

#banner {
  height: 50vh;
  width: 100vw;
  position: relative;
}

#responsive-nav {
  background-color: transparent;
  position: absolute;
  z-index: 2;
}

.navbar-toggle {
  z-index: 9;
}

#logo {
  position: absolute;
  top: 15px;
  left: 60px;
  z-index: 1;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<nav class="navbar navbar-static-top">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
          <!-- Collapsing Hamburger Buttons for mobile -->
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <div class="collapse navbar-collapse" id="responsive-nav">
          <ul class="nav navbar-nav pull-right">
              <li class="nav-item">
                  <a href="#about">Home</a>
              </li>
              <li class="nav-item">
                  <a href="#work">Projects</a>
              </li>
              <li class="nav-item">
                  <a href="#contact">Contact</a>
              </li>
          </ul>
      </div>
      <div class="wrapper">
        <img class="img-responsive" id="logo" src="https://lh3.google.com/u/0/d/0B7B-ke12S7B2ZS1qSS1wOGJCVzg=w1046-h653-iv1">
        <img class="img-responsive" id="banner" src="https://lh3.google.com/u/0/d/0B7B-ke12S7B2SDRjaVpyY3FITFk=w1366-h653-iv1">
      </div>
    </div>
  </div>
</nav>

希望这会对你有所帮助。

答案 1 :(得分:0)

id =“responsive-nav”的父div必须是宽度100%

答案 2 :(得分:0)

width: 100%添加到#responive-nav

  #responsive-nav {
  background-color: transparent;
  position: absolute;
  z-index: 2;
  width: 100%;}