Tabs
Materialize is a modern responsive CSS framework based on Material Design by Google.
The tabs structure consists of an unordered list of tabs that have hashes corresponding to tab ids. Then when you click on each tab, only the container with the
corresponding tab id will become visible. You can add the class
.disabled to make a tab inaccessible.
Flight
Luggage
Explore
Tabs automatically become scrollable when there are too many to fit on screen
Test 1
Test 2
Test 3
Test 4
Test 5
Test 6
Test 7
Test 8
Test 9
Test 10
Test 11
<div class="row">
<div class="col s12">
<ul class="tabs">
<li class="tab"><a href="#test1">Test 1</a></li>
<li class="tab"><a class="active" href="#test2">Test 2</a></li>
<li class="tab disabled"><a href="#test3">Disabled Tab</a></li>
<li class="tab"><a href="#test4">Test 4</a></li>
</ul>
</div>
<div id="test1" class="col s12">Test 1</div>
<div id="test2" class="col s12">Test 2</div>
<div id="test3" class="col s12">Test 3</div>
<div id="test4" class="col s12">Test 4</div>
</div>Initialization
const instance = Tabs.init(el, options);
Options
| Name | Type | Default | Description |
|---|---|---|---|
| duration | Number | 300 | Transition duration in milliseconds. |
| onShow | Function | null | Callback for when a new tab content is shown. |
| swipeable | Boolean | false | Set to true to enable swipeable tabs. This also uses the responsiveThreshold option. |
| responsiveThreshold | Number | Infinity | The maximum width of the screen, in pixels, where the swipeable functionality initializes. |
Methods
All the methods are called on the plugin instance. You can get the plugin instance like this:
const instance = Tabs.getInstance(elem);
.select();
Show tab content that corresponds to the tab with the id
Arguments
String: The id of the tab that you want to switch to
instance.select('tab_id');.updateTabIndicator();
Recalculate tab indicator position. This is useful when the indicator position is not correct.
instance.updateTabIndicator();.destroy();
Destroy plugin instance and teardown
instance.destroy();Properties
| Name | Type | Description |
|---|---|---|
| el | Element | The DOM element the plugin was initialized with. |
| options | Object | The options the instance was initialized with. |
| index | Number | The index of tab that is currently shown. |
Preselecting a tab
By default, the first tab is selected. But if this is not what you want, you can preselect a tab by either passing in the hash in the url ex:#test2. Or you can add the class active to the a tag.
<li class="tab"><a class="active" href="#test3">Test 3</a></li>
Linking to an External Page
By default, Materialize tabs will ignore their default anchor behaviour. To force a tab to behave as a regular hyperlink, just specify the
target property of that link! A list of target values may be
found here.
<li class="tab">
<a target="_blank" href="https://github.com/kekefreedog/kmaterialize">External link in new window</a>
</li>
<li class="tab">
<a target="_self" href="https://github.com/kekefreedog/kmaterialize">External link in same window</a>
</li>Swipeable Tabs
By setting the
swipeable option to true, you can enable tabs where you can swipe on touch enabled
devices to switch tabs. Make sure you keep the tab content divs in the same wrapping container. You can also set the
responsiveThreshold
option to a screen width in pixels where the swipeable functionality will activate.
Note: This is also touch compatible! Try swiping with your finger to scroll through the carousel.
<ul id="tabs-swipe-demo" class="tabs">
<li class="tab"><a href="#test-swipe-1">Test 1</a></li>
<li class="tab"><a class="active" href="#test-swipe-2">Test 2</a></li>
<li class="tab"><a href="#test-swipe-3">Test 3</a></li>
</ul>
<div id="test-swipe-1" class="blue">Test 1</div>
<div id="test-swipe-2" class="red">Test 2</div>
<div id="test-swipe-3" class="green">Test 3</div>Fixed Width Tabs
Add the
.tabs-fixed-width class to the tabs container
Test 1
Test 2
Test 3
Test 4
Test 5
<ul class="tabs tabs-fixed-width tab-demo z-depth-1">
<li class="tab"><a href="#test1">Test 1</a></li>
<li class="tab"><a class="active" href="#test2">Test 2</a></li>
<li class="tab disabled"><a href="#test3">Disabled Tab</a></li>
<li class="tab"><a href="#test4">Test 4</a></li>
<li class="tab"><a href="#test0">Test 5</a></li>
</ul>
<div id="test1"><p>Test 1</p></div>
<div id="test2"><p>Test 2</p></div>
<div id="test3"><p>Test 3</p></div>
<div id="test4"><p>Test 4</p></div>
<div id="test0"><p>Test 5</p></div>Fill a container
Let the tabs fill the width and height of a parent div. The selected panel fills the remaining space and scrolls independently, keeping the navigation visible.
Your workspace
This panel fills the available space inside a 400px-high div. Change the navigation position above to move the tabs to the bottom.
People & teams
Design, engineering, and production share this space.
- Alex Morgan — Creative director
- Sam Rivera — Producer
- Jules Chen — Design lead
- Maya Patel — Product designer
- Noah Kim — Engineering lead
- Léa Martin — Frontend developer
- Robin Park — Quality assurance
- Avery Lee — Technical artist
Long content scrolls inside this panel while the navigation stays in place.
Room for your content
Use this layout for a dashboard, an editor, or a card with several views.
The tabs-fill layout is included in kmaterialize. Give the parent a definite height (or an allocated flex height). Set data-tab-position="bottom" to place the navigation below the panels. The attribute controls its visual position; placing the list after the panels also keeps the reading and keyboard focus order consistent.
<div style="height: 400px">
<div class="tabs-fill" data-tab-position="top">
<ul class="tabs tabs-fixed-width">
<li class="tab"><a href="#first-view">First</a></li>
<li class="tab"><a href="#second-view">Second</a></li>
</ul>
<div id="first-view" class="tabs-fill-panel">First view</div>
<div id="second-view" class="tabs-fill-panel">Second view</div>
</div>
</div>const layout = document.querySelector('.tabs-fill');
const navigation = layout.querySelector('.tabs');
Tabs.init(navigation);
function setTabPosition(position) {
layout.dataset.tabPosition = position;
if (position === 'bottom') layout.append(navigation);
else layout.prepend(navigation);
}
setTabPosition('bottom'); // Or 'top'.Icons
By default icons appear vertically centered. To diplay icons horizontally add .tabs-horizontal
class to the tabs container to indicate text position.
<ul class="tabs tabs-fixed-width tab-demo tabs-horizontal z-depth-1">
<li class="tab"><a href="#image-test1"><i class="material-icons">flight</i>Flight</a></li>
<li class="tab"><a class="active" href="#image-test2"><i class="material-icons">luggage</i>Luggage</a></li>
<li class="tab"><a href="#image-test3"><i class="material-icons">explore</i>Explore</a></li>
</ul>
<div id="image-test1"><p>Flight</p></div>
<div id="image-test2"><p>Luggage</p></div>
<div id="image-test3"><p>Explore</p></div>