- The tabs in Bootstrap can be created by using the data attribute data-toggle=”tab”. You also need to use the role=”tab” attribute.
- Sometimes, you may need to disable a specific tab as using the Bootstrap framework, for example in the checkout process.
- For that, simply remove the data-toggle=”tab” attribute from the <a> tag inside the <li> tag.
- For symbolically showing it as disabled, you may add the disabled CSS class in the <li> tag containing that tab.
- See the demo and code in the following examples.
A demo of disabling a tab
In this example, four tabs are created as follows:
- Home
- Java
- C#
- MySQL
- jQuery
The Java tab is disabled as follows:
The Code
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" integrity="sha512-dTfge/zgoMYpP7QbHy4gWMEGsbsdZeCXz7irItjcC3sPUFtf0kuFbDz/ixG7ArTxmDjLXDmezHubeNikyKGVyQ==" crossorigin="anonymous"> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script> </head> <body> <div class="container"> <h3>A demo of disabling Bootstrap Tab</h3> <!-- Nav tabs --> <ul class="nav nav-tabs" role="tablist"> <li class="active"><a href="#hometab" role="tab" data-toggle="tab">Home</a></li> <li class="disabled"><a href="#javatab">Java</a></li> <li><a href="#csharptab" role="tab" data-toggle="tab">C#</a></li> <li><a href="#mysqltab" role="tab" data-toggle="tab">MySQL</a></li> <li><a href="#jquerytab" role="tab" data-toggle="tab">jQuery</a></li> </ul> </li> <!-- Tab panes --> <div class="tab-content"> <div class="tab-pane active" id="hometab">This is enabled tab</div> <div class="tab-pane" id="javatab">The Java tab is disabled by removing the data attribute</div> <div class="tab-pane" id="csharptab">The C# tab</div> <div class="tab-pane" id="mysqltab">The MySQL tab</div> <div class="tab-pane" id="jquerytab">The content for jQuery tab</div> </div> </body> </html>
Another example with custom CSS
In this example, the C# tab is disabled as creating the Bootstrap tabs. The second tab is active as the demo page loaded:
The markup for this example:
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src="https://code.jquery.com/jquery-1.10.1.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" integrity="sha512-dTfge/zgoMYpP7QbHy4gWMEGsbsdZeCXz7irItjcC3sPUFtf0kuFbDz/ixG7ArTxmDjLXDmezHubeNikyKGVyQ==" crossorigin="anonymous"> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script> <style> .nav-tabs { border-color:#1A3E5E; width:60%; } .nav-tabs > li a { border: 1px solid #1A3E5E; background-color:#C1C1FF; color:#fff; } .nav-tabs > li.active > a, .nav-tabs > li.active > a:focus, .nav-tabs > li.active > a:hover{ background-color:#D6E6F3; color:#000; border: 1px solid #1A3E5E; border-bottom-color: transparent; } .nav-tabs > li > a:hover{ background-color: #D6E6F3 !important; border-radius: 5px; color:#000; } .tab-pane { border:solid 1px #1A3E5E; border-top: 0; width:60%; background-color:#00006F; padding:5px; color: #fff; } </style> </head> <body> <div class="container"> <h3>A demo of Bootstrap Tabs</h3> <!-- Nav tabs --> <ul class="nav nav-tabs" role="tablist"> <li><a href="#hometab" role="tab" data-toggle="tab">Home</a></li> <li class="active"><a href="#javatab" role="tab" data-toggle="tab">Java</a></li> <li class="disabled"><a href="#csharptab">C#</a></li> <li><a href="#mysqltab" role="tab" data-toggle="tab">MySQL</a></li> <li><a href="#jquerytab" role="tab" data-toggle="tab">jQuery</a></li> </ul> </li> <!-- Tab panes --> <div class="tab-content"> <div class="tab-pane" id="hometab">Write something for home tab</div> <div class="tab-pane active" id="javatab">The Java is an object-oriented programming language that was developed by James Gosling from the Sun Microsystems in 1995. <ul> <li>Java Array</li> <li>Java ArrayList</li> <li>Java Set</li> <li>Java enum</li> </ul> </div> <div class="tab-pane" id="csharptab">The C# tab is disabled.</div> <div class="tab-pane" id="mysqltab">MySQL is enabled.</div> <div class="tab-pane" id="jquerytab">jQuery is enabled.</div> </div> </body> </html>
You can see that the C# tab is disabled by removing the data-toggle=”tab” data attribute.
The disabled class is added in the <li> tag. You can see the complete code including custom CSS overriding the default classes above.