CSS Menu Expansion Error in Just Two Lines

My CSS-only menu is causing some trouble by expanding to three lines whenever a submenu option on the first line is clicked. The issue only occurs with options in the middle, as it shifts everything out of place and onto another line.

Expected behavior:

Unexpected behavior:

Below is the CSS code:

#cssmenu ul {
  margin: 0;
  padding: 7px 6px 0;
  background: #74c141 url(overlay.png) repeat-x 0 -110px;
  line-height: 100%;
  font-family: 'Oxygen', sans-serif;
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  -webkit-box-shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
  -moz-box-shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
}
#cssmenu li {
  margin: 0 5px;
  padding: 0 0 8px;
  float: left;
  position: relative;
  list-style: none;
}
#cssmenu a,
#cssmenu a:link {
  font-weight: bold;
  font-size: 13px;  
  color: #e7e5e5;
  text-decoration: none;
  display: block;
  padding: 8px 20px;
  margin: 0;
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
}
#cssmenu a:hover {
  background: #000;
  color: #fff;
}
#cssmenu .active a,
#cssmenu li:hover > a {
  background: #90ce67 url(overlay.png) repeat-x 0 -40px;  
  color: #444;
  border-top: solid 1px  #f8f8f8;
}
#cssmenu ul ul li:hover a,
#cssmenu li:hover li a {
  background: none;
  border: none;
  color: #666;
  -webkit-box-shadow: none;
  -moz-box-shadow: none;
}
#cssmenu ul ul a:hover {
  background: #74c141 url(overlay.png) repeat-x 0 -100px !important;
  color: #fff !important;
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
}
#cssmenu li:hover > ul {
  display: block;
}
#cssmenu ul ul {
  z-index: 1000;
  display: none;
  margin: 0;
  padding: 0;
  width: 250px;
  position: absolute;
  top: 40px;
  left: 0;
  background: #ffffff url(overlay.png) repeat-x 0 0;
  border: solid 1px #b4b4b4;
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
}
#cssmenu ul ul li {
  float: none;
  margin: 0;
  padding: 3px;
}
#cssmenu ul ul a,
#cssmenu ul ul a:link {
  font-weight: normal;
  font-size: 12px;
}
#cssmenu ul:after {
  content: ".";
  display: block;
  clear: both;
  visibility: hidden;
  line-height: 0;
  height: 0;
}
* html #cssmenu ul {
  height: 1%;
}

I want the menu elements to remain stationary and not shift when hovered over or clicked. Thank you for your assistance!

Sincerely, Brian

Answer №1

It seems like the changing width of the items in your list could be causing the issue.

Keep in mind that attributes such as width, padding, margin, and border affect the overall width of your elements:

  • width
  • padding
  • margin
  • border

These attributes need to remain static. In the code snippets provided, I noticed that the border value changes from 1px to 0px (when set to none in the second rule).

#cssmenu .active a,
#cssmenu li:hover > a {
  background: #90ce67 url(overlay.png) repeat-x 0 -40px;  
  color: #444;
  border-top: solid 1px  #f8f8f8;
}
#cssmenu ul ul li:hover a,
#cssmenu li:hover li a {
  background: none;
  border: none; /* consider setting it to 1px with a transparent color */
  color: #666;
  -webkit-box-shadow: none;
  -moz-box-shadow: none;
}

If you could provide the HTML code as well, it would help in further troubleshooting.

Answer №2

Great job, jonhurlock!

I made a successful adjustment by eliminating the border-top from all elements. Turns out I didn't need that part at all, as I had originally sourced this code from a menu generator.

I always strive to keep my code concise by removing unnecessary lines and testing functionality. It seems I overlooked doing that for this particular menu!

Thank you once again, Brian

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

Finding the location of PIE.htc in the Firebug tool

After encountering issues with CSS3 properties not working on IE 7 and IE 8, I decided to include PIE.HTC. Visit this link for more information Upon viewing the page in IE, I realized that the CSS3 properties were not being applied as expected. I attempt ...

Passing a class object from a child component to a parent component in Angular 4

Passing a class object like Person from parent component to a child component is no issue. However, I would also like to manipulate this object in the child component and send it back to the parent component. The child component class looks like this: ex ...

What is the best way to address a row of hyperlink text located at the top of a webpage?

I have encountered an issue where three rows of text links near the top of a page shift up to the very top when a link is pressed, causing them to obscure a line of text that was already at the top. How can I keep the position of these three rows anchored? ...

Material UI Grid List rows are displaying with an unusually large gap between them, creating a

Currently, I am utilizing Material UI in combination with Reactjs. One particular issue that I am encountering involves the Grid List Component. In my attempt to establish a grid of 1000x1000px, I have specified the height and width within the custom gridL ...

Is the post secure if a HTTP web page sends an ajax request to a HTTPS url?

If I developed an HTML/jQuery widget to be embedded on third-party websites with users of very limited technical knowledge and potentially missing SSL certificates, would the information posted securely through AJAX Post to a secure URL remain protected? ...

The element is not defined, so it cannot be set for drag and drop functionality

I am encountering an issue when attempting to drag and drop an element into another element. The error messages I am receiving are: Uncaught TypeError: Cannot read property 'setData' of undefined and Uncaught TypeError: Cannot read property &a ...

Creating a list of definitions in the form of an HTML table using a multidimensional array in

Currently, I am tackling the following challenge: I must create a public static buildDefinitionList() method that produces an HTML list of definitions (using <dl>, <dt>, and <dd> tags) and then returns the resulting string. If the array c ...

Issues with jQuery animate not functioning properly when triggered on scroll position

I found a solution on Stack Overflow at this link, but I'm having trouble implementing it properly. The idea is to make an element change opacity from 0 to 1 when the page is scrolled to it, but it's not working as expected. The element is locat ...

The button's size shrinks significantly when there is no content inside

When text is absent, the button shrinks in size. I am utilizing an angular model to bind the button text: <button type="button" class="btn btn-primary" ng-bind="vm.buttonText" tooltip="{{vm.tooltipText}}" ></button> I prefer not to apply any ...

Unable to exceed a width of 100% in Asp.net Tables

I searched for similar inquiries without success. Here is a snippet from my aspx file: <div align="center" style="height: 350px; overflow-y: scroll; overflow-x: scroll; width: 100%;"> <asp:Table ID="tblReport" Font-Size="11px" runat="server" ...

Align both vertically and horizontally in the center of the body

I have two images aligned vertically inside a wrapper. How do I center the wrapper within the body? What is the best way to center my wrapper inside the body? <!DOCTYPE html> <html lang="en-US"> <head> <meta charset="utf-8"> ...

Create a sleek and sharp design for the menu and buttons

How can I replicate this menu design using CSS? https://i.sstatic.net/bpB9S.png I believe it is achievable with CSS, but I am unsure of where to begin. Can someone provide guidance on how to recreate this menu style? I do not have any existing code to sha ...

The click event listener only seems to fire on the second attempt

This block of code is designed to function as a search algorithm that also provides keyword suggestions. When a user clicks on a keyword, it is supposed to be placed into an input textbox. The possible keywords are "first" and "second". However, there is ...

"Stylish navigation bar overlaying a stunning background image in Bootstrap 4.0

Is there a way to ensure that my navbar stays on top of my background image in bootstrap 4.0 without using negative margin top? I want a solution that is easy to work with even if changes are made to the site. HTML: <div class="masthead"> <n ...

Displaying an element to appear over all client applications when hovering

Currently, I have an application that highlights specific areas when hovered over or clicked. While it functions properly, I would like the hover and click effects to be visible on every client, each with an identical overlay setup. I realize my method may ...

What is preventing simple HTML DOM Parser from traversing certain websites?

Query: After adding error_reporting(-1); I encountered the following error: Notice: Trying to access a property of a non-object in /data/22/2/145/126/2960289/user/3282682/htdocs/add.php on line 106 Notice: Undefined offset: 0 in /data/22/2/145/126/296028 ...

When the Bootstrap carousel slides, enhance the navbar text with motion blur effects

One issue I'm facing is that when the bootstrap carousel changes the image, it adds the class "next" (transform: translate3d(100%, 0, 0); ) to the item and causes motion blur in my navbar menu text. https://i.sstatic.net/kRnb4.png You can check out a ...

What is the best way to apply the addClass method to a list element

I've been searching for a solution to this issue for some time now, and while I believed my code was correct, it doesn't appear to be functioning as expected. HTML <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js ...

Achieving a full width vertical navigation on the left side using Bootstrap's grid system: a step-by

I am looking to design an admin panel using bootstrap. To start off, I want to create a navigation menu on the left side. However, I encountered an issue where my navigation menu with a red background does not stretch the full width of the left column. Her ...

Utilizing the power of Bootstrap, you can create a row of eye-catching

I am struggling to get 3 columns of cards to display in a row using bootstrap and CSS. Currently, only 2 columns are showing up. Here is the code snippet: <style type="text/css"> .card{ width: 350px; } </style> ...