The menu seems to be off-center

I'm struggling to center my menu/navigation bar horizontally. I can't seem to figure out what mistake I've made or what I've forgotten. Additionally, there is right padding after my last list item. How can I get rid of it?

HTML

<div class="wrapper">
   <div class="header"></div>
   <div class="Header"></div>
   <ul class="menu">
     <li>Home</li>
     <li>About Me</li>
     <li>My Services</li>
     <li>Contact</li>
   </ul>
</div>

CSS

.wrapper {
  width: 100%;
}

.menudiv {
  width: 80%;
  border: 1px red solid;
  margin: auto;
}

.menu {
  text-align: center;
  border: 1px red solid;
  position: relative;
  display:inline-block;         
  transform: translateX(-50%);
  left: 50%;
  color: black;
  padding-left: 20%;
  padding-right: 18%;
  min-width: 80%;
  max-width: 80%;       
}

.menu li {
  float: left;
  display: block;
  padding-right: 5%;
}

.menu li:after {
  content: '/';
  padding-left: 20px;
}

https://jsfiddle.net/sdzLn5hd/

Answer №1

Do you think your HTML code is correct? Let's take a closer look.

<div class="Header">
</div>  
<ul class="menu">
   <li>Home </li>
   <li>Over mij </li>
   <li>Mijn diensten </li>
   <li>Contact </li>
</ul>  

Instead of:

<div class="Header">
    <ul class="menu">
       <li>Home </li>
       <li>Over mij </li>
       <li>Mijn diensten </li>
       <li>Contact </li>
    </ul>  
</div>  

I recommend verifying this first.

Next, let's address the centering of your menu. By examining your CSS and adjusting the background-color, we can clarify things.

.wrapper {

    width: 100%;

}

.menudiv {

    width: 80%;

    border: 1px red solid;

    margin: auto;

}

.menu {


    border: 1px red solid;

    padding: 55px 0;

    position: relative;

    display:inline-block;

    transform: translateX(-50%);

    left: 50%;

    color: black;

    width: 80%;

}

.menu li {

    float: left;

    display: block;

    padding-right: 5%;

}

.menu li:after {

    content:'/';

    padding-left: 20px;

}

div.wrapper {
    
    background-color: orange;
}
  
        <title>Webdesign Maarten</title>
        
    
    
    <body>
        <div class="wrapper">
            <div class="header">
                <!-- here to put image JQuery preferably Sliding !-->
            </div>
            <div class="Header">
                <!-- menu float left indent with icon centered view v wrap. no list style -->
            </div>
            <ul class="menu">
                <li>Home</li>
                <li>About Me</li>
                <li>My Services</li>
                <li>Contact</li>
            </ul>
        </div>
    </body>

</html>

Now, for the solutions. There are two possibilities I can suggest based on your requirements.

Solution 1:

Adjust the .menu li class as shown below :

.menu li {
    display: block;
    text-align: center;
}

See updated code below :

.wrapper {
    width: 100%;
}
.menudiv {
    width: 80%;
    border: 1px red solid;
    margin: auto;
}
.menu {
    border: 1px red solid;
    padding: 55px 0;
    position: relative;
    display:inline-block;
    transform: translateX(-50%);
    left: 50%;
    color: black;
    width: 80%;
}
.menu li {
    display: block;
    text-align: center;
}
.menu li:after {
    content:'/';
    padding-left: 20px;
}
<title>Webdesign Maarten</title>
<body>
    <div class="wrapper">
        <div class="header">
            <!-- here to put image JQuery preferably Sliding !-->
        </div>
        <div class="Header">
            <!-- menu float left indent with icon centered view v wrap. no list style -->
            <ul class="menu">
                <li>Home</li>
                <li>About Me</li>
                <li>My Services</li>
                <li>Contact</li>
            </ul>
        </div>
    </div>
</body>

</html>

Solution 2:

Modify the .menu and .menu li class like so :

.menu {
        border: 1px red solid;
        padding: 55px 0;
        position: relative;
        display:inline-block;
        transform: translateX(-50%);
        left: 50%;
        color: black;
        width: 80%;
        text-align: center;   /*Add this property*/
    }

.menu li {
        display: block;
        text-align: center;
    }

See updated code below :

.wrapper {
    width: 100%;
}
.menudiv {
    width: 80%;
    border: 1px red solid;
    margin: auto;
}
.menu {
    border: 1px red solid;
    padding: 55px 0;
    position: relative;
    display:inline-block;
    transform: translateX(-50%);
    left: 50%;
    color: black;
    width: 80%;
    text-align: center;
}
.menu li {
    display: inline-block;
    text-align: center;
}
.menu li:after {
    content:'/';
    padding-left: 20px;
}
<title>Webdesign Maarten</title>
<body>
    <div class="wrapper">
        <div class="header">
            <!-- here to put image JQuery preferably Sliding !-->
        </div>
        <div class="Header">
            <!-- menu float left indent with icon centered view v wrap. no list style -->
            <ul class="menu">
                <li>Home</li>
                <li>About Me</li>
                <li>My Services</li>
                <li>Contact</li>
            </ul>
        </div>
    </div>
</body>

</html>

Answer №2

In order to eliminate the padding from the final li element, you must specifically target it:

.menu li:last-child {
    padding-right: 0;
}

Upon reviewing your code, it appears unclear as to what exactly you are trying to achieve. The HTML structure seems disorganized. Why is the header div being closed before the menu?

Answer №3

Modify the li elements to be displayed as inline-block, and specifically target the last one using :last-child:

.menu {
    text-align:center;
    border: 1px red solid;
    padding: 55px 0;
    position: relative;
    display:inline-block;
    transform: translateX(-50%);
    left: 50%;
    color: black;
    width: 80%;
 }

.menu li {
    display:inline-block;
    padding-right: 5%;
}
.menu li:last-child {
    padding-right: none;
}

View JSFiddle Demo

Answer №4

After much tweaking, I've managed to create a centered layout by utilizing flexbox styling and adjusting the padding.

fiddle: https://jsfiddle.net/sdzLn5hd/7/

HTML

<title>Webdesign Maarten</title>
<body>
    <div class="wrapper">
        <div class="header">
            <!-- Image placement here preferably using JQuery sliding effect -->
        </div>
        <div class="Header">
            <!-- Menu with floating icons centered using wrap property. No list style -->
        </div>
        <ul class="menu">
            <li>Home</li>
            <li>About Me</li>
            <li>My Services</li>
            <li>Contact</li>
        </ul>
    </div>
</body>

</html>

CSS

.wrapper {
    width: 100%;
}
.menudiv {
    width: 80%;
    border: 1px red solid;
    margin: auto;
}
.menu {
    border: 1px red solid;
    padding: 55px 0;
    position: relative;
    display:flex;
    transform: translateX(-50%);
    left: 50%;
    color: black;
    width: 80%;
}
.menu li {
    margin:auto;
    display: block;
}
.menu li:after {
    content:'/';
    padding-left: 20px;
}

Answer №5

Check out this link for a possible solution: https://jsfiddle.net/sdzLn5hd/9/

<title>Webdesign Maarten</title>

<body>
    <div class="wrapper">
        <div class="header">
            <!-- Image can be placed here, JQuery sliding preferred! -->

         <ul class="menu">
            <li>Home</li>
            <li>About Me</li>
            <li>My Services</li>
            <li>Contact</li>
        </ul>
             </div>
    </div>
</body>

Some unnecessary tags have been removed from your HTML code.

Answer №6

Include this code snippet in your .menu section:

.menu { text-align:center; }

.menu li {
    display: inline-block;
    padding-right: 5%;
}

The float:left property will consistently align the elements to the far left of their container.

In addition, insert the following line into your CSS (remember to remove the trailing '/' and padding):

.menu li:last-child:after {
    content: '';
    padding-left: 0px;
}

For a visual representation, refer to this example on JSFiddle.

Answer №7

In order to center text and inline elements, it is recommended to apply text-align: center on a block parent.

If you are working with HTML5, consider using a more semantic approach in your HTML structure. I have optimized the CSS code provided and made some adjustments for better clarity. You can view the updated code snippet below:

* { margin: 0px; padding: 0px; } /* Resetting CSS styles */

header { display: block; text-align: center; }

h1,
nav > ul {
    display: inline-block;
    border: 1px red solid;
    padding: 20px 0px;
    width: 80%;
}

nav > ul { border-top: none; }
nav > ul > li { display: inline; }
nav > ul > li:after { content: "/" }
nav > ul > li:last-child:after { content: "" } /* Removing trailing separator */

nav > ul > li > a {
    padding: 15px; /* Adequate padding for touch responsiveness */
    color: black;
    text-decoration: none;
}
nav > ul > li > a:hover { text-decoration: underline; }
<header>
    <h1>Title</h1>
    <nav>
        <ul>
            <li><a href="#">Home</a></li>
            <li><a href="#">Blog</a></li>
            <li><a href="#">About</a></li>
            <li><a href="#">Contact</a></li>
        </ul>
    </nav>
</header>

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

Include content within the navigation bar of Bootstrap 3.3

I am trying to insert text at the end of the header in a jumbotron template: <div id="navbar" class="navbar-collapse collapse"> <form class="navbar-form navbar-right"> <div class="form-group"> <input type="text ...

What is the best way to align these two divs centrally within a container set to display as inline-block?

I can't seem to get these two floated left divs centered within a container that has a height set to auto and a display of inline-block. The container has a max-width of 1600px, but when the window is stretched wider than that, it stays aligned to the ...

The functionality of the Bootstrap dropdown list button is not functioning properly on mobile devices

Currently, I am in the process of developing a website and testing its mobile view on my iPhone. The website is still using bootstrap 3, but I have encountered some issues. When I tap on the navigation button on my iPhone, nothing happens - no dropdown lis ...

What is the reason behind Chrome's fullscreen mode turning the background black?

I created a webpage with full-screen functionality and made sure to use the correct CSS properties to ensure that my gradient background covers the entire screen perfectly. However, I encountered an issue when clicking the full-screen button in Chrome - th ...

Managing Events for a Menu Item That Cannot Be Selected

As I work on developing a form using Material UI React, my focus is on logging exercise sets to the database. To allow users to select the exercise they wish to log, I have incorporated a list of Material UI Menu Item components as options. Additionally, I ...

Is there a way for me to direct to a different HTML page once I click on a certain data point?

At the moment, I have been utilizing this complete set of calendar codes. <!DOCTYPE html> <html> <head> <meta charset='utf-8' /> <link href='fullcalendar.css' rel='stylesheet' /> <link href=&a ...

The background image fails to display on the button

Why isn't the background image for my button appearing when set using a CSS rule? I have a button with the class "todo" and although other parts of the rule work, the image itself does not show up even after trying "background: url" and "background-im ...

Django failing to detect empty URL configuration

Hey there! I'm new to this, and I was trying out a tutorial on py/django. The web server is up and running, but it's only showing the default page: Even though my urls.py has the default page set. urls.py from django.contrib import admin from d ...

Toggle the submenu with a fade effect when jQuery is clicked

Currently, I am facing an issue with creating links that are supposed to open their respective submenus on click. However, the script I have written is opening all submenus instead of the specific ones assigned to each link. Sample HTML Code: <nav> ...

Vue transition not functioning properly when hovering over text for changes

When you hover over the circular region on the website, the text and description in the red box will change. To add a fade animation effect when the text changes, I attempted to use <transition> as per the documentation provided in this link. Unfor ...

Automatic Submission based on Checkbox Selection

Creating a user-friendly interface for project admins is my goal, but I am encountering difficulties in displaying all tasks. The data is retrieved from the database and put into a table, with each task having a status represented by a binary value - 1 f ...

Does using .stopImmediatePropagation() in the click event of a menu item have any impact on analytical tools?

Scenario I've implemented a navigation menu that loads subpages into a div using AJAX. While everything seems to be working fine, I noticed a bug where if I navigate to a subpage, then return to the main page and revisit the same subpage, it gets loa ...

What is the best way to automatically close a modal window after pressing the submit button

Having some trouble with my modal window using pure CSS. Can't seem to get it to disappear after clicking the submit button. Any suggestions on how to hide it properly? Here is the code snippet of both the HTML and CSS for reference. Open to all ideas ...

Every time I hover, my jQuery code keeps repeating the hover effect

I am currently facing an issue that has me stumped on finding a solution. The problem arises when I hover over the .div multiple times, the animation just doesn't stop and keeps running continuously. What I aim for is to have the .hidden element fad ...

"The application is experiencing issues with loading styles correctly when using jQuery append (Fiddler has been

When attempting to load a div tag using the .append() method, the styles are not loading correctly. Here's the code that was tested: <section id="year-2020" class="tab-panel"> <div class="row accordion"> <header> ...

What is the best way to ensure that the input search bar, microphone icon, and two small boxes adapt to different screen

Upon submitting my exercise for The Odin Project, I was under the impression that everything looked perfectly aligned and centered on my MacBook Pro 15-inch screen. However, when I viewed the completed webpage on a larger computer screen at work, I noticed ...

Step-by-step guide on generating a fluid list with Express and HTML

Looking to create a dynamic list by fetching data from a JSON file. I want the list items to redirect me to specific content based on their IDs when clicked. However, my current approach is not working as expected, and the list needs to be truly dynamic. ...

How can I retrieve XML through a URL using HTML5 and JavaScript?

Currently, I am looking to access an XML file through a URL link. My main goal is to retrieve the file automatically and extract its contents for further processing. However, I am facing difficulties in finding the appropriate method to obtain and read t ...

Creating a Chrome extension that opens multiple tabs using JavaScript

Currently, I am working on a chrome extension that includes several buttons to open different tabs. However, there seems to be an issue where clicking the Seqta button opens the tab three times and clicking the maths book button opens the tab twice. Below, ...

Trouble with placing an absolutely positioned element using Tailwindcss

I am currently working on a project using TailwindCSS in which I have a logo positioned to the left and navigation to the right. Both elements are centered, but because the logo has a position of absolute, it appears in front of the navigation. I would lik ...