Whenever I move my cursor over the NavList, the bottom list expands in width and pushes the top list downward

I am facing an issue with the width of the ul elements in my navigation bar. The first ul has a width equal to the width of the words plus a margin-right of 50px, while the bottom ul has a fixed width of 200px. The problem arises when the top ul's width is 180px and I hover over it. In this scenario, the top ul expands to match the bottom ul's width of 200px, causing the right side to protrude and push the list items.

nav {
  width: 100%;
  height: 50px;
  background-color: #d60d8c;
}

.navbar-tab {
  margin: auto;
  width: 1300px;
}

ul {
  margin: 0px;
  padding: 0px;
  list-style: none;
  font-family: arial;
}

.navbar-tab-1 {
  padding-right: 50px;
}

ul li {
  list-style: none;
  height: 50px;
  display: inline-block;
  font-family: 'Raleway', sans-serif;
  font-size: 19px;
  font-weight: 400;
  line-height: 50px;
  float: left;
}

ul li a {
  text-decoration: none;
  color: black;
  display: block;
}

ul li ul li {
  display: none;
}

ul li:hover ul li {
  display: block;
}

.hover-list li {
  font-size: 15px;
  background-color: #e2e2e2;
  width: 200px;
  text-indent: 20px;
}
<nav id="navbar">
  <ul class="navbar-tab">
    <li class="navbar-tab-1">Home</li>
    <li class="navbar-tab-1">Diamond Search
      <ul class="hover-list">
        <a>
          <li>GIA Diamond Search</li>
        </a>
        <a>
          <li>Diamonds</li>
        </a>
      </ul>
    </li>
    <li class="navbar-tab-1">Wedding Bands</li>
    <li class="navbar-tab-1">Engagement Rings</li>
    <li class="navbar-tab-1">Selection Guide</li>
    <li class="navbar-tab-1">Jewellery Services</li>
  </ul>
</nav>

Answer №1

To prevent the width of the parent from being affected, you can assign absolute positioning to the ul element that contains the submenu:

.hover-list {
  position: absolute;
}

nav {
  width: 100%;
  height: 50px;
  background-color: #d60d8c;
}

.navbar-tab {
  margin: auto;
  width: 1300px;
}

ul {
  margin: 0px;
  padding: 0px;
  list-style: none;
  font-family: arial;
}

.navbar-tab-1 {
  padding-right: 50px;
}

ul li {
  list-style: none;
  height: 50px;
  display: inline-block;
  font-family: 'Raleway', sans-serif;
  font-size: 19px;
  font-weight: 400;
  line-height: 50px;
  float: left;
}

ul li a {
  text-decoration: none;
  color: black;
  display: block;
}

ul li ul li {
  display: none;
}

ul li:hover ul li {
  display: block;
}

.hover-list li {
  font-size: 15px;
  background-color: #e2e2e2;
  width: 200px;
  text-indent: 20px;
}

.hover-list {
  position: absolute;
}
<nav id="navbar">
  <ul class="navbar-tab">
    <li class="navbar-tab-1">Home</li>
    <li class="navbar-tab-1">Diamond Search
      <ul class="hover-list">
        <a>
          <li>GIA Diamond Search</li>
        </a>
        <a>
          <li>Diamonds</li>
        </a>
      </ul>
    </li>
    <li class="navbar-tab-1">Wedding Bands</li>
    <li class="navbar-tab-1">Engagement Rings</li>
    <li class="navbar-tab-1">Selection Guide</li>
    <li class="navbar-tab-1">Jewellery Services</li>
  </ul>
</nav>


It is recommended to use Class selectors instead of tag selectors like ul li, ul li a, or ul li ul li. Class selectors offer better performance and target specific elements, avoiding unintended styling on unrelated elements if there are multiple instances of the same tag in the page.

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

When I print a specific A4 format division containing content as a pdf, I noticed that there is excess white space above the content on the second and third pages. I am curious

Feeling a bit frustrated here, I've tried everything but my project seems to break whenever I try to print 3 pages as a PDF and enter the printing dialog. If you'd like to check out the pages directly, click this link. Pressing cmd shift P will ...

Implementing restrictions for each duplicated field using jQuery: A step-by-step guide

Is there a way to limit the number of words in each duplicated field using jQuery? I want to restrict users from entering more than 5 words per field. I have a snippet of code, but it's not quite working as expected. Can anyone provide some assistanc ...

Adjust hover effects based on true conditions

Currently working on a web app using HTML, CSS, JavaScript, and AngularJS. Progress so far includes a clickable box that triggers a javascript function to display more boxes upon click using ng-click. <div ng-click="!(clickEnabled)||myFunction(app)" cl ...

Adjust SVG size to fit parent container/division

I am trying to make the SVG completely fill the containing TD. I don't care about preserving the proportions of the SVG. Instead, I want the height of the SVG to be the same as its width when the height should be larger than the width. There is paddin ...

Button for exporting data to Excel in PHP/HTML

Can anyone assist me in creating a button that, when clicked, exports all data from the HTML form on the page to an excel file? I'm new to PHP and not sure where to start. <div class="left col-md-12 form-group" > <form method=" ...

The JSON.parse function encountered an unexpected character 'o' in the JSON data at position 1

Hello, I am attempting to parse an API. </div> <div id=test> </div> </body> <script type="text/javascript" src="js/jest1.js"></script> <script> var xhr = new XMLHttpRequest(); xhr.open("GET" ...

An automatic selection in my AngularJS drop-down menu

I am currently utilizing AngularJS to develop a basic web application. My goal is to have the values of city A displayed as the default choice in my select dropdown. index.html <body ng-controller="MainCtrl"> <select ng-model="selectedCity" ...

Achieve center alignment for two column divs without altering the HTML code using only CSS

Recently, I stumbled upon a basic HTML code that I am unable to modify due to restrictions: <div id="container"> <div id="a"> Title col </div> <div id="b"> Left col </div> <div id="c"> ...

PHP code for navigating through the directory/file structure and displaying it as a series of nested unordered lists (UL

I am working with a complex directory tree structure that includes directories, sub-directories, and files. Here is an example of the tree: /food /food/drinks /food/drinks/water.html /food/drinks/milk.html /food/drinks/soda.html /food/entrees /food/entree ...

Developing a JSP page by coding in html and css

Being a web designer, I find myself working on JSP pages to modify HTML and CSS for web application projects. However, I often feel confused by the JSP tags that I don't understand. Has anyone experienced this before? If so, how do you manage to edit ...

What steps can I take to prevent Javascript/JQuery from repositioning other elements on the webpage?

Is there a way for the animation code to only affect the show/hide toggle buttons and not interfere with the other jQuery code I have implemented for hiding and showing div text? When I included the animate code in my js page, it caused issues with the exi ...

The sub menu in IE 8 does not stay open while hovering over it

My website has a navigation menu with a sub-menu that appears when hovering over the parent element. While this works smoothly on modern browsers, Internet Explorer 8 presents an issue. When hovering over the parent li element in IE 8, the sub-menu is disp ...

A guide on handling POST response body parsing in NodeJS

const express = require("express"); const bodyParser = require("body-parser"); const app = express(); app.use(bodyParser.urlencoded({extended: true})); app.get("/", function(req, res){ res.sendFile(__dirname + "/index.html"); }); app.post("/", function(r ...

An unidentified element is displayed as undefined

I am trying to implement a CSS change for mobile devices, but an unknown variable keeps showing up as undefined. Despite researching various tutorials and finding multiple solutions, I am struggling to grasp them as I am a novice in JavaScript and HTML. T ...

Is there a way to bypass or neglect the row style class within a column?

When I applied rowStyleClass to my DataTable, it was based on the following conditions: <style> .deleted { color: darkgray; text-decoration: line-through; } .normal { } </style> However, I encountered an issue where the last t ...

What is the best way to send an image object in a Django HTTP response?

When sending emails with images, I utilize html templates. My goal is to have the images generated dynamically upon opening the email. Therefore, the 'src' attribute in the image tag calls a REST api within my app to create the image on-the-fly. ...

The jQuery UI calendar widget fails to include a button on the webpage

I am currently working with a function that I use to add datepickers to dynamically created input elements of type date after wrapping them in $(...). function createDatePicker(inputElement) { inputElement.datepicker({ showOn: "button", buttonIm ...

Discover how to apply unique styles to specific HTML elements using their index values in jQuery

In the process of creating a party game similar to Tambola or Housie, I am faced with the challenge of automatically detecting and highlighting the numbers called out on each ticket in the game. For instance, if number 86 is announced, I aim to visually di ...

Is it possible to access and view the contents of a live website folder using WebMatrix2?

I am looking to make changes to a website using WebMatrix, but the site was originally built with ASP.net. When I try to open the site's folder, I encounter numerous errors. Can anyone advise me on whether it is possible to edit this website despite t ...

Having trouble adjusting the width of the text input in the inline search box form with an input group dropdown

Looking to create a search box similar to this design: https://i.sstatic.net/H93WQ.png What is the most effective way to implement this using Bootstrap? I tried using an inline form and input group dropdown list, but I encountered issues with the text i ...