What are the methods to center text in a navigation bar?

Struggling to center align the text on my webpage, I've encountered an issue where the text starts drifting to the right inside its container after each menu item selection. Frustrated, I resorted to sprinkling text-align: center; everywhere in my code, but it's just not solving the problem.

My navigation bar has a hover effect that underlines the menu item being hovered over and a different color scheme for the active item.

HTML:

<div class="menu">
    <div id="cssmenu">
        <ul>
            <li><a href="index.html"><span>Home</span></a></li>
            <li><a href="about.html"><span>About</span></a></li>
            <li class="active"><a href="news.html"><span>News</span></a></li>
            <li><a href="gallery.html"><span>Gallery</span></a></li>
            <li class="last"><a href="contact.html"><span>Contact</span></a></li>
        </ul>
    </div>
</div>  

CSS:

/*-- Menu Logo --*/
.logo {
    float: left;
    /*background: #DE5491;*/
    padding: 23px 20px;
    width: 406px;
    height: 80px;
    vertical-align: central;
}

.menu {
    float: right;
    width: 53%;
}

/*--menu--*/
#cssmenu li,
#cssmenu span,
#cssmenu a {
    /*margin: 0;*/
    padding: 0;
    position: relative;
    text-transform: uppercase;
    text-align: center;
}

#cssmenu {
    padding-top: 40px;
}

#cssmenu:after,
#cssmenu ul:after {
    content: '';
    display: block;
    clear: both;
}

#cssmenu a {
    color: #000;
    display: inline-block;
    line-height: 49px;
    padding: 0 51px;
    text-decoration: none;
    font-weight: normal;
    font-size: 1.5em;
    font-family: 'bebas_neueregular';
    text-align: center;
}

#cssmenu ul {
    /*list-style: none;*/
}

#cssmenu > ul {
    /*float: left;*/
    text-align: center;
}

#cssmenu > ul > li {
    float: left;
    /*display:block;*/
    text-align: center;
    /*width: 24.9999%;*/
    width: 19.9999%;
}

#cssmenu > ul > li > a {
    color: #000;
}

#cssmenu > ul > li:hover:after {
    content: '';
    display: block;
    padding-top: 46px;
    border-bottom: 10px solid #003; /*-- Menu Bar Hover Color --*/
    -webkit-transition: .2s all linear;
    -moz-transition: .2s all linear;
    -o-transition: .2s all linear;
    transition: .2s all linear;
}

#cssmenu > ul > li.active:after {
    content: '';
    display: block;
    padding-top: 46px;
    border-bottom: 10px solid #252425; /*-- Menu Bar Color --*/
    -webkit-transition: .2s all linear;
    -moz-transition: .2s all linear;
    -o-transition: .2s all linear;
    transition: .2s all linear;
}

Answer №1

The issue arises due to the implementation of this particular guideline:

#cssmenu a {
  color: #000;
  display: inline-block;
  line-height: 49px;
  text-decoration: none;
  font-weight: normal;
  font-size:1.5em;
  font-family: 'bebas_neueregular';
  text-align:center;
}

It is recommended to eliminate padding: 0 51px; as it causes the anchor element to exceed the width of its container.

Answer №2

/*-- Customize Menu Logo --*/

.logo {
  float: left;
  /*background: #DE5491;*/
  padding: 23px 20px;
  width: 406px;
  height: 80px;
  vertical-align: central;
}
.menu {
  float: right;
  width: 53%;
}
/*--menu design--*/
#cssmenu li,
#cssmenu span,
#cssmenu a {
  /*margin: 0;*/
  padding: 0;
  position: relative;
  text-transform: uppercase;
  text-align: center;
}
#cssmenu {
  padding-top: 40px;
}
#cssmenu:after,
#cssmenu ul:after {
  content: '';
  display: block;
  clear: both;
}
#cssmenu a {
  color: #000;
  display: inline-block;
  line-height: 49px;
  /*padding: 0 51px; */
  text-decoration: none;
  font-weight: normal;
  font-size: 1.5em;
  font-family: 'bebas_neueregular';
  text-align: center;
}
#cssmenu ul {
  list-style: none;
}
#cssmenu > ul {
  /*float: left;*/
  text-align: center;
}

#cssmenu > ul > li {
  float: left;
  /*display:block;*/
  text-align: center;
  /*width: 24.9999%;*/
  width: 19.9999%;
}
#cssmenu > ul > li > a {
  color: #000;
}
#cssmenu > ul > li:hover:after {
  content: '';
  display: block;
  padding-top: 46px;
  border-bottom: 10px solid #003;
  /*-- Changing Menu Bar Hover Color --*/
  -webkit-transition: .2s all linear;
  -moz-transition: .2s all linear;
  -o-transition: .2s all linear;
  transition: .2s all linear;
  
}
#cssmenu > ul > li.active:after {
  content: '';
  display: block;
  padding-top: 46px;
  border-bottom: 10px solid #252425;
  /*-- Customizing Menu Bar Active Color --*/
  -webkit-transition: .2s all linear;
  -moz-transition: .2s all linear;
  -o-transition: .2s all linear;
  transition: .2s all linear;
}
<div class="menu">
  <div id="cssmenu">
    <ul>
      <li><a href="index.html"><span>Home</span></a>
      </li>
      <li><a href="about.html"><span>About</span></a>
      </li>
      <li class="active"><a href="news.html"><span>News</span></a>
      </li>
      <li><a href="gallery.html"><span>Gallery</span></a>
      </li>
      <li class="last"><a href="contact.html"><span>Contact</span></a>
      </li>
    </ul>
  </div>
</div>

Answer №3

One suggestion is to adjust the width of #css > ul > li by giving it a fixed width in addition to setting a fixed width for #cssmenu ul and using margin: 0px auto for #cssmenu li, #cssmenu span, and #cssmenu a elements.

 /*--menu--*/
    #cssmenu li,
    #cssmenu span,
    #cssmenu a {
        margin: 0px auto;
        padding: 0;
        position: relative;
        text-transform: uppercase;
        text-align: center;
        list-style: none;
    }

    #cssmenu ul {
        width: 960px;
    }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Converting a comma-separated string into an array of integers using jQuery

Is there a way in jQuery to convert a string containing multiple numbers into an array? The string in question is as follows: let values = "901,235,342,421,345,745,544,324,445,123,232,986,345,678"; ...

Wireless web platform/program

I have been tasked with creating an app that will display real-time power data from a website. The challenge is that I do not have access to the backend of the site, which means I am unable to use PHP protocols to pull the data into my app. I attempted to ...

Modify table row background color using PHP based on its position in the table with the use of a function

I am having trouble formatting my table to highlight different rows in distinct colors. The top row should be one color, the second row another, and the bottom two rows a different color each. This is to represent winners, teams getting promoted, and tho ...

Is there a way to integrate jQuery and Javascript into a Firefox add-on?

Having trouble creating a new element on the page? After checking both the page and domain during onload, it seems that everything is in order. But how can you successfully create a new element in the correct window page? window.addEventListener("load", f ...

Attempting to assign a specific font style to a dropdown selection menu

Trying to assign a font-family to the options in my select menu but facing issues. The code below seems to work on Google Chrome, but I'm getting a different font-family on Internet Explorer. Any suggestions on how to set a font-family that works on ...

Change the position on the second click

I am trying to implement a manual sorting feature for a table of persons. The idea is to allow users to select a person by clicking on the row, then select another person by clicking on them, and have the two selected rows switch positions without using dr ...

Continuous loop of images displayed in a slideshow (HTML/JavaScript)

Greetings everyone, I am currently working on creating an endless loop slideshow of images for my website. Despite researching several resources, I am still struggling to get the code right. The issue I am facing is that only the image set in the HTML code ...

BorderContainer and ContentPane dojo widgets combine to create a powerful layout system

Currently, I am in a traineeship at a company where I need to use a program developed by another trainee back in 2012. I have made some updates to the program, but encountered an issue: In the original report, the web page had a layout with a column on th ...

`After the initial instance, the menu title will be altered.`

I have a responsive menu using Bootstrap. When the menu is collapsed, I want it to display the name of the page, and when it is open, I want it to show "Menu." Currently, I have named it "Watch the Trailer" (so let's assume that we are on that page). ...

Feature of Thymeleaf: Download functionality

When working with my Thymeleaf template, I have successfully processed Java map content to HTML using the following code: <div th:each="file : ${files}"> <span th:text="${file.key}"></span> <!-- here file.key is retrieved --> ...

Need help speeding up website load times?

My website is loading incredibly slow, even though it has minimal content. I suspect that the high number of images and JavaScript elements on the page are contributing to this issue. Is there a method available to diagnose what exactly is causing the ext ...

Unable to successfully implement the CSS not selector

My webpage's code was created through the use of Wordpress. <div class="header-main"> <h1 class="site-title"><a href="http://wp-themes.com/" rel="home">Theme Preview</a></h1> <div class="searc ...

Difficulty in decoding intricate JSON response using JQuery (or simply JavaScript)

Consider the function below, which retrieves JSON data from a Solr instance: var url = "http://myserver:8080/solr/select?indent=on&version=2.2&q=(title:*Hollis* OR sub_title:*Hollis*+OR+creator:*Hollis*+OR+publisher:*Hollis*+OR+format:*Hollis*++OR ...

Using WebApi 2.2 with the Chrome browser may cause a CORS 405 error to occur

Currently, I am working on 2 projects: A Website built with HTML and Durandal that makes a webapi post method call using $.ajax. This site is located at localhost:33432 and communicates with the second site. The ajax call doesn't contain anything ou ...

Can the TEXT from a <select> be passed as the value of a hidden <input> element?

<form method="POST" action="question-function.php"> <label>Choose Category</label> <select name="catid" id="catid" class="form-control"> <?php $sel3 = mysqli_query($ ...

What steps can I take to stop search engines from crawling and indexing one specific page on my website?

I'm looking for a way to prevent search engines from indexing my imprint page. Is there a method to achieve this? ...

How can I fix the issue of not being able to scroll up on a page in Chrome using jQuery

When at the bottom of the page and clicking on 'Send us your resume', the intent is for the browser to scroll to the top of the page. This functionality works fine in IE and Firefox, unfortunately, it does not behave as expected in Chrome. Here& ...

Calculating the time gap between two consecutive "keyup" occurrences

I am in need of creating a basic point of sale system using Javascript. I have a barcode scanner that functions like a keyboard. My goal is to automatically identify when the input is coming from the barcode scanner and then generate a report with the sc ...

Is it possible to modify an input using a specific code?

Is it possible to dynamically change the value of a form input based on specific codes entered in another input without using PHP? For example, when a user enters "HFS73H" into one input, the value of another input changes to "John" - and if a different ...

creating a gap between two links within a div element

I have a dilemma regarding the spacing between two hyperlinks within a div tag. Can anyone advise on how to create this space effectively? .btn-filter { background-color: #ffffff; font-size: 14px; outline: none; cursor: pointer; height: 40px ! ...