Having difficulty aligning dynamic lists with their container on a horizontal navigation menu

I am trying to create an expanding list that drops relative to the parent's position. Right now, I have set the position to absolute, which lines up the lists incorrectly. When I change it to relative positioning, I get the desired result but there is a weird space on my lists. Here is what I currently have:

the desired result without the space

Is it possible to maintain the positioning and eliminate the space it creates? Thanks, everyone.

Here is my code:

* { margin: 0; padding: 0; box-sizing: border-box; } html, body { background-image: url(images/background.png); } nav { width: 250px; position: absolute; top: 0; bottom: 0; background: rgba(0, 0, 0, 0.25); border-radius: 10px; box-shadow: 5px 7px 10px rgba(0, 0, 0, 0.4); } nav ul { position: relative; list-style-type: none; } nav ul li p { display: flex; align-items: center; font-family: arial; font-size: 1.1em; text-decoration: none; text-transform: capitalize; color: white; padding: 10px 30px; height: 50px; transition: .5s ease; } nav ul li p:hover { background: rgba(0, 0, 0, 0.5); color: white; } nav ul ul { position: absolute; left: 250px; width: 200px; top: 0; background: rgba(75, 136, 162, 0.9); border-radius: 5px; box-shadow: 5px 7px 10px rgba(0, 0, 0, 0.4); display: none; } nav ul span { position: absolute; right: 20px; font-size: 1.5em; } nav ul .dropdown { position relative; } nav ul .dropdown:hover> ul { display: inline-block; } nav ul .dropdown-two ul { position: relative; left: 200px; cursor: pointer; } nav ul .dropdown-two:hover ul { display: inline-block; }

 <! DOCTYPE html > <html lang = "en" dir = "ltr" > <head > <meta charset = "utf-8" > <link rel = "stylesheet" href = "master.css" > <title > < / title > < / head> <body > <nav > <ul class = "" > <li class = "dropdown" > <p > Frozen Food <span > & rsaquo; < / span > < / p > <ul > <li > <p > Hamburger Patties < / p > < / li > <li class = "dropdown-two" > <p > Fish Fingers <span > & rsaquo; < / span > < / p > <ul class = "" > <li > <p > Fish Fingers (500 Grams) < / p > < / li > <li > <p > Fish Fingers (1000 Grams) < / p > < / li > < / ul > < / li > <li > <p > Shelled Prawns < / p > < / li > <li class = "dropdown-two" > <p > Tub Ice Cream <span > & rsaquo; < / span > < / p > <ul class = "" > <li > <p > Tub Ice Cream (1 Litre) < / p > < / li > <li > <p > Tub Ice Cream (2 Litre) < / p > < / li > < / ul > < / li > <li > <p > Fresh Food < / p > < / li > <li > <p > Beverages < / p > < / li > <li > <p > Home Health < / p > < / li > <li > <p > Pet Food < / p > < / li > < / ul > < / nav > < / body > < / html > 

Answer №1

To improve your dropdown menu, adjust the CSS by making `dropdown-two` relative and removing the relative positioning from `dropdown-two ul`. Also, set the top property to 100% for `dropdown-two ul`:

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html,
body {
  background-image: url(images/background.png);
}

nav {
  width: 250px;
  position: absolute;
  top: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.25);
  border-radius: 10px;
  box-shadow: 5px 7px 10px rgba(0, 0, 0, 0.4);
}

nav ul {
  position: relative;
  list-style-type: none;
}


nav ul li p {
  display: flex;
  align-items: center;
  font-family: arial;
  font-size: 1.1em;
  text-decoration: none;
  text-transform: capitalize;
  color: white;
  padding: 10px 30px;
  height: 50px;
  transition: .5s ease;
}

nav ul li p:hover {
  background: rgba(0, 0, 0, 0.5);
  color: white;
}

nav ul ul {
  position: absolute;
  left: 250px;
  width: 200px;
  top: 0;
  background: rgba(75, 136, 162, 0.9);
  border-radius: 5px;
  box-shadow: 5px 7px 10px rgba(0, 0, 0, 0.4);
  display: none;
}

nav ul span {
  position: absolute;
  right: 20px;
  font-size: 1.5em;
}

nav .dropdown-two {
  position: relative;
}

nav ul .dropdown:hover>ul {
  display: block;
}

nav ul .dropdown-two ul {
  top: 100%;
  left: 200px;
  cursor: pointer;
}

nav ul .dropdown-two:hover ul {
  display: inline-block;
}
<nav>
  <ul class="">
    <li class="dropdown">
      <p>Frozen Food<span>&rsaquo;</span></p>
      <ul>
        <li>
          <p>Hamburger Patties</p>
        </li>
        <li class="dropdown-two">
          <p>Fish Fingers<span>&rsaquo;</span></p>
          <ul class="">
            <li>
              <p>Fish Fingers (500 Grams)</p>
            </li>
            <li>
              <p>Fish Fingers (1000 Grams)</p>
            </li>
          </ul>
        </li>
        <li>
          <p>Shelled Prawns</p>
        </li>
        <li class="dropdown-two">
          <p>Tub Ice Cream<span>&rsaquo;</span></p>
          <ul class="">
            <li>
              <p>Tub Ice Cream (1 Litre)</p>
            </li>
            <li>
              <p>Tub Ice Cream (2 Litre)</p>
            </li>
          </ul>
        </li>
      </ul>
    </li>
    <li>
      <p>Fresh Food</p>
    </li>
    <li>
      <p>Beverages</p>
    </li>
    <li>
      <p>Home Health</p>
    </li>
    <li>
      <p>Pet Food</p>
    </li>
  </ul>
</nav>

Additionally, I have corrected your HTML structure by adjusting the closing `li` tag. It's important to optimize the user experience by ensuring the dropdown menus are easily accessible and don't disappear when hovering over them.

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

As you scroll through the page, the menu gracefully descends alongside you

Having trouble with my menu navbar. Looking to make it stick to the top of the browser when scrolling down. Desired effect can be seen here: For residents, news, for tourists, about Gdynia, Any tips on how to modify my code to achieve this effect? Prev ...

Integration issues in RetinaJs and jChartFX collaboration

I am currently utilizing jChartFX for charting purposes on my website. I have included the following line of code to bring in the footer: <?php include $_SERVER["DOCUMENT_ROOT"].'/footer.php' ?> The footer contains a script reference like ...

Ways to eliminate models that are not currently connected to the DOM

Within my form, I have implemented logic using ng-if to determine which elements are displayed based on user selections. For instance, if a user selects USA as the country, the state drop down will be shown as it was conditioned upon an ng-if for the count ...

Is there a way to implement a css/javascript property specifically for a div that is selected in the url?

Take for instance the scenario where I possess the URL example.com/#foo. In this case, CSS styling will be directed to the div with the id foo. If achieving this solely in CSS is not feasible, what methods in JavaScript or jQuery can be used efficiently ...

Elastic canvas to always match the full size of the container or screen

I am facing an issue with my responsive canvas that should resize to fit the screen window within a div. The canvas is intended to be 1100x1100 but needs to scale based on the screen size while maintaining a square aspect ratio. The problem arises when th ...

jQuery Autocomplete without dropdown menu suggestion available

I'm working on a form for my webpage and I want to enhance user experience by adding autocomplete functionality. The goal is to suggest existing names as users type in the 'name' input text box. Although I can see in my console that it&apos ...

Can basic logic be applied to CSS?

Recently, I have been experimenting with CSS to blur NSFW images in a chatroom until they are hovered over. I have successfully accomplished this task and now my next goal is to implement checkboxes that can toggle the display of these images. I am aware o ...

There seems to be a compatibility issue between AngularJS and HTML

Here is a simple AngularJS code snippet I have written: var app=angular.module("myApp",[]); app.controller("MyController",['$scope',function($scope){ $scope.name="Asim"; $scope.f1=function(){ console.log("i am clicked"); $scope.vr="lol"; } co ...

Verify Radio Buttons in AngularJS

I thought validating a selection from a radio group would be simple, but I'm struggling to find the right solution. In my form, I need to ensure that at least one option is selected from the radio buttons. Using the 'required' attribute on e ...

The left-floating elements are only partially functional

I recently encountered an issue with my HTML page that features a basic image gallery enhanced by lightboxes (fancybox). The images are meant to align left, with the first row containing three images and the second row containing two. <div class="image ...

Splitting up database results into groups of three rows and then organizing them into Bootstrap4 rows in Laravel 7

Is it feasible to utilize bootstrap rows when retrieving data from the database? I have designed a layout with 3 columns and my objective is to showcase the database outcomes in these rows. In order to display 3 rows, each containing 3 items on every page, ...

The JavaScript script to retrieve the background color is malfunctioning

I am currently working on developing a highlighting feature for an HTML table that will dynamically change the row colors on mouseover. Below is the code snippet I have been using, but it seems to be experiencing some issues. Any assistance would be greatl ...

Designing a unique pagination layout for your WordPress website

Currently, I am working on a WordPress project. I have created my own template using CSS and HTML format. The implementation is going well, but I am facing difficulty integrating WP pagination design into my template. Below is the pagination code from my ...

Leveraging the browser's built-in validation error messages in Angular for HTML forms

Is there a way to incorporate browser's native HTML validation error messages into Angular applications? https://i.sstatic.net/J0em4.png What I am looking for is the ability to leverage the built-in error messages when working with reactive forms li ...

Why are there no borders around the rows and columns of my table?

I'm just starting to learn HTML so I appreciate your patience with what may be a basic question for some. After creating a table using , I've noticed that there are no visible lines separating the cells. Is there a way to add lines around the ce ...

Is Flash now irrelevant? What makes HTML5 or CSS3 so powerful?

While I may not consider myself a dedicated Flash Activist, it's hard to ignore the fact that despite its flaws, there are some important uses for it on certain websites. However, with all the buzz around HTML5 and CSS3 being the future of the web, ev ...

jQuery .html() function malfunctioning

I am just starting out with front-end development and have taken on the challenge of creating a simple Tic Tac Toe game using HTML5. To set up the game board, I decided to use a table element in my code. Below is the snippet of HTML that I have implemented ...

Stop iFrame from inheriting parent CSS styles

Is there a way to prevent an iFrame object from inheriting CSS properties from the main page? Specifically: I have created an iFrame object in my main class, and I want to adjust the opacity of the main page without affecting the iFrame. However, when I ...

How can I close the menu when a menu item is clicked in a React.js application?

I am facing an issue where I want to close the menu when clicking on any menu item in React JS. The functionality works fine with a button icon but not with individual menu items. How can I resolve this problem? I have been trying to implement it using CSS ...

Converting HTML code to JSON using PHP scripting

Would appreciate your help in resolving a small issue. Although I came across this post, I am still encountering some errors: How can I convert HTML to JSON using PHP? I have created a PHP file that extracts a post from WordPress with the following forma ...