navbar.md 14.8 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 into a concise header. It's easily extensible and thanks to our Collapse plugin, it 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
## Basics
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 a [color scheme](#color-schemes).
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.
21
- Use `.float-*-left` and `.float-*-right` to quickly align sub-components.
Mark Otto's avatar
Mark Otto committed
22
- 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
23

Mark Otto's avatar
Mark Otto committed
24
## Supported content
Mark Otto's avatar
Mark Otto committed
25

26
Navbars come with built-in support for a handful of sub-components. Mix and match from the following as needed:
Mark Otto's avatar
Mark Otto committed
27

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

Mark Otto's avatar
Mark Otto committed
34
Here's an example of all the sub-components included in a default, non-responsive light themed navbar. [See the responsive examples](#collapsible-content) for collapsing nav support.
Mark Otto's avatar
Mark Otto committed
35

Mark Otto's avatar
Mark Otto committed
36
{% example html %}
37
<nav class="navbar navbar-light bg-faded">
Mark Otto's avatar
Mark Otto committed
38
39
  <a class="navbar-brand" href="#">Navbar</a>
  <ul class="nav navbar-nav">
Mark Otto's avatar
Mark Otto committed
40
    <li class="nav-item active">
Mark Otto's avatar
Mark Otto committed
41
      <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
Mark Otto's avatar
Mark Otto committed
42
43
    </li>
    <li class="nav-item">
44
      <a class="nav-link" href="#">Link</a>
Mark Otto's avatar
Mark Otto committed
45
46
    </li>
    <li class="nav-item">
47
      <a class="nav-link disabled" href="#">Disabled</a>
Mark Otto's avatar
Mark Otto committed
48
    </li>
49
    <li class="nav-item dropdown">
Mark Otto's avatar
Mark Otto committed
50
51
      <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">
52
53
54
55
        <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
56
57
    </li>
  </ul>
58
  <form class="form-inline float-right">
Mark Otto's avatar
Mark Otto committed
59
    <input class="form-control" type="text" placeholder="Search">
60
    <button class="btn btn-outline-success" type="submit">Search</button>
Mark Otto's avatar
Mark Otto committed
61
  </form>
Mark Otto's avatar
Mark Otto committed
62
</nav>
Mark Otto's avatar
Mark Otto committed
63
64
{% endexample %}

65
66
### Brand

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

{% example html %}
70
<!-- As a link -->
71
72
73
74
<nav class="navbar navbar-light bg-faded">
  <a class="navbar-brand" href="#">Navbar</a>
</nav>

75
<!-- As a heading -->
76
<nav class="navbar navbar-light bg-faded">
77
  <h1 class="navbar-brand mb-0">Navbar</h1>
78
79
80
</nav>
{% endexample %}

81
82
83
84
85
86
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
87
    <img src="{{ site.baseurl }}/assets/brand/bootstrap-solid.svg" width="30" height="30" alt="">
88
89
90
91
92
93
94
95
  </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
96
    <img src="{{ site.baseurl }}/assets/brand/bootstrap-solid.svg" width="30" height="30" class="d-inline-block align-top" alt="">
97
98
99
100
101
    Bootstrap
  </a>
</nav>
{% endexample %}

102
103
104
105
### Nav

Navbar navigation is similar to our regular nav options—use the `.nav` base class with a modifier to achieve a particular look. In this case you'll want `.nav.navbar-nav`.

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

108
109
110
111
112
113
114
115
116
117
118
119
120
{% 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">
121
      <a class="nav-link disabled" href="#">Disabled</a>
122
123
124
125
126
    </li>
  </ul>
</nav>
{% endexample %}

127
And because we use classes for our navs, you can avoid the list-based approach entirely if you like.
128
129
130
131
132
133
134

{% 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>
135
    <a class="nav-item nav-link disabled" href="#">Disabled</a>
136
137
138
139
  </div>
</nav>
{% endexample %}

140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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">
155
      <a class="nav-link dropdown-toggle" href="http://example.com" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
156
157
158
159
160
161
162
163
164
165
166
167
        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 %}

168
169
170
171
172
173
### Forms

Place various form controls and components within a navbar with `.form-inline`. Align them with `.float-` utilities as needed.

{% example html %}
<nav class="navbar navbar-light bg-faded">
174
  <form class="form-inline float-left">
175
176
177
178
179
180
181
182
183
184
    <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">
185
  <form class="form-inline float-left">
186
187
188
189
190
191
192
193
194
195
196
197
    <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 %}

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

{% example html %}
<nav class="navbar navbar-light bg-faded">
198
  <form class="form-inline float-left">
199
200
201
202
203
204
    <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 %}

205
206
207
208
209
210
### 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">
211
212
213
  <span class="navbar-text">
    Navbar text with an inline element
  </span>
214
215
216
</nav>
{% endexample %}

217
Using our utility classes, you can change the alignment and appearance of your navbar text.
218
219
220

{% example html %}
<nav class="navbar navbar-light bg-faded">
221
  <span class="navbar-text float-right text-muted">
222
223
    Muted navbar text that's floated right
  </span>
224
225
226
</nav>
{% endexample %}

227
228
229
230
231
Similarly, you can use utility classes to align navbar text to other navbar elements like the brand and navigation (which are automatically floated already).

{% example html %}
<nav class="navbar navbar-light bg-faded">
  <a class="navbar-brand" href="#">Navbar</a>
232
  <span class="navbar-text float-left">
233
234
235
236
    Navbar text that's floated left
  </span>
</nav>
{% endexample %}
237

238
## Color schemes
Mark Otto's avatar
Mark Otto committed
239

240
Theming the navbar has never been easier thanks to the combination of a simple link color modifier class and `background-color` utilities. Put another way, you specify light or dark and apply a background color.
Mark Otto's avatar
Mark Otto committed
241

242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
Here are some examples to show what we mean.

<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>
261
    <form class="form-inline float-right">
262
      <input class="form-control" type="text" placeholder="Search">
263
      <button class="btn btn-outline-info" type="submit">Search</button>
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
    </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>
282
    <form class="form-inline float-right">
283
      <input class="form-control" type="text" placeholder="Search">
284
      <button class="btn btn-outline-secondary" type="submit">Search</button>
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
    </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>
303
    <form class="form-inline float-right">
304
      <input class="form-control" type="text" placeholder="Search">
305
      <button class="btn btn-outline-primary" type="submit">Search</button>
306
307
308
309
310
311
312
    </form>
  </nav>
</div>

{% highlight html %}
<nav class="navbar navbar-dark bg-inverse">
  <!-- Navbar content -->
Mark Otto's avatar
Mark Otto committed
313
</nav>
314
315
316
317
318
319
320
321
322

<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
323

Mark Otto's avatar
Mark Otto committed
324
## Containers
325

326
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).
327

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

Mark Otto's avatar
Mark Otto committed
336
{% example html %}
337
<nav class="navbar navbar-light bg-faded">
Mark Otto's avatar
Mark Otto committed
338
339
340
  <div class="container">
    <a class="navbar-brand" href="#">Navbar</a>
  </div>
341
</nav>
Mark Otto's avatar
Mark Otto committed
342
{% endexample %}
343

Mark Otto's avatar
Mark Otto committed
344
345
## Placement

346
347
348
349
350
351
352
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366

{% 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
367
## Collapsible content
Mark Otto's avatar
Mark Otto committed
368

Mark Otto's avatar
Mark Otto committed
369
Our collapse plugin allows you to use a `<button>` or `<a>` to toggle hidden content.
Mark Otto's avatar
Mark Otto committed
370
371

{% example html %}
372
<nav class="navbar navbar-light bg-faded">
373
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#exCollapsingNavbar" aria-controls="exCollapsingNavbar" aria-expanded="false" aria-label="Toggle navigation"></button>
374
  <div class="collapse" id="exCollapsingNavbar">
375
    <div class="bg-inverse text-muted p-1">
376
377
378
379
      <h4>Collapsed content</h4>
      <span class="text-muted">Toggleable via the navbar brand.</span>
    </div>
  </div>
Mark Otto's avatar
Mark Otto committed
380
</nav>
Mark Otto's avatar
Mark Otto committed
381
{% endexample %}
Mark Otto's avatar
Mark Otto committed
382
383
384
385

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 %}
386
<nav class="navbar navbar-light bg-faded">
Mark Otto's avatar
Mark Otto committed
387
  <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>
388
389
  <div class="collapse navbar-toggleable-md" id="navbarResponsive">
    <a class="navbar-brand" href="#">Navbar</a>
Mark Otto's avatar
Mark Otto committed
390
391
392
393
394
    <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">
395
        <a class="nav-link" href="#">Link</a>
Mark Otto's avatar
Mark Otto committed
396
397
      </li>
      <li class="nav-item">
398
        <a class="nav-link" href="#">Link</a>
Mark Otto's avatar
Mark Otto committed
399
      </li>
400
      <li class="nav-item dropdown">
Mark Otto's avatar
Mark Otto committed
401
402
        <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">
403
404
405
406
          <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
407
408
      </li>
    </ul>
409
410
411
412
    <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
413
414
415
  </div>
</nav>
{% endexample %}