What is the best way to centrally align menu items using CSS?

I attempted to center the menu items in CSS. The navigation bar spans the entire width of the screen, which is the desired outcome. However, I am struggling to get the menu items to be centered relative to the screen.

Below is the CSS code that I am currently using:

#cssmenu ul,
#cssmenu li,
#cssmenu span,
#cssmenu a {
  margin: 0;
  padding: 0;
  position: relative;
}
#cssmenu {
  // rest of the CSS styles go here
}

Additionally, here is the HTML code that accompanies it:

<!DOCTYPE html>
<html lang="en">
<head>
    <link href="menu_assets/styles.css" rel="stylesheet" type="text/css">

    <div id='cssmenu'>
        // HTML menu structure goes here
    </div>
</head>

Any assistance on this matter would be highly appreciated.

EDIT: After implementing the suggested changes, I encountered another issue. When hovering over a "sub-menu" item, the "sub-sub menu" item displays with a gap between them, rendering them unclickable. Is there anything else that needs to be adjusted to resolve this?

Answer №1

Make sure to adjust the margin-left setting in this section to 25%:

#cssmenu ul {
  list-style: none;
  margin-left: 25%;

}

Feel free to experiment with different values for this property.

A mistake was found in your HTML code: the opening tag should be <body>, not <bo<; here is the corrected HTML code:

<!DOCTYPE html>
<html lang="en">
<head>
    <link href="menu_assets/styles.css" rel="stylesheet" type="text/css">
     <link href="styles.css" rel="stylesheet" type="text/css" />

</head>
<body>
    <div id='cssmenu'>
<ul>
   <li class='active'><a href='sample/home'><span>Home</span></a></li>
   <li class='has-sub'><a href='#'><span>Upload Data</span></a>
      <ul>
         <li class='has-sub'><a href='sample/index'><span>Test Data</span></a>
            <ul>
               <li><a href='upload_form_att/index'><span>With Attachments</span></a></li>
               <li class='last'><a href='sample/index'><span>Without Attachments</span></a></li>
            </ul>
         </li>
         <li class='has-sub'><a href='#'><span>Protocol</span></a>
            <ul>
               <li><a href='#'><span>With Attachments</span></a></li>
               <li class='last'><a href='#'><span>Without Attachments</span></a></li>
            </ul>
         </li>
      </ul>
   </li>
   <li><a href='#'><span>Stability Data table</span></a></li>
   <li><a href='#'><span>About</span></a></li>
   <li class='last'><a href='#'><span>Logout</span></a></li>
</ul>
</div>
</body>

</html>

Answer №2

Include this in your stylesheet.

#navbar{
   width:80%; //adjust as needed
   margin:auto;
}

The margin:auto property will center the element based on page and content width.

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

Boxes that expand to full width and appear to float on

I am dealing with a situation where I have a container that holds multiple child elements such as boxes or sections. <section> <div>Box 1</div> <div>Box 2</div> <div>Box 3</div> <div>Box 4< ...

How can you use JavaScript to retrieve the position of an element on a webpage?

As mentioned in the title, I am looking for a way to retrieve the x and y positions of an element relative to its location on the webpage and based on its positioning schemes such as absolute or relative. ...

CSS hack for older versions of Internet Explorer (ie6, ie7, and ie8)

Is there a way to selectively hide a single line of CSS from Internet Explorer 6, 7, and 8 so that they do not display that line? I am looking for a method to conceal the start and end of a media query specifically for those browsers so that they only see ...

Adjust the style of cursor - User Interface Expansion Panel Material Design

Using the MiU component "Expansion panel", I encountered an issue. By default, when the user hovers over the panel, the cursor is set to pointer. I attempted to change it to a default cursor, but my modification did not work. The code for my component is ...

Tips for arranging divs in CSS grid with grid-template-areas technique

I am attempting to organize similar groups of elements in a grid: #grid { position: absolute; left: 135px; top: 158px; width: 1080px; height: 1035px; display: grid; grid-template-rows: 99px 1fr 1fr; grid-template-column ...

Assistance in configuring Zurb Foundation 5 for optimal integration with Laravel

As a relatively new user to Laravel with previous experience using older versions of Foundation, I am currently in the process of setting up a new Laravel project. My goal is to integrate the Foundation framework into my project, but I find myself a bit co ...

Adding a disabled internal Style node to the HTML5 DOM: A simple guide

According to this, the style tag can be turned off using the disabled attribute. I attempted the following: <head> <style>body { color: black; }</style> <style disabled>body {color: red; }</style> </head> <bo ...

customizing the appearance of a plugin

Visit the live site here. I am attempting to customize the text displayed in black under the Upcoming Events section. Despite multiple attempts using different combinations such as .vevent-item odd event-1 .description .event-time .event-label, I have not ...

What's the reason behind CSS transitions starting with a black background?

When I apply a transition to the heart, it inexplicably turns black at the start of the animation before displaying the intended gradient effect. Below is the code snippet: .heart { font-size: 2.5rem; padding-right: 2rem; } .heart:hover ...

Concerns regarding the efficiency of JavaScript (Odin Project, Etch-a-Sketch) are arising

Currently, I am delving into Javascript on the Odin Project, tackling the Etch-a-Sketch exercise. This involves creating a board where you can draw with your cursor. As part of the exercise, there's a requirement to add a resize button that allows use ...

Adjust the navigation bar for smaller screens

I am diving into my first project using Bootstrap and am currently in the process of setting up the navbar. My goal: When the XS model is activated, I want to adjust the font size of the header, modify the height of the navbar, and left-align the header. ...

Is swapping out a form input's outline with box-shadow considered a violation of any WCAG guidelines?

Query Are there any accessibility drawbacks to removing the outline CSS property and using box-shadow instead to highlight an active input? Does this approach violate WCAG guidelines? Context By default, user agents highlight an active element's ou ...

Enhance Your Images with Hovering Icons

Is there a way to display an icon checkmark over the main image when a user hovers over it? The icon should appear in the top right corner of the image. <div> <img class="result-image" src="{{$property->photo}}"> <! ...

How can I extract content nested within another element's id?

Below is the HTML code snippet I am working with: <div id='content'> <div id='price'>$100</div> <div id='another'>something</div> </div> I am able to extract content from the element w ...

Is there a way to display shortened text only when hovering without altering the height of the list box?

Here is my script for a vue component : <template> ... <b-card-group deck v-for="row in formattedClubs"> <b-card v-for="club in row" img-src="http://placehold.it/130?text=No-image" img-alt="Img ...

Enable the ability to scroll and click to navigate an overlapping Div element

A customer has a website with a dark teal design and is not pleased with the appearance of the scroll bar, as it disrupts the overall style. The client requested that I find a solution without using third-party libraries, and one that they can easily under ...

Is there a way to dynamically fill a textarea with different data fields from a web form?

We recently acquired a php/CodeIgniter application. For various reasons, I now need to input values into a textarea field in order to consolidate data and send it to an external vendor or another application. While this is the first time we have encountere ...

Is there a way to specifically target the MUI paper component within the select style without relying on the SX props?

I have been experimenting with styling the Select MUI component using the styled function. I am looking to create a reusable style and move away from using sx. Despite trying various methods, I am struggling to identify the correct class in order to direct ...

Tips for creating blurred background images with CSS

I am experiencing an issue where the background image is being blurred along with the content. Despite trying to adjust the z-index, it seems like it's not working as expected. Below is the HTML code, and I would appreciate a solution with an explanat ...

Maintain the default material-ui class styles while making customizations to override others

Currently, I am working on customizing the material-ui tooltip and specifically need to adjust the margin for the tooltipPlacementTop class: const useStyles = makeStyles(theme => ({ tooltipPlacementTop: { margin: '4px 0' ...