Backgrounds filled by nested <li> elements inside another <li> element

My website features a sidebar menu with nested lists. When hovering over a list item, a sub-list appears. However, I am having an issue where the background color fills the entire sub-list instead of just the first list item. See the images below for clarification.

The first image shows my current attempt, while the second image depicts the desired outcome.

https://i.sstatic.net/qVYsN.png

Below is the code snippet for this functionality:

    /* Menu CSS */#cssmenu,
#cssmenu > ul {
  background: url(images/highlight-bg.png) repeat;
  padding-bottom: 3px;
  font-family: Verdana, Geneva, Tahoma, sans-serif;
}
#cssmenu:before,
#cssmenu:after,
#cssmenu > ul:before,
#cssmenu > ul:after {
  content: "";
  display: table;
}
#cssmenu:after,
#cssmenu > ul:after {
  clear: both;
}
#cssmenu {
  width: auto;
  zoom: 1;
  width: 100%;
}
#cssmenu > ul {
  margin: 0;
  padding: 0;
  position: relative;
  background-color: rgba(0, 0, 0, 0.7);
}
#cssmenu > ul li {
  margin: 0;
  padding: 0;
  list-style: none;
}
#cssmenu > ul > li {
  float: left;
  position: relative;
  cursor: pointer;
}
#cssmenu > ul > li > a {
  padding-top: 6px;
  padding-bottom: 6px;
  display: block;
  color: white;
  font-size: 13px;
  text-decoration: none;
  text-transform: uppercase;
  line-height: 18px;
}
#cssmenu > ul > li:hover > a {
  background: #d6d6d6; /* Old browsers */
  background: -moz-linear-gradient(top, #d6d6d6 0%, #e56e1d 49%); /* FF3.6-15 */
  background: -webkit-linear-gradient(top, #d6d6d6 0%,#e56e1d 49%); /* Chrome10-25,Safari5.1-6 */
  background: linear-gradient(to bottom, #d6d6d6 0%,#e56e1d 49%); /* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */
  filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#d6d6d6', endColorstr='#e56e1d',GradientType=0 ); /* IE6-9 */
}
#cssmenu > ul > li > a > span {
  line-height: 18px;
  font-size: 12px;
  border-right: rgb(255, 255, 255, 0.5) solid 1px;
  padding-right: 38px;
  padding-left: 38px;
}
/*
#cssmenu > ul > li.active > a,
#cssmenu > ul > li > a:active {
  background-color: rgba(0, 0, 0, 0.7);
}
*/
/* Childs */
#cssmenu > ul ul {
  opacity: 0;
  visibility: hidden;
  position: absolute;
  margin: 0;
  padding: 0;
  z-index: -1;
}
#cssmenu > ul li:hover ul {
  opacity: 1;
  visibility: visible;
  margin: 0;
  color: #000;
  z-index: 2;
  left: 0;
}
#cssmenu > ul ul:before {
  content: "";
  position: absolute;
  top: -10px;
  width: 100%;
  height: 20px;
  background: transparent;
}
#cssmenu > ul ul li {
  list-style: none;
  padding: 0;
  margin: 0;
  width: 100%;
}

#cssmenu > ul ul li a {
  padding: 6px 26px;
  background-color: #e56e1d;
  display: block;
  color: white;
  font-size: 13px;
  text-decoration: none;
  width: 194px;
  border-left: 4px solid transparent;
}
#cssmenu > ul ul li a:hover {
  background-color: #ee974f;
}
<ul id="sidebar">
    <li><a href='#'><span>Lorem ipsum dolor sit </span></a></li>
    <li><a href='#'><span>Amet, consectetur </span></a></li>
    <li><a href='#'><span>Adipisici elit, sed </span></a></li>
    <li><a href='#'><span>Eiusmod </span></a>
    <li><a href='#'><span>Tempor incidunt </span></a>
        <ul>
            <li><a href='#'><span>Excepteur</span></a></li> 
            <li><a href='#'><span>Sint obcaecat</span></a></li>
                <ul>
                    <li><a href='#'><span>Obcaecat</span></a></li> 
                    <li><a href='#'><span>Cupiditat</span></a></li>
                </ul>
            <li><a href='#'><span>Cupiditat non </span></a></li>
            <li><a href='#'><span>Proident.</span></a></li>
        </ul>
    </li>
    <li><a href='#'><span>Ut labore et </span></a>
    <li><a href='#'><span>Dolore magna aliqua</span></a>
</ul>

Answer №1

The Tempor incidunt list item contains a nested ul within it. This means that when you apply a background to the li, the background also extends to the nested list.

To achieve the desired result, here are two possible solutions:

  1. If
    <a href='#'><span>Tempor incidunt </span></a>
    spans the entire navigation width, apply the background to this anchor element instead of the li. This approach works because the anchor tag does not wrap the nested ul.
  2. Alternatively, give the nested ul a grey background. By doing so, the grey background will overlay the blue gradient and align with your design expectations.

Both of these solutions are effective. Personally, I recommend option (1) as it is cleaner. However, keep in mind that it requires the anchor tag to be full width. This can enhance usability by providing a consistent click target width across the navigation.

Answer №2

Consider updating the background color of your target link specifically, rather than the entire li element containing the sub-menu...

.sidebar > li > a {
  background-color: blue;
}

I've noticed in the CSS that you are changing the link color, but it initially appears as the hover color... here's a fiddle for reference:

https://jsfiddle.net/kbmr4wy9/2/

You may need to adjust the background styling within this selector:

#cssmenu > ul ul li a {}

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

The use of event.returnValue is outdated and no longer supported. It is recommended to use the standard event.preventDefault() method instead. You may encounter

Hey there :) Currently, I am utilizing JQuery 1.9.1.js to search records using JSON. I am able to retrieve the search list locally, but when attempting to publish it on Windows Server 2008 and IIS 7, I face issues as it throws an error stating "event.ret ...

Discovering specific keywords within HTML tags and performing replacements using jQuery

I am searching for specific strings within my HTML code, and if I find them, I want to replace them with nothing. For example: HTML: <img src=`javascript:alert("hello ,world!")`> My goal is to locate keywords like javascript, alert, etc, within H ...

What is the best way to detect if a user has reached the end of a container div while implementing Infinite Scroll

I am in the process of implementing infinite scroll on our website's dashboard, but I am currently facing a challenge in determining the bottom of the container div in my jsfiddle mockup. The original function works on a blank page with no container ...

Make the div stretch across the entire width of the page, excluding the width of a fixed div located on the right side, without needing to set a margin-right property

I've been struggling to find a solution for my wiki development project. My challenge involves designing a page layout with a fixed-width sidebar on the right, while ensuring that content to the left of the sidebar adjusts its width accordingly. I wan ...

Execute functions during jquery's .animate() operation

This is a snippet of code I use to smoothly scroll the browser back to the top: $('html, body').animate({scrollTop: 0}, 500, "easeOutQuart"); Now, I am looking for a way to prevent the user from scrolling while this animation is running. Once t ...

CSS Problem: Background Color Not Showing Up in Table Rows

Check out my fiddle below: https://jsfiddle.net/teo7auv3/ I'm facing a challenge where I want to change the background color of the entire table row (TR) to red when there are validation errors. The problem seems to be related to this code snippet: ...

Steps for retrieving the currently selected text inside a scrolled DIV

An imperfect DIV with text that requires a scroll bar For example: <div id="text" style='overflow:scroll;width:200px;height:200px'> <div style='font-size:64px;'>BIG TEXT</div> Lorem Ipsum is simply dummy text of th ...

Searching for a specific element using a CSS selector

Looking for a way to find an element using a CSS selector in Chrome, I followed these steps: Right-click on the object Select "inspect" Right-click on the highlighted area in Chrome developer tools Choose "copy" Click on "copy selector" This is the cod ...

The appearance of my sidebar items' right border varies depending on the web browser being used

My sidebar item's right border appears differently in Mozilla and Chrome. How can I resolve this issue? Here are the images from both browsers: https://i.stack.imgur.com/YGkkz.png https://i.stack.imgur.com/JxNs3.png "use client"; import { ...

Unveiling Elements as You Scroll Within a Specified Area

I have implemented a jQuery and CSS code to reveal my contact form as I scroll down the page. However, I am facing an issue with setting a specific range for displaying the element while scrolling. Currently, I have only managed to handle the scrolling dow ...

Exploring the impact of JavaScript tags on website performance in accordance with W3

While researching website optimization strategies today, I came across an article discussing the benefits of moving JavaScript scripts to the bottom of the HTML page. I am curious if this approach aligns with W3C's recommendations since traditionally ...

After the animation finishes, the CSS3 :hover effect ceases to function

There seems to be an issue with my DIV element. When hovering, the background color changes to blue. However, when a button is clicked, a class is added which animates the background color to green. After this animation, the hover effect no longer works an ...

Creating Radial Fades with CSS3

Seeking guidance on creating a background similar to the one shown here using just CSS. I believe it can be done, but I struggle with CSS3. The goal is for the background to be compatible with all modern browsers, not overly concerned about support for br ...

What is the process for including an external .js file in my VueJS2 index.html?

Currently, I am faced with a challenge involving two external Javascript files that are responsible for managing animations and vector triangulation for a background animation. In a typical html/css/js project, adding these two .js files would involve incl ...

Creating a series of spots arranged in a semi-transparent line using JavaScript for a unique canvas

My attempt at creating a highlighter is encountering issues with transparency The problem might be due to using lineCap=round, resulting in multiple dark spots appearing in a single line (which shouldn't happen). It's difficult to fully describe ...

Ensuring responsive design: Forcing Bootstrap 3 / Knockout Navbar to collapse on mobile site view

While working on a website, I incorporated Knockout.js and added a click event to a href in the navbar. The issue is that the navbar doesn't automatically close when using it on a mobile device after the click event triggers. Is there a way to ensure ...

Challenges arising from CSS position: fixed on iPhone 6+ in landscape orientation running iOS 9

Encountered a strange issue with the fixed header on my website under specific conditions: Using an iPhone 6+ in landscape mode. Using Safari with at least two tabs opened. The page has a fixed position header with html and body set to position: relative ...

Having difficulty inserting an image with style="background-image:url(' ')" in Rails from the database

Hi everyone! I am new to both Rails and Stack Overflow, and I would really appreciate it if someone could guide me here. I am currently working on a test site that resembles a personal blog. It's mainly for showcasing one user's portfolio. In t ...

A creative way to display a div upon hovering over a link dynamically

My website has dynamically generated a tags, each with its own corresponding div containing unique content and height. I want to make it so that when a user hovers over an a tag, the matching div is displayed. The div should appear at the position of the m ...

Adjusting the heights of various divs as a percentage to fill the containing parent div

Is it possible for child divs to adjust their height automatically based on a percentage within a parent div with set max dimensions using CSS? HTML: <div id="parent"> <div id="top"></div> <div id="bottom"></div> < ...