Stop flexbox list items from altering the width of their parent container when the children are hovered over

In my project, I am dealing with a series of list items where some have child lists containing sub navigation links. To create a hover effect using CSS, I set display: none on the child <ul> and then change it to display: block when hovering over the parent element.

The issue I'm facing is that when hovering, the parent <li> expands in size, which is not the desired behavior. Is there a way to prevent this growth? I want the width of <li> items without child lists to remain constant even when hovered over.

<ul class="flex unstyled-list">
  <li><a href="/">link</a></li>
  <li><a href="/" target="_blank">link 2</a></li>
  <li>
    <a href="/">parent 1</a>
    <ul class="unstyled-list">
      <li><a href="/">Hgdfgdf</a></li>
      <li><a href="/">Hgdfgdf</a></li>
    </ul>
  </li>
  <li>
    <a href="/">parent 2</a>
    <ul class="unstyled-list">
      <li><a href="/">Hgdfgdf gdf ggf dfs</a></li>
      <li><a href="/">Hgdfgdf gdf gdf gfd</a></li>
    </ul>
  </li>
</ul>


.unstyled-list {
  margin: 0;
  padding: 0;
  list-style-type: none;
}

.flex {
  width: 970px;
  height: 37px;
  display: flex;
  flex: 1 0 auto;
  flex-basis: 100%;
  background-color: #000;
}

.flex li {
  flex-grow: 1;
  height: 37px;
  line-height: 37px;
  color: #fff;
  text-align: center;
}

#nav-strip li:hover {
  background-color: #000;
}

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

.flex li a {
  display: block;
  color: #fff;
  font-size: 14px;
  font-weight: bold;
}

.flex li ul {
  display: none;
  width: 200px;
  padding: 0;
}

.flex li ul li {
  width: 185px;
  height: 30px;
  line-height: 30px;
  padding: 0 0 0 15px;
  background: #000;
  text-align: left;
}

.flex li ul li:hover {
  background: #1e1d3f;
}

.flex li ul li a {
  display: block;
  font-size: 12px;
  font-weight: normal;
}

For a live example, visit:

https://jsfiddle.net/ndp1y79b/1/

Answer №1

Big shoutout to @Temani Afif and @Pete for their assistance.

position: fixed;
bottom: 0;

Implementing these CSS changes on the child element <ul> successfully resolved the problem. A much simpler solution than anticipated!

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

Python: Convert a variable to an HTML file

This question may seem simple, but I've been searching for a solution without success for hours. I have an HTML file called "teste.html" I am using jinja 2 to make changes in my HTML. env = Environment(loader=FileSystemLoader('.')) template ...

Alteration of Content Height Input

I have designed a layout that I am excited to experiment with more in the future. Here is the current structure of my HTML: <!DOCTYPE html> <html> <head> <title>Title</title> <link rel="stylesheet" type="text/css" hre ...

IE's inconsistent float alignment causing display issues

My challenge is getting my div elements to float correctly in Internet Explorer. They are displaying perfectly in Chrome and Firefox, but IE seems to be messing up the code. The jsfiddle link showcasing the issue can be found here: http://jsfiddle.net/vlya ...

I have tried to create two apps in AngularJS, but unfortunately, they are not functioning as expected

Having trouble with implementing 2 ng-app in a single html page. The second one is not working. Please review my code and point out where I am making a mistake. <div id="App1" ng-app="shoppingCart" ng-controller="ShoppingCartController"> &l ...

My goal is to design a dynamic textbox for a website that allows users to customize the text in any format they desire

I want to create a versatile textbox on my website that allows users to change the text format as they desire. Unfortunately, I am facing some issues with implementing this feature. Here is the code I have developed so far. <body> <h1>< ...

Console not displaying any logs following the occurrence of an onClick event

One interesting feature I have on my website is an HTML div that functions as a star rating system. Currently, I am experimenting with some JavaScript code to test its functionality. My goal is for the console to log 'hello' whenever I click on ...

Submenu in submenu not registering CSS hover events

Currently, on a website project that I'm developing, there is a submenu nested within another submenu. Interestingly enough, the level-2 submenu happens to be taller than its parent level-1 submenu. To control the visibility of these submenus, I' ...

The color of the link remains blue when using Firefox

Check out my website at kenastonchiropractic.com When viewing the site in Chrome, the "Home" link remains the same color as the other links and they all turn white when hovered over. However, in Firefox, the "Home" link stays blue even after being clicked ...

What is the best way to ensure an image fits perfectly within a div container

I'm attempting to design a straightforward card layout where the image spans the entire width of the container div. My vision is for it to resemble something like the example in the linked image below. https://i.sstatic.net/X5oJD.png I experiment ...

Issue with checkboxes preventing submission of form

Currently working on a website project for a company that requires certain steps to be completed for deliveries. Unfortunately, I am facing issues with the submit button, and I apologize as I am still new to this. The code I have pasted is divided into tw ...

Learn the methods for successfully transferring dynamic values from Angular to a jQuery script

This is the script that I have included in my HTML code: <div class="progress-bar"></div> <script type="text/javascript"> $('.progress-bar').gradientProgressBar({ value: $(this).attr('$scope.moodvalue'), ...

What steps should I take to correct the alignment of this grid using CSS?

I'm currently working on creating a grid layout for my website and I've almost achieved the desired result. However, I'm facing an issue with the size of the "Projects" title within the grid. I want it to stand out and be larger compared to ...

How is it possible to retrieve the flex-direction property value of a div using JavaScript?

While attempting to troubleshoot this issue using Windows Chrome 59, I utilized devtools to examine the properties. let element = document.getElementById("divId"); Upon checking the value of the element, I noticed that the object structure displayed: &g ...

PHP not displaying radio button values

My latest project involved creating a quiz program using only PHP. The program consists of 4 different questions and radio buttons, and when an option is selected, the value of the radio button (A, B, C, or D) should be printed. I have set up 4 PHP varia ...

What is the best way to create a dynamic hyperlink that leads to a detailed information page based on the specific link clicked?

I'm currently working on a web page that displays a list of users, and I want each user's name to be clickable, leading to a page with specific details about that user. I'm new to this field, so I'm unsure where to start. My idea is to ...

What is the best way to insert a new row into a table upon clicking a button with Javascript?

Hi everyone, I'm facing an issue with my code. Whenever I click on "Add Product", I want a new row with the same fields to be added. However, it's not working as expected when I run the code. Below is the HTML: <table class="table" id="conci ...

Locate an original identifier using Selenium WebDriver

In search of a distinctive identifier for the "update profile picture button" on my Facebook account to utilize it in automation testing with Selenium WebDriver and Java. I attempted driver.findElement(By.className("_156p")).click();, but unfortunately, i ...

I'm encountering difficulties implementing anchor tags within my single-page application

Currently, I am in the process of learning how to create single page applications with AngularJS using the ui-router module. However, I have encountered an issue where the anchor tag on my main HTML page is not functioning correctly. I am now at a standsti ...

In search of a comprehensive AJAX-enabled content management system

Is there a Content Management System (CMS) available that can create a fully ajax-driven website, allowing for a persistent Flash component without the need to reload it with each page navigation? ...

Encrypting and salting the data provided by the user

Similar Question: Secure hash and salt for PHP passwords My current code snippet is as follows: $insert_query = 'insert into '.$this->tablename.'( name, email, username, password, confirmcode ...