Home >Web Front-end >CSS Tutorial >How Do I Center Navbar Content in Bootstrap 3 Using CSS?

How Do I Center Navbar Content in Bootstrap 3 Using CSS?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-29 19:58:15274browse

How Do I Center Navbar Content in Bootstrap 3 Using CSS?

CSS to Center Navbar Content

Struggling to center content within Bootstrap's navbar? Don't give up! Let's troubleshoot using a provided HTML and CSS code snippet.

Problem:

Using Bootstrap 3, but existing methods don't align content centrally within the navbar.

HTML Code:

Provided for reference (but not included here).

Revised CSS:

.navbar .navbar-nav {
  display: inline-block;
  float: none;
  vertical-align: top;
}

.navbar .navbar-collapse {
  text-align: center;
}

Explanation:

  • Removed float: left from .navbar .navbar-nav to center the inner navigation.
  • Set display: inline-block to maintain horizontal alignment.
  • Adjusted vertical-align: top for proper alignment.
  • Included .navbar .navbar-collapse to align the inner content of collapsed navbars.

Improved Snippet:

<div class="navbar navbar-default" role="navigation">
  <!-- ... -->

  <div class="collapse navbar-collapse navbar-ex1-collapse">
    <ul class="nav navbar-nav">
      <!-- ... -->
    </ul>
  </div>

  <!-- ... -->
</div>

Responsive Adjustments:

To restrict the centering effect to uncollapsed navbars, add this media query:

@media (min-width: 768px) {
  .navbar .navbar-nav {
    display: inline-block;
    float: none;
    vertical-align: top;
  }

  .navbar .navbar-collapse {
    text-align: center;
  }
}

Now, your navbar content will be effortlessly centered both in collapsed and uncollapsed states.

The above is the detailed content of How Do I Center Navbar Content in Bootstrap 3 Using CSS?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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