CSS Vertical Accordion Menu

I am struggling to modify the vertical accordion menu CSS. I am having difficulty adjusting the sub-menu list items.

  <div id="menuleft">
    <div class="top">header</div>
  <ul>
    <li><a href="#">Main 1</a>
     <ul>
      <li><a href="#">Sub 1</a></li>
      <li><a href="#">Sub 2</a></li>
    </ul>
   </li>
   <li><a href="#">Main 2</a></li>
   <li><a href="#">Main 3</a></li>
   <li><a href="#">Main 4</a></li>
   </ul>    
  </div>

This is my current CSS:

#menuleft{
position: absolute;
bottom:0px; left:100px;
height: 100%;
width: 160px;
background-color: #BAB3D6;
}
#menuleft .top{
float:right;
width: 160px;
color:#FFFFFF;
font-size:110%;
margin:20px 0px 20px 0px;
height: 30px;
}
#menuleft ul{
position:absolute;
width: 160px;
top:130px;
left:0;
margin: 0;
padding: 0;
}
#menuleft li{
width: 130px;
height: 30px;
left:0; 
font-size: 95%;
line-height: 30px; 
list-style: none;
cursor:pointer;
}
#menuleft li a { 
text-decoration:none; 
display: block; 
width: 100%; 
height: 100%; 
padding-left:30px;
vertical-align:middle;
}
#menuleft ul ul li a { 
text-decoration:none; 
display: block; 
width: 100%; 
height: 100%; 
padding-left:30px;
vertical-align:middle;
background-color: #d8d4e8;
}
#menuleft li a:hover{
background-color:#652D91;
color:#FFFFFF;
font-weight:bold;   
}
#menuleft li a:active {
background-color:#ad45c5;
color:#FFFFFF;
}

I have also implemented jQuery for expanding and collapsing submenus. Any assistance would be greatly appreciated.

Here is the link for reference: JSfiddle

UPDATE

With the help of misterManSam, the submenu now works functionally. However, there is still a slight issue where the background of expanded submenus is not consistent. You can observe this in the updated JSFiddle provided.

Updated JSFiddle

Answer №1

  • Make sure to remove the position: absolute property from #menuleft ul and replace it with a top margin.

  • Eliminate the defined height on #menuleft li.

  • Assign a class (such as .nav) to the top level ul for styling, instead of targeting #menuleft ul, to prevent affecting second level uls.

  • Ensure that the .nav has the same background as #menuleft to maintain a consistent background behind the links, even at varying heights.

Additionally, remove all default margin and padding settings from the ul and its list items.

Demo

ul,li { margin: 0; padding: 0; }
#menuleft {
  position: absolute;
  bottom: 0px;
  left: 100px;
  height: 100%;
  width: 160px;
  background-color: #BAB3D6;
}
#menuleft .top {
  float: right;
  width: 160px;
  color: #FFFFFF;
  font-size: 110%;
  margin: 20px 0px 20px 0px;
  height: 30px;
}
#menuleft .nav {
  width: 160px;
  margin-top: 130px;
  margin: 130px 0 0 0;
  background-color: #BAB3D6
}
#menuleft li {
  width: 130px;
  font-size: 95%;
  line-height: 30px;
  list-style: none;
  cursor: pointer;
  padding: 0;
  margin: 0;
}
#menuleft li a {
  text-decoration: none;
  display: block;
  width: 100%;
  height: 100%;
  padding-left: 30px;
  vertical-align: middle;
}
#menuleft ul ul li a {
  text-decoration: none;
  display: block;
  width: 100%;
  height: 100%;
  padding-left: 30px;
  vertical-align: middle;
  background-color: #d8d4e8;
}
#menuleft li a:hover {
  background-color: #652D91;
  color: #FFFFFF;
  font-weight: bold;
}
#menuleft li a:active {
  background-color: #ad45c5;
  color: #FFFFFF;
}
<div id="menuleft">
  <h1 class="top">header</h1>

  <ul class="nav">
    <li><a href="#">Main 1</a>
        <ul>
            <li><a href="#">Sub 1</a></li>
            <li><a href="#">Sub 1</a></li>
        </ul>
    </li>
    <li><a href="#">Main 2</a></li>
    <li><a href="#">Main 3</a></li>
    <li><a href="#">Main 4</a></li>
  </ul>
</div>

Answer №2

My belief is that by hovering over the tag li element, we can reveal the ul sub-menu. It appears to be controlled through CSS.

/* Here is my customized code */
#sidebar navigation ul li submenu {
    display: none;
}

#sidebar navigation ul li:hover submenu {
    display: block;
}

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

What is the most streamlined way to send data from HTML forms using solely JavaScript?

I currently employ jQuery to transmit data from my HTML forms and then I utilize PHP to save it in my database. Below is the code snippet that I am using: HTML FORM: <form id="formpost" action="" method="post" onsubmit="return false"> <input t ...

When using Laravel's Response::json, a cross-domain error may still occur in the browser despite setting the header to Access-Control-Allow-Origin

I am facing a perplexing Laravel 4 issue. I have set up two methods in the same controller, which is declared to be restful. The problem arises when the ajax request comes from a different domain. The first method does not work: public function getOwnlis ...

Excessive content and the upper limit of height

I'm in the process of creating a scrolling table with a maximum height, and here's what I've done so far: <table> <tbody style="height: 300px; overflow:auto;"> //php for loop to populate table with <tr>s/<td>s </t ...

Understanding the reason why "undefined" is occurring in Javascript

Can anyone help me understand why the alert at the start is showing "undefined"? The alerts are displayed in this order: "success!" "Data" (the correct value) "undefined" I have gone through several threads and found that the issue usually arises du ...

Images failing to load in jQuery Colorbox plugin

I am having an issue with the Color Box jQuery plugin. You can find more information about the plugin here: Here is the HTML code I am using: <center> <div class='images'> <a class="group1" href="http://placehold.it/ ...

Align Bootstrap navigation bar items at the center horizontally

I am currently working on a navigation bar that features two icons, evenly distributed. To achieve this look, I have been experimenting with scaling the icons horizontally in order to make them center-aligned within the navigation bar. However, I have not ...

Utilizing jQuery arrays to retrieve JSON object properties

Here is the JSON data structure I am working with: { "head": { "heading": ["header1", "header2", "header3"] }, "body": { "elements": [{ "header1": { "value": "value1" }, "header2": { "value": "valu ...

Transfer the data-url attribute to the jQuery ajax URL

I am facing an issue with my form which includes a data-attribute holding a URL to an API JSON file: <form class="product" action="#" data-url="dist/scripts/main.js"> [...] </form> My goal is to transfer the URL from the data attribute to ...

developing a dropdown menu feature

I'm struggling with a small issue related to my drop-down menu function. My goal is to hide the visibility of a menu tab after it has been clicked for the second time. Below is the code snippet I've been working on: HTML:- <nav class="clea ...

Checking the validity of data before submission can help ensure the accuracy and security of your application

Is there a more effective method for validating input data in real-time as it is being entered into a form? One approach I am considering involves placing a DIV element next to the input field where users can see a message indicating whether their data is ...

Troubleshoot: Border problem within nested columns in Bootstrap

https://i.sstatic.net/RDWfp.png My layout is based on the bootstrap 4 grid system, as shown in the image above. The problem I'm facing is that the border at the bottom row is misaligned. I have four nested columns in the first and second rows, but on ...

Attempting to enlarge logo or image when cursor hovers over it

Seeking to enhance logo/image size when cursor hovers over it, but facing an issue where it enlarges even on areas outside of the image itself - View Example. I followed a tutorial to achieve this effect, and specified the width as 1024x1024 for the logo/i ...

The appearance of the logo varies across various web pages on the site

Isn't it strange that my logo appears pixelated only on the home page, even though I used the same HTML and CSS code for all pages? The Html: <a href="Home.html"><img id="logo" src="../CONTENT/Images/Logo/1Sr2l8а.png" alt="logo"/></ ...

What are the best ways to engage with a div element using keyboard shortcuts?

Is it possible to enable keyboard shortcuts for interacting with div elements? I am working on a project tailored for seniors who may have difficulty using a mouse. Is there a way to utilize keyboard shortcuts to click on divs and access their contents? H ...

`Problem with data representation in PDF regarding page dimensions and layout`

I am currently using the datatable plugin to enable exporting to PDF, CSV, XLS, and for printing purposes. However, I am facing an issue with the PDF view. When I attempt to download the PDF file by clicking on the button, the data in the PDF does not app ...

I've exhausted all options from stackoverflow with no success. Google Chrome's Autofill feature is wreaking havoc on my theme. Any suggestions on how to tackle this issue?

I designed a template with a stunning CSS layout. All I want is to have a transparent background with white font color, but Google Chrome seems to be causing issues with my theme. What steps should I take? I've exhausted all resources available onlin ...

Tips on managing Array indexes in AngularJS

Just diving into the world of Angularjs and I have some JSON files containing an array of car objects that I'm displaying on my webpage. My goal is to have a "button" that, when clicked, will alert me with data specific to that particular button. The ...

How to dynamically add list items to a jQuery Mobile list using Ajax requests

Included in my index.html is a list view: <section id="dashboard" data-role="page" data-transition="slide"> <header data-role="header"> <h1>Trips</h1> <a href="#addTrip" id="createNewTrip" d ...

I'm having trouble getting my CSS and HTML to connect properly, and I'm not sure what the issue is

Hi there, I'm having some trouble with my HTML code. I haven't even started building my website because I can't seem to connect it to the CSS file, and I have no idea why. I'm using Aptana Studio 3 by the way. <head> <titl ...

Incorporate a Bootstrap panel featuring a stylish border design and perfectly centered text

I am attempting to design two panels in Bootstrap with centered text and different fonts. Similar to the layout shown in the image below https://i.sstatic.net/aVGwV.png Here is my code: <div class="row"> <div class="col-lg-6&q ...