响应式Bootstrap Navbar

时间:2014-10-20 00:47:59

标签: html css twitter-bootstrap less

我正在尝试为我的网站创建一个Navbar,其左侧是下拉菜单,中间是图像,右侧是可折叠项目。我使用较少的bootstrap来设置导航栏的样式,但我遇到了麻烦。

我搜索过堆栈溢出寻找方法来完成这项工作,例如this问题,但那里的解决方案对我不起作用。我似乎无法将我的图像排成一行,以便其水平轴以下拉菜单,可折叠项和导航栏容器为中心。当我尝试上述链接中的解决方案时,通过最大化其位置固定将图像从页面流中取出,它总是出现在导航栏下方。我可以通过搞乱CSS属性来解决这个问题,但是一旦我调整窗口大小,事情看起来很糟糕。

是否有一种方法(可能使用更少)来设置引导导航栏的样式,这样无论窗口的大小是多少,我左边都有一个项目,中间有图像,右边有项目,所有人共享一个常见的水平中心轴与导航栏?

修改

好的,所以我改变了主意,决定只在左边添加一些链接,中心还有一个图像,右边有一些链接。当屏幕变小时,我希望链接向右折叠,为菜单提供切换按钮。折叠工作正常,但当链接在屏幕上时,我希望它们在导航栏中与图像一起水平居中。现在,图像部分位于导航栏中,部分位于外部。当链接以较小的屏幕尺寸折叠时,我还希望图像向左滑动。这是我现在的代码:

请注意我正在使用django来提供html,因此图像src的语法。

#logo-with-slogan {
  max-width: 35%;
  padding: 3% 0 3% 0;
}

.navbar-brand {
  position: absolute;
  width: 100%;
  left: 0;
  text-align: center;
  margin: auto;
}
<nav class="navbar navbar-default" role="navigation">
  <div class="navbar-header">
    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
  </div>
  <a class="navbar-brand" href="#">
    <img id="logo-with-slogan" src="{% static " img/LogoWslogan.jpg " %}" />
  </a>
  <div class="navbar-collapse collapse">
    <ul class="nav navbar-nav navbar-left">
      <li><a href="#itemonelink">Item One</a></li>
      <li><a href="#itemtwolink">Item Two</a></li>
    </ul>
    <ul class="nav navbar-nav navbar-right">
      <li><a href="#signuplink">Signup</a></li>
      <li><a href="#loginlink">Login</a></li>
    </ul>
  </div>
</nav>

3 个答案:

答案 0 :(得分:5)

所以你想要做这样的事情,配合图像:

<强> HTML

<nav class="navbar navbar-default" role="navigation">
    <div class="navbar-header">
        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
        </button>    
    </div>
    <a class="navbar-brand navbar-center" href="#">
        <img src="http://placehold.it/100x40" alt="Logo Here">
    </a>
    <div class="navbar-collapse collapse">
        <ul class="nav navbar-nav navbar-left">
            <li><a href="#itemonelink">Item One</a></li>
            <li><a href="#itemtwolink">Item Two</a></li>
        </ul>
        <ul class="nav navbar-nav navbar-right">
            <li><a href="#signuplink">Signup</a></li>
            <li><a href="#loginlink">Login</a></li>
        </ul>
    </div>
</nav>

<强> CSS

.navbar-brand {
    float: none;
    padding: 5px;
}
.navbar-center {
    position: absolute;
    width: 100%;
    left: 0;
    top: 0;
    text-align: center;
    margin: auto;
    height:100%;
}

演示:JSFiddle

答案 1 :(得分:0)

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Case</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<nav class="navbar navbar-inverse">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>                        
      </button>
      <a class="navbar-brand" href="#">WebSiteName</a>
    </div>
    <div class="collapse navbar-collapse" id="myNavbar">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Home</a></li>
        <li class="dropdown">
          <a class="dropdown-toggle" data-toggle="dropdown" href="#">Page 1 <span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="#">Page 1-1</a></li>
            <li><a href="#">Page 1-2</a></li>
            <li><a href="#">Page 1-3</a></li>
          </ul>
        </li>
        <li><a href="#">Page 2</a></li>
        <li><a href="#">Page 3</a></li>
      </ul>
      <ul class="nav navbar-nav navbar-right">
        <li><a href="#"><span class="glyphicon glyphicon-user"></span> Sign Up</a></li>
        <li><a href="#"><span class="glyphicon glyphicon-log-in"></span> Login</a></li>
      </ul>
    </div>
  </div>
</nav>

<div class="container">
  <h3>Navbar bootstrap</h3>
  <p>In this example, the navigation bar is hidden on small screens and replaced by a button in the top right corner (try to re-size this window).
  <p>Only when the button is clicked, the navigation bar will be displayed.</p>
</div>

</body>
</html>

答案 2 :(得分:-2)

导航栏是引导程序中最酷的组件,您可以轻松地为您的网站创建很酷的响应式导航标题。

有关信息和详细信息,请参阅此文章.. steps to create navbar in bootstrap

&#13;
&#13;
 <nav class="navbar navbar-inverse">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#testNavBar">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>                        
      </button>
      <a class="navbar-brand" href="#">Logo / SiteName</a>
    </div>
    <div class="collapse navbar-collapse" id="testNavBar">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Home</a></li>
        <li><a href="#">Page 1</a></li>
        <li><a href="#">Page 2</a></li>
      </ul>
     
    </div>
  </div>
</nav>
&#13;
&#13;
&#13;