navbar.md 14.7 KB
Newer Older
Mark Otto's avatar
Mark Otto committed
1
---
Mark Otto's avatar
Mark Otto committed
2
layout: docs
Mark Otto's avatar
Mark Otto committed
3
title: Navbar
Mark Otto's avatar
Mark Otto committed
4
description: Documentation and examples for Bootstrap's powerful, responsive navigation header.
5
group: components
Mark Otto's avatar
Mark Otto committed
6
7
---

8
The navbar is a wrapper that positions branding, navigation, and other elements in a concise header. It's easily extensible and, thanks to our Collapse plugin, can easily integrate responsive behaviors.
Mark Otto's avatar
Mark Otto committed
9

Mark Otto's avatar
Mark Otto committed
10
11
12
13
14
## Contents

* Will be replaced with the ToC, excluding the "Contents" header
{:toc}

Mark Otto's avatar
Mark Otto committed
15
## How it works
Mark Otto's avatar
Mark Otto committed
16
17
18

Here's what you need to know before getting started with the navbar:

19
- Navbars require a wrapping `.navbar` and [color scheme](#color-schemes) classes.
Mark Otto's avatar
Mark Otto committed
20
- Navbars and their contents are fluid by default. Use [optional containers](#containers) to limit their horizontal width.
Mark Otto's avatar
Mark Otto committed
21
22
- Navbars and their contents are built with flexbox, providing easy alignment options via utility classes.
- Navbars are responsive by default, but you can easily modify them to change that. Responsive behavior depends on our Collapse JavaScript plugin.
Mark Otto's avatar
Mark Otto committed
23
- Ensure accessibility by using a `<nav>` element or, if using a more generic element such as a `<div>`, add a `role="navigation"` to every navbar to explicitly identify it as a landmark region for users of assistive technologies.
Mark Otto's avatar
Mark Otto committed
24

Mark Otto's avatar
Mark Otto committed
25
26
Read on for an example and list of supported sub-components.

Mark Otto's avatar
Mark Otto committed
27
## Supported content
Mark Otto's avatar
Mark Otto committed
28

29
Navbars come with built-in support for a handful of sub-components. Choose from the following as needed:
Mark Otto's avatar
Mark Otto committed
30

Mark Otto's avatar
Mark Otto committed
31
32
- `.navbar-brand` for your company, product, or project name
- `.navbar-nav` for a full-height and lightweight navigation (including support for dropdowns)
33
- `.navbar-toggler` for use with our collapse plugin and other [navigation toggling](#collapsible-content) behaviors.
34
35
- Inline forms with `.float-` utilities for form controls and components.
- `.navbar-text` for adding vertically centered strings of text.
36
- `.inline-form` for form controls and more.
Mark Otto's avatar
Mark Otto committed
37

38
Here's an example of all the sub-components included in a responsive light-themed navbar.
Mark Otto's avatar
Mark Otto committed
39

Mark Otto's avatar
Mark Otto committed
40
{% example html %}
41
<nav class="navbar navbar-light bg-faded">
Mark Otto's avatar
Mark Otto committed
42
43
  <a class="navbar-brand" href="#">Navbar</a>
  <ul class="nav navbar-nav">
Mark Otto's avatar
Mark Otto committed
44
    <li class="nav-item active">
Mark Otto's avatar
Mark Otto committed
45
      <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
Mark Otto's avatar
Mark Otto committed
46
47
    </li>
    <li class="nav-item">
48
      <a class="nav-link" href="#">Link</a>
Mark Otto's avatar
Mark Otto committed
49
50
    </li>
    <li class="nav-item">
51
      <a class="nav-link disabled" href="#">Disabled</a>
Mark Otto's avatar
Mark Otto committed
52
    </li>
53
    <li class="nav-item dropdown">
Mark Otto's avatar
Mark Otto committed
54
55
      <a class="nav-link dropdown-toggle" href="http://example.com" id="supportedContentDropdown" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdown</a>
      <div class="dropdown-menu" aria-labelledby="supportedContentDropdown">
56
57
58
59
        <a class="dropdown-item" href="#">Action</a>
        <a class="dropdown-item" href="#">Another action</a>
        <a class="dropdown-item" href="#">Something else here</a>
      </div>
Mark Otto's avatar
Mark Otto committed
60
61
    </li>
  </ul>
62
  <form class="form-inline">
Mark Otto's avatar
Mark Otto committed
63
    <input class="form-control" type="text" placeholder="Search">
64
    <button class="btn btn-outline-success" type="submit">Search</button>
Mark Otto's avatar
Mark Otto committed
65
  </form>
Mark Otto's avatar
Mark Otto committed
66
</nav>
Mark Otto's avatar
Mark Otto committed
67
68
{% endexample %}

69
70
### Brand

Mark Otto's avatar
Mark Otto committed
71
The `.navbar-brand` can be applied to most elements, but an anchor works best as some elements might require utility classes or custom styles.
72
73

{% example html %}
74
<!-- As a link -->
75
76
77
78
<nav class="navbar navbar-light bg-faded">
  <a class="navbar-brand" href="#">Navbar</a>
</nav>

79
<!-- As a heading -->
80
<nav class="navbar navbar-light bg-faded">
81
  <h1 class="navbar-brand mb-0">Navbar</h1>
82
83
84
</nav>
{% endexample %}

85
86
87
88
89
90
Adding images to the `.navbar-brand` will likely always require custom styles or utilities to properly size. Here are some examples to demonstrate.

{% example html %}
<!-- Just an image -->
<nav class="navbar navbar-light bg-faded">
  <a class="navbar-brand" href="#">
Mark Otto's avatar
Mark Otto committed
91
    <img src="{{ site.baseurl }}/assets/brand/bootstrap-solid.svg" width="30" height="30" alt="">
92
93
94
95
96
97
98
99
  </a>
</nav>
{% endexample %}

{% example html %}
<!-- Image and text -->
<nav class="navbar navbar-light bg-faded">
  <a class="navbar-brand" href="#">
Mark Otto's avatar
Mark Otto committed
100
    <img src="{{ site.baseurl }}/assets/brand/bootstrap-solid.svg" width="30" height="30" class="d-inline-block align-top" alt="">
101
102
103
104
105
    Bootstrap
  </a>
</nav>
{% endexample %}

106
107
### Nav

108
Navbar navigation is similar to our regular nav options—use the `.nav` base class with the `.navbar-nav` modifier correctly position your links. **Navbar navigation will grow to occupy as much horizontal space as possible** to keep your navbar contents aligned properly.
109

110
111
Active states—with `.active`—to indicate the current page can be applied directly to `.nav-link`s or their immediate parent `.nav-item`s.

112
113
114
115
116
117
118
119
120
121
122
123
124
{% example html %}
<nav class="navbar navbar-light bg-faded">
  <ul class="nav navbar-nav">
    <li class="nav-item active">
      <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Features</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Pricing</a>
    </li>
    <li class="nav-item">
125
      <a class="nav-link disabled" href="#">Disabled</a>
126
127
128
129
130
    </li>
  </ul>
</nav>
{% endexample %}

131
And because we use classes for our navs, you can avoid the list-based approach entirely if you like.
132
133
134
135
136
137
138

{% example html %}
<nav class="navbar navbar-light bg-faded">
  <div class="nav navbar-nav">
    <a class="nav-item nav-link active" href="#">Home <span class="sr-only">(current)</span></a>
    <a class="nav-item nav-link" href="#">Features</a>
    <a class="nav-item nav-link" href="#">Pricing</a>
139
    <a class="nav-item nav-link disabled" href="#">Disabled</a>
140
141
142
143
  </div>
</nav>
{% endexample %}

144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
You may also utilize dropdowns in your navbar nav. Dropdown menus require a wrapping element for positioning, so be sure to use separate and nested elements for `.nav-item` and `.nav-link` as shown below.

{% example html %}
<nav class="navbar navbar-light bg-faded">
  <ul class="nav navbar-nav">
    <li class="nav-item active">
      <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Features</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Pricing</a>
    </li>
    <li class="nav-item dropdown">
159
      <a class="nav-link dropdown-toggle" href="http://example.com" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
160
161
162
163
164
165
166
167
168
169
170
171
        Dropdown link
      </a>
      <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
        <a class="dropdown-item" href="#">Action</a>
        <a class="dropdown-item" href="#">Another action</a>
        <a class="dropdown-item" href="#">Something else here</a>
      </div>
    </li>
  </ul>
</nav>
{% endexample %}

172
173
### Forms

174
Place various form controls and components within a navbar with `.form-inline`.
175
176
177

{% example html %}
<nav class="navbar navbar-light bg-faded">
178
179
180
181
182
183
184
185
186
187
188
189
  <form class="form-inline">
    <input class="form-control" type="text" placeholder="Search">
    <button class="btn btn-outline-success" type="submit">Search</button>
  </form>
</nav>
{% endexample %}

Align the contents of your inline forms with utilities as needed.

{% example html %}
<nav class="navbar navbar-light bg-faded flex-items-right">
  <form class="form-inline">
190
191
192
193
194
195
196
197
198
199
    <input class="form-control" type="text" placeholder="Search">
    <button class="btn btn-outline-success" type="submit">Search</button>
  </form>
</nav>
{% endexample %}

Input groups work, too:

{% example html %}
<nav class="navbar navbar-light bg-faded">
200
  <form class="form-inline">
201
202
203
204
205
206
207
208
    <div class="input-group">
      <span class="input-group-addon" id="basic-addon1">@</span>
      <input type="text" class="form-control" placeholder="Username" aria-describedby="basic-addon1">
    </div>
  </form>
</nav>
{% endexample %}

209
Various buttons are supported as part of these navbar forms, too. This is also a great reminder that vertical alignment utilities can be used to align different sized elements.
210
211
212

{% example html %}
<nav class="navbar navbar-light bg-faded">
213
  <form class="form-inline">
214
215
216
217
218
219
    <button class="btn btn-outline-success" type="button">Main button</button>
    <button class="btn btn-sm align-middle btn-outline-secondary" type="button">Smaller button</button>
  </form>
</nav>
{% endexample %}

220
221
222
223
224
225
### Text

Navbars may contain bits of text with the help of `.navbar-text`. This class adjusts vertical alignment and horizontal spacing for strings of text.

{% example html %}
<nav class="navbar navbar-light bg-faded">
226
227
228
  <span class="navbar-text">
    Navbar text with an inline element
  </span>
229
230
231
</nav>
{% endexample %}

232
## Color schemes
Mark Otto's avatar
Mark Otto committed
233

234
Theming the navbar has never been easier thanks to the combination of theming classes and `background-color` utilities. Chose from `.navbar-light` for use with light background colors, or `.navbar-dark` for dark background colors. Then, customize with `.bg-*` utilities.
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252

<div class="bd-example">
  <nav class="navbar navbar-dark bg-inverse">
    <a class="navbar-brand" href="#">Navbar</a>
    <ul class="nav navbar-nav">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Features</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Pricing</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">About</a>
      </li>
    </ul>
253
    <form class="form-inline">
254
      <input class="form-control" type="text" placeholder="Search">
255
      <button class="btn btn-outline-info" type="submit">Search</button>
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
    </form>
  </nav>
  <nav class="navbar navbar-dark bg-primary">
    <a class="navbar-brand" href="#">Navbar</a>
    <ul class="nav navbar-nav">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Features</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Pricing</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">About</a>
      </li>
    </ul>
274
    <form class="form-inline">
275
      <input class="form-control" type="text" placeholder="Search">
276
      <button class="btn btn-outline-secondary" type="submit">Search</button>
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
    </form>
  </nav>
  <nav class="navbar navbar-light" style="background-color: #e3f2fd;">
    <a class="navbar-brand" href="#">Navbar</a>
    <ul class="nav navbar-nav">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Features</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Pricing</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">About</a>
      </li>
    </ul>
295
    <form class="form-inline">
296
      <input class="form-control" type="text" placeholder="Search">
297
      <button class="btn btn-outline-primary" type="submit">Search</button>
298
299
300
301
302
303
304
    </form>
  </nav>
</div>

{% highlight html %}
<nav class="navbar navbar-dark bg-inverse">
  <!-- Navbar content -->
Mark Otto's avatar
Mark Otto committed
305
</nav>
306
307
308
309
310
311
312
313
314

<nav class="navbar navbar-dark bg-primary">
  <!-- Navbar content -->
</nav>

<nav class="navbar navbar-light" style="background-color: #e3f2fd;">
  <!-- Navbar content -->
</nav>
{% endhighlight %}
Mark Otto's avatar
Mark Otto committed
315

Mark Otto's avatar
Mark Otto committed
316
## Containers
317

318
Although it's not required, you can wrap a navbar in a `.container` to center it on a page or add one within to only center the contents of a [fixed or static top navbar](#placement).
319

Mark Otto's avatar
Mark Otto committed
320
321
{% example html %}
<div class="container">
322
  <nav class="navbar navbar-light bg-faded">
Mark Otto's avatar
Mark Otto committed
323
    <a class="navbar-brand" href="#">Navbar</a>
324
325
  </nav>
</div>
Mark Otto's avatar
Mark Otto committed
326
{% endexample %}
327

Mark Otto's avatar
Mark Otto committed
328
{% example html %}
329
<nav class="navbar navbar-light bg-faded">
Mark Otto's avatar
Mark Otto committed
330
331
332
  <div class="container">
    <a class="navbar-brand" href="#">Navbar</a>
  </div>
333
</nav>
Mark Otto's avatar
Mark Otto committed
334
{% endexample %}
335

Mark Otto's avatar
Mark Otto committed
336
337
## Placement

338
339
340
341
342
343
344
Navbars can be statically placed (their default behavior), static without rounded corners, or fixed to the top or bottom of the viewport.

{% example html %}
<nav class="navbar navbar-full navbar-light bg-faded">
  <a class="navbar-brand" href="#">Full width</a>
</nav>
{% endexample %}
Mark Otto's avatar
Mark Otto committed
345
346
347
348
349
350
351
352
353
354
355
356
357
358

{% example html %}
<nav class="navbar navbar-fixed-top navbar-light bg-faded">
  <a class="navbar-brand" href="#">Fixed top</a>
</nav>
{% endexample %}

{% example html %}
<nav class="navbar navbar-fixed-bottom navbar-light bg-faded">
  <a class="navbar-brand" href="#">Fixed bottom</a>
</nav>
{% endexample %}


Mark Otto's avatar
Mark Otto committed
359
## Collapsible content
Mark Otto's avatar
Mark Otto committed
360

Mark Otto's avatar
Mark Otto committed
361
Our collapse plugin allows you to use a `<button>` or `<a>` to toggle hidden content.
Mark Otto's avatar
Mark Otto committed
362
363

{% example html %}
364
<nav class="navbar navbar-light bg-faded">
365
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#exCollapsingNavbar" aria-controls="exCollapsingNavbar" aria-expanded="false" aria-label="Toggle navigation"></button>
366
  <div class="collapse" id="exCollapsingNavbar">
367
    <div class="bg-inverse text-muted p-1">
368
369
370
371
      <h4>Collapsed content</h4>
      <span class="text-muted">Toggleable via the navbar brand.</span>
    </div>
  </div>
Mark Otto's avatar
Mark Otto committed
372
</nav>
Mark Otto's avatar
Mark Otto committed
373
{% endexample %}
Mark Otto's avatar
Mark Otto committed
374
375
376
377

For more complex navbar patterns, like those used in Bootstrap v3, use the `.navbar-toggleable-*` classes in conjunction with the `.navbar-toggler`. These classes override our responsive utilities to show navigation only when content is meant to be shown.

{% example html %}
378
<nav class="navbar navbar-light bg-faded">
Mark Otto's avatar
Mark Otto committed
379
  <button class="navbar-toggler hidden-lg-up" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation"></button>
380
381
  <div class="collapse navbar-toggleable-md" id="navbarResponsive">
    <a class="navbar-brand" href="#">Navbar</a>
Mark Otto's avatar
Mark Otto committed
382
383
384
385
386
    <ul class="nav navbar-nav">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
387
        <a class="nav-link" href="#">Link</a>
Mark Otto's avatar
Mark Otto committed
388
389
      </li>
      <li class="nav-item">
390
        <a class="nav-link" href="#">Link</a>
Mark Otto's avatar
Mark Otto committed
391
      </li>
392
      <li class="nav-item dropdown">
Mark Otto's avatar
Mark Otto committed
393
394
        <a class="nav-link dropdown-toggle" href="http://example.com" id="responsiveNavbarDropdown" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdown</a>
        <div class="dropdown-menu" aria-labelledby="responsiveNavbarDropdown">
395
396
397
398
          <a class="dropdown-item" href="#">Action</a>
          <a class="dropdown-item" href="#">Another action</a>
          <a class="dropdown-item" href="#">Something else here</a>
        </div>
Mark Otto's avatar
Mark Otto committed
399
400
      </li>
    </ul>
401
402
403
404
    <form class="form-inline float-lg-right">
      <input class="form-control" type="text" placeholder="Search">
      <button class="btn btn-outline-success" type="submit">Search</button>
    </form>
Mark Otto's avatar
Mark Otto committed
405
406
407
  </div>
</nav>
{% endexample %}