]> git.ipfire.org Git - ipfire.org.git/blame - src/scss/bootstrap-4.0.0-alpha.6/docs/components/navs.md
Introduce autotools
[ipfire.org.git] / src / scss / bootstrap-4.0.0-alpha.6 / docs / components / navs.md
CommitLineData
91e44d91
S
1---
2layout: docs
3title: Navs
4description: Documentation and examples for how to use Bootstrap's included navigation components.
5group: components
6---
7
8Navigation available in Bootstrap share general markup and styles, from the base `.nav` class to the active and disabled states. Swap modifier classes to switch between each style.
9
10## Contents
11
12* Will be replaced with the ToC, excluding the "Contents" header
13{:toc}
14
15## Base nav
16
17The base `.nav` component is built with flexbox and provide a strong foundation for building all types of navigation components. It includes some style overrides (for working with lists), some link padding for larger hit areas, and basic disabled styling. No active states are included in the base nav.
18
19{% example html %}
20<ul class="nav">
21 <li class="nav-item">
22 <a class="nav-link active" href="#">Active</a>
23 </li>
24 <li class="nav-item">
25 <a class="nav-link" href="#">Link</a>
26 </li>
27 <li class="nav-item">
28 <a class="nav-link" href="#">Link</a>
29 </li>
30 <li class="nav-item">
31 <a class="nav-link disabled" href="#">Disabled</a>
32 </li>
33</ul>
34{% endexample %}
35
36Classes are used throughout, so your markup can be super flexible. Use `<ul>`s like above, or roll your own with say a `<nav>` element. Because the `.nav` uses `display: flex`, the nav links behave the same as nav items would, but without the extra markup.
37
38{% example html %}
39<nav class="nav">
40 <a class="nav-link active" href="#">Active</a>
41 <a class="nav-link" href="#">Link</a>
42 <a class="nav-link" href="#">Link</a>
43 <a class="nav-link disabled" href="#">Disabled</a>
44</nav>
45{% endexample %}
46
47## Available styles
48
49Change the style of `.nav`s component with modifiers and utilities. Mix and match as needed, or build your own.
50
51### Horizontal alignment
52
53Change the horizontal alignment of your nav with [flexbox utilities]({{ site.baseurl }}/layout/grid/#horizontal-alignment). By default, navs are left-aligned, but you can easily change them to center or right aligned.
54
55Centered with `.justify-content-center`:
56
57{% example html %}
58<ul class="nav justify-content-center">
59 <li class="nav-item">
60 <a class="nav-link active" href="#">Active</a>
61 </li>
62 <li class="nav-item">
63 <a class="nav-link" href="#">Link</a>
64 </li>
65 <li class="nav-item">
66 <a class="nav-link" href="#">Link</a>
67 </li>
68 <li class="nav-item">
69 <a class="nav-link disabled" href="#">Disabled</a>
70 </li>
71</ul>
72{% endexample %}
73
74Right-aligned with `.justify-content-end`:
75
76{% example html %}
77<ul class="nav justify-content-end">
78 <li class="nav-item">
79 <a class="nav-link active" href="#">Active</a>
80 </li>
81 <li class="nav-item">
82 <a class="nav-link" href="#">Link</a>
83 </li>
84 <li class="nav-item">
85 <a class="nav-link" href="#">Link</a>
86 </li>
87 <li class="nav-item">
88 <a class="nav-link disabled" href="#">Disabled</a>
89 </li>
90</ul>
91{% endexample %}
92
93### Vertical
94
95Stack your navigation by changing the flex item direction with the `.flex-column` utility. Need to stack them on some viewports but not others? Use the responsive versions (e.g., `.flex-sm-column`).
96
97{% example html %}
98<ul class="nav flex-column">
99 <li class="nav-item">
100 <a class="nav-link active" href="#">Active</a>
101 </li>
102 <li class="nav-item">
103 <a class="nav-link" href="#">Link</a>
104 </li>
105 <li class="nav-item">
106 <a class="nav-link" href="#">Link</a>
107 </li>
108 <li class="nav-item">
109 <a class="nav-link disabled" href="#">Disabled</a>
110 </li>
111</ul>
112{% endexample %}
113
114As always, vertical navigation is possible without `<ul>`s, too.
115
116{% example html %}
117<nav class="nav flex-column">
118 <a class="nav-link active" href="#">Active</a>
119 <a class="nav-link" href="#">Link</a>
120 <a class="nav-link" href="#">Link</a>
121 <a class="nav-link disabled" href="#">Disabled</a>
122</nav>
123{% endexample %}
124
125### Tabs
126
127Takes the basic nav from above and adds the `.nav-tabs` class to generate a tabbed interface. Use them to create tabbable regions with our [tab JavaScript plugin](#javascript-behavior).
128
129{% example html %}
130<ul class="nav nav-tabs">
131 <li class="nav-item">
132 <a class="nav-link active" href="#">Active</a>
133 </li>
134 <li class="nav-item">
135 <a class="nav-link" href="#">Link</a>
136 </li>
137 <li class="nav-item">
138 <a class="nav-link" href="#">Link</a>
139 </li>
140 <li class="nav-item">
141 <a class="nav-link disabled" href="#">Disabled</a>
142 </li>
143</ul>
144{% endexample %}
145
146### Pills
147
148Take that same HTML, but use `.nav-pills` instead:
149
150{% example html %}
151<ul class="nav nav-pills">
152 <li class="nav-item">
153 <a class="nav-link active" href="#">Active</a>
154 </li>
155 <li class="nav-item">
156 <a class="nav-link" href="#">Link</a>
157 </li>
158 <li class="nav-item">
159 <a class="nav-link" href="#">Link</a>
160 </li>
161 <li class="nav-item">
162 <a class="nav-link disabled" href="#">Disabled</a>
163 </li>
164</ul>
165{% endexample %}
166
167### Fill and justify
168
169Force your `.nav`'s contents to extend the full available width one of two modifier classes. To proportionately fill all available space with your `.nav-item`s, use `.nav-fill`. Notice that all horizontal space is occupied, but not every nav item has the same width.
170
171{% example html %}
172<ul class="nav nav-pills nav-fill">
173 <li class="nav-item">
174 <a class="nav-link active" href="#">Active</a>
175 </li>
176 <li class="nav-item">
177 <a class="nav-link" href="#">Longer nav link</a>
178 </li>
179 <li class="nav-item">
180 <a class="nav-link" href="#">Link</a>
181 </li>
182 <li class="nav-item">
183 <a class="nav-link disabled" href="#">Disabled</a>
184 </li>
185</ul>
186{% endexample %}
187
188For equal-width elements, use `.nav-justified`. All horizontal space will be occupied by nav links, but unlike the `.nav-fill` above, every nav item will be the same width.
189
190{% example html %}
191<ul class="nav nav-pills nav-justified">
192 <li class="nav-item">
193 <a class="nav-link active" href="#">Active</a>
194 </li>
195 <li class="nav-item">
196 <a class="nav-link" href="#">Longer nav link</a>
197 </li>
198 <li class="nav-item">
199 <a class="nav-link" href="#">Link</a>
200 </li>
201 <li class="nav-item">
202 <a class="nav-link disabled" href="#">Disabled</a>
203 </li>
204</ul>
205{% endexample %}
206
207## Working with flex utilities
208
209If you need responsive nav variations, consider using a series of [flexbox utilities]({{ site.baseurl }}/utilities/flexbox/). While more verbose, these utilities offer greater customization across responsive breakpoints. In the example below, our nav will be stacked on the lowest breakpoint, then adapt to a horizontal layout that fills the available width starting from the small breakpoint.
210
211{% example html %}
212<nav class="nav nav-pills flex-column flex-sm-row">
213 <a class="flex-sm-fill text-sm-center nav-link active" href="#">Active</a>
214 <a class="flex-sm-fill text-sm-center nav-link" href="#">Link</a>
215 <a class="flex-sm-fill text-sm-center nav-link" href="#">Link</a>
216 <a class="flex-sm-fill text-sm-center nav-link disabled" href="#">Disabled</a>
217</nav>
218{% endexample %}
219
220## Regarding accessibility
221
222If you're using navs to provide a navigation bar, be sure to add a `role="navigation"` to the most logical parent container of the `<ul>`, or wrap a `<nav>` element around the whole navigation. Do not add the role to the `<ul>` itself, as this would prevent it from being announced as an actual list by assistive technologies.
223
224## Using dropdowns
225
226Add dropdown menus with a little extra HTML and the [dropdowns JavaScript plugin]({{ site.baseurl }}/components/dropdowns/#usage).
227
228### Tabs with dropdowns
229
230{% example html %}
231<ul class="nav nav-tabs">
232 <li class="nav-item">
233 <a class="nav-link active" href="#">Active</a>
234 </li>
235 <li class="nav-item dropdown">
236 <a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">Dropdown</a>
237 <div class="dropdown-menu">
238 <a class="dropdown-item" href="#">Action</a>
239 <a class="dropdown-item" href="#">Another action</a>
240 <a class="dropdown-item" href="#">Something else here</a>
241 <div class="dropdown-divider"></div>
242 <a class="dropdown-item" href="#">Separated link</a>
243 </div>
244 </li>
245 <li class="nav-item">
246 <a class="nav-link" href="#">Link</a>
247 </li>
248 <li class="nav-item">
249 <a class="nav-link disabled" href="#">Disabled</a>
250 </li>
251</ul>
252{% endexample %}
253
254### Pills with dropdowns
255
256{% example html %}
257<ul class="nav nav-pills">
258 <li class="nav-item">
259 <a class="nav-link active" href="#">Active</a>
260 </li>
261 <li class="nav-item dropdown">
262 <a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">Dropdown</a>
263 <div class="dropdown-menu">
264 <a class="dropdown-item" href="#">Action</a>
265 <a class="dropdown-item" href="#">Another action</a>
266 <a class="dropdown-item" href="#">Something else here</a>
267 <div class="dropdown-divider"></div>
268 <a class="dropdown-item" href="#">Separated link</a>
269 </div>
270 </li>
271 <li class="nav-item">
272 <a class="nav-link" href="#">Link</a>
273 </li>
274 <li class="nav-item">
275 <a class="nav-link disabled" href="#">Disabled</a>
276 </li>
277</ul>
278{% endexample %}
279
280## JavaScript behavior
281
282Use the tab JavaScript plugin—include it individually or through the compiled `bootstrap.js` file—to extend our navigational tabs and pills to create tabbable panes of local content, even via dropdown menus.
283
284<div class="bd-example bd-example-tabs" role="tabpanel">
285 <ul class="nav nav-tabs" id="myTab" role="tablist">
286 <li class="nav-item">
287 <a class="nav-link active" id="home-tab" data-toggle="tab" href="#home" role="tab" aria-controls="home" aria-expanded="true">Home</a>
288 </li>
289 <li class="nav-item">
290 <a class="nav-link" id="profile-tab" data-toggle="tab" href="#profile" role="tab" aria-controls="profile">Profile</a>
291 </li>
292 <li class="nav-item dropdown">
293 <a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">
294 Dropdown
295 </a>
296 <div class="dropdown-menu">
297 <a class="dropdown-item" id="dropdown1-tab" href="#dropdown1" role="tab" data-toggle="tab" aria-controls="dropdown1">@fat</a>
298 <a class="dropdown-item" id="dropdown2-tab" href="#dropdown2" role="tab" data-toggle="tab" aria-controls="dropdown2">@mdo</a>
299 </div>
300 </li>
301 </ul>
302 <div class="tab-content" id="myTabContent">
303 <div role="tabpanel" class="tab-pane fade show active" id="home" aria-labelledBy="home-tab">
304 <p>Raw denim you probably haven't heard of them jean shorts Austin. Nesciunt tofu stumptown aliqua, retro synth master cleanse. Mustache cliche tempor, williamsburg carles vegan helvetica. Reprehenderit butcher retro keffiyeh dreamcatcher synth. Cosby sweater eu banh mi, qui irure terry richardson ex squid. Aliquip placeat salvia cillum iphone. Seitan aliquip quis cardigan american apparel, butcher voluptate nisi qui.</p>
305 </div>
306 <div class="tab-pane fade" id="profile" role="tabpanel" aria-labelledBy="profile-tab">
307 <p>Food truck fixie locavore, accusamus mcsweeney's marfa nulla single-origin coffee squid. Exercitation +1 labore velit, blog sartorial PBR leggings next level wes anderson artisan four loko farm-to-table craft beer twee. Qui photo booth letterpress, commodo enim craft beer mlkshk aliquip jean shorts ullamco ad vinyl cillum PBR. Homo nostrud organic, assumenda labore aesthetic magna delectus mollit. Keytar helvetica VHS salvia yr, vero magna velit sapiente labore stumptown. Vegan fanny pack odio cillum wes anderson 8-bit, sustainable jean shorts beard ut DIY ethical culpa terry richardson biodiesel. Art party scenester stumptown, tumblr butcher vero sint qui sapiente accusamus tattooed echo park.</p>
308 </div>
309 <div class="tab-pane fade" id="dropdown1" role="tabpanel" aria-labelledBy="dropdown1-tab">
310 <p>Etsy mixtape wayfarers, ethical wes anderson tofu before they sold out mcsweeney's organic lomo retro fanny pack lo-fi farm-to-table readymade. Messenger bag gentrify pitchfork tattooed craft beer, iphone skateboard locavore carles etsy salvia banksy hoodie helvetica. DIY synth PBR banksy irony. Leggings gentrify squid 8-bit cred pitchfork. Williamsburg banh mi whatever gluten-free, carles pitchfork biodiesel fixie etsy retro mlkshk vice blog. Scenester cred you probably haven't heard of them, vinyl craft beer blog stumptown. Pitchfork sustainable tofu synth chambray yr.</p>
311 </div>
312 <div class="tab-pane fade" id="dropdown2" role="tabpanel" aria-labelledBy="dropdown2-tab">
313 <p>Trust fund seitan letterpress, keytar raw denim keffiyeh etsy art party before they sold out master cleanse gluten-free squid scenester freegan cosby sweater. Fanny pack portland seitan DIY, art party locavore wolf cliche high life echo park Austin. Cred vinyl keffiyeh DIY salvia PBR, banh mi before they sold out farm-to-table VHS viral locavore cosby sweater. Lomo wolf viral, mustache readymade thundercats keffiyeh craft beer marfa ethical. Wolf salvia freegan, sartorial keffiyeh echo park vegan.</p>
314 </div>
315 </div>
316</div>
317
318### Using data attributes
319
320You can activate a tab or pill navigation without writing any JavaScript by simply specifying `data-toggle="tab"` or `data-toggle="pill"` on an element. Use these data attributes on `.nav-tabs` or `.nav-pills`.
321
322<div role="tabpanel">
323{% highlight html %}
324<!-- Nav tabs -->
325<ul class="nav nav-tabs" role="tablist">
326 <li class="nav-item">
327 <a class="nav-link active" data-toggle="tab" href="#home" role="tab">Home</a>
328 </li>
329 <li class="nav-item">
330 <a class="nav-link" data-toggle="tab" href="#profile" role="tab">Profile</a>
331 </li>
332 <li class="nav-item">
333 <a class="nav-link" data-toggle="tab" href="#messages" role="tab">Messages</a>
334 </li>
335 <li class="nav-item">
336 <a class="nav-link" data-toggle="tab" href="#settings" role="tab">Settings</a>
337 </li>
338</ul>
339
340<!-- Tab panes -->
341<div class="tab-content">
342 <div class="tab-pane active" id="home" role="tabpanel">...</div>
343 <div class="tab-pane" id="profile" role="tabpanel">...</div>
344 <div class="tab-pane" id="messages" role="tabpanel">...</div>
345 <div class="tab-pane" id="settings" role="tabpanel">...</div>
346</div>
347{% endhighlight %}
348</div>
349
350### Via JavaScript
351
352Enable tabbable tabs via JavaScript (each tab needs to be activated individually):
353
354{% highlight js %}
355$('#myTab a').click(function (e) {
356 e.preventDefault()
357 $(this).tab('show')
358})
359{% endhighlight %}
360
361You can activate individual tabs in several ways:
362
363{% highlight js %}
364$('#myTab a[href="#profile"]').tab('show') // Select tab by name
365$('#myTab a:first').tab('show') // Select first tab
366$('#myTab a:last').tab('show') // Select last tab
367$('#myTab li:eq(2) a').tab('show') // Select third tab (0-indexed)
368{% endhighlight %}
369
370### Fade effect
371
372To make tabs fade in, add `.fade` to each `.tab-pane`. The first tab pane must also have `.show` to make the initial content visible.
373
374{% highlight html %}
375<div class="tab-content">
376 <div class="tab-pane fade show active" id="home" role="tabpanel">...</div>
377 <div class="tab-pane fade" id="profile" role="tabpanel">...</div>
378 <div class="tab-pane fade" id="messages" role="tabpanel">...</div>
379 <div class="tab-pane fade" id="settings" role="tabpanel">...</div>
380</div>
381{% endhighlight %}
382
383### Methods
384
385#### $().tab
386
387Activates a tab element and content container. Tab should have either a `data-target` or an `href` targeting a container node in the DOM.
388
389{% highlight html %}
390<ul class="nav nav-tabs" id="myTab" role="tablist">
391 <li class="nav-item">
392 <a class="nav-link active" data-toggle="tab" href="#home" role="tab" aria-controls="home">Home</a>
393 </li>
394 <li class="nav-item">
395 <a class="nav-link" data-toggle="tab" href="#profile" role="tab" aria-controls="profile">Profile</a>
396 </li>
397 <li class="nav-item">
398 <a class="nav-link" data-toggle="tab" href="#messages" role="tab" aria-controls="messages">Messages</a>
399 </li>
400 <li class="nav-item">
401 <a class="nav-link" data-toggle="tab" href="#settings" role="tab" aria-controls="settings">Settings</a>
402 </li>
403</ul>
404
405<div class="tab-content">
406 <div class="tab-pane active" id="home" role="tabpanel">...</div>
407 <div class="tab-pane" id="profile" role="tabpanel">...</div>
408 <div class="tab-pane" id="messages" role="tabpanel">...</div>
409 <div class="tab-pane" id="settings" role="tabpanel">...</div>
410</div>
411
412<script>
413 $(function () {
414 $('#myTab a:last').tab('show')
415 })
416</script>
417{% endhighlight %}
418
419#### .tab('show')
420
421Selects the given tab and shows its associated pane. Any other tab that was previously selected becomes unselected and its associated pane is hidden. **Returns to the caller before the tab pane has actually been shown** (i.e. before the `shown.bs.tab` event occurs).
422
423{% highlight js %}
424$('#someTab').tab('show')
425{% endhighlight %}
426
427### Events
428
429When showing a new tab, the events fire in the following order:
430
4311. `hide.bs.tab` (on the current active tab)
4322. `show.bs.tab` (on the to-be-shown tab)
4333. `hidden.bs.tab` (on the previous active tab, the same one as for the `hide.bs.tab` event)
4344. `shown.bs.tab` (on the newly-active just-shown tab, the same one as for the `show.bs.tab` event)
435
436If no tab was already active, then the `hide.bs.tab` and `hidden.bs.tab` events will not be fired.
437
438<table class="table table-bordered table-striped table-responsive">
439 <thead>
440 <tr>
441 <th style="width: 150px;">Event Type</th>
442 <th>Description</th>
443 </tr>
444 </thead>
445 <tbody>
446 <tr>
447 <td>show.bs.tab</td>
448 <td>This event fires on tab show, but before the new tab has been shown. Use <code>event.target</code> and <code>event.relatedTarget</code> to target the active tab and the previous active tab (if available) respectively.</td>
449 </tr>
450 <tr>
451 <td>shown.bs.tab</td>
452 <td>This event fires on tab show after a tab has been shown. Use <code>event.target</code> and <code>event.relatedTarget</code> to target the active tab and the previous active tab (if available) respectively.</td>
453 </tr>
454 <tr>
455 <td>hide.bs.tab</td>
456 <td>This event fires when a new tab is to be shown (and thus the previous active tab is to be hidden). Use <code>event.target</code> and <code>event.relatedTarget</code> to target the current active tab and the new soon-to-be-active tab, respectively.</td>
457 </tr>
458 <tr>
459 <td>hidden.bs.tab</td>
460 <td>This event fires after a new tab is shown (and thus the previous active tab is hidden). Use <code>event.target</code> and <code>event.relatedTarget</code> to target the previous active tab and the new active tab, respectively.</td>
461 </tr>
462 </tbody>
463</table>
464
465{% highlight js %}
466$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
467 e.target // newly activated tab
468 e.relatedTarget // previous active tab
469})
470{% endhighlight %}