Adjusting the width of titles in a CSS menu upon hover

I am currently creating a basic CSS menu. However, I am facing an issue where selecting a menu title in the menu bar causes the width of the title to extend to match the width of the associated menu, which is not my desired outcome (the title's width should remain unchanged). You can view the JSFiddle or take a look at the code below:

<div id="menu">
  <ul>
    <li><a href="javascript: showLogin()">you</a>
      <ul>
        <li><a href="javascript: showLogin()">register...</a></li>
        <li><a href="javascript: showLogin()">login...</a></li>
        <li><a href="javascript: showLogin()">forgot password...</a></li>
      </ul>
    </li>
    <li>.</li>
    <li><a href="javascript: selectMenu()">qan</a></li>
    <li>.</li>
    <li style="width: 20px"><a class="site">be</a>
      <ul>
        <li><a href="javascript: false">be</a></li>
        <li><a href="javascript: false">do</a></li>
      </ul>
    </li>
  </ul>
</div>

Here are the corresponding CSS definitions:

#menu {
  font-family: Arial, Helvetica, sans-serif;
  padding: 0px 5px;
  font-size: 12px;
  line-height: 18px;
  color: darkgrey;
  position: absolute;
  top: 0px;
  left: 0px;
  height: 20px;
  background-color: black;
  z-index: 3;
  /*opacity: 0;*/
  white-space: nowrap;
}
#menu ul {
  margin: 0px;
  padding: 0px;
  list-style-type: none;
  list-style-image: none;
}
#menu>ul>li {
  font-weight: bold;
  display: inline-block;
  float: left;
  padding: 2px 1px 0px 1px;
  width: auto;
  /*width: 10px;*/
}
#menu a { color: inherit; text-decoration: none;}
#menu>ul>li>a:hover { background-color: grey;}
#menu>ul ul {
  display: none;
  background-color: lightgrey;
  padding: 2px 5px;
  line-height: 14px;
  min-width: 100px;
}
#menu>ul ul>li {
  color: black;
  padding: 2px 8px 2px 5px;
  margin: 0px -3px;
}
#menu>ul ul>li:hover { color: lightgrey; background-color: grey;}
#menu>ul>li:hover ul { display: block;}

Since the menus are dynamically generated and meant to change on-the-fly, while using a proportional font, I am unable to set a fixed width for the titles to prevent resizing. The title's width should only be determined by the width of the text.
Previously, this worked when yuimenus was implemented, but it caused unexpected changes to my CSS that were hard to manage. Hence, I opted to create the menu myself and everything is working fine except for the width discrepancy. I haven't been able to identify which part of yui prevented this issue. Any suggestions?

Answer №1

I differ on the use of max-width; it can lead to content-independent link width. Instead, consider using position:absolute; for the submenu: Check out this jsFiddle example

Answer №2

Adjusting the width of li elements

Check out an updated example here: http://jsfiddle.net/8U5An/9/

CSS code snippet:-

#menu ul li {
    width: 25px;
}

Explore some other examples showcasing how they handle similar cases using only the width property:-

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

Unable to access frame: Error - Unable to retrieve 'add' property from undefined

I am working on customizing the chatbot functionality for my website built with Nuxt.js. I want the chatbot to display on certain pages while remaining hidden on others. Unfortunately, I encountered an issue when trying to hide it on specific pages. To im ...

Tips for avoiding a large <ul> element from spilling over the body in a flexbox design

I struggled to articulate the problem I am facing, and I apologize if it's still unclear So, in my app, I have 3 cards, and the 2 side ones contain lists (<ul>) that can become quite lengthy. I want the scrollbar to appear on the <ul> its ...

Javascript editing enhancement for real-time changes

Are there any tools for in-place editing of Javascript code? I'm looking for something similar to Firebug, which is great for instant CSS editing and previewing but doesn't have the capability to edit JavaScript directly. Is there a tool or addon ...

Dropdown trigger changing image with AJAX

When a color is selected from one dropdown menu, it triggers a change in another dropdown menu and the image displayed. The ID for the dropdown affected by the color selection is "style_code". <script> function getState(val) { $.ajax({ ...

How can I assign a unique background color to each individual column within a RadioButtonList?

I have an issue with setting 3 repeated columns. I want to assign different background colors to each of them. If you have any ideas on how I can achieve this, please share. Here is the code for my RadioButtonList: <asp:RadioButtonList ID="rblTimeSlot ...

How do I easily show/hide a submenu in AngularJS with a toggle menu?

After browsing some Stacks, I've come to the realization that I need to create separate directives in order to hide or toggle a menu from its sub-menu items. I want the menu to be hidden when any of the sub-menu items are clicked. However, I can&apo ...

Having trouble using jQuery's .off() method to remove an event handler?

I'm facing an issue with my jQuery code where the .off('click') method doesn't seem to be working. I've tried removing the event binding from '.reveal-menu', but it's not working as expected. The initial animation wo ...

Larger icon graphics

Is it possible to increase the size of Glyphicons in Twitter Bootstrap 3.0 without using the btn-lg class? I found this code that makes glyphicons big: <button type="button" class="btn btn-default btn-lg"> <span class="glyphicon glyphicon-th- ...

Achieving the Menu Hover Effect - step by step guide to recreating this stylish effect

I recently came across a stunning hover effect on the main menu of a website (red rectangles above menu). It's quite impressive! I found it here: Now, I want to incorporate this same effect into my own website. Unfortunately, there doesn't seem ...

Displaying a box with a question mark using Glyphicon and Font Awesome icons

I'm having a problem with my website not displaying the Glyphicons or Font Awesome Icons. When I enter the code, it shows up like this: http://prntscr.com/8qwwmd. This is the first time I've encountered this issue and it's only affecting som ...

Should we consider using extra url query parameters as a legitimate method to avoid caching or enforce the updating of css/js files?

Is it acceptable to include extra URL query parameters in order to avoid caching or enforce the updating of CSS/JS files? /style.css?v=1 Or would it be preferable to rename the file/directory instead? /style.1.css I've heard that this could potent ...

maximum distance a button can respond to in a CSS layout

I'm trying to figure out why my CSS button has such a large clickable area. Is there a way to reduce it? When I hover over the button, the clickable area extends beyond the text, which is not ideal. Any suggestions on how to fix this without altering ...

An issue occurred during the compilation of an Angular6 project

I am embarking on my first Angular project and may not grasp every detail perfectly. In my search for guidance, I came across the command "ng build --prod" on Google and decided to give it a try. Everything seemed to be going smoothly at first until an err ...

How to Delete the Bottom Border on Selected Tabs in MUI Using React

I am attempting to customize the appearance of MUI Tabs to achieve a design similar to the image below: https://i.sstatic.net/tBS1K.png I have created a sample code example in a codesandbox environment. You can view it here: Codesandbox Example. In this e ...

The fixed-top navigation bar in Bootstrap obscures the links within the #content

As a student studying software development, I am currently working on a web development project for my class. Utilizing Bootstrap, I have implemented a navbar-fixed-top and structured the body as a table-striped table with each row containing a <a href= ...

When arranging elements using the @media feature in CSS grid, a phantom element seems to mysteriously appear

Apologies for the extensive uploads, I'm not sure how else to proceed. I'm attempting to rearrange my grid layout at a specific width so that the paragraphs repeat with the images. However, I'm encountering a random space between 1 and 2 whe ...

Tips on adjusting the default width of the React player interface

I'm currently utilizing react-player to display a video in my app. The default width it comes with is not responsive, and applying CSS didn't prove to be effective. import ReactPlayer from 'react-player'; <ReactPlayer url="https:// ...

How to incorporate an image as a background in a Bootstrap 4 card

I'm working on achieving a specific design, as shown in the image https://i.sstatic.net/GRjwo.jpg In Bootstrap 4, it's simply a group of cards with the "card-img-overlay" class. The issue arises when the height of the card is dependent on the im ...

Subdomain Dilemma: Trouble with Images in Internet Explorer

Currently, I am working on creating a basic website on a subdomain. Everything appears to be running smoothly in Google Chrome except for one issue with the header image not displaying in IE8. The image link is . It seems that the problem may stem from IE ...

Attempting to showcase a pair of unordered lists side by side using the power of flexbox

I am having trouble getting this to display on a single line, no matter what I try. While there may be simpler ways to achieve this without using flexbox, I am eager to learn how to do it using flexbox properties. My desired outcome is to have the first u ...