"Resolving the issue of overflow in dropdown submenus using Css

Observing my CSS menu in the provided image: https://i.sstatic.net/cJMge.jpg

Upon hovering over "Haschildren," I notice: https://i.sstatic.net/gJ5M8.jpg

The concern arises regarding the overflow issue (grey area around sub-voices). Despite tweaking the ul and li properties in the CSS, the desired results are not achieved. I aim to make minimal modifications while preserving the integrity of the rest of the design.

Here is a snippet of the HTML code:

$(document).ready(function() {
  $('.has-children').hover(
    function() {
      $(".sub-menu li").slideDown("slow");

    });

  $(".sub-menu").mouseleave(function() {
    $('.sub-menu li').hide();
  });
});
.header {
  background-color: #333;
  position: absolute;
  width: 100%;
  z-index: 3;
}
.header ul {
  margin: 0;
  padding: 0;
  list-style: none;
  overflow: hidden;
  background-color: #333;
}
.header li a {
  display: block;
  padding: 20px 20px;
  color: #f2f2f2;
  border-right: 1px solid #f4f4f4;
  text-decoration: none;
}
.header li {
  float: left;
}
.submenu ul {
  background-color: #fff;
}
.sub-menu li {
  clear: both;
  display: none;
}
.sub-menu li a {
  clear: both;
  border-right: 0px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>

<head>
  <link type="text/css" rel="stylesheet" href="mystyle.css">
  
</head>

<body>
  <header class="header">
    <ul class="menu">
      <li><a href="#work">Work</a>
      </li>
      <li><a class="has-children" href="#about">Haschildren</a>
        <ul class="sub-menu">
          <li><a href="#link1">Child 1</a>
          </li>
          <li><a href="#link2">Child 2</a>
          </li>
          <li><a href="#link3">Child 3</a>
          </li>
        </ul>
      </li>
      <li><a href="#careers">Careers</a>
      </li>
    </ul>
  </header>


</body>

</html>

Answer №1

After making some adjustments, I modified the code by commenting out two lines and including a background-color property to .header li a:

.header {
   background-color: #333;
   /*position: relative;*/
   width: 100%;
   z-index: 3;
}
.header ul {
   margin: 0;
   padding: 0;
   list-style: none;
   /*overflow: hidden;*/
   background-color: #333;
}
.header li a {
   display: block;
   padding: 20px 20px;
   color: #f2f2f2;
   border-right: 1px solid #f4f4f4;
   text-decoration: none;
   background-color: #333;
}
.header li {
   float: left;
}
.submenu ul {
   background-color: #fff;
}
.sub-menu li {
   clear: both;
   display: none;
}
.sub-menu li a {
   clear: both;
   border-right: 0px;
}

Answer №2

To create a dropdown menu, set the parent .has-children li relative and the child .submenu absolute. Adjust the positioning using properties like top, left to customize it for your layout

Answer №3

    <ul class="top-level-menu">
            <li><a href="index.html">Home</a></li>
<li><a href="#">About Us</a>
     <ul class="second-level-menu">
        <li><a href="#">History of the organisation</a></li>
        <li><a href="#">Objective of the company</a></li>
        <li><a href="#">Our Mission</a></li>
         <li><a href="#">Our Vision</a></li>
     </ul>
</li>
    </ul>

.third-level-menu
{
    position: absolute;
    top: 0;
    right: -150px;
    width: 150px;
    list-style: none;
    padding: 0;
    margin: 0;
    display: none;
}

.third-level-menu > li {
    background: #3a3836;
    border-bottom: 1px solid #fff;width: 135%;
}
.third-level-menu > li:hover { background: #CCCCCC; }

.second-level-menu
{
    position: absolute;
    top: 30px;
    left: 0;
    width: 150px;
    list-style: none;
    padding: 0;
    margin: 0;
    display: none;
}
.second-level-menu > li a{
    font-family: 'FuturaBT-Medium', sans-serif;
    font-size: 14px;
    font-weight: normal;    text-transform: none;}
.second-level-menu > li {
    position: relative;
    background: #231f1c;
    z-index: 9999999;
    text-align: left;width: 140%;
    border-bottom: 1px solid #fff;
}
.second-level-menu > li:hover { background: #CCCCCC; }

.top-level-menu {
    list-style: none;
    padding: 0;
    margin: 0;
    text-align: center;
}

.top-level-menu > li {
    position: relative;
    float: left;
    width: 154px;
    margin: 0 0 0 10px;
    display: inline;
    border-right: 1px solid;
    border-left: 1px solid;background-color: #231f1c;
}
.top-level-menu > li:hover {    background: #f4821e;
}

.top-level-menu li:hover > ul
{
    /* On hover, display the next level's menu */
    display: inline;
}


/* Menu Link Styles */

.top-level-menu a /* Apply to all links inside the multi-level menu */
{
    font: bold 14px Arial, Helvetica, sans-serif;
    color: #FFFFFF;
    text-decoration: none;
    padding: 0 0 0 10px;
    
    /* Make the link cover the entire list item-container */
    display: block;
    line-height: 30px;    text-transform: uppercase;
}
.top-level-menu a:hover {
    color: #000;
}
       <body>
          
                <ul class="top-level-menu">
                <li><a href="index.html">Home</a></li>
    <li><a href="#">About Us</a>
    <ul class="second-level-menu">
            <li><a href="#">History of the organisation</a></li>
            <li><a href="#">Objective of the company</a></li>
            <li><a href="#">Our Mission</a></li>
             <li><a href="#">Our Vision</a></li>
         </ul>
    </li>
        </ul>

           
</body>

Answer №4

Below is the solution provided - the menu position has been set to absolute and submenu li position to relative. Additionally, 'left right 0' and 'top: 40' have been used to match the line-height of the links. For a live demonstration, you can check out the liveFiddle

$(document).ready(function() {
  $('.has-children').hover(
    function() {
      $(".sub-menu li").slideDown("slow");

    });

  $(".sub-menu").mouseleave(function() {
    $('.sub-menu li').hide();
  });
});
 

   header {
  background-color: #000;
  padding: 10px 0;
}

header ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
}

header ul li {
  display: inline-block;
  position: relative;
}


/* sub navigation */

.menu li ul {
  background-color: #000;
  position: absolute;
  left: 0;
  top: 40px;
  /* make this equal to the line-height of the links (specified below) */
  right: 0;
}

.menu a {
  line-height: 40px;
  padding: 0 12px;
  margin: 0 12px;
}

.menu a {
  color: #fff;
  text-decoration: none;
  display: block;
}

.menu a:hover,
.menu a:focus,
.menu a:active {
  color: #bbb;
}


/* style sub level links */

.sub-menu li a {
  margin: 0 10px;
  padding: 0;
}

.sub-menu li {
  position: relative;
  margin: 0;
  text-align: center;
   display: none; /* hide sub with css */
}
<script
  src="https://code.jquery.com/jquery-3.1.1.min.js"
  integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
  crossorigin="anonymous"></script>
<header class="header">
  <ul class="menu">
    <li><a href="#work">Work</a></li>
    <li><a class="has-children" href="#about">Haschildren</a>
      <ul class="sub-menu">
        <li><a href="#link1">Child 1</a></li>
        <li><a href="#link2">Child 2</a></li>
        <li><a href="#link3">Child 3</a></li>
      </ul>
    </li>
    <li><a href="#careers">Careers</a></li>
  </ul>
</header>

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

Display a loading message before fetching remote data in the Bootstrap modal box

Here is the code I am using to load remote PHP data into a Bootstrap modal box: $(function() { $(window).bind("resize", function() { if ($(this).width() < 400) { $('#tabs-content').removeClass('col-xs-10').ad ...

Concealing options within Select2

Is there a way to hide certain Select2 options? When I attempt to use CSS like this: <option style="display: none;">...</option> Select2 does not seem to recognize it, unlike when an option is disabled or set as "readonly". Does any ...

What is the best way to retrieve the current value of an *ngFor loop upon button click?

I have been attempting to retrieve the data.login value of the data item that the user has clicked on the avatar image. Despite my efforts, I have not been able to successfully achieve this through various methods found online. How can I access the current ...

"Encountering a failure with PHP PDO prepared delete statement in MySQL while executing an Ajax

I am troubleshooting a PDO Prepared DELETE statement issue within a PHP file that is called using AJAX. Below is the AJAX call: var data = {action: "deleteTask", value: "1"}; $.ajax({ type: "POST", dataType: "json", url: "aja ...

Clearing Up CSS Height Definitions

Based on my observations from other posts, it seems that to establish the height of an element in percentages or pixels, etc., the parent element's height also needs to be explicitly defined. This implies that the height settings must be specified all ...

Function cannot be triggered by pressing the ENTER key

I'm struggling to pass values to my function and make it run when I press the ENTER KEY in PHP The function I'm dealing with is called update() Below is the PHP code snippet: echo '<input type="text" size="23" id= n'.$row["Cont ...

CSS, Assistance in incorporating a sub menu into a drop-down menu

I'm looking to enhance my drop down menu by adding a sub menu. Specifically, I would like to create a sub menu for Item 3 in the HTML below. How can I achieve this? Thank you, Below is my CSS code: .nav_menu { width:100%; background-color:# ...

Expanding Two HTML Elements to Fill the Screen Width Using HTML/CSS

This is the current setup: The screenshot spans the width of my screen. The image is aligned to the left side, while the Level information section remains fixed to the right of the image. This layout meets my requirements. However, I would like the border ...

Can we limit the number of items to just two per row in a grid with two columns?

I'm currently facing issues with aligning items within a two-column grid. When looking at this image, you'll notice that the alignment is off. Specifically, 'Example 3' does not align properly with 'Example 4'. This seems to b ...

"Step-by-step guide on positioning a video in the center over another

I am designing a landing page and I want to incorporate a background video with a centrally aligned logo on top of the video. My goal is for the center of the logo to always align with the center of the background image, regardless of how the page responds ...

An issue with the font display

I'm having trouble with a specific font on my webpage and jsfiddle, even though it works fine on w3schools website. Here's an example on jsfiddle HTML <div> With CSS3, websites can finally use fonts other than the pre-selected "web-safe" ...

Python script to extract data from a dynamic JavaScript webpage

I've been involved in a research project where we are aiming to gather a list of reference articles from the Brazil Hemeroteca (The specific page reference we are seeking: , needs to be located by extracting data from two hidden elements on this page: ...

The function .on will not be effective for elements generated at a later time

Whenever I click on a modal, I load some elements into a ul-list within the modal content. An issue arises where for my later generated elements, this functionality does not work and I cannot figure out why? This is the function inside the main page: $( ...

Adjust the appearance of "FAQS" to show as "FAQs" in Lodash

I've integrated the lodash library - a powerful JavaScript utility tool - into my AngularJS project. <div ng-repeat="question in questions"> <label>{{question | startCase}}</label> </div> If you want to learn more about th ...

Challenges faced when utilizing AJAX within a PHP loop

I am currently using a PHP WHILE loop to showcase multiple products, each with its own unique id#. These products can be rated using a PHP AJAX JQUERY-based system. However, after a user rates a product and the AJAX response returns the value, it is curren ...

Adjust the size of the mat-expansion indicator to your desired height and width

Trying to modify the width and height of the mat indicator has been a bit challenging. Despite following suggestions from other similar questions, such as adjusting the border width and padding, I am still unable to see the changes reflect in my CSS file ...

Firebase web authentication is most effective upon the second attempt

I am currently working on a website that interacts with Google's firebase to read and write data. The website has both anonymous and email authentication enabled. Users can view the data anonymously, but in order to edit or write new data, they must s ...

The mobile display does not support scrolling in Material-UI Drawer

I recently implemented the React Material UI library and integrated the Drawer component as a side-bar menu in my project. However, I encountered an issue where the Drawer component does not support scrolling on mobile devices. You can check out the websi ...

Rotate the circular border in a clockwise direction when clicked

I have successfully designed a heart icon using SVG that can be filled with color upon clicking. Now, I am looking to add an outer circle animation that rotates clockwise around the heart as it is being created. Currently, the circle only spins in the code ...

Using HTML strings in JavaScript code

I am currently working on a basic modal feature where I pass a string to be displayed as the content of the modal window. Here is a snippet of the script I am using: $("[id^='mod_']").click( function() { var line1 = $(this).attr("d ...