equidistant navigation bar

Exploring layout design with CSS and encountered an unusual issue: where does the red highlighted space in the image below originate, and how can it be removed?

HTML:

<body>
<div class="menu-bar">
    <ul>
      <li>
        <a href="#home">Home</a>
        <ul>
            <li><a href="#beachouse">Beach House</a></li>
            <li><a href="#skicabin">Ski Cabin</a></li>
            <li><a href="#countrycottage">Country Cottage</a></li> 
        </ul>
      </li>
      <li>
        <a href="#news">News</a>
        <ul>
            <li><a href="#worldnews">World News</a></li>
            <li><a href="#nationalnews">National News</a></li>
            <li><a href="#localnews">Local News</a></li> 
        </ul>
      </li>
      <li>
        <a href="#contact">Contact</a>
        <ul>
            <li><a href="#emailaddress">Email Address</a></li>
            <li><a href="#phonenumber">Phone Number</a></li>
            <li><a href="#postaladdress">Postal Address</a></li> 
        </ul>
      </li>
      <li>
        <a href="#about">About</a>
        <ul>
            <li><a href="#aboutme">About Me</a></li>
            <li><a href="#aboutyou">About You</a></li>
            <li><a href="#aboutus">About Us</a></li> 
        </ul>
      </li>
    </ul>
</div>

</body>

CSS:

body {background: #77c4d3; padding:1%; }

div.menu-bar{position: relative; max-width: 700px;}

/*Styles for both menu and submenus: */
div.menu-bar ul { list-style-type: none; margin: 0; padding:20px; background: gray;} 
div.menu-bar li { background:white; text-align:center; display:inline-block; padding:10px;}

/*Menu-specific styles: */
div.menu-bar > ul {width: 100%;}
div.menu-bar > ul > li {width:20%; border:0; margin: 0; padding-top: 10px; padding-bottom: 10px;}

/* Submenu-specific styles */
div.menu-bar ul ul {background-color: blue; padding-left: 10px; padding-right: 10px;}

/*Hide any unodered lists that are descendents of a list element by default*/
div.menu-bar li ul { 
  display: none;
}

/*Select any unordered lists that are children of list elements that are being hovered on.*/
/* The <ul> is display:block, but the individual <li> elements are inline-block, which is what matters.*/
div.menu-bar li:hover > ul {
  display: block;
  position: absolute;
}

Answer №1

The <ul> within the <div class="menu-bar"> is responsible for the extra grey space due to its width being set to 100% in your css:

div.menu-bar > ul {
     width: 100%;
}

To center your buttons within the <ul>, you can add text-align: center; to the style, like this:

div.menu-bar > ul {
     width: 100%;
     text-align: center;
}

Alternatively, you can view an example on my JSFiddle.

Answer №2

Here are a few key points to keep in mind.

1.) Check for spacing issues within your code, especially between <li> elements. This can be resolved by ensuring the closing tag is placed immediately before the next opening tag.

2.) Consider adjusting the width of your elements, such as increasing it to 25% to prevent items from dropping to the next line.

3.) Pay attention to padding settings, ensuring consistency in all directions. Utilizing box-sizing can help include padding in element widths.

div.menu-bar > ul > li {
width: 25%;
margin: 0;
padding: 12px;
box-sizing: border-box;
}

Implementing these recommendations will result in a cleaner layout with evenly spaced list items.

We hope this information proves useful!

Answer №3

The width of each navigation item is determined by the text it contains, without considering the width of its parent element. Each item is positioned as far left as possible next to its adjacent item.

If you want the navigation items to evenly fill the navigation bar, you should implement a flex solution. Check out this detailed explanation on using flexbox at this link.

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

Trouble aligning CSS Nav Bar to the right with Flexbox

Having some trouble with implementing "flex" in CSS. My nav bar just won't move to the right no matter what I try. Spent hours adjusting margins, displays, and floats but it's stuck in the middle... Still learning, so any help is appreciated! If ...

What is the best way to position a div at the center with a set width and fixed positioning?

I'm currently working on coding and attempting to center a div horizontally in the middle of the page. The div is empty at the moment, just a plain white rectangle with specific dimensions. Here's my code snippet: #wrapper{ width:500px; height:1 ...

Unable to retrieve the desired dropdown element using xpath location strategy

On my website, I have a drop-down menu for selecting time zones and I need to be able to access the options easily. https://i.sstatic.net/jR4gx.png Here is the HTML code snippet: <li> <div id="ember2503" class= ...

The images have not been fading as intended

My HTML code is supposed to display one image fading into another, but the issue I'm facing is that the new image appears before the previous one has completely faded out. Can someone please help me identify what's wrong with my code? <!docty ...

How can I apply inline styles to all child elements in one go?

Looking for a solution to style a post on an online forum that restricts the use of stylesheets, allowing only inline styles. However, I am facing an issue where it seems that these inline styles do not get inherited by child elements, affecting only the ...

Achieving the full height of a parent element for a relatively positioned child

I am trying to create a layout with specific height requirements. Here is the code snippet: body, html { height: 90%; } #content{ width: 100%; height: 100%; } #sidebar { position: relative; width: 200px; float: left; height: 100%; backg ...

Implementing dynamic element selection with jQuery: combining onClick and onChange events

I am looking to update the appearance of the arrow on a select option. By default, it is styled as caret-down. When a user clicks in the input area, I want to change the style to caret-up. If the select option has been changed or no action has been taken, ...

Unable to retrieve the WebDriver value in Java

Could you please locate the HTML code? <input type="password" maxlength="20" id="txtRegPassword" data-bind="value: Password, valueUpdate: 'afterkeydown',qtipValMessage:Password" class="input-txt ErrorControl" data-orig- title="" data-hasqt ...

Adjust width when hovering or moving the mouse in and out

Apologies for the lack of a creative title... I'm sharing the following code snippet: .wrapper { display: flex; border: 1px solid red; padding: 10px; } .groups { display: flex; border: 2px solid blue; width: 70%; } .leftColumn { widt ...

Exploration of User Location: A Detailed Examination

Our Django app stores information about global destinations. We aim to utilize geolocation to pinpoint visitors' locations and display nearby places on Google maps. We seek advice on the best methods to implement this feature, considering that not al ...

Steps for setting all textareas on a website to "readonly" mode

My website contains numerous textareas but they currently do not have the read-only attribute. It would be very time-consuming to manually add the readonly attribute to each one. Is there a more efficient method to make all textareas read-only? ...

`Why is my table row not appearing horizontally in Cordova/Phonegap?`

Why is my tr tag displaying buttons vertically instead of horizontally? A similar code works properly on jsfiddle What am I doing wrong? Here is my HTML: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="form ...

Is there a way to keep my footer fixed to the bottom of the page in Google Chrome?

I recently made some updates to my aging website by adding a footer menu. However, I encountered an issue with the placement of the footer on Google Chrome. It appears too high, overlapping certain elements of the site. I attempted to resolve this problem ...

Passing data from one NodeJS page to another

I am working on creating a User management system in Node. My goal is to have a page that displays all users in a table format. When the edit button is clicked, I want to navigate to another page with detailed information about the selected user. However, ...

Looking for assistance on positioning an image of an icon both vertically and horizontally within a div that has a border radius. Can anyone help with

I am attempting to center an image within each of the four divs below. I have experimented with using the image as a background, but due to the border radius, it does not appear quite right. To provide context, I have included a fiddle. HTML: <div ...

Incorporating Sass into an HTML document

Recently, I discovered Sass and went through its documentation. I successfully installed the package on my Ubuntu system alongside Ruby and Haml. Now I'm eager to learn how to use it with Wordpress or a basic HTML file. Being new to this, I'm see ...

Achieving a similar behavior to a Node.TEXT_NODE with Node.ELEMENT_NODE in the context of flex-direction: column CSS

Below, you'll notice that the div._b5a is set as a flex container with flex-direction: column. Typically, its child is just a text sentence (nodeType == Node.TEXT_NODE): 新增個人簡介,讓大家更瞭解你, which means the text sentence remains ...

Is there a way to trigger a click event on an href using asp code behind?

Is there a way to programmatically trigger a click event on the href "shipping_question_ID_product_received_as_ordered_link" from the code behind of an ASP form? This href triggers a complex function in jquery when clicked by the user. I need to simulate ...

Modifying CSS class attributes with JavaScript

I am looking for a way to dynamically change the value of an attribute in a CSS class. Here is my situation: I have multiple elements that are all using the same class. Instead of individually selecting and applying styles to each one, I want to alter a ...

Struggling with Compatibility Issues Between LESS and Twitter Bootstrap

Currently, I am in the process of building a complex WordPress website using the 320press Twitter Bootstrap theme and incorporating LESS CSS. Here are some key details to keep in mind: WordPress 3.5 Bootstrap 2.0 LESS 1.3.3 Everything seems to be fun ...