Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I center the twitter bootstrap tabs on the page?

I'm using twitter bootstrap to create togglable tabs. Here is the css I'm using:

<div class="container">   <ul class="nav nav-tabs">     <li><a href="#home" data-toggle="tab">Home</a></li>     <li><a href="#profile" data-toggle="tab">Profile</a></li>     <li><a href="#messages" data-toggle="tab">Messages</a></li>     <li><a href="#settings" data-toggle="tab">Settings</a></li>   </ul> </div> 

This html displays the tabs correctly, but pulled to the left (which is what is supposed to happen). I've tried tinkering with the CSS to get the tabs to center on the page, but no luck yet. I thought someone with more css experience would know how to do this.

As a note, there is another question about centering nav pills, which I tried, but it didn't work with just plain nav tabs.

Thanks.

like image 750
mofeeta Avatar asked Feb 23 '12 21:02

mofeeta


People also ask

How do I center a tab in bootstrap?

Use the . nav-justified class in Bootstrap to center tabs in Bootstrap.

How do I center align a tab?

Center tabs inhabit one-line paragraphs. Click the Tab gizmo on the ruler until the center tab stop shows up. The Center Tab Stop icon is shown in the margin. Click the ruler to set the center tab stop's position.


1 Answers

nav-tabs list items are automatically floated to the left by the bootstrap so you have to reset that and instead display them as inline-block, and to center menu items we have to add the attribute of text-align:center to the container, like so:

CSS

.nav-tabs > li, .nav-pills > li {     float:none;     display:inline-block;     *display:inline; /* ie7 fix */      zoom:1; /* hasLayout ie7 trigger */ }  .nav-tabs, .nav-pills {     text-align:center; } 

Demo: http://jsfiddle.net/U8HGz/2/show/ Edit here: http://jsfiddle.net/U8HGz/2/


Edit: patched version that works in IE7+ provided by user @My Head Hurts down in the comments below.

Demo: http://jsfiddle.net/U8HGz/3/show/ Edit here: http://jsfiddle.net/U8HGz/3/

like image 193
Andres Ilich Avatar answered Nov 07 '22 04:11

Andres Ilich