Table of Contents
Modify Twitter Bootstrap Navbar: Centering Navigation Links
Problem: Centering Navigation Links
Solution: Inline-Block Layout and Text Alignment
Targeting Specific Navbar Instances
Aligning Submenu Items
Demo
Home Web Front-end CSS Tutorial How to Center Twitter Bootstrap Navbar Navigation Links?

How to Center Twitter Bootstrap Navbar Navigation Links?

Nov 01, 2024 am 10:53 AM

How to Center Twitter Bootstrap Navbar Navigation Links?

The Twitter Bootstrap navbar offers a practical solution for website navigation. However, sometimes you may want to customize its appearance, such as centering the navigation links.

Modifying the navbar's alignment requires some CSS code. However, some methods may not yield the desired result. Let's explore the correct approach.

Solution: Inline-Block Layout and Text Alignment

To center nav menu items, set their display property to inline-block instead of float: left. This will allow the items to be aligned horizontally without breaking the layout. Additionally, set the text-align property of the navbar's inner container to center.

<code class="css">.navbar .nav,
.navbar .nav &gt; li {
    float: none;
    display: inline-block;
    *display: inline; /* ie7 fix */
    *zoom: 1; /* hasLayout ie7 trigger */
    vertical-align: top;
}

.navbar-inner {
    text-align: center;
}</code>
Copy after login

Targeting Specific Navbar Instances

If you have multiple navbars on your web page, it's recommended to create a dedicated CSS class to target the specific navbar you want to center. This will prevent unwanted effects on other navbars.

<code class="html">&lt;div class=&quot;navbar navbar-fixed-top center&quot;&gt;
    &lt;div class=&quot;navbar-inner&quot;&gt;
        ....
    &lt;/div&gt;
&lt;/div&gt;</code>
Copy after login
<code class="css">.center.navbar .nav,
.center.navbar .nav &gt; li {
    float: none;
    display: inline-block;
    *display: inline; /* ie7 fix */
    *zoom: 1; /* hasLayout ie7 trigger */
    vertical-align: top;
}

.center .navbar-inner {
    text-align: center;
}</code>
Copy after login

Aligning Submenu Items

It's worth noting that the above CSS will also affect submenu items, causing them to be centered as well. If you prefer to have submenu items aligned to the left, add the following style:

<code class="css">.center .dropdown-menu {
    text-align: left;
}</code>
Copy after login

Demo

See a live demo at: http://jsfiddle.net/C7LWm/show/

The above is the detailed content of How to Center Twitter Bootstrap Navbar Navigation Links?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Adding Box Shadows to WordPress Blocks and Elements Adding Box Shadows to WordPress Blocks and Elements Mar 09, 2025 pm 12:53 PM

Adding Box Shadows to WordPress Blocks and Elements

Create a JavaScript Contact Form With the Smart Forms Framework Create a JavaScript Contact Form With the Smart Forms Framework Mar 07, 2025 am 11:33 AM

Create a JavaScript Contact Form With the Smart Forms Framework

Create an Inline Text Editor With the contentEditable Attribute Create an Inline Text Editor With the contentEditable Attribute Mar 02, 2025 am 09:03 AM

Create an Inline Text Editor With the contentEditable Attribute

Making Your First Custom Svelte Transition Making Your First Custom Svelte Transition Mar 15, 2025 am 11:08 AM

Making Your First Custom Svelte Transition

Comparing the 5 Best PHP Form Builders (And 3 Free Scripts) Comparing the 5 Best PHP Form Builders (And 3 Free Scripts) Mar 04, 2025 am 10:22 AM

Comparing the 5 Best PHP Form Builders (And 3 Free Scripts)

File Upload With Multer in Node.js and Express File Upload With Multer in Node.js and Express Mar 02, 2025 am 09:15 AM

File Upload With Multer in Node.js and Express

Working With GraphQL Caching Working With GraphQL Caching Mar 19, 2025 am 09:36 AM

Working With GraphQL Caching

Best CSS Animations and Effects on CodeCanyon 2025 (Paid   Free) Best CSS Animations and Effects on CodeCanyon 2025 (Paid Free) Mar 01, 2025 am 09:32 AM

Best CSS Animations and Effects on CodeCanyon 2025 (Paid Free)

See all articles